BxBaseModGeneralFormsEntryHelper.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 bx_is_api() ? [bx_api_get_msg('_sys_txt_error_occured')] : $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $bAsJson, 'msg');
  208. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  209. // check access
  210. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
  211. $oProfile = BxDolProfile::getInstance();
  212. if ($oProfile && ($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
  213. return bx_is_api() ? [bx_api_get_msg('_sys_txt_access_denied')] : $this->prepareResponse(MsgBox(_t('_sys_txt_access_denied')), $bAsJson, 'msg');
  214. else
  215. return bx_is_api() ? [bx_api_get_msg($sMsg)] : $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  216. }
  217. // check and display form
  218. $oForm->initChecker();
  219. if (!$oForm->isSubmittedAndValid())
  220. return bx_is_api() ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['name' => $this->_oModule->getName(), 'request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/entity_create', 'immutable' => true]]])] : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  221. 'form_id' => $oForm->getId()
  222. ));
  223. // insert data into database
  224. $aValsToAdd = array ();
  225. $iContentId = $oForm->insert ($aValsToAdd);
  226. if (!$iContentId) {
  227. if (!$oForm->isValid())
  228. return bx_is_api() ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['name' => $this->_oModule->getName(), 'request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/entity_create', 'immutable' => true]]])] : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  229. 'form_id' => $oForm->getId()
  230. ));
  231. else
  232. return bx_is_api() ? [bx_api_get_msg('_sys_txt_error_entry_creation')] : $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $bAsJson, 'msg');
  233. }
  234. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  235. if ($sResult)
  236. return $this->prepareResponse($sResult, $bAsJson, 'msg');
  237. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  238. /*
  239. * Process metas.
  240. * Note. It's essential to process metas a the very end,
  241. * because all data related to an entry should be already
  242. * processed and are ready to be passed to alert.
  243. */
  244. $this->_oModule->processMetasAdd($iContentId);
  245. // Perform ACL action
  246. $this->_oModule->$sCheckFunction(true);
  247. // Create alert about the completed action.
  248. $this->_oModule->alertAfterAdd($aContentInfo);
  249. // Redirect
  250. if (bx_is_api())
  251. return $this->redirectAfterAdd($aContentInfo);
  252. else
  253. $this->redirectAfterAdd($aContentInfo);
  254. }
  255. public function redirectAfterAdd($aContentInfo, $sUrl = '')
  256. {
  257. $CNF = &$this->_oModule->_oConfig->CNF;
  258. if ($sUrl == '')
  259. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  260. /**
  261. * @hooks
  262. * @hookdef hook-bx_base_general-redirect_after_add '{module_name}', 'redirect_after_add' - hook to override redirect URL which is used after content creation
  263. * - $unit_name - module name
  264. * - $action - equals `redirect_after_add`
  265. * - $object_id - not used
  266. * - $sender_id - not used
  267. * - $extra_params - array of additional params with the following array keys:
  268. * - `ajax_mode` - [boolean] dynamic loading is used or not
  269. * - `content` - [array] content info array as key&value pairs
  270. * - `override_result` - [string] by ref, redirect URL, can be overridden in hook processing
  271. * @hook @ref hook-bx_base_general-redirect_after_add
  272. */
  273. bx_alert($this->_oModule->getName(), 'redirect_after_add', 0, false, [
  274. 'ajax_mode' => $this->_bAjaxMode,
  275. 'content' => $aContentInfo,
  276. 'override_result' => &$sUrl,
  277. ]);
  278. if (bx_is_api())
  279. return [bx_api_get_block('redirect', ['uri' => BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000])];
  280. if($this->_bAjaxMode) {
  281. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  282. exit;
  283. }
  284. else
  285. $this->_redirectAndExit($sUrl);
  286. }
  287. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  288. {
  289. if (!$sCheckFunction)
  290. $sCheckFunction = 'checkAllowedEdit';
  291. $CNF = &$this->_oModule->_oConfig->CNF;
  292. // get content data and profile info
  293. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  294. if (!$aContentInfo)
  295. return $bErrorMsg && ($sMsg = '_sys_txt_error_entry_is_not_defined') ? ($this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg))) : '';
  296. // check access
  297. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  298. return $bErrorMsg ? ($this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg)) : '';
  299. // check and display form
  300. $oForm = $this->getObjectFormEdit($sDisplay);
  301. if (!$oForm)
  302. return $bErrorMsg && ($sMsg = '_sys_txt_error_occured') ? ($this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg))) : '';
  303. $aSpecificValues = array();
  304. if (!empty($CNF['OBJECT_METATAGS'])) {
  305. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  306. if ($oMetatags->locationsIsEnabled())
  307. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  308. }
  309. $oForm->initChecker($aContentInfo, $aSpecificValues);
  310. if (!$oForm->isSubmittedAndValid())
  311. return $this->_bIsApi ? $oForm : $oForm->getCode();
  312. // update data in the DB
  313. $aTrackTextFieldsChanges = null;
  314. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  315. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  316. if (!$oForm->isValid())
  317. return $this->_bIsApi ? $oForm : $oForm->getCode();
  318. else
  319. return ($sMsg = '_sys_txt_error_entry_update') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  320. }
  321. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  322. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  323. if ($sResult)
  324. return $this->_bIsApi ? bx_api_get_msg($sResult) : $sResult;
  325. /*
  326. * Process metas.
  327. * Note. It's essential to process metas a the very end,
  328. * because all data related to an entry should be already
  329. * processed and are ready to be passed to alert.
  330. */
  331. $this->_oModule->processMetasEdit($iContentId, $oForm);
  332. // Perform ACL action
  333. $this->_oModule->$sCheckFunction($aContentInfo, true);
  334. // Create alert about the completed action.
  335. $this->_oModule->alertAfterEdit($aContentInfo);
  336. // Redirect
  337. if (bx_is_api())
  338. return $this->redirectAfterEdit($aContentInfo);
  339. else
  340. $this->redirectAfterEdit($aContentInfo);
  341. }
  342. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  343. {
  344. $CNF = &$this->_oModule->_oConfig->CNF;
  345. if ($sUrl == '')
  346. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  347. /**
  348. * @hooks
  349. * @hookdef hook-bx_base_general-redirect_after_edit '{module_name}', 'redirect_after_edit' - hook to override redirect URL which is used after content changing
  350. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  351. * except `ajax_mode` parameter in $extra_params is missing
  352. * @hook @ref hook-bx_base_general-redirect_after_edit
  353. */
  354. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, [
  355. 'content' => $aContentInfo,
  356. 'override_result' => &$sUrl,
  357. ]);
  358. if (bx_is_api())
  359. return bx_api_get_block('redirect', ['uri' => '/' . BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000]);
  360. $this->_redirectAndExit($sUrl);
  361. }
  362. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  363. {
  364. if (!$sCheckFunction)
  365. $sCheckFunction = 'checkAllowedDelete';
  366. $CNF = &$this->_oModule->_oConfig->CNF;
  367. // get content data and profile info
  368. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  369. if (!$aContentInfo)
  370. return ($sMsg = '_sys_txt_error_entry_is_not_defined') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  371. // check access
  372. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  373. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  374. // check and display form
  375. $oForm = $this->getObjectFormDelete($sDisplay);
  376. if (!$oForm)
  377. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  378. $oForm->initChecker($aContentInfo);
  379. if (!$oForm->isSubmittedAndValid())
  380. return $this->_bIsApi ? $oForm : $oForm->getCode();
  381. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  382. return $this->_bIsApi ? bx_api_get_msg($sError) : MsgBox($sError);
  383. // perform action
  384. $this->_oModule->$sCheckFunction($aContentInfo, true);
  385. // redirect
  386. if (bx_is_api())
  387. return $this->redirectAfterDelete($aContentInfo);
  388. else
  389. $this->redirectAfterDelete($aContentInfo);
  390. }
  391. protected function redirectAfterDelete($aContentInfo)
  392. {
  393. $CNF = &$this->_oModule->_oConfig->CNF;
  394. $sUrl = $CNF['URL_HOME'];
  395. $aMarkers = array(
  396. 'account_id' => getLoggedId(),
  397. 'profile_id' => bx_get_logged_profile_id(),
  398. );
  399. /**
  400. * @hooks
  401. * @hookdef hook-bx_base_general-redirect_after_delete '{module_name}', 'redirect_after_delete' - hook to override redirect URL which is used after content deletion
  402. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  403. * except `markers` parameter was added to $extra_params. It allows to override an array of markers, which can be parsed in URL.
  404. * @hook @ref hook-bx_base_general-redirect_after_delete
  405. */
  406. bx_alert($this->_oModule->getName(), 'redirect_after_delete', 0, false, [
  407. 'content' => $aContentInfo,
  408. 'markers' => &$aMarkers,
  409. 'override_result' => &$sUrl,
  410. ]);
  411. if (bx_is_api())
  412. return bx_api_get_block('redirect', ['uri' => '/' . BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000]);
  413. $this->_redirectAndExit($sUrl, true, $aMarkers);
  414. }
  415. /**
  416. * Delete data entry
  417. * @param $iContentId entry id
  418. * @param $oForm optional content info array
  419. * @param $aContentInfo optional content info array
  420. * @param $oProfile optional content author profile
  421. * @return error string on error or empty string on success
  422. */
  423. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  424. {
  425. $CNF = &$this->_oModule->_oConfig->CNF;
  426. if (!$aContentInfo || !$oProfile)
  427. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  428. if (!$aContentInfo)
  429. return _t('_sys_txt_error_entry_is_not_defined');
  430. if (!$oForm)
  431. $oForm = $this->getObjectFormDelete();
  432. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  433. return _t('_sys_txt_error_entry_delete');
  434. // remove data from nested forms
  435. $aNestedForms = BxDolFormQuery::getNestedFormObjects($oForm->aParams['object']);
  436. foreach ($aNestedForms as $aNestedForm) {
  437. BxDolFormQuery::deleteDataFromNestedForm($aNestedForm['table'], $iContentId);
  438. }
  439. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  440. return $sResult;
  441. /**
  442. * @hooks
  443. * @hookdef hook-bx_base_general-deleted '{module_name}', 'deleted' - hook after content was deleted
  444. * - $unit_name - module name
  445. * - $action - equals `deleted`
  446. * - $object_id - content id
  447. * - $sender_id - not used
  448. * - $extra_params - array of additional params with the following array keys:
  449. * - `content` - [array] by ref, content info array as key&value pairs, can be overridden in hook processing
  450. * @hook @ref hook-bx_base_general-deleted
  451. */
  452. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']], false, [
  453. 'content' => &$aContentInfo
  454. ]);
  455. return '';
  456. }
  457. public function viewDataForm ($iContentId, $sDisplay = false)
  458. {
  459. $CNF = &$this->_oModule->_oConfig->CNF;
  460. // get content data and profile info
  461. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  462. if (!$aContentInfo)
  463. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_entry_is_not_defined') : MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  464. // check access
  465. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  466. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  467. // get form
  468. $oForm = $this->getObjectFormView($sDisplay);
  469. if (!$oForm)
  470. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  471. // process metatags
  472. $aSpecificValues =[];
  473. if (!empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) !== false) {
  474. if ($oMetatags->keywordsIsEnabled()) {
  475. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  476. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  477. }
  478. if ($oMetatags->locationsIsEnabled())
  479. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  480. }
  481. // display profile
  482. $oForm->initChecker($aContentInfo, $aSpecificValues);
  483. if ($this->_bIsApi)
  484. return [bx_api_get_block('entity_info', $oForm->getCodeAPI())];
  485. return $oForm->getCode();
  486. }
  487. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  488. {
  489. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  490. return $sMsg;
  491. return '';
  492. }
  493. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  494. {
  495. $CNF = &$this->_oModule->_oConfig->CNF;
  496. if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  497. $oPrivacy->deleteGroupCustomByContentId($iContentId);
  498. bx_audit(
  499. $iContentId,
  500. $this->_oModule->getName(),
  501. '_sys_audit_action_detete_content',
  502. $this->_oModule->_prepareAuditParams($aContentInfo)
  503. );
  504. return '';
  505. }
  506. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  507. {
  508. $CNF = &$this->_oModule->_oConfig->CNF;
  509. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  510. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  511. }
  512. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  513. {
  514. $CNF = &$this->_oModule->_oConfig->CNF;
  515. /*
  516. * Load update data.
  517. */
  518. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  519. if(isset($CNF['FIELD_PHOTO']))
  520. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  521. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  522. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  523. $this->_oModule->alertAfterAdd($aContentInfo);
  524. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  525. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  526. bx_audit(
  527. $iContentId,
  528. $this->_oModule->getName(),
  529. '_sys_audit_action_edit_content',
  530. $this->_oModule->_prepareAuditParams($aContentInfo)
  531. );
  532. return '';
  533. }
  534. public function onDataAddAfter ($iAccountId, $iContentId)
  535. {
  536. $CNF = &$this->_oModule->_oConfig->CNF;
  537. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  538. if(($oForm = $this->getObjectFormAdd()) !== false) {
  539. if(isset($CNF['FIELD_PHOTO']))
  540. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  541. }
  542. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  543. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  544. bx_audit(
  545. $iContentId,
  546. $this->_oModule->getName(),
  547. '_sys_audit_action_add_content',
  548. $this->_oModule->_prepareAuditParams($aContentInfo)
  549. );
  550. return '';
  551. }
  552. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  553. {
  554. $CNF = &$this->_oModule->_oConfig->CNF;
  555. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  556. $aMarkers = array(
  557. '{profile_id}',
  558. '{content_id}',
  559. '{module}',
  560. );
  561. $aReplacements = array(
  562. $oProfile ? $oProfile->id() : 0,
  563. $aContentInfo[$CNF['FIELD_ID']],
  564. $this->_oModule->getName(),
  565. );
  566. $s = str_replace($aMarkers, $aReplacements, $s);
  567. $s = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($s));
  568. return $s;
  569. }
  570. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  571. {
  572. if(!$bAsJson)
  573. return $mixedResponse;
  574. $aResponse = array(
  575. $sKey => $mixedResponse,
  576. '_dt' => 'json'
  577. );
  578. if(!empty($aAdditional) && is_array($aAdditional))
  579. $aResponse = array_merge($aResponse, $aAdditional);
  580. return $aResponse;
  581. }
  582. protected function _setAbsoluteActionUrl($sType, &$oForm)
  583. {
  584. $CNF = &$this->_oModule->_oConfig->CNF;
  585. $sKeyUri = 'URI_' . strtoupper($sType) . '_ENTRY';
  586. if(empty($CNF[$sKeyUri]))
  587. return;
  588. $oForm->setAbsoluteActionUrl(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF[$sKeyUri])));
  589. }
  590. }
  591. /** @} */