BxBaseModGeneralFormsEntryHelper.php 18 KB

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