BxBaseModGeneralFormsEntryHelper.php 8.9 KB

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