1
0

BxBaseModGeneralFormsEntryHelper.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. if (bx_is_api())
  319. return $this->redirectAfterEdit($aContentInfo);
  320. else
  321. $this->redirectAfterEdit($aContentInfo);
  322. }
  323. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  324. {
  325. $CNF = &$this->_oModule->_oConfig->CNF;
  326. if ($sUrl == '')
  327. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  328. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, array(
  329. 'content' => $aContentInfo,
  330. 'override_result' => &$sUrl,
  331. ));
  332. if (bx_is_api())
  333. return bx_api_get_block('redirect', ['uri' => '/' . BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000]);
  334. $this->_redirectAndExit($sUrl);
  335. }
  336. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  337. {
  338. if (!$sCheckFunction)
  339. $sCheckFunction = 'checkAllowedDelete';
  340. $CNF = &$this->_oModule->_oConfig->CNF;
  341. // get content data and profile info
  342. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  343. if (!$aContentInfo)
  344. return ($sMsg = '_sys_txt_error_entry_is_not_defined') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  345. // check access
  346. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  347. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  348. // check and display form
  349. $oForm = $this->getObjectFormDelete($sDisplay);
  350. if (!$oForm)
  351. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  352. $oForm->initChecker($aContentInfo);
  353. if (!$oForm->isSubmittedAndValid())
  354. return $this->_bIsApi ? $oForm : $oForm->getCode();
  355. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  356. return $this->_bIsApi ? bx_api_get_msg($sError) : MsgBox($sError);
  357. // perform action
  358. $this->_oModule->$sCheckFunction($aContentInfo, true);
  359. // redirect
  360. if (bx_is_api())
  361. return $this->redirectAfterDelete($aContentInfo);
  362. else
  363. $this->redirectAfterDelete($aContentInfo);
  364. }
  365. protected function redirectAfterDelete($aContentInfo)
  366. {
  367. $CNF = &$this->_oModule->_oConfig->CNF;
  368. $sUrl = $CNF['URL_HOME'];
  369. $aMarkers = array(
  370. 'account_id' => getLoggedId(),
  371. 'profile_id' => bx_get_logged_profile_id(),
  372. );
  373. bx_alert($this->_oModule->getName(), 'redirect_after_delete', 0, false, array(
  374. 'content' => $aContentInfo,
  375. 'markers' => &$aMarkers,
  376. 'override_result' => &$sUrl,
  377. ));
  378. if (bx_is_api())
  379. return bx_api_get_block('redirect', ['uri' => '/' . BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000]);
  380. $this->_redirectAndExit($sUrl, true, $aMarkers);
  381. }
  382. /**
  383. * Delete data entry
  384. * @param $iContentId entry id
  385. * @param $oForm optional content info array
  386. * @param $aContentInfo optional content info array
  387. * @param $oProfile optional content author profile
  388. * @return error string on error or empty string on success
  389. */
  390. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  391. {
  392. $CNF = &$this->_oModule->_oConfig->CNF;
  393. if (!$aContentInfo || !$oProfile)
  394. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  395. if (!$aContentInfo)
  396. return _t('_sys_txt_error_entry_is_not_defined');
  397. if (!$oForm)
  398. $oForm = $this->getObjectFormDelete();
  399. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  400. return _t('_sys_txt_error_entry_delete');
  401. // remove data from nested forms
  402. $aNestedForms = BxDolFormQuery::getNestedFormObjects($oForm->aParams['object']);
  403. foreach ($aNestedForms as $aNestedForm) {
  404. BxDolFormQuery::deleteDataFromNestedForm($aNestedForm['table'], $iContentId);
  405. }
  406. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  407. return $sResult;
  408. // create an alert
  409. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']], false, array(
  410. 'content' => &$aContentInfo
  411. ));
  412. return '';
  413. }
  414. public function viewDataForm ($iContentId, $sDisplay = false)
  415. {
  416. $CNF = &$this->_oModule->_oConfig->CNF;
  417. // get content data and profile info
  418. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  419. if (!$aContentInfo)
  420. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_entry_is_not_defined') : MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  421. // check access
  422. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  423. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  424. // get form
  425. $oForm = $this->getObjectFormView($sDisplay);
  426. if (!$oForm)
  427. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  428. // process metatags
  429. if (!empty($CNF['OBJECT_METATAGS'])) {
  430. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  431. if ($oMetatags->keywordsIsEnabled()) {
  432. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  433. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  434. }
  435. }
  436. // display profile
  437. $oForm->initChecker($aContentInfo);
  438. if ($this->_bIsApi)
  439. return [bx_api_get_block('entity_info', $oForm->getCodeAPI())];
  440. return $oForm->getCode();
  441. }
  442. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  443. {
  444. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  445. return $sMsg;
  446. return '';
  447. }
  448. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  449. {
  450. $CNF = &$this->_oModule->_oConfig->CNF;
  451. if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  452. $oPrivacy->deleteGroupCustomByContentId($iContentId);
  453. bx_audit(
  454. $iContentId,
  455. $this->_oModule->getName(),
  456. '_sys_audit_action_detete_content',
  457. $this->_oModule->_prepareAuditParams($aContentInfo)
  458. );
  459. return '';
  460. }
  461. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  462. {
  463. $CNF = &$this->_oModule->_oConfig->CNF;
  464. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  465. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  466. }
  467. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  468. {
  469. $CNF = &$this->_oModule->_oConfig->CNF;
  470. /*
  471. * Load update data.
  472. */
  473. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  474. if(isset($CNF['FIELD_PHOTO']))
  475. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  476. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  477. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  478. $this->_oModule->alertAfterAdd($aContentInfo);
  479. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  480. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  481. bx_audit(
  482. $iContentId,
  483. $this->_oModule->getName(),
  484. '_sys_audit_action_edit_content',
  485. $this->_oModule->_prepareAuditParams($aContentInfo)
  486. );
  487. return '';
  488. }
  489. public function onDataAddAfter ($iAccountId, $iContentId)
  490. {
  491. $CNF = &$this->_oModule->_oConfig->CNF;
  492. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  493. if(($oForm = $this->getObjectFormAdd()) !== false) {
  494. if(isset($CNF['FIELD_PHOTO']))
  495. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  496. }
  497. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  498. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  499. bx_audit(
  500. $iContentId,
  501. $this->_oModule->getName(),
  502. '_sys_audit_action_add_content',
  503. $this->_oModule->_prepareAuditParams($aContentInfo)
  504. );
  505. return '';
  506. }
  507. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  508. {
  509. $CNF = &$this->_oModule->_oConfig->CNF;
  510. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  511. $aMarkers = array(
  512. '{profile_id}',
  513. '{content_id}',
  514. '{module}',
  515. );
  516. $aReplacements = array(
  517. $oProfile ? $oProfile->id() : 0,
  518. $aContentInfo[$CNF['FIELD_ID']],
  519. $this->_oModule->getName(),
  520. );
  521. $s = str_replace($aMarkers, $aReplacements, $s);
  522. $s = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($s));
  523. return $s;
  524. }
  525. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  526. {
  527. if(!$bAsJson)
  528. return $mixedResponse;
  529. $aResponse = array(
  530. $sKey => $mixedResponse,
  531. '_dt' => 'json'
  532. );
  533. if(!empty($aAdditional) && is_array($aAdditional))
  534. $aResponse = array_merge($aResponse, $aAdditional);
  535. return $aResponse;
  536. }
  537. protected function _setAbsoluteActionUrl($sType, &$oForm)
  538. {
  539. $CNF = &$this->_oModule->_oConfig->CNF;
  540. $sKeyUri = 'URI_' . strtoupper($sType) . '_ENTRY';
  541. if(empty($CNF[$sKeyUri]))
  542. return;
  543. $oForm->setAbsoluteActionUrl(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF[$sKeyUri])));
  544. }
  545. }
  546. /** @} */