BxBaseModGeneralFormsEntryHelper.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Entry forms helper functions
  13. */
  14. class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
  15. {
  16. protected $_oModule;
  17. protected $_bIsApi;
  18. protected $_bDynamicMode;
  19. /**
  20. * 'Ajax Mode' determines the format of response. If it's TRUE the response
  21. * (a form or an error appeared during form creation) should be returned
  22. * as text during initial loading, while all other responses appeared after
  23. * form submit should be arrays, which are ready to path to echoJson.
  24. */
  25. protected $_bAjaxMode;
  26. /**
  27. * Use absolute Action URL in generated form object.
  28. * It's needed in Ajax Mode.
  29. */
  30. protected $_bAbsoluteActionUrl;
  31. protected $_mixedContextId;
  32. public function __construct($oModule)
  33. {
  34. parent::__construct();
  35. $this->_oModule = $oModule;
  36. $this->_bIsApi = bx_is_api();
  37. $this->_bDynamicMode = false;
  38. $this->_bAjaxMode = false;
  39. $mixedAjaxMode = bx_get('ajax_mode');
  40. if($mixedAjaxMode !== false)
  41. $this->setAjaxMode($mixedAjaxMode);
  42. $this->_bAbsoluteActionUrl = false;
  43. $mixedAbsoluteActionUrl = bx_get('absolute_action_url');
  44. if($mixedAbsoluteActionUrl !== false)
  45. $this->setAbsoluteActionUrl($mixedAbsoluteActionUrl);
  46. $this->_mixedContextId = false;
  47. }
  48. public function setAjaxMode($bAjaxMode)
  49. {
  50. $this->_bAjaxMode = (bool)$bAjaxMode;
  51. if($this->_bAjaxMode)
  52. $this->setDynamicMode(true);
  53. }
  54. public function setAbsoluteActionUrl($bAbsoluteActionUrl)
  55. {
  56. $this->_bAbsoluteActionUrl = (bool)$bAbsoluteActionUrl;
  57. }
  58. public function setContextId($mixedContextId)
  59. {
  60. $this->_mixedContextId = is_numeric($mixedContextId) ? (int)$mixedContextId : (bool)$mixedContextId;
  61. }
  62. public function setDynamicMode($bDynamicMode)
  63. {
  64. $this->_bDynamicMode = (bool)$bDynamicMode;
  65. }
  66. public function getObjectStorage()
  67. {
  68. return BxDolStorage::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_STORAGE']);
  69. }
  70. public function getObjectFormAdd ($sDisplay = false)
  71. {
  72. $CNF = &$this->_oModule->_oConfig->CNF;
  73. if(false === $sDisplay)
  74. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'];
  75. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  76. if($this->_bAjaxMode)
  77. $oForm->setAjaxMode($this->_bAjaxMode);
  78. if($this->_bAbsoluteActionUrl)
  79. $this->_setAbsoluteActionUrl('add', $oForm);
  80. return $oForm;
  81. }
  82. public function getObjectFormEdit ($sDisplay = false)
  83. {
  84. $CNF = &$this->_oModule->_oConfig->CNF;
  85. if(false === $sDisplay)
  86. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  87. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  88. if($this->_bAjaxMode)
  89. $oForm->setAjaxMode($this->_bAjaxMode);
  90. if($this->_bAbsoluteActionUrl)
  91. $this->_setAbsoluteActionUrl('edit', $oForm);
  92. return $oForm;
  93. }
  94. public function getObjectFormView ($sDisplay = false)
  95. {
  96. $CNF = &$this->_oModule->_oConfig->CNF;
  97. if (false === $sDisplay)
  98. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'];
  99. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  100. }
  101. public function getObjectFormDelete ($sDisplay = false)
  102. {
  103. $CNF = &$this->_oModule->_oConfig->CNF;
  104. if (false === $sDisplay)
  105. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  106. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  107. if($this->_bAjaxMode)
  108. $oForm->setAjaxMode($this->_bAjaxMode);
  109. if($this->_bAbsoluteActionUrl)
  110. $this->_setAbsoluteActionUrl('delete', $oForm);
  111. return $oForm;
  112. }
  113. public function viewDataEntry ($iContentId)
  114. {
  115. $CNF = &$this->_oModule->_oConfig->CNF;
  116. $sMsgCnt = '';
  117. // get content data and profile info
  118. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  119. if (!$aContentInfo)
  120. $sMsgCnt = _t('_sys_txt_error_entry_is_not_defined');
  121. // check access
  122. if (empty($sMsgCnt) && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  123. $sMsgCnt = $sMsg;
  124. if ($sMsgCnt)
  125. return bx_is_api() ? [bx_api_get_msg($sMsgCnt)] : MsgBox($sMsgCnt);
  126. $oForm = $this->getObjectFormView();
  127. if (!$oForm)
  128. return '';
  129. $oForm->initChecker($aContentInfo);
  130. if(!empty($CNF['FIELD_TEXT']) && !$oForm->isInputVisible($CNF['FIELD_TEXT']))
  131. $s = '';
  132. else
  133. $s = $this->_oModule->_oTemplate->entryText($aContentInfo);
  134. return bx_is_api() ? [bx_api_get_block('entity_text', $s)] : $s;
  135. }
  136. public function addData ($iProfile, $aValues, $sDisplay = false)
  137. {
  138. $CNF = &$this->_oModule->_oConfig->CNF;
  139. // check and display form
  140. $oForm = $this->getObjectFormAdd($sDisplay);
  141. if (!$oForm)
  142. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  143. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  144. $oForm->aParams['csrf']['disable'] = true;
  145. if(!empty($oForm->aParams['db']['submit_name'])) {
  146. $sSubmitName = false;
  147. if (is_array($oForm->aParams['db']['submit_name'])) {
  148. foreach ($oForm->aParams['db']['submit_name'] as $sVal) {
  149. if (isset($oForm->aInputs[$sVal])) {
  150. $sSubmitName = $sVal;
  151. break;
  152. }
  153. }
  154. }
  155. else {
  156. $sSubmitName = $oForm->aParams['db']['submit_name'];
  157. }
  158. if ($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  159. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  160. }
  161. $oForm->initChecker(array(), $aValues);
  162. if (!$oForm->isSubmittedAndValid()) {
  163. $aErrors = array();
  164. array_walk($oForm->aInputs, function($aInput, $sKey) use (&$aErrors) {
  165. if(!empty($aInput['error']))
  166. $aErrors[$sKey] = $aInput['error'];
  167. });
  168. return array('code' => 2, 'message' => '_sys_txt_error_occured', 'errors' => $aErrors);
  169. }
  170. // insert data into database
  171. $aValsToAdd = array ();
  172. if(isset($CNF['FIELD_AUTHOR']))
  173. $aValsToAdd[$CNF['FIELD_AUTHOR']] = $iProfile;
  174. $iContentId = $oForm->insert($aValsToAdd);
  175. if (!$iContentId) {
  176. if (!$oForm->isValid())
  177. return array('code' => 2, 'message' => '_sys_txt_error_occured');
  178. else
  179. return array('code' => 3, 'message' => '_sys_txt_error_entry_creation');
  180. }
  181. $sResult = $this->onDataAddAfter(BxDolProfile::getInstance($iProfile)->getAccountId(), $iContentId);
  182. if($sResult)
  183. return array('code' => 4, 'message' => $sResult);
  184. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  185. /*
  186. * Process metas.
  187. * Note. It's essential to process metas a the very end,
  188. * because all data related to an entry should be already
  189. * processed and are ready to be passed to alert.
  190. */
  191. $this->_oModule->processMetasAdd($iContentId);
  192. /*
  193. * Create alert about the completed action.
  194. */
  195. $this->_oModule->alertAfterAdd($aContentInfo);
  196. return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
  197. }
  198. public function addDataForm ($sDisplay = false, $sCheckFunction = false)
  199. {
  200. if (!$sCheckFunction)
  201. $sCheckFunction = 'checkAllowedAdd';
  202. $CNF = &$this->_oModule->_oConfig->CNF;
  203. $bAsJson = false;
  204. // get form object
  205. $oForm = $this->getObjectFormAdd($sDisplay);
  206. if (!$oForm)
  207. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox(_t($sMsg)), $bAsJson, 'msg');
  208. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  209. // check access
  210. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
  211. if (($oProfile = BxDolProfile::getInstance()) !== false && ($aProfileInfo = $oProfile->getInfo()) && $aProfileInfo['type'] == 'system' && is_subclass_of($this->_oModule, 'BxBaseModProfileModule') && $this->_oModule->serviceActAsProfile()) // special check for system profile is needed, because of incorrect error message
  212. return ($sMsg = '_sys_txt_access_denied') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox(_t($sMsg)), $bAsJson, 'msg');
  213. else
  214. return $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  215. }
  216. // check and display form
  217. $oForm->initChecker();
  218. if (!$oForm->isSubmittedAndValid()) {
  219. if(!$oForm->isSubmitted() && isset($CNF['FIELD_ALLOW_VIEW_TO']) && ($oProfile = BxDolProfile::getInstance()) !== false && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  220. $oPrivacy->deleteGroupCustomByProfileIdUnused($oProfile->id());
  221. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_create']) : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', [
  222. 'form_id' => $oForm->getId()
  223. ]);
  224. }
  225. // insert data into database
  226. $aValsToAdd = array ();
  227. $iContentId = $oForm->insert ($aValsToAdd);
  228. if (!$iContentId) {
  229. if (!$oForm->isValid())
  230. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_create']) : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', [
  231. 'form_id' => $oForm->getId()
  232. ]);
  233. else
  234. return ($sMsg = '_sys_txt_error_entry_creation') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : $this->prepareResponse(MsgBox(_t($sMsg)), $bAsJson, 'msg');
  235. }
  236. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  237. if ($sResult)
  238. return $this->_bIsApi ? $this->prepareResponseAPI('msg', $sResult) : $this->prepareResponse($sResult, $bAsJson, 'msg');
  239. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  240. /*
  241. * Process metas.
  242. * Note. It's essential to process metas a the very end,
  243. * because all data related to an entry should be already
  244. * processed and are ready to be passed to alert.
  245. */
  246. $this->_oModule->processMetasAdd($iContentId);
  247. // Perform ACL action
  248. $this->_oModule->$sCheckFunction(true);
  249. // Create alert about the completed action.
  250. $this->_oModule->alertAfterAdd($aContentInfo);
  251. // Redirect
  252. if ($this->_bIsApi)
  253. return $this->redirectAfterAdd($aContentInfo);
  254. else
  255. $this->redirectAfterAdd($aContentInfo);
  256. }
  257. public function redirectAfterAdd($aContentInfo, $sUrl = '')
  258. {
  259. $CNF = &$this->_oModule->_oConfig->CNF;
  260. if ($sUrl == '')
  261. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  262. if(($mixedUrl = $this->_getRedirectFromContext('add', $aContentInfo)) !== false)
  263. $sUrl = $mixedUrl;
  264. /**
  265. * @hooks
  266. * @hookdef hook-bx_base_general-redirect_after_add '{module_name}', 'redirect_after_add' - hook to override redirect URL which is used after content creation
  267. * - $unit_name - module name
  268. * - $action - equals `redirect_after_add`
  269. * - $object_id - not used
  270. * - $sender_id - not used
  271. * - $extra_params - array of additional params with the following array keys:
  272. * - `ajax_mode` - [boolean] dynamic loading is used or not
  273. * - `content` - [array] content info array as key&value pairs
  274. * - `override_result` - [string] by ref, redirect URL, can be overridden in hook processing
  275. * @hook @ref hook-bx_base_general-redirect_after_add
  276. */
  277. bx_alert($this->_oModule->getName(), 'redirect_after_add', 0, false, [
  278. 'ajax_mode' => $this->_bAjaxMode,
  279. 'content' => $aContentInfo,
  280. 'override_result' => &$sUrl,
  281. ]);
  282. if($this->_bIsApi)
  283. return !empty($sUrl) ? [bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000])] : [];
  284. if($this->_bAjaxMode) {
  285. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  286. exit;
  287. }
  288. else
  289. $this->_redirectAndExit($sUrl);
  290. }
  291. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  292. {
  293. if (!$sCheckFunction)
  294. $sCheckFunction = 'checkAllowedEdit';
  295. $CNF = &$this->_oModule->_oConfig->CNF;
  296. // get content data and profile info
  297. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  298. if (!$aContentInfo)
  299. return $bErrorMsg && ($sMsg = '_sys_txt_error_entry_is_not_defined') ? ($this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox(_t($sMsg))) : '';
  300. // check access
  301. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  302. return $bErrorMsg ? ($this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox($sMsg)) : '';
  303. // check and display form
  304. $oForm = $this->getObjectFormEdit($sDisplay);
  305. if (!$oForm)
  306. return $bErrorMsg && ($sMsg = '_sys_txt_error_occured') ? ($this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox(_t($sMsg))) : '';
  307. $aSpecificValues = array();
  308. if (!empty($CNF['OBJECT_METATAGS'])) {
  309. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  310. if ($oMetatags->locationsIsEnabled())
  311. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  312. }
  313. $oForm->initChecker($aContentInfo, $aSpecificValues);
  314. if (!$oForm->isSubmittedAndValid())
  315. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_edit', 'request_params' => ['params' => [$iContentId, $oForm->aParams['display']]]]) : $oForm->getCode();
  316. // update data in the DB
  317. $aTrackTextFieldsChanges = null;
  318. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  319. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  320. if (!$oForm->isValid())
  321. return $this->_bIsApi ? $this->prepareResponseAPI('form', $oForm->getCodeAPI(), ['request_uri' => 'entity_edit', 'request_params' => ['params' => [$iContentId, $oForm->aParams['display']]]]) : $oForm->getCode();
  322. else
  323. return ($sMsg = '_sys_txt_error_entry_update') && $this->_bIsApi ? $this->prepareResponseAPI('msg', $sMsg) : MsgBox(_t($sMsg));
  324. }
  325. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  326. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  327. if ($sResult)
  328. return $this->_bIsApi ? $this->prepareResponseAPI('msg', $sResult) : $sResult;
  329. /*
  330. * Process metas.
  331. * Note. It's essential to process metas a the very end,
  332. * because all data related to an entry should be already
  333. * processed and are ready to be passed to alert.
  334. */
  335. $this->_oModule->processMetasEdit($iContentId, $oForm);
  336. // Perform ACL action
  337. $this->_oModule->$sCheckFunction($aContentInfo, true);
  338. // Create alert about the completed action.
  339. $this->_oModule->alertAfterEdit($aContentInfo);
  340. // Redirect
  341. if ($this->_bIsApi)
  342. return $this->redirectAfterEdit($aContentInfo);
  343. else
  344. $this->redirectAfterEdit($aContentInfo);
  345. }
  346. /**
  347. * @return empty string on success or error message string on error
  348. */
  349. public function editData ($iContentId, $aValues, $sDisplay = false)
  350. {
  351. $CNF = &$this->_oModule->_oConfig->CNF;
  352. // get content data and profile info
  353. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  354. if (!$aContentInfo)
  355. return _t('_sys_txt_error_entry_is_not_defined');
  356. // get display form
  357. $oForm = $this->getObjectFormEdit($sDisplay);
  358. if (!$oForm)
  359. return _t('_sys_txt_error_occured');
  360. foreach ($aValues as $k => $v) {
  361. if (!isset($oForm->aInputs[$k]))
  362. return _t('_sys_txt_forms_unknown_field_err', $k);
  363. }
  364. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  365. $oForm->aParams['csrf']['disable'] = true;
  366. $oForm->initChecker($aContentInfo, $aContentInfo);
  367. // before edit hook
  368. $aTrackTextFieldsChanges = null;
  369. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  370. // update data in the DB
  371. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], $aValues, $aTrackTextFieldsChanges)) {
  372. return _t('_sys_txt_error_entry_update');
  373. }
  374. // after edit hook
  375. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  376. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  377. if ($sResult)
  378. return $sResult;
  379. /*
  380. * Process metas.
  381. * Note. It's essential to process metas a the very end,
  382. * because all data related to an entry should be already
  383. * processed and are ready to be passed to alert.
  384. */
  385. $this->_oModule->processMetasEdit($iContentId, $oForm);
  386. // Create alert about the completed action.
  387. $this->_oModule->alertAfterEdit($aContentInfo);
  388. return '';
  389. }
  390. protected function redirectAfterEdit($aContentInfo, $sUrl = '')
  391. {
  392. $CNF = &$this->_oModule->_oConfig->CNF;
  393. if ($sUrl == '')
  394. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  395. if(($mixedUrl = $this->_getRedirectFromContext('edit', $aContentInfo)) !== false)
  396. $sUrl = $mixedUrl;
  397. /**
  398. * @hooks
  399. * @hookdef hook-bx_base_general-redirect_after_edit '{module_name}', 'redirect_after_edit' - hook to override redirect URL which is used after content changing
  400. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  401. * except `ajax_mode` parameter in $extra_params is missing
  402. * @hook @ref hook-bx_base_general-redirect_after_edit
  403. */
  404. bx_alert($this->_oModule->getName(), 'redirect_after_edit', 0, false, [
  405. 'content' => $aContentInfo,
  406. 'override_result' => &$sUrl,
  407. ]);
  408. if($this->_bIsApi)
  409. return !empty($sUrl) ? bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000]) : [];
  410. $this->_redirectAndExit($sUrl);
  411. }
  412. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  413. {
  414. if (!$sCheckFunction)
  415. $sCheckFunction = 'checkAllowedDelete';
  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 ($sMsg = '_sys_txt_error_entry_is_not_defined') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  421. // check access
  422. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  423. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  424. // check and display form
  425. $oForm = $this->getObjectFormDelete($sDisplay);
  426. if (!$oForm)
  427. return ($sMsg = '_sys_txt_error_occured') && $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox(_t($sMsg));
  428. $oForm->initChecker($aContentInfo);
  429. if (!$oForm->isSubmittedAndValid())
  430. return $this->_bIsApi ? $oForm : $oForm->getCode();
  431. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  432. return $this->_bIsApi ? bx_api_get_msg($sError) : MsgBox($sError);
  433. // perform action
  434. $this->_oModule->$sCheckFunction($aContentInfo, true);
  435. // redirect
  436. if (bx_is_api())
  437. return $this->redirectAfterDelete($aContentInfo);
  438. else
  439. $this->redirectAfterDelete($aContentInfo);
  440. }
  441. protected function redirectAfterDelete($aContentInfo)
  442. {
  443. $CNF = &$this->_oModule->_oConfig->CNF;
  444. $sUrl = $CNF['URL_HOME'];
  445. $aMarkers = array(
  446. 'account_id' => getLoggedId(),
  447. 'profile_id' => bx_get_logged_profile_id(),
  448. );
  449. /**
  450. * @hooks
  451. * @hookdef hook-bx_base_general-redirect_after_delete '{module_name}', 'redirect_after_delete' - hook to override redirect URL which is used after content deletion
  452. * It's equivalent to @ref hook-bx_base_general-redirect_after_add
  453. * except `markers` parameter was added to $extra_params. It allows to override an array of markers, which can be parsed in URL.
  454. * @hook @ref hook-bx_base_general-redirect_after_delete
  455. */
  456. bx_alert($this->_oModule->getName(), 'redirect_after_delete', 0, false, [
  457. 'content' => $aContentInfo,
  458. 'markers' => &$aMarkers,
  459. 'override_result' => &$sUrl,
  460. ]);
  461. if($this->_bIsApi)
  462. return bx_api_get_block('redirect', ['uri' => bx_api_get_relative_url(BxDolPermalinks::getInstance()->permalink($sUrl)), 'timeout' => 1000]);
  463. $this->_redirectAndExit($sUrl, true, $aMarkers);
  464. }
  465. /**
  466. * Delete data entry
  467. * @param $iContentId entry id
  468. * @param $oForm optional content info array
  469. * @param $aContentInfo optional content info array
  470. * @param $oProfile optional content author profile
  471. * @return error string on error or empty string on success
  472. */
  473. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  474. {
  475. $CNF = &$this->_oModule->_oConfig->CNF;
  476. if (!$aContentInfo || !$oProfile)
  477. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  478. if (!$aContentInfo)
  479. return _t('_sys_txt_error_entry_is_not_defined');
  480. if (!$oForm)
  481. $oForm = $this->getObjectFormDelete();
  482. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  483. return _t('_sys_txt_error_entry_delete');
  484. // remove data from nested forms
  485. $aNestedForms = BxDolFormQuery::getNestedFormObjects($oForm->aParams['object']);
  486. foreach ($aNestedForms as $aNestedForm) {
  487. BxDolFormQuery::deleteDataFromNestedForm($aNestedForm['table'], $iContentId);
  488. }
  489. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  490. return $sResult;
  491. /**
  492. * @hooks
  493. * @hookdef hook-bx_base_general-deleted '{module_name}', 'deleted' - hook after content was deleted
  494. * - $unit_name - module name
  495. * - $action - equals `deleted`
  496. * - $object_id - content id
  497. * - $sender_id - not used
  498. * - $extra_params - array of additional params with the following array keys:
  499. * - `content` - [array] by ref, content info array as key&value pairs, can be overridden in hook processing
  500. * @hook @ref hook-bx_base_general-deleted
  501. */
  502. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']], false, [
  503. 'content' => &$aContentInfo
  504. ]);
  505. return '';
  506. }
  507. public function viewDataForm ($iContentId, $sDisplay = false)
  508. {
  509. $CNF = &$this->_oModule->_oConfig->CNF;
  510. // get content data and profile info
  511. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  512. if (!$aContentInfo)
  513. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_entry_is_not_defined') : MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  514. // check access
  515. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  516. return $this->_bIsApi ? bx_api_get_msg($sMsg) : MsgBox($sMsg);
  517. // get form
  518. $oForm = $this->getObjectFormView($sDisplay);
  519. if (!$oForm)
  520. return $this->_bIsApi ? bx_api_get_msg('_sys_txt_error_occured') : MsgBox(_t('_sys_txt_error_occured'));
  521. // process metatags
  522. $aSpecificValues =[];
  523. if (!empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) !== false) {
  524. if ($oMetatags->keywordsIsEnabled()) {
  525. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  526. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  527. }
  528. if ($oMetatags->locationsIsEnabled())
  529. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  530. }
  531. // display profile
  532. $oForm->initChecker($aContentInfo, $aSpecificValues);
  533. if ($this->_bIsApi)
  534. return [bx_api_get_block('entity_info', $oForm->getCodeAPI())];
  535. return $oForm->getCode();
  536. }
  537. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  538. {
  539. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  540. return $sMsg;
  541. return '';
  542. }
  543. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  544. {
  545. $CNF = &$this->_oModule->_oConfig->CNF;
  546. if(($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  547. $oPrivacy->deleteGroupCustomByContentId($iContentId);
  548. $sKey = 'FIELD_ALLOW_VIEW_TO';
  549. if(isset($CNF[$sKey], $aContentInfo[$CNF[$sKey]]) && ($iContextPid = (int)$aContentInfo[$CNF[$sKey]]) < 0) {
  550. $iContextPid = abs($iContextPid);
  551. if(($oContext = BxDolProfile::getInstance($iContextPid)) !== false) {
  552. $sModule = $oContext->getModule();
  553. $sMethod = 'on_content_deleted';
  554. if(bx_is_srv($sModule, $sMethod))
  555. bx_srv($sModule, $sMethod, [$this->_oModule->getName(), $iContentId, $oContext->getContentId()]);
  556. }
  557. }
  558. bx_audit(
  559. $iContentId,
  560. $this->_oModule->getName(),
  561. '_sys_audit_action_detete_content',
  562. $this->_oModule->_prepareAuditParams($aContentInfo)
  563. );
  564. return '';
  565. }
  566. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  567. {
  568. $CNF = &$this->_oModule->_oConfig->CNF;
  569. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  570. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  571. }
  572. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  573. {
  574. $CNF = &$this->_oModule->_oConfig->CNF;
  575. /*
  576. * Load update data.
  577. */
  578. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  579. if(isset($CNF['FIELD_PHOTO']))
  580. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  581. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  582. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  583. $this->_oModule->alertAfterAdd($aContentInfo);
  584. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  585. $oPrivacy->reassociateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  586. bx_audit(
  587. $iContentId,
  588. $this->_oModule->getName(),
  589. '_sys_audit_action_edit_content',
  590. $this->_oModule->_prepareAuditParams($aContentInfo)
  591. );
  592. return '';
  593. }
  594. public function onDataAddAfter ($iAccountId, $iContentId)
  595. {
  596. $CNF = &$this->_oModule->_oConfig->CNF;
  597. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  598. if(($oForm = $this->getObjectFormAdd()) !== false) {
  599. if(isset($CNF['FIELD_PHOTO']))
  600. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  601. }
  602. if(isset($CNF['FIELD_ALLOW_VIEW_TO']) && !empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false)
  603. $oPrivacy->associateGroupCustomWithContent($oProfile->id(), $iContentId, (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]);
  604. bx_audit(
  605. $iContentId,
  606. $this->_oModule->getName(),
  607. '_sys_audit_action_add_content',
  608. $this->_oModule->_prepareAuditParams($aContentInfo)
  609. );
  610. return '';
  611. }
  612. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  613. {
  614. $CNF = &$this->_oModule->_oConfig->CNF;
  615. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  616. $aMarkers = array(
  617. '{profile_id}',
  618. '{content_id}',
  619. '{module}',
  620. );
  621. $aReplacements = array(
  622. $oProfile ? $oProfile->id() : 0,
  623. $aContentInfo[$CNF['FIELD_ID']],
  624. $this->_oModule->getName(),
  625. );
  626. $s = str_replace($aMarkers, $aReplacements, $s);
  627. $s = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($s));
  628. return $s;
  629. }
  630. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  631. {
  632. if(!$bAsJson)
  633. return $mixedResponse;
  634. $aResponse = array(
  635. $sKey => $mixedResponse,
  636. '_dt' => 'json'
  637. );
  638. if(!empty($aAdditional) && is_array($aAdditional))
  639. $aResponse = array_merge($aResponse, $aAdditional);
  640. return $aResponse;
  641. }
  642. protected function prepareResponseAPI($sType, $mixedData, $aParams = [])
  643. {
  644. $aResult = [];
  645. switch($sType) {
  646. case 'msg':
  647. $aResult = bx_api_get_msg($mixedData);
  648. break;
  649. case 'form':
  650. $sModule = $this->_oModule->getName();
  651. $sRequestUrl = '/api.php?r=' . $sModule . '/' . $aParams['request_uri'];
  652. if(!empty($aParams['request_params']) && is_array($aParams['request_params']))
  653. $sRequestUrl = bx_append_url_params($sRequestUrl, $aParams['request_params']);
  654. $aResult = bx_api_get_block('form', $mixedData, [
  655. 'ext' => [
  656. 'name' => $sModule,
  657. 'request' => ['url' => $sRequestUrl, 'immutable' => true]
  658. ]
  659. ]);
  660. break;
  661. }
  662. return [$aResult];
  663. }
  664. protected function _setAbsoluteActionUrl($sType, &$oForm)
  665. {
  666. $CNF = &$this->_oModule->_oConfig->CNF;
  667. $sUri = '';
  668. if(($sKeyUri = 'URI_' . strtoupper($sType) . '_ENTRY') && !empty($CNF[$sKeyUri]))
  669. $sUri = $CNF[$sKeyUri];
  670. if(!$sUri && $sType == 'add' && ($sKeyUri = 'URI_EDIT_ENTRY') && !empty($CNF[$sKeyUri]))
  671. $sUri = str_replace('edit', 'create', $CNF[$sKeyUri]);
  672. if(!$sUri)
  673. return;
  674. $oForm->setAbsoluteActionUrl(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $sUri)));
  675. }
  676. protected function _getRedirectFromContext($sAction, $aContentInfo)
  677. {
  678. $CNF = &$this->_oModule->_oConfig->CNF;
  679. $sKey = 'FIELD_ALLOW_VIEW_TO';
  680. if(empty($CNF[$sKey]) || !isset($aContentInfo[$CNF[$sKey]]) || (int)$aContentInfo[$CNF[$sKey]] >= 0)
  681. return false;
  682. $iContextPid = abs($aContentInfo[$CNF[$sKey]]);
  683. $oContext = BxDolProfile::getInstance($iContextPid);
  684. if(!$oContext)
  685. return false;
  686. $sModuleContext = $oContext->getModule();
  687. $sMethodContext = 'on_content_' . $sAction . 'ed_redirect';
  688. if(!bx_is_srv($sModuleContext, $sMethodContext))
  689. return false;
  690. return bx_srv($sModuleContext, $sMethodContext, [$this->_oModule->getName(), $aContentInfo[$CNF['FIELD_ID']]]);
  691. }
  692. }
  693. /** @} */