BxBaseModGeneralFormsEntryHelper.php 16 KB

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