BxBaseModGeneralFormsEntryHelper.php 15 KB

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