BxBaseModGeneralFormsEntryHelper.php 11 KB

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