BxBaseModGeneralFormsEntryHelper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. /**
  18. * 'Ajax Mode' determines the format of response. If it's TRUE the response
  19. * (a form or an error appeared during form creation) should be returned
  20. * as text during initial loading, while all other responses appeared after
  21. * form submit should be arrays, which are ready to path to echoJson.
  22. */
  23. protected $_bAjaxMode;
  24. protected $_bDynamicMode;
  25. public function __construct($oModule)
  26. {
  27. parent::__construct();
  28. $this->_oModule = $oModule;
  29. $this->_bDynamicMode = false;
  30. $this->_bAjaxMode = false;
  31. $mixedAjaxMode = bx_get('ajax_mode');
  32. if($mixedAjaxMode !== false)
  33. $this->setAjaxMode($mixedAjaxMode);
  34. }
  35. public function setAjaxMode($bAjaxMode)
  36. {
  37. $this->_bAjaxMode = (bool)$bAjaxMode;
  38. if($this->_bAjaxMode)
  39. $this->setDynamicMode(true);
  40. }
  41. public function setDynamicMode($bDynamicMode)
  42. {
  43. $this->_bDynamicMode = (bool)$bDynamicMode;
  44. }
  45. public function getObjectStorage()
  46. {
  47. return BxDolStorage::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_STORAGE']);
  48. }
  49. public function getObjectFormAdd ($sDisplay = false)
  50. {
  51. $CNF = &$this->_oModule->_oConfig->CNF;
  52. if (false === $sDisplay)
  53. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'];
  54. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  55. }
  56. public function getObjectFormEdit ($sDisplay = false)
  57. {
  58. $CNF = &$this->_oModule->_oConfig->CNF;
  59. if (false === $sDisplay)
  60. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  61. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  62. }
  63. public function getObjectFormView ($sDisplay = false)
  64. {
  65. $CNF = &$this->_oModule->_oConfig->CNF;
  66. if (false === $sDisplay)
  67. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'];
  68. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  69. }
  70. public function getObjectFormDelete ($sDisplay = false)
  71. {
  72. $CNF = &$this->_oModule->_oConfig->CNF;
  73. if (false === $sDisplay)
  74. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  75. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  76. }
  77. public function viewDataEntry ($iContentId)
  78. {
  79. // get content data and profile info
  80. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  81. if (!$aContentInfo)
  82. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  83. // check access
  84. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  85. return MsgBox($sMsg);
  86. return $this->_oModule->_oTemplate->entryText($aContentInfo);
  87. }
  88. public function addData ($iProfile, $aValues, $sDisplay = false)
  89. {
  90. $CNF = &$this->_oModule->_oConfig->CNF;
  91. // check and display form
  92. $oForm = $this->getObjectFormAdd($sDisplay);
  93. if (!$oForm)
  94. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  95. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  96. $oForm->aParams['csrf']['disable'] = true;
  97. if(!empty($oForm->aParams['db']['submit_name'])) {
  98. $sSubmitName = false;
  99. if (is_array($oForm->aParams['db']['submit_name'])) {
  100. foreach ($oForm->aParams['db']['submit_name'] as $sVal) {
  101. if (isset($oForm->aInputs[$sVal])) {
  102. $sSubmitName = $sVal;
  103. break;
  104. }
  105. }
  106. }
  107. else {
  108. $sSubmitName = $oForm->aParams['db']['submit_name'];
  109. }
  110. if ($sSubmitName && isset($oForm->aInputs[$sSubmitName]))
  111. $aValues[$sSubmitName] = $oForm->aInputs[$sSubmitName]['value'];
  112. }
  113. $oForm->initChecker(array(), $aValues);
  114. if (!$oForm->isSubmittedAndValid())
  115. return array('code' => 2, 'message' => '_sys_txt_error_occured');
  116. // insert data into database
  117. $aValsToAdd = array ();
  118. if(isset($CNF['FIELD_AUTHOR']))
  119. $aValsToAdd[$CNF['FIELD_AUTHOR']] = $iProfile;
  120. $iContentId = $oForm->insert($aValsToAdd);
  121. if (!$iContentId) {
  122. if (!$oForm->isValid())
  123. return array('code' => 2, 'message' => '_sys_txt_error_occured');
  124. else
  125. return array('code' => 3, 'message' => '_sys_txt_error_entry_creation');
  126. }
  127. $sResult = $this->onDataAddAfter(BxDolProfile::getInstance($iProfile)->getAccountId(), $iContentId);
  128. if($sResult)
  129. return array('code' => 4, 'message' => $sResult);
  130. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  131. return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
  132. }
  133. public function addDataForm ($sDisplay = false, $sCheckFunction = false)
  134. {
  135. if (!$sCheckFunction)
  136. $sCheckFunction = 'checkAllowedAdd';
  137. $CNF = &$this->_oModule->_oConfig->CNF;
  138. $bAsJson = false;
  139. // get form object
  140. $oForm = $this->getObjectFormAdd($sDisplay);
  141. if (!$oForm)
  142. return $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $bAsJson, 'msg');
  143. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  144. // check access
  145. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
  146. $oProfile = BxDolProfile::getInstance();
  147. if ($oProfile && ($aProfileInfo = $oProfile->getInfo()) && $aProfileInfo['type'] == 'system' && is_subclass_of($this->_oModule, 'BxBaseModProfileModule') && $this->_oModule->serviceActAsProfile()) // special check for system profile is needed, because of incorrect error message
  148. return $this->prepareResponse(MsgBox(_t('_sys_txt_access_denied')), $bAsJson, 'msg');
  149. else
  150. return $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  151. }
  152. // check and display form
  153. $oForm->initChecker();
  154. if (!$oForm->isSubmittedAndValid())
  155. return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  156. 'form_id' => $oForm->getId()
  157. ));
  158. // insert data into database
  159. $aValsToAdd = array ();
  160. $iContentId = $oForm->insert ($aValsToAdd);
  161. if (!$iContentId) {
  162. if (!$oForm->isValid())
  163. return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  164. 'form_id' => $oForm->getId()
  165. ));
  166. else
  167. return $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $bAsJson, 'msg');
  168. }
  169. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  170. if ($sResult)
  171. return $this->prepareResponse($sResult, $bAsJson, 'msg');
  172. // perform action
  173. $this->_oModule->$sCheckFunction(true);
  174. // redirect
  175. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  176. $this->redirectAfterAdd($aContentInfo);
  177. }
  178. public function redirectAfterAdd($aContentInfo)
  179. {
  180. $CNF = &$this->_oModule->_oConfig->CNF;
  181. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  182. if($this->_bAjaxMode) {
  183. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  184. exit;
  185. }
  186. else
  187. $this->_redirectAndExit($sUrl);
  188. }
  189. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  190. {
  191. if (!$sCheckFunction)
  192. $sCheckFunction = 'checkAllowedEdit';
  193. $CNF = &$this->_oModule->_oConfig->CNF;
  194. // get content data and profile info
  195. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  196. if (!$aContentInfo)
  197. return $bErrorMsg ? MsgBox(_t('_sys_txt_error_entry_is_not_defined')) : '';
  198. // check access
  199. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  200. return $bErrorMsg ? MsgBox($sMsg) : '';
  201. // check and display form
  202. $oForm = $this->getObjectFormEdit($sDisplay);
  203. if (!$oForm)
  204. return $bErrorMsg ? MsgBox(_t('_sys_txt_error_occured')) : '';
  205. $aSpecificValues = array();
  206. if (!empty($CNF['OBJECT_METATAGS'])) {
  207. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  208. if ($oMetatags->locationsIsEnabled())
  209. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  210. }
  211. $oForm->initChecker($aContentInfo, $aSpecificValues);
  212. if (!$oForm->isSubmittedAndValid())
  213. return $oForm->getCode();
  214. // update data in the DB
  215. $aTrackTextFieldsChanges = null;
  216. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  217. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  218. if (!$oForm->isValid())
  219. return $oForm->getCode();
  220. else
  221. return MsgBox(_t('_sys_txt_error_entry_update'));
  222. }
  223. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  224. if ($sResult)
  225. return $sResult;
  226. // perform action
  227. $this->_oModule->$sCheckFunction($aContentInfo, true);
  228. // redirect
  229. $this->redirectAfterEdit($aContentInfo);
  230. }
  231. protected function redirectAfterEdit($aContentInfo)
  232. {
  233. $CNF = &$this->_oModule->_oConfig->CNF;
  234. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  235. }
  236. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  237. {
  238. if (!$sCheckFunction)
  239. $sCheckFunction = 'checkAllowedDelete';
  240. $CNF = &$this->_oModule->_oConfig->CNF;
  241. // get content data and profile info
  242. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  243. if (!$aContentInfo)
  244. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  245. // check access
  246. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  247. return MsgBox($sMsg);
  248. // check and display form
  249. $oForm = $this->getObjectFormDelete($sDisplay);
  250. if (!$oForm)
  251. return MsgBox(_t('_sys_txt_error_occured'));
  252. $oForm->initChecker($aContentInfo);
  253. if (!$oForm->isSubmittedAndValid())
  254. return $oForm->getCode();
  255. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  256. return MsgBox($sError);
  257. // perform action
  258. $this->_oModule->$sCheckFunction($aContentInfo, true);
  259. // redirect
  260. $this->redirectAfterDelete($aContentInfo);
  261. }
  262. protected function redirectAfterDelete($aContentInfo)
  263. {
  264. $CNF = &$this->_oModule->_oConfig->CNF;
  265. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  266. 'account_id' => getLoggedId(),
  267. 'profile_id' => bx_get_logged_profile_id(),
  268. ));
  269. }
  270. /**
  271. * Delete data entry
  272. * @param $iContentId entry id
  273. * @param $oForm optional content info array
  274. * @param $aContentInfo optional content info array
  275. * @param $oProfile optional content author profile
  276. * @return error string on error or empty string on success
  277. */
  278. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  279. {
  280. $CNF = &$this->_oModule->_oConfig->CNF;
  281. if (!$aContentInfo || !$oProfile)
  282. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  283. if (!$aContentInfo)
  284. return _t('_sys_txt_error_entry_is_not_defined');
  285. if (!$oForm)
  286. $oForm = $this->getObjectFormDelete();
  287. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  288. return _t('_sys_txt_error_entry_delete');
  289. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  290. return $sResult;
  291. // create an alert
  292. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  293. return '';
  294. }
  295. public function viewDataForm ($iContentId, $sDisplay = false)
  296. {
  297. $CNF = &$this->_oModule->_oConfig->CNF;
  298. // get content data and profile info
  299. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  300. if (!$aContentInfo)
  301. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  302. // check access
  303. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  304. return MsgBox($sMsg);
  305. // get form
  306. $oForm = $this->getObjectFormView($sDisplay);
  307. if (!$oForm)
  308. return MsgBox(_t('_sys_txt_error_occured'));
  309. // process metatags
  310. if (!empty($CNF['OBJECT_METATAGS'])) {
  311. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  312. if ($oMetatags->keywordsIsEnabled()) {
  313. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  314. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  315. }
  316. }
  317. // display profile
  318. $oForm->initChecker($aContentInfo);
  319. return $oForm->getCode();
  320. }
  321. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  322. {
  323. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  324. return $sMsg;
  325. return '';
  326. }
  327. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  328. {
  329. return '';
  330. }
  331. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges, &$oProfile, &$oForm)
  332. {
  333. $CNF = &$this->_oModule->_oConfig->CNF;
  334. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  335. $oForm->addTrackFields($CNF['FIELD_STATUS'], $aContentInfo);
  336. }
  337. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  338. {
  339. $CNF = &$this->_oModule->_oConfig->CNF;
  340. list($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  341. if(isset($CNF['FIELD_PHOTO']))
  342. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  343. if(!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works
  344. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  345. $oMetatags->metaAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']);
  346. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($oForm->aInputs[$CNF['FIELD_LOCATION_PREFIX']]) && $oMetatags->locationsIsEnabled())
  347. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  348. if (!empty($CNF['FIELD_LABELS']) && ($aLabels = bx_get($CNF['FIELD_LABELS'])) && $oMetatags->keywordsIsEnabled()) {
  349. foreach ($aLabels as $sLabel)
  350. $oMetatags->keywordsAddOne($aContentInfo[$CNF['FIELD_ID']], $sLabel, false);
  351. }
  352. }
  353. if(isset($CNF['FIELD_STATUS']) && ($aTrackResult = $oForm->isTrackFieldChanged($CNF['FIELD_STATUS'], true)) !== false)
  354. if($aTrackResult['old'] == 'failed' && $aTrackResult['new'] == 'active')
  355. $this->_alertAfterAdd($aContentInfo);
  356. return '';
  357. }
  358. public function onDataAddAfter ($iAccountId, $iContentId)
  359. {
  360. $CNF = &$this->_oModule->_oConfig->CNF;
  361. if(($oForm = $this->getObjectFormAdd()) !== false) {
  362. if(isset($CNF['FIELD_PHOTO']))
  363. $oForm->processFiles($CNF['FIELD_PHOTO'], $iContentId, true);
  364. }
  365. $this->_processMetas($iAccountId, $iContentId);
  366. return '';
  367. }
  368. protected function _processMetas($iAccountId, $iContentId)
  369. {
  370. $CNF = &$this->_oModule->_oConfig->CNF;
  371. if (!empty($CNF['OBJECT_METATAGS'])) {
  372. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  373. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  374. if ($aContentInfo)
  375. $oMetatags->metaAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  376. if ($oMetatags->locationsIsEnabled() && $aContentInfo)
  377. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], $CNF['FIELD_LOCATION_PREFIX']);
  378. if ($aContentInfo && !empty($CNF['FIELD_LABELS']) && ($aLabels = bx_get($CNF['FIELD_LABELS'])) && $oMetatags->keywordsIsEnabled()) {
  379. foreach ($aLabels as $sLabel)
  380. $oMetatags->keywordsAddOne($aContentInfo[$CNF['FIELD_ID']], $sLabel, false);
  381. }
  382. }
  383. }
  384. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  385. {
  386. $CNF = &$this->_oModule->_oConfig->CNF;
  387. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  388. $aMarkers = array(
  389. '{profile_id}',
  390. '{content_id}',
  391. '{module}',
  392. );
  393. $aReplacements = array(
  394. $oProfile ? $oProfile->id() : 0,
  395. $aContentInfo[$CNF['FIELD_ID']],
  396. $this->_oModule->getName(),
  397. );
  398. $s = str_replace($aMarkers, $aReplacements, $s);
  399. $s = BxDolPermalinks::getInstance()->permalink($s);
  400. if (false === strpos($s, 'http://') && false === strpos($s, 'https://'))
  401. $s = BX_DOL_URL_ROOT . $s;
  402. return $s;
  403. }
  404. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  405. {
  406. if(!$bAsJson)
  407. return $mixedResponse;
  408. $aResponse = array(
  409. $sKey => $mixedResponse,
  410. '_dt' => 'json'
  411. );
  412. if(!empty($aAdditional) && is_array($aAdditional))
  413. $aResponse = array_merge($aResponse, $aAdditional);
  414. return $aResponse;
  415. }
  416. }
  417. /** @} */