BxBaseModGeneralFormsEntryHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 addDataForm ()
  60. {
  61. $CNF = &$this->_oModule->_oConfig->CNF;
  62. // check access
  63. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
  64. return MsgBox($sMsg);
  65. // check and display form
  66. $oForm = $this->getObjectFormAdd();
  67. if (!$oForm)
  68. return MsgBox(_t('_sys_txt_error_occured'));
  69. $oForm->initChecker();
  70. if (!$oForm->isSubmittedAndValid())
  71. return $oForm->getCode();
  72. // insert data into database
  73. $aValsToAdd = array ();
  74. $iContentId = $oForm->insert ($aValsToAdd);
  75. if (!$iContentId) {
  76. if (!$oForm->isValid())
  77. return $oForm->getCode();
  78. else
  79. return MsgBox(_t('_sys_txt_error_entry_creation'));
  80. }
  81. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  82. if ($sResult)
  83. return $sResult;
  84. // perform action
  85. $this->_oModule->checkAllowedAdd(true);
  86. // redirect
  87. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  88. $this->redirectAfterAdd($aContentInfo);
  89. }
  90. protected function redirectAfterAdd($aContentInfo)
  91. {
  92. $CNF = &$this->_oModule->_oConfig->CNF;
  93. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  94. }
  95. public function editDataForm ($iContentId, $sDisplay = false)
  96. {
  97. $CNF = &$this->_oModule->_oConfig->CNF;
  98. // get content data and profile info
  99. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  100. if (!$aContentInfo)
  101. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  102. // check access
  103. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedEdit($aContentInfo)))
  104. return MsgBox($sMsg);
  105. // check and display form
  106. $oForm = $this->getObjectFormEdit($sDisplay);
  107. if (!$oForm)
  108. return MsgBox(_t('_sys_txt_error_occured'));
  109. $aSpecificValues = array();
  110. if (!empty($CNF['OBJECT_METATAGS'])) {
  111. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  112. if ($oMetatags->locationsIsEnabled())
  113. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  114. }
  115. $oForm->initChecker($aContentInfo, $aSpecificValues);
  116. if (!$oForm->isSubmittedAndValid())
  117. return $oForm->getCode();
  118. // update data in the DB
  119. $aTrackTextFieldsChanges = null;
  120. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges);
  121. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  122. if (!$oForm->isValid())
  123. return $oForm->getCode();
  124. else
  125. return MsgBox(_t('_sys_txt_error_entry_update'));
  126. }
  127. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  128. if ($sResult)
  129. return $sResult;
  130. // perform action
  131. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  132. // redirect
  133. $this->redirectAfterEdit($aContentInfo);
  134. }
  135. protected function redirectAfterEdit($aContentInfo)
  136. {
  137. $CNF = &$this->_oModule->_oConfig->CNF;
  138. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  139. }
  140. public function deleteDataForm ($iContentId, $sDisplay = false)
  141. {
  142. $CNF = &$this->_oModule->_oConfig->CNF;
  143. if (false === $sDisplay)
  144. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  145. // get content data and profile info
  146. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  147. if (!$aContentInfo)
  148. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  149. // check access
  150. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedDelete($aContentInfo)))
  151. return MsgBox($sMsg);
  152. // check and display form
  153. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  154. if (!$oForm)
  155. return MsgBox(_t('_sys_txt_error_occured'));
  156. $oForm->initChecker($aContentInfo);
  157. if (!$oForm->isSubmittedAndValid())
  158. return $oForm->getCode();
  159. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  160. return MsgBox($sError);
  161. // perform action
  162. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  163. // redirect
  164. $this->redirectAfterDelete($aContentInfo);
  165. }
  166. protected function redirectAfterDelete($aContentInfo)
  167. {
  168. $CNF = &$this->_oModule->_oConfig->CNF;
  169. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  170. 'account_id' => getLoggedId(),
  171. 'profile_id' => bx_get_logged_profile_id(),
  172. ));
  173. }
  174. /**
  175. * Delete data entry
  176. * @param $iContentId entry id
  177. * @param $oForm optional content info array
  178. * @param $aContentInfo optional content info array
  179. * @param $oProfile optional content author profile
  180. * @return error string on error or empty string on success
  181. */
  182. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  183. {
  184. $CNF = &$this->_oModule->_oConfig->CNF;
  185. if (!$aContentInfo || !$oProfile)
  186. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  187. if (!$aContentInfo)
  188. return _t('_sys_txt_error_entry_is_not_defined');
  189. if (!$oForm)
  190. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'], $this->_oModule->_oTemplate);
  191. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  192. return _t('_sys_txt_error_entry_delete');
  193. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  194. return $sResult;
  195. // create an alert
  196. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  197. return '';
  198. }
  199. public function viewDataForm ($iContentId, $sDisplay = false)
  200. {
  201. $CNF = &$this->_oModule->_oConfig->CNF;
  202. // get content data and profile info
  203. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  204. if (!$aContentInfo)
  205. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  206. // check access
  207. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  208. return MsgBox($sMsg);
  209. // get form
  210. $oForm = $this->getObjectFormView($sDisplay);
  211. if (!$oForm)
  212. return MsgBox(_t('_sys_txt_error_occured'));
  213. // process metatags
  214. if (!empty($CNF['OBJECT_METATAGS'])) {
  215. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  216. if ($oMetatags->keywordsIsEnabled()) {
  217. $aFields = $oMetatags->keywordsFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  218. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  219. }
  220. }
  221. // display profile
  222. $oForm->initChecker($aContentInfo);
  223. return $oForm->getCode();
  224. }
  225. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  226. {
  227. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  228. return $sMsg;
  229. return '';
  230. }
  231. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  232. {
  233. return '';
  234. }
  235. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  236. {
  237. }
  238. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  239. {
  240. $CNF = &$this->_oModule->_oConfig->CNF;
  241. if (!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works
  242. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  243. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  244. if ($oMetatags->keywordsIsEnabled())
  245. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']);
  246. if ($oMetatags->locationsIsEnabled())
  247. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  248. }
  249. return '';
  250. }
  251. public function onDataAddAfter ($iAccountId, $iContentId)
  252. {
  253. $this->_processMetas($iAccountId, $iContentId);
  254. return '';
  255. }
  256. protected function _processMetas($iAccountId, $iContentId)
  257. {
  258. $CNF = &$this->_oModule->_oConfig->CNF;
  259. if (!empty($CNF['OBJECT_METATAGS'])) {
  260. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  261. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  262. if ($oMetatags->keywordsIsEnabled() && $aContentInfo)
  263. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  264. if ($oMetatags->locationsIsEnabled() && $aContentInfo)
  265. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], $CNF['FIELD_LOCATION_PREFIX']);
  266. }
  267. }
  268. }
  269. /** @} */