1
0

BxBaseModGeneralFormsEntryHelper.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Entry forms helper functions
  13. */
  14. class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
  15. {
  16. protected $_oModule;
  17. protected $_bIsApi;
  18. protected $_bDynamicMode;
  19. /**
  20. * 'Ajax Mode' determines the format of response. If it's TRUE the response
  21. * (a form or an error appeared during form creation) should be returned
  22. * as text during initial loading, while all other responses appeared after
  23. * form submit should be arrays, which are ready to path to echoJson.
  24. */
  25. protected $_bAjaxMode;
  26. /**
  27. * 'Valid Mode' allows to create content even if some fields values
  28. * didn't pass checking. It's needed when content was created
  29. * with a service call. For example, automatic profile creation after join.
  30. */
  31. protected $_bValidMode;
  32. /**
  33. * Use absolute Action URL in generated form object.
  34. * It's needed in Ajax Mode.
  35. */
  36. protected $_bAbsoluteActionUrl;
  37. protected $_mixedContextId;
  38. public function __construct($oModule)
  39. {
  40. parent::__construct();
  41. $this->_oModule = $oModule;
  42. $this->_bIsApi = bx_is_api();
  43. $this->_bDynamicMode = false;
  44. $this->_bAjaxMode = false;
  45. $mixedAjaxMode = bx_get('ajax_mode');
  46. if($mixedAjaxMode !== false)
  47. $this->setAjaxMode($mixedAjaxMode);
  48. $this->_bAbsoluteActionUrl = false;
  49. $mixedAbsoluteActionUrl = bx_get('absolute_action_url');
  50. if($mixedAbsoluteActionUrl !== false)
  51. $this->setAbsoluteActionUrl($mixedAbsoluteActionUrl);
  52. $this->_mixedContextId = false;
  53. }
  54. public function setAjaxMode($bAjaxMode)
  55. {
  56. $this->_bAjaxMode = (bool)$bAjaxMode;
  57. if($this->_bAjaxMode)
  58. $this->setDynamicMode(true);
  59. }
  60. public function setValidMode($bValidMode)
  61. {
  62. $this->_bValidMode = (bool)$bValidMode;
  63. }
  64. public function setAbsoluteActionUrl($bAbsoluteActionUrl)
  65. {
  66. $this->_bAbsoluteActionUrl = (bool)$bAbsoluteActionUrl;
  67. }
  68. public function setContextId($mixedContextId)
  69. {
  70. $this->_mixedContextId = is_numeric($mixedContextId) ? (int)$mixedContextId : (bool)$mixedContextId;
  71. }
  72. public function setDynamicMode($bDynamicMode)
  73. {
  74. $this->_bDynamicMode = (bool)$bDynamicMode;
  75. }
  76. public function getObjectStorage()
  77. {
  78. return BxDolStorage::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_STORAGE']);
  79. }
  80. public function getObjectFormAdd ($sDisplay = false)
  81. {
  82. $CNF = &$this->_oModule->_oConfig->CNF;
  83. if(false === $sDisplay)
  84. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'];
  85. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  86. if($this->_bAjaxMode)
  87. $oForm->setAjaxMode($this->_bAjaxMode);
  88. if($this->_bAbsoluteActionUrl)
  89. $this->_setAbsoluteActionUrl('add', $oForm);
  90. return $oForm;
  91. }
  92. public function getObjectFormEdit ($sDisplay = false)
  93. {
  94. $CNF = &$this->_oModule->_oConfig->CNF;
  95. if(false === $sDisplay)
  96. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  97. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  98. if($this->_bAjaxMode)
  99. $oForm->setAjaxMode($this->_bAjaxMode);
  100. if($this->_bAbsoluteActionUrl)
  101. $this->_setAbsoluteActionUrl('edit', $oForm);
  102. return $oForm;
  103. }
  104. public function getObjectFormView ($sDisplay = false)
  105. {
  106. $CNF = &$this->_oModule->_oConfig->CNF;
  107. if (false === $sDisplay)
  108. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'];
  109. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  110. }
  111. public function getObjectFormDelete ($sDisplay = false)
  112. {
  113. $CNF = &$this->_oModule->_oConfig->CNF;
  114. if (false === $sDisplay)
  115. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  116. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  117. if($this->_bAjaxMode)
  118. $oForm->setAjaxMode($this->_bAjaxMode);
  119. if($this->_bAbsoluteActionUrl)
  120. $this->_setAbsoluteActionUrl('delete', $oForm);
  121. return $oForm;
  122. }
  123. public function viewDataEntry ($iContentId)
  124. {
  125. $CNF = &$this->_oModule->_oConfig->CNF;
  126. $sMsgCnt = '';
  127. // get content data and profile info
  128. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  129. if (!$aContentInfo)
  130. $sMsgCnt = _t('_sys_txt_error_entry_is_not_defined');
  131. // check access
  132. if (empty($sMsgCnt) && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  133. $sMsgCnt = $sMsg;
  134. if ($sMsgCnt)
  135. return bx_is_api() ? [bx_api_get_msg($sMsgCnt)] : MsgBox($sMsgCnt);
  136. $oForm = $this->getObjectFormView();
  137. if (!$oForm)
  138. return '';
  139. $oForm->initChecker($aContentInfo);
  140. if(!empty($CNF['FIELD_TEXT']) && !$oForm->isInputVisible($CNF['FIELD_TEXT']))
  141. $s = '';
  142. else
  143. $s = $this->_oModule->_oTemplate->entryText($aContentInfo);
  144. return bx_is_api() ? [bx_api_get_block('entity_text', $s)] : $s;
  145. }
  146. public function addData ($iProfile, $aValues, $sDisplay = false)
  147. {
  148. $CNF = &$this->_oModule->_oConfig->CNF;
  149. // check and display form
  150. $oForm = $this->getObjectFormAdd($sDisplay);
  151. if (!$oForm)
  152. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  153. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  154. $oForm->aParams['csrf']['disable'] = true;
  155. if(!empty($oForm->aParams['db']['submit_name'])) {
  156. $sSubmitName = false;
  157. if (is_array($oForm->aParams['db']['submit_name'])) {
  158. foreach ($oForm->aParams['db']['submit_name'] as $sVal) {
  159. if (isset($oForm->aInputs[$sVal])) {
  160. $sSubmitName = $sVal;
  161. break;
  162. }
  163. }
  164. }
  165. else {
  166. $sSubmitName = $oForm->aParams['db']['submit_name'];
  167. }
  168. if ($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  169. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  170. }
  171. //TODO: Use in Edit too.
  172. $oForm->initChecker([], $aValues);
  173. if($this->_bValidMode && !$oForm->isValid())
  174. $oForm->setValid(true);
  175. if(!$oForm->isSubmittedAndValid()) {
  176. $aErrors = array();
  177. array_walk($oForm->aInputs, function($aInput, $sKey) use (&$aErrors) {
  178. if(!empty($aInput['error']))
  179. $aErrors[$sKey] = $aInput['error'];
  180. });
  181. return array('code' => 2, 'message' => '_sys_txt_error_occured', 'errors' => $aErrors);
  182. }
  183. // insert data into database
  184. $aValsToAdd = array ();
  185. if(isset($CNF['FIELD_AUTHOR']))
  186. $aValsToAdd[$CNF['FIELD_AUTHOR']] = $iProfile;
  187. $iContentId = $oForm->insert($aValsToAdd);
  188. if (!$iContentId) {
  189. if (!$oForm->isValid())
  190. return array('code' => 2, 'message' => '_sys_txt_error_occured');
  191. else
  192. return array('code' => 3, 'message' => '_sys_txt_error_entry_creation');
  193. }
  194. $sResult = $this->onDataAddAfter(BxDolProfile::getInstance($iProfile)->getAccountId(), $iContentId);
  195. if($sResult)
  196. return array('code' => 4, 'message' => $sResult);
  197. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  198. /*
  199. * Process metas.
  200. * Note. It's essential to process metas a the very end,
  201. * because all data related to an entry should be already
  202. * processed and are ready to be passed to alert.
  203. */
  204. $this->_oModule->processMetasAdd($iContentId);
  205. /*
  206. * Create alert about the completed action.
  207. */
  208. $this->_oModule->alertAfterAdd($aContentInfo);
  209. return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
  210. }
  211. public function addDataForm ($sDisplay = false, $sCheckFunction = false)
  212. {
  213. if (!$sCheckFunction)
  214. $sCheckFunction = 'checkAllowedAdd';
  215. $CNF = &$this->_oModule->_oConfig->CNF;
  216. $bAsJson = false;
  217. // get form object
  218. $oForm = $this->getObjectFormAdd($sDisplay);
  219. if (!$oForm)
  220. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox(_t($sMsg)), $bAsJson, 'msg');
  221. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  222. // check access
  223. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
  224. if (($oProfile = BxDolProfile::getInstance()) !== false && ($aProfileInfo = $oProfile->getInfo()) && $aProfileInfo['type'] == 'system' && is_subclass_of($this->_oModule, 'BxBaseModProfileModule') && $this->_oModule->serviceActAsProfile()) // special check for system profile is needed, because of incorrect error message
  225. return ($sMsg = '_sys_txt_access_denied') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox(_t($sMsg)), $bAsJson, 'msg');
  226. else
  227. return $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  228. }
  229. // check and display form
  230. $oForm->initChecker();
  231. if (!$oForm->isSubmittedAndValid()) {
  232. if(!$oForm->isSubmitted() && isset($CNF['FIELD_ALLOW_VIEW_TO']) && ($oProfile = BxDolProfile::getInstance()) !== false && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  233. $oPrivacy->deleteGroupCustomByProfileIdUnused($oProfile->id());
  234. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_create']) : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', [
  235. 'form_id' => $oForm->getId()
  236. ]);
  237. }
  238. // insert data into database
  239. $aValsToAdd = array ();
  240. $iContentId = $oForm->insert ($aValsToAdd);
  241. if (!$iContentId) {
  242. if (!$oForm->isValid())
  243. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_create']) : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', [
  244. 'form_id' => $oForm->getId()
  245. ]);
  246. else
  247. return ($sMsg = '_sys_txt_error_entry_creation') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox(_t($sMsg)), $bAsJson, 'msg');
  248. }
  249. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  250. if ($sResult)
  251. return $this->_bIsApi ? $this->prepareResponseAPI('msg', $sResult) : $this->prepareResponse($sResult, $bAsJson, 'msg');
  252. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  253. /*
  254. * Process metas.
  255. * Note. It's essential to process metas a the very end,
  256. * because all data related to an entry should be already
  257. * processed and are ready to be passed to alert.
  258. */
  259. $this->_oModule->processMetasAdd($iContentId);
  260. // Perform ACL action
  261. $this->_oModule->$sCheckFunction(true);
  262. // Create alert about the completed action.
  263. $this->_oModule->alertAfterAdd($aContentInfo);
  264. // Redirect
  265. if ($this->_bIsApi)
  266. return $this->redirectAfterAdd($aContentInfo);
  267. else
  268. $this->redirectAfterAdd($aContentInfo);
  269. }
  270. public function redirectAfterAdd($aContentInfo, $sUrl = '')
  271. {
  272. $CNF = &$this->_oModule->_oConfig->CNF;
  273. if ($sUrl == '')
  274. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  275. if(($mixedUrl = $this->_getRedirectFromContext('add', $aContentInfo)) !== false)
  276. $sUrl = $mixedUrl;
  277. /**
  278. * @hooks
  279. * @hookdef hook-bx_base_general-redirect_after_add '{module_name}', 'redirect_after_add' - hook to override redirect URL which is used after content creation
  280. * - $unit_name - module name
  281. * - $action - equals `redirect_after_add`
  282. * - $object_id - not used
  283. * - $sender_id - not used
  284. * - $extra_params - array of additional params with the following array keys:
  285. * - `ajax_mode` - [boolean] dynamic loading is used or not
  286. * - `content` - [array] content info array as key&value pairs
  287. * - `override_result` - [string] by ref, redirect URL, can be overridden in hook processing
  288. * @hook @ref hook-bx_base_general-redirect_after_add
  289. */
  290. bx_alert($this->_oModule->getName(), 'redirect_after_add', 0, false, [
  291. 'ajax_mode' => $this->_bAjaxMode,
  292. 'content' => $aContentInfo,
  293. 'override_result' => &$sUrl,
  294. ]);
  295. if($this->_bIsApi)
  296. return !empty($sUrl) ? [bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000])] : [];
  297. if($this->_bAjaxMode) {
  298. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  299. exit;
  300. }
  301. else
  302. $this->_redirectAndExit($sUrl);
  303. }
  304. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  305. {
  306. if (!$sCheckFunction)
  307. $sCheckFunction = 'checkAllowedEdit';
  308. $CNF = &$this->_oModule->_oConfig->CNF;
  309. // get content data and profile info
  310. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  311. if (!$aContentInfo)
  312. return $bErrorMsg && ($sMsg = '_sys_txt_error_entry_is_not_defined') ? ($this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox(_t($sMsg))) : '';
  313. // check access
  314. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  315. return $bErrorMsg ? ($this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox($sMsg)) : '';
  316. // check and display form
  317. $oForm = $this->getObjectFormEdit($sDisplay);
  318. if (!$oForm)
  319. return $bErrorMsg && ($sMsg = '_sys_txt_error_occured') ? ($this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox(_t($sMsg))) : '';
  320. $aSpecificValues = array();
  321. if (!empty($CNF['OBJECT_METATAGS'])) {
  322. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  323. if ($oMetatags->locationsIsEnabled())
  324. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  325. }
  326. $oForm->initChecker($aContentInfo, $aSpecificValues);
  327. if (!$oForm->isSubmittedAndValid())
  328. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_edit', 'request_params' => ['params' => [$iContentId, $oForm->aParams['display']]]]) : $oForm->getCode();
  329. // update data in the DB
  330. $aTrackTextFieldsChanges = null;
  331. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  332. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  333. if (!$oForm->isValid())
  334. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_edit', 'request_params' => ['params' => [$iContentId, $oForm->aParams['display']]]]) : $oForm->getCode();
  335. else
  336. return ($sMsg = '_sys_txt_error_entry_update') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox(_t($sMsg));
  337. }
  338. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  339. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  340. if ($sResult)
  341. return $this->_bIsApi ? $this->prepareResponseAPI('msg', $sResult) : $sResult;
  342. /*
  343. * Process metas.
  344. * Note. It's essential to process metas a the very end,
  345. * because all data related to an entry should be already
  346. * processed and are ready to be passed to alert.
  347. */
  348. $this->_oModule->processMetasEdit($iContentId, $oForm);
  349. // Perform ACL action
  350. $this->_oModule->$sCheckFunction($aContentInfo, true);
  351. // Create alert about the completed action.
  352. $this->_oModule->alertAfterEdit($aContentInfo);
  353. // Redirect
  354. if ($this->_bIsApi)
  355. return $this->redirectAfterEdit($aContentInfo);
  356. else
  357. $this->redirectAfterEdit($aContentInfo);
  358. }
  359. /**
  360. * @return empty string on success or error message string on error
  361. */
  362. public function editData ($iContentId, $aValues, $sDisplay = false)
  363. {
  364. $CNF = &$this->_oModule->_oConfig->CNF;
  365. // get content data and profile info
  366. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  367. if (!$aContentInfo)
  368. return _t('_sys_txt_error_entry_is_not_defined');
  369. // get display form
  370. $oForm = $this->getObjectFormEdit($sDisplay);
  371. if (!$oForm)
  372. return _t('_sys_txt_error_occured');
  373. foreach ($aValues as $k => $v) {
  374. if (!isset($oForm->aInputs[$k]))
  375. return _t('_sys_txt_forms_unknown_field_err', $k);
  376. }
  377. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  378. $oForm->aParams['csrf']['disable'] = true;
  379. $oForm->initChecker($aContentInfo, $aContentInfo);
  380. // before edit hook
  381. $aTrackTextFieldsChanges = null;
  382. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  383. // update data in the DB
  384. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], $aValues, $aTrackTextFieldsChanges)) {
  385. return _t('_sys_txt_error_entry_update');
  386. }
  387. // after edit hook
  388. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  389. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  390. if ($sResult)
  391. return $sResult;
  392. /*
  393. * Process metas.
  394. * Note. It's essential to process metas a the very end,
  395. * because all data related to an entry should be already
  396. * processed and are ready to be passed to alert.
  397. */
  398. $this->_oModule->processMetasEdit($iContentId, $oForm);
  399. // Create alert about the completed action.
  400. $this->_oModule->alertAfterEdit($aContentInfo);
  401. return '';
  402. }
  403. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  404. {
  405. $CNF = &$this->_oModule->_oConfig->CNF;
  406. if ($sUrl == '')
  407. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  408. if(($mixedUrl = $this->_getRedirectFromContext('edit', $aContentInfo)) !== false)
  409. $sUrl = $mixedUrl;
  410. /**
  411. * @hooks
  412. * @hookdef hook-bx_base_general-redirect_after_edit '{module_name}', 'redirect_after_edit' - hook to override redirect URL which is used after content changing
  413. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  414. * except `ajax_mode` parameter in $extra_params is missing
  415. * @hook @ref hook-bx_base_general-redirect_after_edit
  416. */
  417. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, [
  418. 'content' => $aContentInfo,
  419. 'override_result' => &$sUrl,
  420. ]);
  421. if($this->_bIsApi)
  422. return !empty($sUrl) ? bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000]) : [];
  423. $this->_redirectAndExit($sUrl);
  424. }
  425. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  426. {
  427. if (!$sCheckFunction)
  428. $sCheckFunction = 'checkAllowedDelete';
  429. $CNF = &$this->_oModule->_oConfig->CNF;
  430. // get content data and profile info
  431. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  432. if (!$aContentInfo)
  433. return ($sMsg = '_sys_txt_error_entry_is_not_defined') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  434. // check access
  435. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  436. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  437. // check and display form
  438. $oForm = $this->getObjectFormDelete($sDisplay);
  439. if (!$oForm)
  440. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  441. $oForm->initChecker($aContentInfo);
  442. if (!$oForm->isSubmittedAndValid())
  443. return $this->_bIsApi ? $oForm : $oForm->getCode();
  444. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  445. return $this->_bIsApi ? bx_api_get_msg($sError) : MsgBox($sError);
  446. // perform action
  447. $this->_oModule->$sCheckFunction($aContentInfo, true);
  448. // redirect
  449. if (bx_is_api())
  450. return $this->redirectAfterDelete($aContentInfo);
  451. else
  452. $this->redirectAfterDelete($aContentInfo);
  453. }
  454. protected function redirectAfterDelete($aContentInfo)
  455. {
  456. $CNF = &$this->_oModule->_oConfig->CNF;
  457. $sUrl = $CNF['URL_HOME'];
  458. $aMarkers = array(
  459. 'account_id' => getLoggedId(),
  460. 'profile_id' => bx_get_logged_profile_id(),
  461. );
  462. /**
  463. * @hooks
  464. * @hookdef hook-bx_base_general-redirect_after_delete '{module_name}', 'redirect_after_delete' - hook to override redirect URL which is used after content deletion
  465. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  466. * except `markers` parameter was added to $extra_params. It allows to override an array of markers, which can be parsed in URL.
  467. * @hook @ref hook-bx_base_general-redirect_after_delete
  468. */
  469. bx_alert($this->_oModule->getName(), 'redirect_after_delete', 0, false, [
  470. 'content' => $aContentInfo,
  471. 'markers' => &$aMarkers,
  472. 'override_result' => &$sUrl,
  473. ]);
  474. if($this->_bIsApi)
  475. return bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000]);
  476. $this->_redirectAndExit($sUrl, true, $aMarkers);
  477. }
  478. /**
  479. * Delete data entry
  480. * @param $iContentId entry id
  481. * @param $oForm optional content info array
  482. * @param $aContentInfo optional content info array
  483. * @param $oProfile optional content author profile
  484. * @return error string on error or empty string on success
  485. */
  486. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  487. {
  488. $CNF = &$this->_oModule->_oConfig->CNF;
  489. if (!$aContentInfo || !$oProfile)
  490. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  491. if (!$aContentInfo)
  492. return _t('_sys_txt_error_entry_is_not_defined');
  493. if (!$oForm)
  494. $oForm = $this->getObjectFormDelete();
  495. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  496. return _t('_sys_txt_error_entry_delete');
  497. // remove data from nested forms
  498. $aNestedForms = BxDolFormQuery::getNestedFormObjects($oForm->aParams['object']);
  499. foreach ($aNestedForms as $aNestedForm) {
  500. BxDolFormQuery::deleteDataFromNestedForm($aNestedForm['table'], $iContentId);
  501. }
  502. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  503. return $sResult;
  504. /**
  505. * @hooks
  506. * @hookdef hook-bx_base_general-deleted '{module_name}', 'deleted' - hook after content was deleted
  507. * - $unit_name - module name
  508. * - $action - equals `deleted`
  509. * - $object_id - content id
  510. * - $sender_id - not used
  511. * - $extra_params - array of additional params with the following array keys:
  512. * - `content` - [array] by ref, content info array as key&value pairs, can be overridden in hook processing
  513. * @hook @ref hook-bx_base_general-deleted
  514. */
  515. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']], false, [
  516. 'content' => &$aContentInfo
  517. ]);
  518. return '';
  519. }
  520. public function viewDataForm ($iContentId, $sDisplay = false)
  521. {
  522. $CNF = &$this->_oModule->_oConfig->CNF;
  523. // get content data and profile info
  524. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  525. if (!$aContentInfo)
  526. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_entry_is_not_defined') : MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  527. // check access
  528. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  529. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  530. // get form
  531. $oForm = $this->getObjectFormView($sDisplay);
  532. if (!$oForm)
  533. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  534. // process metatags
  535. $aSpecificValues =[];
  536. if (!empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) !== false) {
  537. if ($oMetatags->keywordsIsEnabled()) {
  538. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  539. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  540. }
  541. if ($oMetatags->locationsIsEnabled())
  542. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  543. }
  544. // display profile
  545. $oForm->initChecker($aContentInfo, $aSpecificValues);
  546. if ($this->_bIsApi)
  547. return [bx_api_get_block('entity_info', $oForm->getCodeAPI())];
  548. return $oForm->getCode();
  549. }
  550. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  551. {
  552. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  553. return $sMsg;
  554. return '';
  555. }
  556. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  557. {
  558. $CNF = &$this->_oModule->_oConfig->CNF;
  559. if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  560. $oPrivacy->deleteGroupCustomByContentId($iContentId);
  561. $sKey = 'FIELD_ALLOW_VIEW_TO';
  562. if(isset($CNF[$sKey], $aContentInfo[$CNF[$sKey]]) && ($iContextPid = (int)$aContentInfo[$CNF[$sKey]]) < 0) {
  563. $iContextPid = abs($iContextPid);
  564. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  565. $sModule = $oContext->getModule();
  566. $sMethod = 'on_content_deleted';
  567. if(bx_is_srv($sModule, $sMethod))
  568. bx_srv($sModule, $sMethod, [$this->_oModule->getName(), $iContentId, $oContext->getContentId()]);
  569. }
  570. }
  571. $this->_oModule->_oDb->deletePolls(['content_id' => $iContentId]);
  572. bx_audit(
  573. $iContentId,
  574. $this->_oModule->getName(),
  575. '_sys_audit_action_detete_content',
  576. $this->_oModule->_prepareAuditParams($aContentInfo)
  577. );
  578. return '';
  579. }
  580. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  581. {
  582. $CNF = &$this->_oModule->_oConfig->CNF;
  583. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  584. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  585. }
  586. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  587. {
  588. $CNF = &$this->_oModule->_oConfig->CNF;
  589. /*
  590. * Load update data.
  591. */
  592. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  593. if(isset($CNF['FIELD_PHOTO']))
  594. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  595. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  596. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  597. $this->_oModule->alertAfterAdd($aContentInfo);
  598. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  599. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  600. bx_audit(
  601. $iContentId,
  602. $this->_oModule->getName(),
  603. '_sys_audit_action_edit_content',
  604. $this->_oModule->_prepareAuditParams($aContentInfo)
  605. );
  606. return '';
  607. }
  608. public function onDataAddAfter ($iAccountId, $iContentId)
  609. {
  610. $CNF = &$this->_oModule->_oConfig->CNF;
  611. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  612. if(($oForm = $this->getObjectFormAdd()) !== false) {
  613. if(isset($CNF['FIELD_PHOTO']))
  614. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  615. }
  616. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  617. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  618. bx_audit(
  619. $iContentId,
  620. $this->_oModule->getName(),
  621. '_sys_audit_action_add_content',
  622. $this->_oModule->_prepareAuditParams($aContentInfo)
  623. );
  624. return '';
  625. }
  626. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  627. {
  628. $CNF = &$this->_oModule->_oConfig->CNF;
  629. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  630. $aMarkers = array(
  631. '{profile_id}',
  632. '{content_id}',
  633. '{module}',
  634. );
  635. $aReplacements = array(
  636. $oProfile ? $oProfile->id() : 0,
  637. $aContentInfo[$CNF['FIELD_ID']],
  638. $this->_oModule->getName(),
  639. );
  640. $s = str_replace($aMarkers, $aReplacements, $s);
  641. $s = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($s));
  642. return $s;
  643. }
  644. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  645. {
  646. if(!$bAsJson)
  647. return $mixedResponse;
  648. $aResponse = array(
  649. $sKey => $mixedResponse,
  650. '_dt' => 'json'
  651. );
  652. if(!empty($aAdditional) && is_array($aAdditional))
  653. $aResponse = array_merge($aResponse, $aAdditional);
  654. return $aResponse;
  655. }
  656. protected function prepareResponseAPI($sType, $mixedData, $aParams = [])
  657. {
  658. $aResult = [];
  659. switch($sType) {
  660. case 'msg':
  661. $aResult = bx_api_get_msg($mixedData);
  662. break;
  663. case 'form':
  664. $sModule = $this->_oModule->getName();
  665. $sRequestUrl = '/api.php?r=' . $sModule . '/' . $aParams['request_uri'];
  666. if(!empty($aParams['request_params']) && is_array($aParams['request_params']))
  667. $sRequestUrl = bx_append_url_params($sRequestUrl, $aParams['request_params']);
  668. $aResult = bx_api_get_block('form', $mixedData, [
  669. 'ext' => [
  670. 'name' => $sModule,
  671. 'request' => ['url' => $sRequestUrl, 'immutable' => true]
  672. ]
  673. ]);
  674. break;
  675. }
  676. return [$aResult];
  677. }
  678. protected function _setAbsoluteActionUrl($sType, &$oForm)
  679. {
  680. $sUri = $this->_oModule->_oConfig->getEntryUri($sType);
  681. if(!$sUri)
  682. return;
  683. $oForm->setAbsoluteActionUrl(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $sUri)));
  684. }
  685. protected function _getRedirectFromContext($sAction, $aContentInfo)
  686. {
  687. $CNF = &$this->_oModule->_oConfig->CNF;
  688. $sKey = 'FIELD_ALLOW_VIEW_TO';
  689. if(empty($CNF[$sKey]) || !isset($aContentInfo[$CNF[$sKey]]) || (int)$aContentInfo[$CNF[$sKey]] >= 0)
  690. return false;
  691. $iContextPid = abs($aContentInfo[$CNF[$sKey]]);
  692. $oContext = BxDolProfile::getInstance($iContextPid);
  693. if(!$oContext)
  694. return false;
  695. $sModuleContext = $oContext->getModule();
  696. $sMethodContext = 'on_content_' . $sAction . 'ed_redirect';
  697. if(!bx_is_srv($sModuleContext, $sMethodContext))
  698. return false;
  699. return bx_srv($sModuleContext, $sMethodContext, [$this->_oModule->getName(), $aContentInfo[$CNF['FIELD_ID']]]);
  700. }
  701. }
  702. /** @} */