BxBaseModGeneralFormsEntryHelper.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. /**
  19. * 'Ajax Mode' determines the format of response. If it's TRUE the response
  20. * (a form or an error appeared during form creation) should be returned
  21. * as text during initial loading, while all other responses appeared after
  22. * form submit should be arrays, which are ready to path to echoJson.
  23. */
  24. protected $_bAjaxMode;
  25. /**
  26. * Use absolute Action URL in generated form object.
  27. * It's needed in Ajax Mode.
  28. */
  29. protected $_bAbsoluteActionUrl;
  30. protected $_bDynamicMode;
  31. public function __construct($oModule)
  32. {
  33. parent::__construct();
  34. $this->_oModule = $oModule;
  35. $this->_bIsApi = bx_is_api();
  36. $this->_bDynamicMode = false;
  37. $this->_bAjaxMode = false;
  38. $mixedAjaxMode = bx_get('ajax_mode');
  39. if($mixedAjaxMode !== false)
  40. $this->setAjaxMode($mixedAjaxMode);
  41. $this->_bAbsoluteActionUrl = false;
  42. $mixedAbsoluteActionUrl = bx_get('absolute_action_url');
  43. if($mixedAbsoluteActionUrl !== false)
  44. $this->setAbsoluteActionUrl($mixedAbsoluteActionUrl);
  45. }
  46. public function setAjaxMode($bAjaxMode)
  47. {
  48. $this->_bAjaxMode = (bool)$bAjaxMode;
  49. if($this->_bAjaxMode)
  50. $this->setDynamicMode(true);
  51. }
  52. public function setAbsoluteActionUrl($bAbsoluteActionUrl)
  53. {
  54. $this->_bAbsoluteActionUrl = (bool)$bAbsoluteActionUrl;
  55. }
  56. public function setDynamicMode($bDynamicMode)
  57. {
  58. $this->_bDynamicMode = (bool)$bDynamicMode;
  59. }
  60. public function getObjectStorage()
  61. {
  62. return BxDolStorage::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_STORAGE']);
  63. }
  64. public function getObjectFormAdd ($sDisplay = false)
  65. {
  66. $CNF = &$this->_oModule->_oConfig->CNF;
  67. if(false === $sDisplay)
  68. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'];
  69. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  70. if($this->_bAjaxMode)
  71. $oForm->setAjaxMode($this->_bAjaxMode);
  72. if($this->_bAbsoluteActionUrl)
  73. $this->_setAbsoluteActionUrl('add', $oForm);
  74. return $oForm;
  75. }
  76. public function getObjectFormEdit ($sDisplay = false)
  77. {
  78. $CNF = &$this->_oModule->_oConfig->CNF;
  79. if(false === $sDisplay)
  80. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  81. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  82. if($this->_bAjaxMode)
  83. $oForm->setAjaxMode($this->_bAjaxMode);
  84. if($this->_bAbsoluteActionUrl)
  85. $this->_setAbsoluteActionUrl('edit', $oForm);
  86. return $oForm;
  87. }
  88. public function getObjectFormView ($sDisplay = false)
  89. {
  90. $CNF = &$this->_oModule->_oConfig->CNF;
  91. if (false === $sDisplay)
  92. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'];
  93. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  94. }
  95. public function getObjectFormDelete ($sDisplay = false)
  96. {
  97. $CNF = &$this->_oModule->_oConfig->CNF;
  98. if (false === $sDisplay)
  99. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  100. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  101. if($this->_bAjaxMode)
  102. $oForm->setAjaxMode($this->_bAjaxMode);
  103. if($this->_bAbsoluteActionUrl)
  104. $this->_setAbsoluteActionUrl('delete', $oForm);
  105. return $oForm;
  106. }
  107. public function viewDataEntry ($iContentId)
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. $sMsgCnt = '';
  111. // get content data and profile info
  112. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  113. if (!$aContentInfo)
  114. $sMsgCnt = _t('_sys_txt_error_entry_is_not_defined');
  115. // check access
  116. if (empty($sMsgCnt) && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  117. $sMsgCnt = $sMsg;
  118. if ($sMsgCnt)
  119. return bx_is_api() ? [bx_api_get_msg($sMsgCnt)] : MsgBox($sMsgCnt);
  120. $oForm = $this->getObjectFormView();
  121. if (!$oForm)
  122. return '';
  123. $oForm->initChecker($aContentInfo);
  124. if(!empty($CNF['FIELD_TEXT']) && !$oForm->isInputVisible($CNF['FIELD_TEXT']))
  125. $s = '';
  126. else
  127. $s = $this->_oModule->_oTemplate->entryText($aContentInfo);
  128. return bx_is_api() ? [bx_api_get_block('entity_text', $s)] : $s;
  129. }
  130. public function addData ($iProfile, $aValues, $sDisplay = false)
  131. {
  132. $CNF = &$this->_oModule->_oConfig->CNF;
  133. // check and display form
  134. $oForm = $this->getObjectFormAdd($sDisplay);
  135. if (!$oForm)
  136. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  137. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  138. $oForm->aParams['csrf']['disable'] = true;
  139. if(!empty($oForm->aParams['db']['submit_name'])) {
  140. $sSubmitName = false;
  141. if (is_array($oForm->aParams['db']['submit_name'])) {
  142. foreach ($oForm->aParams['db']['submit_name'] as $sVal) {
  143. if (isset($oForm->aInputs[$sVal])) {
  144. $sSubmitName = $sVal;
  145. break;
  146. }
  147. }
  148. }
  149. else {
  150. $sSubmitName = $oForm->aParams['db']['submit_name'];
  151. }
  152. if ($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  153. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  154. }
  155. $oForm->initChecker(array(), $aValues);
  156. if (!$oForm->isSubmittedAndValid()) {
  157. $aErrors = array();
  158. array_walk($oForm->aInputs, function($aInput, $sKey) use (&$aErrors) {
  159. if(!empty($aInput['error']))
  160. $aErrors[$sKey] = $aInput['error'];
  161. });
  162. return array('code' => 2, 'message' => '_sys_txt_error_occured', 'errors' => $aErrors);
  163. }
  164. // insert data into database
  165. $aValsToAdd = array ();
  166. if(isset($CNF['FIELD_AUTHOR']))
  167. $aValsToAdd[$CNF['FIELD_AUTHOR']] = $iProfile;
  168. $iContentId = $oForm->insert($aValsToAdd);
  169. if (!$iContentId) {
  170. if (!$oForm->isValid())
  171. return array('code' => 2, 'message' => '_sys_txt_error_occured');
  172. else
  173. return array('code' => 3, 'message' => '_sys_txt_error_entry_creation');
  174. }
  175. $sResult = $this->onDataAddAfter(BxDolProfile::getInstance($iProfile)->getAccountId(), $iContentId);
  176. if($sResult)
  177. return array('code' => 4, 'message' => $sResult);
  178. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  179. /*
  180. * Process metas.
  181. * Note. It's essential to process metas a the very end,
  182. * because all data related to an entry should be already
  183. * processed and are ready to be passed to alert.
  184. */
  185. $this->_oModule->processMetasAdd($iContentId);
  186. /*
  187. * Create alert about the completed action.
  188. */
  189. $this->_oModule->alertAfterAdd($aContentInfo);
  190. return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
  191. }
  192. public function addDataForm ($sDisplay = false, $sCheckFunction = false)
  193. {
  194. if (!$sCheckFunction)
  195. $sCheckFunction = 'checkAllowedAdd';
  196. $CNF = &$this->_oModule->_oConfig->CNF;
  197. $bAsJson = false;
  198. // get form object
  199. $oForm = $this->getObjectFormAdd($sDisplay);
  200. if (!$oForm)
  201. return bx_is_api() ? [bx_api_get_msg('_sys_txt_error_occured')] : $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $bAsJson, 'msg');
  202. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  203. // check access
  204. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
  205. $oProfile = BxDolProfile::getInstance();
  206. 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
  207. return bx_is_api() ? [bx_api_get_msg('_sys_txt_access_denied')] : $this->prepareResponse(MsgBox(_t('_sys_txt_access_denied')), $bAsJson, 'msg');
  208. else
  209. return bx_is_api() ? [bx_api_get_msg($sMsg)] : $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  210. }
  211. // check and display form
  212. $oForm->initChecker();
  213. if (!$oForm->isSubmittedAndValid())
  214. 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(
  215. 'form_id' => $oForm->getId()
  216. ));
  217. // insert data into database
  218. $aValsToAdd = array ();
  219. $iContentId = $oForm->insert ($aValsToAdd);
  220. if (!$iContentId) {
  221. if (!$oForm->isValid())
  222. 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(
  223. 'form_id' => $oForm->getId()
  224. ));
  225. else
  226. return bx_is_api() ? [bx_api_get_msg('_sys_txt_error_entry_creation')] : $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $bAsJson, 'msg');
  227. }
  228. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  229. if ($sResult)
  230. return $this->prepareResponse($sResult, $bAsJson, 'msg');
  231. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  232. /*
  233. * Process metas.
  234. * Note. It's essential to process metas a the very end,
  235. * because all data related to an entry should be already
  236. * processed and are ready to be passed to alert.
  237. */
  238. $this->_oModule->processMetasAdd($iContentId);
  239. // Perform ACL action
  240. $this->_oModule->$sCheckFunction(true);
  241. // Create alert about the completed action.
  242. $this->_oModule->alertAfterAdd($aContentInfo);
  243. // Redirect
  244. if (bx_is_api())
  245. return $this->redirectAfterAdd($aContentInfo);
  246. else
  247. $this->redirectAfterAdd($aContentInfo);
  248. }
  249. public function redirectAfterAdd($aContentInfo, $sUrl = '')
  250. {
  251. $CNF = &$this->_oModule->_oConfig->CNF;
  252. if ($sUrl == '')
  253. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  254. bx_alert($this->_oModule->getName(), 'redirect_after_add', 0, false, array(
  255. 'ajax_mode' => $this->_bAjaxMode,
  256. 'content' => $aContentInfo,
  257. 'override_result' => &$sUrl,
  258. ));
  259. if (bx_is_api())
  260. return [bx_api_get_block('redirect', ['uri' => BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000])];
  261. if($this->_bAjaxMode) {
  262. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  263. exit;
  264. }
  265. else
  266. $this->_redirectAndExit($sUrl);
  267. }
  268. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  269. {
  270. if (!$sCheckFunction)
  271. $sCheckFunction = 'checkAllowedEdit';
  272. $CNF = &$this->_oModule->_oConfig->CNF;
  273. // get content data and profile info
  274. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  275. if (!$aContentInfo)
  276. return $bErrorMsg && ($sMsg = '_sys_txt_error_entry_is_not_defined') ? ($this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg))) : '';
  277. // check access
  278. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  279. return $bErrorMsg ? ($this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg)) : '';
  280. // check and display form
  281. $oForm = $this->getObjectFormEdit($sDisplay);
  282. if (!$oForm)
  283. return $bErrorMsg && ($sMsg = '_sys_txt_error_occured') ? ($this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg))) : '';
  284. $aSpecificValues = array();
  285. if (!empty($CNF['OBJECT_METATAGS'])) {
  286. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  287. if ($oMetatags->locationsIsEnabled())
  288. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  289. }
  290. $oForm->initChecker($aContentInfo, $aSpecificValues);
  291. if (!$oForm->isSubmittedAndValid())
  292. return $this->_bIsApi ? $oForm : $oForm->getCode();
  293. // update data in the DB
  294. $aTrackTextFieldsChanges = null;
  295. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  296. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  297. if (!$oForm->isValid())
  298. return $this->_bIsApi ? $oForm : $oForm->getCode();
  299. else
  300. return ($sMsg = '_sys_txt_error_entry_update') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  301. }
  302. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  303. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  304. if ($sResult)
  305. return $this->_bIsApi ? bx_api_get_msg($sResult) : $sResult;
  306. /*
  307. * Process metas.
  308. * Note. It's essential to process metas a the very end,
  309. * because all data related to an entry should be already
  310. * processed and are ready to be passed to alert.
  311. */
  312. $this->_oModule->processMetasEdit($iContentId, $oForm);
  313. // Perform ACL action
  314. $this->_oModule->$sCheckFunction($aContentInfo, true);
  315. // Create alert about the completed action.
  316. $this->_oModule->alertAfterEdit($aContentInfo);
  317. // Redirect
  318. $this->redirectAfterEdit($aContentInfo);
  319. }
  320. protected function redirectAfterEdit($aContentInfo)
  321. {
  322. $CNF = &$this->_oModule->_oConfig->CNF;
  323. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  324. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, array(
  325. 'content' => $aContentInfo,
  326. 'override_result' => &$sUrl,
  327. ));
  328. $this->_redirectAndExit($sUrl);
  329. }
  330. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  331. {
  332. if (!$sCheckFunction)
  333. $sCheckFunction = 'checkAllowedDelete';
  334. $CNF = &$this->_oModule->_oConfig->CNF;
  335. // get content data and profile info
  336. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  337. if (!$aContentInfo)
  338. return ($sMsg = '_sys_txt_error_entry_is_not_defined') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  339. // check access
  340. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  341. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  342. // check and display form
  343. $oForm = $this->getObjectFormDelete($sDisplay);
  344. if (!$oForm)
  345. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  346. $oForm->initChecker($aContentInfo);
  347. if (!$oForm->isSubmittedAndValid())
  348. return $this->_bIsApi ? $oForm : $oForm->getCode();
  349. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  350. return $this->_bIsApi ? bx_api_get_msg($sError) : MsgBox($sError);
  351. // perform action
  352. $this->_oModule->$sCheckFunction($aContentInfo, true);
  353. // redirect
  354. $this->redirectAfterDelete($aContentInfo);
  355. }
  356. protected function redirectAfterDelete($aContentInfo)
  357. {
  358. $CNF = &$this->_oModule->_oConfig->CNF;
  359. $sUrl = $CNF['URL_HOME'];
  360. $aMarkers = array(
  361. 'account_id' => getLoggedId(),
  362. 'profile_id' => bx_get_logged_profile_id(),
  363. );
  364. bx_alert($this->_oModule->getName(), 'redirect_after_delete', 0, false, array(
  365. 'content' => $aContentInfo,
  366. 'markers' => &$aMarkers,
  367. 'override_result' => &$sUrl,
  368. ));
  369. $this->_redirectAndExit($sUrl, true, $aMarkers);
  370. }
  371. /**
  372. * Delete data entry
  373. * @param $iContentId entry id
  374. * @param $oForm optional content info array
  375. * @param $aContentInfo optional content info array
  376. * @param $oProfile optional content author profile
  377. * @return error string on error or empty string on success
  378. */
  379. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  380. {
  381. $CNF = &$this->_oModule->_oConfig->CNF;
  382. if (!$aContentInfo || !$oProfile)
  383. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  384. if (!$aContentInfo)
  385. return _t('_sys_txt_error_entry_is_not_defined');
  386. if (!$oForm)
  387. $oForm = $this->getObjectFormDelete();
  388. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  389. return _t('_sys_txt_error_entry_delete');
  390. // remove data from nested forms
  391. $aNestedForms = BxDolFormQuery::getNestedFormObjects($oForm->aParams['object']);
  392. foreach ($aNestedForms as $aNestedForm) {
  393. BxDolFormQuery::deleteDataFromNestedForm($aNestedForm['table'], $iContentId);
  394. }
  395. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  396. return $sResult;
  397. // create an alert
  398. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']], false, array(
  399. 'content' => &$aContentInfo
  400. ));
  401. return '';
  402. }
  403. public function viewDataForm ($iContentId, $sDisplay = false)
  404. {
  405. $CNF = &$this->_oModule->_oConfig->CNF;
  406. // get content data and profile info
  407. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  408. if (!$aContentInfo)
  409. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_entry_is_not_defined') : MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  410. // check access
  411. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  412. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  413. // get form
  414. $oForm = $this->getObjectFormView($sDisplay);
  415. if (!$oForm)
  416. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  417. // process metatags
  418. if (!empty($CNF['OBJECT_METATAGS'])) {
  419. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  420. if ($oMetatags->keywordsIsEnabled()) {
  421. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  422. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  423. }
  424. }
  425. // display profile
  426. $oForm->initChecker($aContentInfo);
  427. if ($this->_bIsApi)
  428. return [bx_api_get_block('entity_info', $oForm->getCodeAPI())];
  429. return $oForm->getCode();
  430. }
  431. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  432. {
  433. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  434. return $sMsg;
  435. return '';
  436. }
  437. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  438. {
  439. $CNF = &$this->_oModule->_oConfig->CNF;
  440. if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  441. $oPrivacy->deleteGroupCustomByContentId($iContentId);
  442. bx_audit(
  443. $iContentId,
  444. $this->_oModule->getName(),
  445. '_sys_audit_action_detete_content',
  446. $this->_oModule->_prepareAuditParams($aContentInfo)
  447. );
  448. return '';
  449. }
  450. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  451. {
  452. $CNF = &$this->_oModule->_oConfig->CNF;
  453. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  454. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  455. }
  456. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  457. {
  458. $CNF = &$this->_oModule->_oConfig->CNF;
  459. /*
  460. * Load update data.
  461. */
  462. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  463. if(isset($CNF['FIELD_PHOTO']))
  464. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  465. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  466. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  467. $this->_oModule->alertAfterAdd($aContentInfo);
  468. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  469. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  470. bx_audit(
  471. $iContentId,
  472. $this->_oModule->getName(),
  473. '_sys_audit_action_edit_content',
  474. $this->_oModule->_prepareAuditParams($aContentInfo)
  475. );
  476. return '';
  477. }
  478. public function onDataAddAfter ($iAccountId, $iContentId)
  479. {
  480. $CNF = &$this->_oModule->_oConfig->CNF;
  481. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  482. if(($oForm = $this->getObjectFormAdd()) !== false) {
  483. if(isset($CNF['FIELD_PHOTO']))
  484. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  485. }
  486. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  487. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  488. bx_audit(
  489. $iContentId,
  490. $this->_oModule->getName(),
  491. '_sys_audit_action_add_content',
  492. $this->_oModule->_prepareAuditParams($aContentInfo)
  493. );
  494. return '';
  495. }
  496. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  497. {
  498. $CNF = &$this->_oModule->_oConfig->CNF;
  499. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  500. $aMarkers = array(
  501. '{profile_id}',
  502. '{content_id}',
  503. '{module}',
  504. );
  505. $aReplacements = array(
  506. $oProfile ? $oProfile->id() : 0,
  507. $aContentInfo[$CNF['FIELD_ID']],
  508. $this->_oModule->getName(),
  509. );
  510. $s = str_replace($aMarkers, $aReplacements, $s);
  511. $s = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($s));
  512. return $s;
  513. }
  514. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  515. {
  516. if(!$bAsJson)
  517. return $mixedResponse;
  518. $aResponse = array(
  519. $sKey => $mixedResponse,
  520. '_dt' => 'json'
  521. );
  522. if(!empty($aAdditional) && is_array($aAdditional))
  523. $aResponse = array_merge($aResponse, $aAdditional);
  524. return $aResponse;
  525. }
  526. protected function _setAbsoluteActionUrl($sType, &$oForm)
  527. {
  528. $CNF = &$this->_oModule->_oConfig->CNF;
  529. $sKeyUri = 'URI_' . strtoupper($sType) . '_ENTRY';
  530. if(empty($CNF[$sKeyUri]))
  531. return;
  532. $oForm->setAbsoluteActionUrl(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF[$sKeyUri])));
  533. }
  534. }
  535. /** @} */