BxBaseModGeneralFormsEntryHelper.php 9.9 KB

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