1
0

BxBaseModTextFormsEntryHelper.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 BaseText Base classes for text modules
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolProfileForms');
  12. bx_import('BxDolProfile');
  13. bx_import('BxDolForm');
  14. /**
  15. * Entry forms helper functions
  16. */
  17. class BxBaseModTextFormsEntryHelper extends BxDolProfileForms
  18. {
  19. protected $_oModule;
  20. public function __construct($oModule)
  21. {
  22. parent::__construct();
  23. $this->_oModule = $oModule;
  24. }
  25. /**
  26. * @return add data html
  27. */
  28. public function addDataForm ()
  29. {
  30. $CNF = &$this->_oModule->_oConfig->CNF;
  31. // check access
  32. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
  33. return MsgBox($sMsg);
  34. // check and display form
  35. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  36. if (!$oForm)
  37. return MsgBox(_t('_sys_txt_error_occured'));
  38. $oForm->initChecker();
  39. if (!$oForm->isSubmittedAndValid())
  40. return $oForm->getCode();
  41. // insert data into database
  42. $aValsToAdd = array ();
  43. $iContentId = $oForm->insert ($aValsToAdd);
  44. if (!$iContentId) {
  45. if (!$oForm->isValid())
  46. return $oForm->getCode();
  47. else
  48. return MsgBox(_t('_sys_txt_error_entry_creation'));
  49. }
  50. if (!($aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId)))
  51. return MsgBox(_t('_sys_txt_error_occured'));
  52. // perform action
  53. $this->_oModule->checkAllowedAdd(true);
  54. // alert
  55. bx_import('BxDolPrivacy');
  56. bx_alert($this->_oModule->getName(), 'added', $iContentId, false, array('privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]));
  57. // redirect
  58. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId);
  59. }
  60. /**
  61. * @return edit data html
  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);
  77. if (!$oForm)
  78. return MsgBox(_t('_sys_txt_error_occured'));
  79. $oForm->initChecker($aContentInfo);
  80. if (!$oForm->isSubmittedAndValid())
  81. return $oForm->getCode();
  82. // update data in the DB
  83. $aTrackTextFieldsChanges = null;
  84. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  85. if (!$oForm->isValid())
  86. return $oForm->getCode();
  87. else
  88. return MsgBox(_t('_sys_txt_error_entry_update'));
  89. }
  90. if (!($aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId)))
  91. return MsgBox(_t('_sys_txt_error_occured'));
  92. // change profile to 'pending' only if profile is 'active'
  93. if ($oProfile->isActive() && !empty($aTrackTextFieldsChanges['changed_fields']))
  94. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO);
  95. // perform action
  96. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  97. // create an alert
  98. bx_import('BxDolPrivacy');
  99. bx_alert($this->_oModule->getName(), 'edited', $aContentInfo[$CNF['FIELD_ID']], false, array('privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]));
  100. // redirect
  101. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  102. }
  103. /**
  104. * @return delete data html
  105. */
  106. public function deleteDataForm ($iContentId, $sDisplay = false)
  107. {
  108. $CNF = &$this->_oModule->_oConfig->CNF;
  109. if (false === $sDisplay)
  110. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  111. // get content data and profile info
  112. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  113. if (!$aContentInfo)
  114. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  115. // check access
  116. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedDelete($aContentInfo)))
  117. return MsgBox($sMsg);
  118. // check and display form
  119. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay);
  120. if (!$oForm)
  121. return MsgBox(_t('_sys_txt_error_occured'));
  122. $oForm->initChecker($aContentInfo);
  123. if (!$oForm->isSubmittedAndValid())
  124. return $oForm->getCode();
  125. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']])) {
  126. if (!$oForm->isValid())
  127. return $oForm->getCode();
  128. else
  129. return MsgBox(_t('_sys_txt_error_entry_delete'));
  130. }
  131. // perform action
  132. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  133. // create an alert
  134. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  135. // redirect
  136. $this->_redirectAndExit('page.php?i=' . $CNF['URI_HOME']);
  137. }
  138. /**
  139. * @return view data html
  140. */
  141. public function viewDataForm ($iContentId)
  142. {
  143. $CNF = &$this->_oModule->_oConfig->CNF;
  144. // get content data and profile info
  145. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  146. if (!$aContentInfo)
  147. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  148. // check access
  149. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  150. return MsgBox($sMsg);
  151. // get form
  152. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  153. if (!$oForm)
  154. return MsgBox(_t('_sys_txt_error_occured'));
  155. // perform action
  156. $this->_oModule->checkAllowedView($aContentInfo);
  157. // display profile
  158. $oForm->initChecker($aContentInfo);
  159. return $oForm->getCode();
  160. }
  161. /**
  162. * @return main content text
  163. */
  164. public function viewDataText ($iContentId)
  165. {
  166. $CNF = &$this->_oModule->_oConfig->CNF;
  167. // get content data and profile info
  168. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  169. if (!$aContentInfo)
  170. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  171. // check access
  172. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  173. return MsgBox($sMsg);
  174. return $aContentInfo[$CNF['FIELD_TEXT']];
  175. }
  176. /**
  177. * @return main content text
  178. */
  179. public function viewDataEntry ($iContentId)
  180. {
  181. // get content data and profile info
  182. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  183. if (!$aContentInfo)
  184. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  185. // check access
  186. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  187. return MsgBox($sMsg);
  188. return $this->_oModule->_oTemplate->entryText($aContentInfo);
  189. }
  190. /**
  191. * @return array of profile object, profile info and content info
  192. */
  193. protected function _getProfileAndContentData ($iContentId)
  194. {
  195. $CNF = &$this->_oModule->_oConfig->CNF;
  196. $aContentInfo = array();
  197. $oProfile = false;
  198. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  199. if (!$aContentInfo)
  200. return array (false, false);
  201. $oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
  202. if (!$oProfile) {
  203. bx_import('BxDolProfileUndefined');
  204. $oProfile = BxDolProfileUndefined::getInstance();
  205. }
  206. return array ($oProfile, $aContentInfo);
  207. }
  208. }
  209. /** @} */