BxBaseModGeneralFormsEntryHelper.php 33 KB

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