BxBaseModGeneralFormsEntryHelper.php 17 KB

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