BxBaseModGeneralFormsEntryHelper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. // process uploaded files
  131. if (isset($CNF['FIELD_PHOTO']))
  132. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
  133. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  134. return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
  135. }
  136. public function addDataForm ($sDisplay = false, $sCheckFunction = false)
  137. {
  138. if (!$sCheckFunction)
  139. $sCheckFunction = 'checkAllowedAdd';
  140. $CNF = &$this->_oModule->_oConfig->CNF;
  141. $bAsJson = false;
  142. // get form object
  143. $oForm = $this->getObjectFormAdd($sDisplay);
  144. if (!$oForm)
  145. return $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $bAsJson, 'msg');
  146. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  147. // check access
  148. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
  149. $oProfile = BxDolProfile::getInstance();
  150. 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
  151. return $this->prepareResponse(MsgBox(_t('_sys_txt_access_denied')), $bAsJson, 'msg');
  152. else
  153. return $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  154. }
  155. // check and display form
  156. $oForm->initChecker();
  157. if (!$oForm->isSubmittedAndValid())
  158. return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  159. 'form_id' => $oForm->getId()
  160. ));
  161. // insert data into database
  162. $aValsToAdd = array ();
  163. $iContentId = $oForm->insert ($aValsToAdd);
  164. if (!$iContentId) {
  165. if (!$oForm->isValid())
  166. return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  167. 'form_id' => $oForm->getId()
  168. ));
  169. else
  170. return $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $bAsJson, 'msg');
  171. }
  172. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  173. if ($sResult)
  174. return $this->prepareResponse($sResult, $bAsJson, 'msg');
  175. // process uploaded files
  176. if (isset($CNF['FIELD_PHOTO']))
  177. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
  178. // perform action
  179. $this->_oModule->$sCheckFunction(true);
  180. // redirect
  181. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  182. $this->redirectAfterAdd($aContentInfo);
  183. }
  184. public function redirectAfterAdd($aContentInfo)
  185. {
  186. $CNF = &$this->_oModule->_oConfig->CNF;
  187. $sUrl = 'page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']];
  188. if($this->_bAjaxMode) {
  189. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  190. exit;
  191. }
  192. else
  193. $this->_redirectAndExit($sUrl);
  194. }
  195. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  196. {
  197. if (!$sCheckFunction)
  198. $sCheckFunction = 'checkAllowedEdit';
  199. $CNF = &$this->_oModule->_oConfig->CNF;
  200. // get content data and profile info
  201. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  202. if (!$aContentInfo)
  203. return $bErrorMsg ? MsgBox(_t('_sys_txt_error_entry_is_not_defined')) : '';
  204. // check access
  205. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  206. return $bErrorMsg ? MsgBox($sMsg) : '';
  207. // check and display form
  208. $oForm = $this->getObjectFormEdit($sDisplay);
  209. if (!$oForm)
  210. return $bErrorMsg ? MsgBox(_t('_sys_txt_error_occured')) : '';
  211. $aSpecificValues = array();
  212. if (!empty($CNF['OBJECT_METATAGS'])) {
  213. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  214. if ($oMetatags->locationsIsEnabled())
  215. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  216. }
  217. $oForm->initChecker($aContentInfo, $aSpecificValues);
  218. if (!$oForm->isSubmittedAndValid())
  219. return $oForm->getCode();
  220. // update data in the DB
  221. $aTrackTextFieldsChanges = null;
  222. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges);
  223. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  224. if (!$oForm->isValid())
  225. return $oForm->getCode();
  226. else
  227. return MsgBox(_t('_sys_txt_error_entry_update'));
  228. }
  229. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  230. if ($sResult)
  231. return $sResult;
  232. // process uploaded files
  233. if (isset($CNF['FIELD_PHOTO']))
  234. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  235. // perform action
  236. $this->_oModule->$sCheckFunction($aContentInfo, true);
  237. // redirect
  238. $this->redirectAfterEdit($aContentInfo);
  239. }
  240. protected function redirectAfterEdit($aContentInfo)
  241. {
  242. $CNF = &$this->_oModule->_oConfig->CNF;
  243. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  244. }
  245. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  246. {
  247. if (!$sCheckFunction)
  248. $sCheckFunction = 'checkAllowedDelete';
  249. $CNF = &$this->_oModule->_oConfig->CNF;
  250. // get content data and profile info
  251. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  252. if (!$aContentInfo)
  253. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  254. // check access
  255. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  256. return MsgBox($sMsg);
  257. // check and display form
  258. $oForm = $this->getObjectFormDelete($sDisplay);
  259. if (!$oForm)
  260. return MsgBox(_t('_sys_txt_error_occured'));
  261. $oForm->initChecker($aContentInfo);
  262. if (!$oForm->isSubmittedAndValid())
  263. return $oForm->getCode();
  264. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  265. return MsgBox($sError);
  266. // perform action
  267. $this->_oModule->$sCheckFunction($aContentInfo, true);
  268. // redirect
  269. $this->redirectAfterDelete($aContentInfo);
  270. }
  271. protected function redirectAfterDelete($aContentInfo)
  272. {
  273. $CNF = &$this->_oModule->_oConfig->CNF;
  274. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  275. 'account_id' => getLoggedId(),
  276. 'profile_id' => bx_get_logged_profile_id(),
  277. ));
  278. }
  279. /**
  280. * Delete data entry
  281. * @param $iContentId entry id
  282. * @param $oForm optional content info array
  283. * @param $aContentInfo optional content info array
  284. * @param $oProfile optional content author profile
  285. * @return error string on error or empty string on success
  286. */
  287. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  288. {
  289. $CNF = &$this->_oModule->_oConfig->CNF;
  290. if (!$aContentInfo || !$oProfile)
  291. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  292. if (!$aContentInfo)
  293. return _t('_sys_txt_error_entry_is_not_defined');
  294. if (!$oForm)
  295. $oForm = $this->getObjectFormDelete();
  296. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  297. return _t('_sys_txt_error_entry_delete');
  298. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  299. return $sResult;
  300. // create an alert
  301. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  302. return '';
  303. }
  304. public function viewDataForm ($iContentId, $sDisplay = false)
  305. {
  306. $CNF = &$this->_oModule->_oConfig->CNF;
  307. // get content data and profile info
  308. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  309. if (!$aContentInfo)
  310. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  311. // check access
  312. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  313. return MsgBox($sMsg);
  314. // get form
  315. $oForm = $this->getObjectFormView($sDisplay);
  316. if (!$oForm)
  317. return MsgBox(_t('_sys_txt_error_occured'));
  318. // process metatags
  319. if (!empty($CNF['OBJECT_METATAGS'])) {
  320. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  321. if ($oMetatags->keywordsIsEnabled()) {
  322. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  323. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  324. }
  325. }
  326. // display profile
  327. $oForm->initChecker($aContentInfo);
  328. return $oForm->getCode();
  329. }
  330. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  331. {
  332. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  333. return $sMsg;
  334. return '';
  335. }
  336. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  337. {
  338. return '';
  339. }
  340. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  341. {
  342. }
  343. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  344. {
  345. $CNF = &$this->_oModule->_oConfig->CNF;
  346. if (!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works
  347. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  348. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  349. $oMetatags->metaAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']);
  350. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($oForm->aInputs[$CNF['FIELD_LOCATION_PREFIX']]) && $oMetatags->locationsIsEnabled())
  351. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  352. if (!empty($CNF['FIELD_LABELS']) && ($aLabels = bx_get($CNF['FIELD_LABELS'])) && $oMetatags->keywordsIsEnabled()) {
  353. foreach ($aLabels as $sLabel)
  354. $oMetatags->keywordsAddOne($aContentInfo[$CNF['FIELD_ID']], $sLabel, false);
  355. }
  356. }
  357. return '';
  358. }
  359. public function onDataAddAfter ($iAccountId, $iContentId)
  360. {
  361. $this->_processMetas($iAccountId, $iContentId);
  362. return '';
  363. }
  364. protected function _processMetas($iAccountId, $iContentId)
  365. {
  366. $CNF = &$this->_oModule->_oConfig->CNF;
  367. if (!empty($CNF['OBJECT_METATAGS'])) {
  368. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  369. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  370. if ($aContentInfo)
  371. $oMetatags->metaAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  372. if ($oMetatags->locationsIsEnabled() && $aContentInfo)
  373. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], $CNF['FIELD_LOCATION_PREFIX']);
  374. if ($aContentInfo && !empty($CNF['FIELD_LABELS']) && ($aLabels = bx_get($CNF['FIELD_LABELS'])) && $oMetatags->keywordsIsEnabled()) {
  375. foreach ($aLabels as $sLabel)
  376. $oMetatags->keywordsAddOne($aContentInfo[$CNF['FIELD_ID']], $sLabel, false);
  377. }
  378. }
  379. }
  380. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  381. {
  382. $CNF = &$this->_oModule->_oConfig->CNF;
  383. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  384. $aMarkers = array(
  385. '{profile_id}',
  386. '{content_id}',
  387. '{module}',
  388. );
  389. $aReplacements = array(
  390. $oProfile ? $oProfile->id() : 0,
  391. $aContentInfo[$CNF['FIELD_ID']],
  392. $this->_oModule->getName(),
  393. );
  394. $s = str_replace($aMarkers, $aReplacements, $s);
  395. $s = BxDolPermalinks::getInstance()->permalink($s);
  396. if (false === strpos($s, 'http://') && false === strpos($s, 'https://'))
  397. $s = BX_DOL_URL_ROOT . $s;
  398. return $s;
  399. }
  400. protected function prepareResponse($mixedResponse, $bAsJson = false, $sKey = 'msg', $aAdditional = array())
  401. {
  402. if(!$bAsJson)
  403. return $mixedResponse;
  404. $aResponse = array(
  405. $sKey => $mixedResponse
  406. );
  407. if(!empty($aAdditional) && is_array($aAdditional))
  408. $aResponse = array_merge($aResponse, $aAdditional);
  409. return $aResponse;
  410. }
  411. }
  412. /** @} */