BxBaseModGeneralFormsEntryHelper.php 35 KB

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