BxBaseModGeneralFormsEntryHelper.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  100. bx_import('BxDolMetatags');
  101. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  102. if ($oMetatags->keywordsIsEnabled())
  103. $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $sDisplay);
  104. if ($oMetatags->locationsIsEnabled())
  105. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  106. }
  107. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile);
  108. if ($sResult)
  109. return $sResult;
  110. // perform action
  111. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  112. // redirect
  113. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  114. }
  115. public function deleteDataForm ($iContentId, $sDisplay = false)
  116. {
  117. $CNF = &$this->_oModule->_oConfig->CNF;
  118. if (false === $sDisplay)
  119. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  120. // get content data and profile info
  121. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  122. if (!$aContentInfo)
  123. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  124. // check access
  125. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedDelete($aContentInfo)))
  126. return MsgBox($sMsg);
  127. // check and display form
  128. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  129. if (!$oForm)
  130. return MsgBox(_t('_sys_txt_error_occured'));
  131. $oForm->initChecker($aContentInfo);
  132. if (!$oForm->isSubmittedAndValid())
  133. return $oForm->getCode();
  134. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  135. return MsgBox($sError);
  136. // perform action
  137. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  138. // redirect
  139. bx_import('BxDolPermalinks');
  140. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  141. 'account_id' => method_exists($oProfile, 'getAccountId') ? $oProfile->getAccountId() : getLoggedId(),
  142. 'profile_id' => $oProfile->id() ? $oProfile->id() : bx_get_logged_profile_id(),
  143. ));
  144. }
  145. /**
  146. * Delete data entry
  147. * @param $iContentId entry id
  148. * @param $oForm optional content info array
  149. * @param $aContentInfo optional content info array
  150. * @param $oProfile optional content author profile
  151. * @return error string on error or empty string on success
  152. */
  153. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  154. {
  155. $CNF = &$this->_oModule->_oConfig->CNF;
  156. if (!$aContentInfo || !$oProfile)
  157. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  158. if (!$aContentInfo)
  159. return _t('_sys_txt_error_entry_is_not_defined');
  160. if (!$oForm)
  161. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'], $this->_oModule->_oTemplate);
  162. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  163. return _t('_sys_txt_error_entry_delete');
  164. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  165. return $sResult;
  166. // create an alert
  167. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  168. return '';
  169. }
  170. public function viewDataForm ($iContentId, $sDisplay = false)
  171. {
  172. $CNF = &$this->_oModule->_oConfig->CNF;
  173. if (false === $sDisplay)
  174. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'];
  175. // get content data and profile info
  176. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  177. if (!$aContentInfo)
  178. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  179. // check access
  180. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  181. return MsgBox($sMsg);
  182. // get form
  183. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  184. if (!$oForm)
  185. return MsgBox(_t('_sys_txt_error_occured'));
  186. // process metatags
  187. if (!empty($CNF['OBJECT_METATAGS'])) {
  188. bx_import('BxDolMetatags');
  189. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  190. if ($oMetatags->keywordsIsEnabled()) {
  191. $aFields = $oMetatags->keywordsFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  192. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  193. }
  194. }
  195. // display profile
  196. $oForm->initChecker($aContentInfo);
  197. return $oForm->getCode();
  198. }
  199. protected function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  200. {
  201. return '';
  202. }
  203. protected function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  204. {
  205. }
  206. protected function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile)
  207. {
  208. return '';
  209. }
  210. protected function onDataAddAfter ($iContentId)
  211. {
  212. return '';
  213. }
  214. }
  215. /** @} */