BxBaseModGeneralFormsEntryHelper.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  88. return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
  89. }
  90. public function addDataForm ()
  91. {
  92. $CNF = &$this->_oModule->_oConfig->CNF;
  93. // check access
  94. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd())) {
  95. $oProfile = BxDolProfile::getInstance();
  96. $aProfileInfo = $oProfile->getInfo();
  97. if ($aProfileInfo['type'] == 'system' && is_subclass_of($this->_oModule, 'BxBaseModProfileModule') && $this->_oModule->serviceActAsProfile()) // special check for system profile is needed, because of incorrect error message
  98. return MsgBox(_t('_sys_txt_access_denied'));
  99. else
  100. return MsgBox($sMsg);
  101. }
  102. // check and display form
  103. $oForm = $this->getObjectFormAdd();
  104. if (!$oForm)
  105. return MsgBox(_t('_sys_txt_error_occured'));
  106. $oForm->initChecker();
  107. if (!$oForm->isSubmittedAndValid())
  108. return $oForm->getCode();
  109. // insert data into database
  110. $aValsToAdd = array ();
  111. $iContentId = $oForm->insert ($aValsToAdd);
  112. if (!$iContentId) {
  113. if (!$oForm->isValid())
  114. return $oForm->getCode();
  115. else
  116. return MsgBox(_t('_sys_txt_error_entry_creation'));
  117. }
  118. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  119. if ($sResult)
  120. return $sResult;
  121. // perform action
  122. $this->_oModule->checkAllowedAdd(true);
  123. // redirect
  124. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  125. $this->redirectAfterAdd($aContentInfo);
  126. }
  127. protected function redirectAfterAdd($aContentInfo)
  128. {
  129. $CNF = &$this->_oModule->_oConfig->CNF;
  130. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  131. }
  132. public function editDataForm ($iContentId, $sDisplay = false)
  133. {
  134. $CNF = &$this->_oModule->_oConfig->CNF;
  135. // get content data and profile info
  136. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  137. if (!$aContentInfo)
  138. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  139. // check access
  140. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedEdit($aContentInfo)))
  141. return MsgBox($sMsg);
  142. // check and display form
  143. $oForm = $this->getObjectFormEdit($sDisplay);
  144. if (!$oForm)
  145. return MsgBox(_t('_sys_txt_error_occured'));
  146. $aSpecificValues = array();
  147. if (!empty($CNF['OBJECT_METATAGS'])) {
  148. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  149. if ($oMetatags->locationsIsEnabled())
  150. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  151. }
  152. $oForm->initChecker($aContentInfo, $aSpecificValues);
  153. if (!$oForm->isSubmittedAndValid())
  154. return $oForm->getCode();
  155. // update data in the DB
  156. $aTrackTextFieldsChanges = null;
  157. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges);
  158. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  159. if (!$oForm->isValid())
  160. return $oForm->getCode();
  161. else
  162. return MsgBox(_t('_sys_txt_error_entry_update'));
  163. }
  164. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  165. if ($sResult)
  166. return $sResult;
  167. // perform action
  168. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  169. // redirect
  170. $this->redirectAfterEdit($aContentInfo);
  171. }
  172. protected function redirectAfterEdit($aContentInfo)
  173. {
  174. $CNF = &$this->_oModule->_oConfig->CNF;
  175. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  176. }
  177. public function deleteDataForm ($iContentId, $sDisplay = false)
  178. {
  179. $CNF = &$this->_oModule->_oConfig->CNF;
  180. if (false === $sDisplay)
  181. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  182. // get content data and profile info
  183. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  184. if (!$aContentInfo)
  185. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  186. // check access
  187. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedDelete($aContentInfo)))
  188. return MsgBox($sMsg);
  189. // check and display form
  190. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  191. if (!$oForm)
  192. return MsgBox(_t('_sys_txt_error_occured'));
  193. $oForm->initChecker($aContentInfo);
  194. if (!$oForm->isSubmittedAndValid())
  195. return $oForm->getCode();
  196. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  197. return MsgBox($sError);
  198. // perform action
  199. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  200. // redirect
  201. $this->redirectAfterDelete($aContentInfo);
  202. }
  203. protected function redirectAfterDelete($aContentInfo)
  204. {
  205. $CNF = &$this->_oModule->_oConfig->CNF;
  206. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  207. 'account_id' => getLoggedId(),
  208. 'profile_id' => bx_get_logged_profile_id(),
  209. ));
  210. }
  211. /**
  212. * Delete data entry
  213. * @param $iContentId entry id
  214. * @param $oForm optional content info array
  215. * @param $aContentInfo optional content info array
  216. * @param $oProfile optional content author profile
  217. * @return error string on error or empty string on success
  218. */
  219. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  220. {
  221. $CNF = &$this->_oModule->_oConfig->CNF;
  222. if (!$aContentInfo || !$oProfile)
  223. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  224. if (!$aContentInfo)
  225. return _t('_sys_txt_error_entry_is_not_defined');
  226. if (!$oForm)
  227. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'], $this->_oModule->_oTemplate);
  228. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  229. return _t('_sys_txt_error_entry_delete');
  230. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  231. return $sResult;
  232. // create an alert
  233. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  234. return '';
  235. }
  236. public function viewDataForm ($iContentId, $sDisplay = false)
  237. {
  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 ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  245. return MsgBox($sMsg);
  246. // get form
  247. $oForm = $this->getObjectFormView($sDisplay);
  248. if (!$oForm)
  249. return MsgBox(_t('_sys_txt_error_occured'));
  250. // process metatags
  251. if (!empty($CNF['OBJECT_METATAGS'])) {
  252. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  253. if ($oMetatags->keywordsIsEnabled()) {
  254. $aFields = $oMetatags->keywordsFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  255. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  256. }
  257. }
  258. // display profile
  259. $oForm->initChecker($aContentInfo);
  260. return $oForm->getCode();
  261. }
  262. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  263. {
  264. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  265. return $sMsg;
  266. return '';
  267. }
  268. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  269. {
  270. return '';
  271. }
  272. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  273. {
  274. }
  275. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  276. {
  277. $CNF = &$this->_oModule->_oConfig->CNF;
  278. if (!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works
  279. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  280. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  281. if ($oMetatags->keywordsIsEnabled())
  282. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']);
  283. if ($oMetatags->locationsIsEnabled())
  284. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  285. }
  286. return '';
  287. }
  288. public function onDataAddAfter ($iAccountId, $iContentId)
  289. {
  290. $this->_processMetas($iAccountId, $iContentId);
  291. return '';
  292. }
  293. protected function _processMetas($iAccountId, $iContentId)
  294. {
  295. $CNF = &$this->_oModule->_oConfig->CNF;
  296. if (!empty($CNF['OBJECT_METATAGS'])) {
  297. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  298. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  299. if ($oMetatags->keywordsIsEnabled() && $aContentInfo)
  300. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  301. if ($oMetatags->locationsIsEnabled() && $aContentInfo)
  302. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], $CNF['FIELD_LOCATION_PREFIX']);
  303. }
  304. }
  305. }
  306. /** @} */