1
0

BxBaseModGeneralFormsEntryHelper.php 34 KB

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