1
0

BxBaseModGeneralFormsEntryHelper.php 11 KB

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