BxBaseModGeneralFormsEntryHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseGeneral Base classes for modules
  7. * @ingroup TridentModules
  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. if (false === $sDisplay)
  33. $sDisplay = $this->_oModule->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  34. return BxDolForm::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  35. }
  36. public function getObjectFormDelete ()
  37. {
  38. return BxDolForm::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_FORM_ENTRY'], $this->_oModule->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'], $this->_oModule->_oTemplate);
  39. }
  40. public function viewDataEntry ($iContentId)
  41. {
  42. // get content data and profile info
  43. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  44. if (!$aContentInfo)
  45. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  46. // check access
  47. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  48. return MsgBox($sMsg);
  49. return $this->_oModule->_oTemplate->entryText($aContentInfo);
  50. }
  51. public function addDataForm ()
  52. {
  53. $CNF = &$this->_oModule->_oConfig->CNF;
  54. // check access
  55. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
  56. return MsgBox($sMsg);
  57. // check and display form
  58. $oForm = $this->getObjectFormAdd();
  59. if (!$oForm)
  60. return MsgBox(_t('_sys_txt_error_occured'));
  61. $oForm->initChecker();
  62. if (!$oForm->isSubmittedAndValid())
  63. return $oForm->getCode();
  64. // insert data into database
  65. $aValsToAdd = array ();
  66. $iContentId = $oForm->insert ($aValsToAdd);
  67. if (!$iContentId) {
  68. if (!$oForm->isValid())
  69. return $oForm->getCode();
  70. else
  71. return MsgBox(_t('_sys_txt_error_entry_creation'));
  72. }
  73. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  74. if ($sResult)
  75. return $sResult;
  76. // perform action
  77. $this->_oModule->checkAllowedAdd(true);
  78. // redirect
  79. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId);
  80. }
  81. public function editDataForm ($iContentId, $sDisplay = false)
  82. {
  83. $CNF = &$this->_oModule->_oConfig->CNF;
  84. if (false === $sDisplay)
  85. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  86. // get content data and profile info
  87. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  88. if (!$aContentInfo)
  89. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  90. // check access
  91. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedEdit($aContentInfo)))
  92. return MsgBox($sMsg);
  93. // check and display form
  94. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  95. if (!$oForm)
  96. return MsgBox(_t('_sys_txt_error_occured'));
  97. $aSpecificValues = array();
  98. if (!empty($CNF['OBJECT_METATAGS'])) {
  99. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  100. if ($oMetatags->locationsIsEnabled())
  101. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  102. }
  103. $oForm->initChecker($aContentInfo, $aSpecificValues);
  104. if (!$oForm->isSubmittedAndValid())
  105. return $oForm->getCode();
  106. // update data in the DB
  107. $aTrackTextFieldsChanges = null;
  108. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges);
  109. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  110. if (!$oForm->isValid())
  111. return $oForm->getCode();
  112. else
  113. return MsgBox(_t('_sys_txt_error_entry_update'));
  114. }
  115. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  116. if ($sResult)
  117. return $sResult;
  118. // perform action
  119. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  120. // redirect
  121. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  122. }
  123. public function deleteDataForm ($iContentId, $sDisplay = false)
  124. {
  125. $CNF = &$this->_oModule->_oConfig->CNF;
  126. if (false === $sDisplay)
  127. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  128. // get content data and profile info
  129. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  130. if (!$aContentInfo)
  131. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  132. // check access
  133. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedDelete($aContentInfo)))
  134. return MsgBox($sMsg);
  135. // check and display form
  136. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  137. if (!$oForm)
  138. return MsgBox(_t('_sys_txt_error_occured'));
  139. $oForm->initChecker($aContentInfo);
  140. if (!$oForm->isSubmittedAndValid())
  141. return $oForm->getCode();
  142. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  143. return MsgBox($sError);
  144. // perform action
  145. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  146. // redirect
  147. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  148. 'account_id' => getLoggedId(),
  149. 'profile_id' => bx_get_logged_profile_id(),
  150. ));
  151. }
  152. /**
  153. * Delete data entry
  154. * @param $iContentId entry id
  155. * @param $oForm optional content info array
  156. * @param $aContentInfo optional content info array
  157. * @param $oProfile optional content author profile
  158. * @return error string on error or empty string on success
  159. */
  160. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  161. {
  162. $CNF = &$this->_oModule->_oConfig->CNF;
  163. if (!$aContentInfo || !$oProfile)
  164. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  165. if (!$aContentInfo)
  166. return _t('_sys_txt_error_entry_is_not_defined');
  167. if (!$oForm)
  168. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'], $this->_oModule->_oTemplate);
  169. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  170. return _t('_sys_txt_error_entry_delete');
  171. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  172. return $sResult;
  173. // create an alert
  174. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  175. return '';
  176. }
  177. public function viewDataForm ($iContentId, $sDisplay = false)
  178. {
  179. $CNF = &$this->_oModule->_oConfig->CNF;
  180. if (false === $sDisplay)
  181. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'];
  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 ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  188. return MsgBox($sMsg);
  189. // get 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. // process metatags
  194. if (!empty($CNF['OBJECT_METATAGS'])) {
  195. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  196. if ($oMetatags->keywordsIsEnabled()) {
  197. $aFields = $oMetatags->keywordsFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  198. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  199. }
  200. }
  201. // display profile
  202. $oForm->initChecker($aContentInfo);
  203. return $oForm->getCode();
  204. }
  205. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  206. {
  207. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  208. return $sMsg;
  209. return '';
  210. }
  211. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  212. {
  213. return '';
  214. }
  215. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  216. {
  217. }
  218. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  219. {
  220. $CNF = &$this->_oModule->_oConfig->CNF;
  221. if (!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works
  222. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  223. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  224. if ($oMetatags->keywordsIsEnabled())
  225. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']);
  226. if ($oMetatags->locationsIsEnabled())
  227. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  228. }
  229. return '';
  230. }
  231. public function onDataAddAfter ($iAccountId, $iContentId)
  232. {
  233. $CNF = &$this->_oModule->_oConfig->CNF;
  234. if (!empty($CNF['OBJECT_METATAGS'])) {
  235. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  236. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  237. if ($oMetatags->keywordsIsEnabled())
  238. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  239. if ($oMetatags->locationsIsEnabled())
  240. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], $CNF['FIELD_LOCATION_PREFIX']);
  241. }
  242. return '';
  243. }
  244. }
  245. /** @} */