BxBaseModGeneralFormsEntryHelper.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. bx_import('BxDolProfileForms');
  12. bx_import('BxDolForm');
  13. /**
  14. * Entry forms helper functions
  15. */
  16. class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
  17. {
  18. protected $_oModule;
  19. public function __construct($oModule)
  20. {
  21. parent::__construct();
  22. $this->_oModule = $oModule;
  23. }
  24. public function addDataForm ()
  25. {
  26. $CNF = &$this->_oModule->_oConfig->CNF;
  27. // check access
  28. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
  29. return MsgBox($sMsg);
  30. // check and display form
  31. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'], $this->_oModule->_oTemplate);
  32. if (!$oForm)
  33. return MsgBox(_t('_sys_txt_error_occured'));
  34. $oForm->initChecker();
  35. if (!$oForm->isSubmittedAndValid())
  36. return $oForm->getCode();
  37. // insert data into database
  38. $aValsToAdd = array ();
  39. $iContentId = $oForm->insert ($aValsToAdd);
  40. if (!$iContentId) {
  41. if (!$oForm->isValid())
  42. return $oForm->getCode();
  43. else
  44. return MsgBox(_t('_sys_txt_error_entry_creation'));
  45. }
  46. if (!empty($CNF['OBJECT_METATAGS'])) {
  47. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  48. bx_import('BxDolMetatags');
  49. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  50. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  51. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], $CNF['FIELD_LOCATION_PREFIX']);
  52. }
  53. $sResult = $this->onDataAddAfter ($iContentId);
  54. if ($sResult)
  55. return $sResult;
  56. // perform action
  57. $this->_oModule->checkAllowedAdd(true);
  58. // redirect
  59. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId);
  60. }
  61. public function editDataForm ($iContentId, $sDisplay = false)
  62. {
  63. $CNF = &$this->_oModule->_oConfig->CNF;
  64. if (false === $sDisplay)
  65. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  66. // get content data and profile info
  67. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  68. if (!$aContentInfo)
  69. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  70. // check access
  71. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedEdit($aContentInfo)))
  72. return MsgBox($sMsg);
  73. // check and display form
  74. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  75. if (!$oForm)
  76. return MsgBox(_t('_sys_txt_error_occured'));
  77. $aSpecificValues = array();
  78. if (!empty($CNF['OBJECT_METATAGS'])) {
  79. bx_import('BxDolMetatags');
  80. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  81. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  82. }
  83. $oForm->initChecker($aContentInfo, $aSpecificValues);
  84. if (!$oForm->isSubmittedAndValid())
  85. return $oForm->getCode();
  86. // update data in the DB
  87. $aTrackTextFieldsChanges = null;
  88. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges);
  89. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  90. if (!$oForm->isValid())
  91. return $oForm->getCode();
  92. else
  93. return MsgBox(_t('_sys_txt_error_entry_update'));
  94. }
  95. if (!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works
  96. bx_import('BxDolMetatags');
  97. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  98. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $sDisplay);
  99. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  100. }
  101. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile);
  102. if ($sResult)
  103. return $sResult;
  104. // perform action
  105. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  106. // redirect
  107. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  108. }
  109. public function deleteDataForm ($iContentId, $sDisplay = false)
  110. {
  111. $CNF = &$this->_oModule->_oConfig->CNF;
  112. if (false === $sDisplay)
  113. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  114. // get content data and profile info
  115. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  116. if (!$aContentInfo)
  117. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  118. // check access
  119. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedDelete($aContentInfo)))
  120. return MsgBox($sMsg);
  121. // check and display form
  122. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  123. if (!$oForm)
  124. return MsgBox(_t('_sys_txt_error_occured'));
  125. $oForm->initChecker($aContentInfo);
  126. if (!$oForm->isSubmittedAndValid())
  127. return $oForm->getCode();
  128. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  129. return MsgBox($sError);
  130. // perform action
  131. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  132. // redirect
  133. bx_import('BxDolPermalinks');
  134. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  135. 'account_id' => $oProfile->getAccountId(),
  136. 'profile_id' => $oProfile->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. if (!empty($CNF['OBJECT_METATAGS'])) {
  181. bx_import('BxDolMetatags');
  182. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  183. $aFields = $oMetatags->keywordsFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  184. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  185. }
  186. // display profile
  187. $oForm->initChecker($aContentInfo);
  188. return $oForm->getCode();
  189. }
  190. protected function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  191. {
  192. return '';
  193. }
  194. protected function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  195. {
  196. }
  197. protected function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile)
  198. {
  199. return '';
  200. }
  201. protected function onDataAddAfter ($iContentId)
  202. {
  203. return '';
  204. }
  205. }
  206. /** @} */