1
0

BxBaseModGeneralFormsEntryHelper.php 12 KB

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