BxBaseModGeneralFormsEntryHelper.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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' => bx_api_get_relative_url(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->aParams['csrf']['disable'] = true;
  363. $oForm->initChecker($aContentInfo, $aContentInfo);
  364. // before edit hook
  365. $aTrackTextFieldsChanges = null;
  366. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  367. // update data in the DB
  368. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], $aValues, $aTrackTextFieldsChanges)) {
  369. return _t('_sys_txt_error_entry_update');
  370. }
  371. // after edit hook
  372. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  373. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  374. if ($sResult)
  375. return $sResult;
  376. /*
  377. * Process metas.
  378. * Note. It's essential to process metas a the very end,
  379. * because all data related to an entry should be already
  380. * processed and are ready to be passed to alert.
  381. */
  382. $this->_oModule->processMetasEdit($iContentId, $oForm);
  383. // Create alert about the completed action.
  384. $this->_oModule->alertAfterEdit($aContentInfo);
  385. return '';
  386. }
  387. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  388. {
  389. $CNF = &$this->_oModule->_oConfig->CNF;
  390. if ($sUrl == '')
  391. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  392. if(($mixedUrl = $this->_getRedirectFromContext('edit', $aContentInfo)) !== false)
  393. $sUrl = $mixedUrl;
  394. /**
  395. * @hooks
  396. * @hookdef hook-bx_base_general-redirect_after_edit '{module_name}', 'redirect_after_edit' - hook to override redirect URL which is used after content changing
  397. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  398. * except `ajax_mode` parameter in $extra_params is missing
  399. * @hook @ref hook-bx_base_general-redirect_after_edit
  400. */
  401. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, [
  402. 'content' => $aContentInfo,
  403. 'override_result' => &$sUrl,
  404. ]);
  405. if($this->_bIsApi)
  406. return !empty($sUrl) ? bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000]) : [];
  407. $this->_redirectAndExit($sUrl);
  408. }
  409. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  410. {
  411. if (!$sCheckFunction)
  412. $sCheckFunction = 'checkAllowedDelete';
  413. $CNF = &$this->_oModule->_oConfig->CNF;
  414. // get content data and profile info
  415. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  416. if (!$aContentInfo)
  417. return ($sMsg = '_sys_txt_error_entry_is_not_defined') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  418. // check access
  419. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  420. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  421. // check and display form
  422. $oForm = $this->getObjectFormDelete($sDisplay);
  423. if (!$oForm)
  424. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  425. $oForm->initChecker($aContentInfo);
  426. if (!$oForm->isSubmittedAndValid())
  427. return $this->_bIsApi ? $oForm : $oForm->getCode();
  428. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  429. return $this->_bIsApi ? bx_api_get_msg($sError) : MsgBox($sError);
  430. // perform action
  431. $this->_oModule->$sCheckFunction($aContentInfo, true);
  432. // redirect
  433. if (bx_is_api())
  434. return $this->redirectAfterDelete($aContentInfo);
  435. else
  436. $this->redirectAfterDelete($aContentInfo);
  437. }
  438. protected function redirectAfterDelete($aContentInfo)
  439. {
  440. $CNF = &$this->_oModule->_oConfig->CNF;
  441. $sUrl = $CNF['URL_HOME'];
  442. $aMarkers = array(
  443. 'account_id' => getLoggedId(),
  444. 'profile_id' => bx_get_logged_profile_id(),
  445. );
  446. /**
  447. * @hooks
  448. * @hookdef hook-bx_base_general-redirect_after_delete '{module_name}', 'redirect_after_delete' - hook to override redirect URL which is used after content deletion
  449. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  450. * except `markers` parameter was added to $extra_params. It allows to override an array of markers, which can be parsed in URL.
  451. * @hook @ref hook-bx_base_general-redirect_after_delete
  452. */
  453. bx_alert($this->_oModule->getName(), 'redirect_after_delete', 0, false, [
  454. 'content' => $aContentInfo,
  455. 'markers' => &$aMarkers,
  456. 'override_result' => &$sUrl,
  457. ]);
  458. if($this->_bIsApi)
  459. return bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000]);
  460. $this->_redirectAndExit($sUrl, true, $aMarkers);
  461. }
  462. /**
  463. * Delete data entry
  464. * @param $iContentId entry id
  465. * @param $oForm optional content info array
  466. * @param $aContentInfo optional content info array
  467. * @param $oProfile optional content author profile
  468. * @return error string on error or empty string on success
  469. */
  470. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  471. {
  472. $CNF = &$this->_oModule->_oConfig->CNF;
  473. if (!$aContentInfo || !$oProfile)
  474. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  475. if (!$aContentInfo)
  476. return _t('_sys_txt_error_entry_is_not_defined');
  477. if (!$oForm)
  478. $oForm = $this->getObjectFormDelete();
  479. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  480. return _t('_sys_txt_error_entry_delete');
  481. // remove data from nested forms
  482. $aNestedForms = BxDolFormQuery::getNestedFormObjects($oForm->aParams['object']);
  483. foreach ($aNestedForms as $aNestedForm) {
  484. BxDolFormQuery::deleteDataFromNestedForm($aNestedForm['table'], $iContentId);
  485. }
  486. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  487. return $sResult;
  488. /**
  489. * @hooks
  490. * @hookdef hook-bx_base_general-deleted '{module_name}', 'deleted' - hook after content was deleted
  491. * - $unit_name - module name
  492. * - $action - equals `deleted`
  493. * - $object_id - content id
  494. * - $sender_id - not used
  495. * - $extra_params - array of additional params with the following array keys:
  496. * - `content` - [array] by ref, content info array as key&value pairs, can be overridden in hook processing
  497. * @hook @ref hook-bx_base_general-deleted
  498. */
  499. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']], false, [
  500. 'content' => &$aContentInfo
  501. ]);
  502. return '';
  503. }
  504. public function viewDataForm ($iContentId, $sDisplay = false)
  505. {
  506. $CNF = &$this->_oModule->_oConfig->CNF;
  507. // get content data and profile info
  508. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  509. if (!$aContentInfo)
  510. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_entry_is_not_defined') : MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  511. // check access
  512. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  513. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  514. // get form
  515. $oForm = $this->getObjectFormView($sDisplay);
  516. if (!$oForm)
  517. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  518. // process metatags
  519. $aSpecificValues =[];
  520. if (!empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) !== false) {
  521. if ($oMetatags->keywordsIsEnabled()) {
  522. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  523. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  524. }
  525. if ($oMetatags->locationsIsEnabled())
  526. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  527. }
  528. // display profile
  529. $oForm->initChecker($aContentInfo, $aSpecificValues);
  530. if ($this->_bIsApi)
  531. return [bx_api_get_block('entity_info', $oForm->getCodeAPI())];
  532. return $oForm->getCode();
  533. }
  534. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  535. {
  536. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  537. return $sMsg;
  538. return '';
  539. }
  540. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  541. {
  542. $CNF = &$this->_oModule->_oConfig->CNF;
  543. if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  544. $oPrivacy->deleteGroupCustomByContentId($iContentId);
  545. $sKey = 'FIELD_ALLOW_VIEW_TO';
  546. if(isset($CNF[$sKey], $aContentInfo[$CNF[$sKey]]) && ($iContextPid = (int)$aContentInfo[$CNF[$sKey]]) < 0) {
  547. $iContextPid = abs($iContextPid);
  548. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  549. $sModule = $oContext->getModule();
  550. $sMethod = 'on_content_deleted';
  551. if(bx_is_srv($sModule, $sMethod))
  552. bx_srv($sModule, $sMethod, [$this->_oModule->getName(), $iContentId, $oContext->getContentId()]);
  553. }
  554. }
  555. bx_audit(
  556. $iContentId,
  557. $this->_oModule->getName(),
  558. '_sys_audit_action_detete_content',
  559. $this->_oModule->_prepareAuditParams($aContentInfo)
  560. );
  561. return '';
  562. }
  563. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  564. {
  565. $CNF = &$this->_oModule->_oConfig->CNF;
  566. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  567. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  568. }
  569. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  570. {
  571. $CNF = &$this->_oModule->_oConfig->CNF;
  572. /*
  573. * Load update data.
  574. */
  575. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  576. if(isset($CNF['FIELD_PHOTO']))
  577. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  578. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  579. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  580. $this->_oModule->alertAfterAdd($aContentInfo);
  581. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  582. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  583. bx_audit(
  584. $iContentId,
  585. $this->_oModule->getName(),
  586. '_sys_audit_action_edit_content',
  587. $this->_oModule->_prepareAuditParams($aContentInfo)
  588. );
  589. return '';
  590. }
  591. public function onDataAddAfter ($iAccountId, $iContentId)
  592. {
  593. $CNF = &$this->_oModule->_oConfig->CNF;
  594. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  595. if(($oForm = $this->getObjectFormAdd()) !== false) {
  596. if(isset($CNF['FIELD_PHOTO']))
  597. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  598. }
  599. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  600. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  601. bx_audit(
  602. $iContentId,
  603. $this->_oModule->getName(),
  604. '_sys_audit_action_add_content',
  605. $this->_oModule->_prepareAuditParams($aContentInfo)
  606. );
  607. return '';
  608. }
  609. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  610. {
  611. $CNF = &$this->_oModule->_oConfig->CNF;
  612. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  613. $aMarkers = array(
  614. '{profile_id}',
  615. '{content_id}',
  616. '{module}',
  617. );
  618. $aReplacements = array(
  619. $oProfile ? $oProfile->id() : 0,
  620. $aContentInfo[$CNF['FIELD_ID']],
  621. $this->_oModule->getName(),
  622. );
  623. $s = str_replace($aMarkers, $aReplacements, $s);
  624. $s = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($s));
  625. return $s;
  626. }
  627. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  628. {
  629. if(!$bAsJson)
  630. return $mixedResponse;
  631. $aResponse = array(
  632. $sKey => $mixedResponse,
  633. '_dt' => 'json'
  634. );
  635. if(!empty($aAdditional) && is_array($aAdditional))
  636. $aResponse = array_merge($aResponse, $aAdditional);
  637. return $aResponse;
  638. }
  639. protected function prepareResponseAPI($sType, $mixedData, $aParams = [])
  640. {
  641. $aResult = [];
  642. switch($sType) {
  643. case 'msg':
  644. $aResult = bx_api_get_msg($mixedData);
  645. break;
  646. case 'form':
  647. $sModule = $this->_oModule->getName();
  648. $sRequestUrl = '/api.php?r=' . $sModule . '/' . $aParams['request_uri'];
  649. if(!empty($aParams['request_params']) && is_array($aParams['request_params']))
  650. $sRequestUrl = bx_append_url_params($sRequestUrl, $aParams['request_params']);
  651. bx_append_url_params($sUrl, $mixedParams);
  652. $aResult = bx_api_get_block('form', $mixedData, [
  653. 'ext' => [
  654. 'name' => $sModule,
  655. 'request' => ['url' => $sRequestUrl, 'immutable' => true]
  656. ]
  657. ]);
  658. break;
  659. }
  660. return [$aResult];
  661. }
  662. protected function _setAbsoluteActionUrl($sType, &$oForm)
  663. {
  664. $CNF = &$this->_oModule->_oConfig->CNF;
  665. $sKeyUri = 'URI_' . strtoupper($sType) . '_ENTRY';
  666. if(empty($CNF[$sKeyUri]))
  667. return;
  668. $oForm->setAbsoluteActionUrl(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF[$sKeyUri])));
  669. }
  670. protected function _getRedirectFromContext($sAction, $aContentInfo)
  671. {
  672. $CNF = &$this->_oModule->_oConfig->CNF;
  673. $sKey = 'FIELD_ALLOW_VIEW_TO';
  674. if(empty($CNF[$sKey]) || !isset($aContentInfo[$CNF[$sKey]]) || (int)$aContentInfo[$CNF[$sKey]] >= 0)
  675. return false;
  676. $iContextPid = abs($aContentInfo[$CNF[$sKey]]);
  677. $oContext = BxDolProfile::getInstance($iContextPid);
  678. if(!$oContext)
  679. return false;
  680. $sModuleContext = $oContext->getModule();
  681. $sMethodContext = 'on_content_' . $sAction . 'ed_redirect';
  682. if(!bx_is_srv($sModuleContext, $sMethodContext))
  683. return false;
  684. return bx_srv($sModuleContext, $sMethodContext, [$this->_oModule->getName(), $aContentInfo[$CNF['FIELD_ID']]]);
  685. }
  686. }
  687. /** @} */