BxBaseModGeneralFormsEntryHelper.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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() ? [['id' => 1, 'type' => 'msg', 'data' => $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() ? [['id' => 1, 'type' => 'entry', 'data' => $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 $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 $this->prepareResponse(MsgBox(_t('_sys_txt_access_denied')), $bAsJson, 'msg');
  208. else
  209. return $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  210. }
  211. // check and display form
  212. $oForm->initChecker();
  213. if (!$oForm->isSubmittedAndValid())
  214. return $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 $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  223. 'form_id' => $oForm->getId()
  224. ));
  225. else
  226. return $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. $this->redirectAfterAdd($aContentInfo);
  245. }
  246. public function redirectAfterAdd($aContentInfo, $sUrl = '')
  247. {
  248. $CNF = &$this->_oModule->_oConfig->CNF;
  249. if ($sUrl == '')
  250. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  251. bx_alert($this->_oModule->getName(), 'redirect_after_add', 0, false, array(
  252. 'ajax_mode' => $this->_bAjaxMode,
  253. 'content' => $aContentInfo,
  254. 'override_result' => &$sUrl,
  255. ));
  256. if($this->_bAjaxMode) {
  257. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  258. exit;
  259. }
  260. else
  261. $this->_redirectAndExit($sUrl);
  262. }
  263. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  264. {
  265. if (!$sCheckFunction)
  266. $sCheckFunction = 'checkAllowedEdit';
  267. $CNF = &$this->_oModule->_oConfig->CNF;
  268. // get content data and profile info
  269. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  270. if (!$aContentInfo)
  271. return $bErrorMsg && ($sMsg = '_sys_txt_error_entry_is_not_defined') ? ($this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => _t($sMsg)] : MsgBox(_t($sMsg))) : '';
  272. // check access
  273. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  274. return $bErrorMsg ? ($this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => $sMsg] : MsgBox($sMsg)) : '';
  275. // check and display form
  276. $oForm = $this->getObjectFormEdit($sDisplay);
  277. if (!$oForm)
  278. return $bErrorMsg && ($sMsg = '_sys_txt_error_occured') ? ($this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => _t($sMsg)] : MsgBox(_t($sMsg))) : '';
  279. $aSpecificValues = array();
  280. if (!empty($CNF['OBJECT_METATAGS'])) {
  281. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  282. if ($oMetatags->locationsIsEnabled())
  283. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  284. }
  285. $oForm->initChecker($aContentInfo, $aSpecificValues);
  286. if (!$oForm->isSubmittedAndValid())
  287. return $this->_bIsApi ? $oForm : $oForm->getCode();
  288. // update data in the DB
  289. $aTrackTextFieldsChanges = null;
  290. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  291. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  292. if (!$oForm->isValid())
  293. return $this->_bIsApi ? $oForm : $oForm->getCode();
  294. else
  295. return ($sMsg = '_sys_txt_error_entry_update') && $this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => _t($sMsg)] : MsgBox(_t($sMsg));
  296. }
  297. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  298. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  299. if ($sResult)
  300. return $this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => $sResult] : $sResult;
  301. /*
  302. * Process metas.
  303. * Note. It's essential to process metas a the very end,
  304. * because all data related to an entry should be already
  305. * processed and are ready to be passed to alert.
  306. */
  307. $this->_oModule->processMetasEdit($iContentId, $oForm);
  308. // Perform ACL action
  309. $this->_oModule->$sCheckFunction($aContentInfo, true);
  310. // Create alert about the completed action.
  311. $this->_oModule->alertAfterEdit($aContentInfo);
  312. // Redirect
  313. $this->redirectAfterEdit($aContentInfo);
  314. }
  315. protected function redirectAfterEdit($aContentInfo)
  316. {
  317. $CNF = &$this->_oModule->_oConfig->CNF;
  318. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  319. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, array(
  320. 'content' => $aContentInfo,
  321. 'override_result' => &$sUrl,
  322. ));
  323. $this->_redirectAndExit($sUrl);
  324. }
  325. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  326. {
  327. if (!$sCheckFunction)
  328. $sCheckFunction = 'checkAllowedDelete';
  329. $CNF = &$this->_oModule->_oConfig->CNF;
  330. // get content data and profile info
  331. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  332. if (!$aContentInfo)
  333. return ($sMsg = '_sys_txt_error_entry_is_not_defined') && $this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => _t($sMsg)] : MsgBox(_t($sMsg));
  334. // check access
  335. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  336. return $this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => $sMsg] : MsgBox($sMsg);
  337. // check and display form
  338. $oForm = $this->getObjectFormDelete($sDisplay);
  339. if (!$oForm)
  340. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => _t($sMsg)] : MsgBox(_t($sMsg));
  341. $oForm->initChecker($aContentInfo);
  342. if (!$oForm->isSubmittedAndValid())
  343. return $this->_bIsApi ? $oForm : $oForm->getCode();
  344. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  345. return $this->_bIsApi ? ['id' => 1, 'type' => 'msg', 'data' => $sError] : MsgBox($sError);
  346. // perform action
  347. $this->_oModule->$sCheckFunction($aContentInfo, true);
  348. // redirect
  349. $this->redirectAfterDelete($aContentInfo);
  350. }
  351. protected function redirectAfterDelete($aContentInfo)
  352. {
  353. $CNF = &$this->_oModule->_oConfig->CNF;
  354. $sUrl = $CNF['URL_HOME'];
  355. $aMarkers = array(
  356. 'account_id' => getLoggedId(),
  357. 'profile_id' => bx_get_logged_profile_id(),
  358. );
  359. bx_alert($this->_oModule->getName(), 'redirect_after_delete', 0, false, array(
  360. 'content' => $aContentInfo,
  361. 'markers' => &$aMarkers,
  362. 'override_result' => &$sUrl,
  363. ));
  364. $this->_redirectAndExit($sUrl, true, $aMarkers);
  365. }
  366. /**
  367. * Delete data entry
  368. * @param $iContentId entry id
  369. * @param $oForm optional content info array
  370. * @param $aContentInfo optional content info array
  371. * @param $oProfile optional content author profile
  372. * @return error string on error or empty string on success
  373. */
  374. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  375. {
  376. $CNF = &$this->_oModule->_oConfig->CNF;
  377. if (!$aContentInfo || !$oProfile)
  378. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  379. if (!$aContentInfo)
  380. return _t('_sys_txt_error_entry_is_not_defined');
  381. if (!$oForm)
  382. $oForm = $this->getObjectFormDelete();
  383. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  384. return _t('_sys_txt_error_entry_delete');
  385. // remove data from nested forms
  386. $aNestedForms = BxDolFormQuery::getNestedFormObjects($oForm->aParams['object']);
  387. foreach ($aNestedForms as $aNestedForm) {
  388. BxDolFormQuery::deleteDataFromNestedForm($aNestedForm['table'], $iContentId);
  389. }
  390. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  391. return $sResult;
  392. // create an alert
  393. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']], false, array(
  394. 'content' => &$aContentInfo
  395. ));
  396. return '';
  397. }
  398. public function viewDataForm ($iContentId, $sDisplay = false)
  399. {
  400. $CNF = &$this->_oModule->_oConfig->CNF;
  401. // get content data and profile info
  402. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  403. if (!$aContentInfo)
  404. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  405. // check access
  406. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  407. return MsgBox($sMsg);
  408. // get form
  409. $oForm = $this->getObjectFormView($sDisplay);
  410. if (!$oForm)
  411. return MsgBox(_t('_sys_txt_error_occured'));
  412. // process metatags
  413. if (!empty($CNF['OBJECT_METATAGS'])) {
  414. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  415. if ($oMetatags->keywordsIsEnabled()) {
  416. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  417. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  418. }
  419. }
  420. // display profile
  421. $oForm->initChecker($aContentInfo);
  422. return $oForm->getCode();
  423. }
  424. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  425. {
  426. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  427. return $sMsg;
  428. return '';
  429. }
  430. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  431. {
  432. $CNF = &$this->_oModule->_oConfig->CNF;
  433. if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  434. $oPrivacy->deleteGroupCustomByContentId($iContentId);
  435. bx_audit(
  436. $iContentId,
  437. $this->_oModule->getName(),
  438. '_sys_audit_action_detete_content',
  439. $this->_oModule->_prepareAuditParams($aContentInfo)
  440. );
  441. return '';
  442. }
  443. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  444. {
  445. $CNF = &$this->_oModule->_oConfig->CNF;
  446. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  447. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  448. }
  449. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  450. {
  451. $CNF = &$this->_oModule->_oConfig->CNF;
  452. /*
  453. * Load update data.
  454. */
  455. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  456. if(isset($CNF['FIELD_PHOTO']))
  457. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  458. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  459. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  460. $this->_oModule->alertAfterAdd($aContentInfo);
  461. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  462. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  463. bx_audit(
  464. $iContentId,
  465. $this->_oModule->getName(),
  466. '_sys_audit_action_edit_content',
  467. $this->_oModule->_prepareAuditParams($aContentInfo)
  468. );
  469. return '';
  470. }
  471. public function onDataAddAfter ($iAccountId, $iContentId)
  472. {
  473. $CNF = &$this->_oModule->_oConfig->CNF;
  474. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  475. if(($oForm = $this->getObjectFormAdd()) !== false) {
  476. if(isset($CNF['FIELD_PHOTO']))
  477. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  478. }
  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->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  481. bx_audit(
  482. $iContentId,
  483. $this->_oModule->getName(),
  484. '_sys_audit_action_add_content',
  485. $this->_oModule->_prepareAuditParams($aContentInfo)
  486. );
  487. return '';
  488. }
  489. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  490. {
  491. $CNF = &$this->_oModule->_oConfig->CNF;
  492. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  493. $aMarkers = array(
  494. '{profile_id}',
  495. '{content_id}',
  496. '{module}',
  497. );
  498. $aReplacements = array(
  499. $oProfile ? $oProfile->id() : 0,
  500. $aContentInfo[$CNF['FIELD_ID']],
  501. $this->_oModule->getName(),
  502. );
  503. $s = str_replace($aMarkers, $aReplacements, $s);
  504. $s = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($s));
  505. return $s;
  506. }
  507. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  508. {
  509. if(!$bAsJson)
  510. return $mixedResponse;
  511. $aResponse = array(
  512. $sKey => $mixedResponse,
  513. '_dt' => 'json'
  514. );
  515. if(!empty($aAdditional) && is_array($aAdditional))
  516. $aResponse = array_merge($aResponse, $aAdditional);
  517. return $aResponse;
  518. }
  519. protected function _setAbsoluteActionUrl($sType, &$oForm)
  520. {
  521. $CNF = &$this->_oModule->_oConfig->CNF;
  522. $sKeyUri = 'URI_' . strtoupper($sType) . '_ENTRY';
  523. if(empty($CNF[$sKeyUri]))
  524. return;
  525. $oForm->setAbsoluteActionUrl(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF[$sKeyUri])));
  526. }
  527. }
  528. /** @} */