1
0

BxBaseModGeneralFormsEntryHelper.php 7.5 KB

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