1
0

BxBaseModTextFormsEntryHelper.php 8.7 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 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. $aParams = isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? array('privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) : array();
  57. bx_alert($this->_oModule->getName(), 'added', $iContentId, false, $aParams);
  58. // redirect
  59. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId);
  60. }
  61. /**
  62. * @return edit data html
  63. */
  64. public function editDataForm ($iContentId, $sDisplay = false)
  65. {
  66. $CNF = &$this->_oModule->_oConfig->CNF;
  67. if (false === $sDisplay)
  68. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  69. // get content data and profile info
  70. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  71. if (!$aContentInfo)
  72. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  73. // check access
  74. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedEdit($aContentInfo)))
  75. return MsgBox($sMsg);
  76. // check and display form
  77. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay);
  78. if (!$oForm)
  79. return MsgBox(_t('_sys_txt_error_occured'));
  80. $oForm->initChecker($aContentInfo);
  81. if (!$oForm->isSubmittedAndValid())
  82. return $oForm->getCode();
  83. // update data in the DB
  84. $aTrackTextFieldsChanges = null;
  85. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  86. if (!$oForm->isValid())
  87. return $oForm->getCode();
  88. else
  89. return MsgBox(_t('_sys_txt_error_entry_update'));
  90. }
  91. if (!($aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId)))
  92. return MsgBox(_t('_sys_txt_error_occured'));
  93. // change profile to 'pending' only if profile is 'active'
  94. if ($oProfile->isActive() && !empty($aTrackTextFieldsChanges['changed_fields']))
  95. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO);
  96. // perform action
  97. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  98. // create an alert
  99. bx_import('BxDolPrivacy');
  100. bx_alert($this->_oModule->getName(), 'edited', $aContentInfo[$CNF['FIELD_ID']], false, array('privacy_view' => $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]));
  101. // redirect
  102. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  103. }
  104. /**
  105. * @return delete data html
  106. */
  107. public function deleteDataForm ($iContentId, $sDisplay = false)
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. if (false === $sDisplay)
  111. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  112. // get content data and profile info
  113. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  114. if (!$aContentInfo)
  115. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  116. // check access
  117. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedDelete($aContentInfo)))
  118. return MsgBox($sMsg);
  119. // check and display form
  120. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay);
  121. if (!$oForm)
  122. return MsgBox(_t('_sys_txt_error_occured'));
  123. $oForm->initChecker($aContentInfo);
  124. if (!$oForm->isSubmittedAndValid())
  125. return $oForm->getCode();
  126. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']])) {
  127. if (!$oForm->isValid())
  128. return $oForm->getCode();
  129. else
  130. return MsgBox(_t('_sys_txt_error_entry_delete'));
  131. }
  132. // perform action
  133. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  134. // create an alert
  135. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  136. // redirect
  137. bx_import('BxDolPermalinks');
  138. $this->_redirectAndExit(BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']));
  139. }
  140. /**
  141. * @return view data html
  142. */
  143. public function viewDataForm ($iContentId)
  144. {
  145. $CNF = &$this->_oModule->_oConfig->CNF;
  146. // get content data and profile info
  147. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  148. if (!$aContentInfo)
  149. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  150. // check access
  151. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  152. return MsgBox($sMsg);
  153. // get form
  154. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  155. if (!$oForm)
  156. return MsgBox(_t('_sys_txt_error_occured'));
  157. // perform action
  158. $this->_oModule->checkAllowedView($aContentInfo);
  159. // display profile
  160. $oForm->initChecker($aContentInfo);
  161. return $oForm->getCode();
  162. }
  163. /**
  164. * @return main content text
  165. */
  166. public function viewDataText ($iContentId)
  167. {
  168. $CNF = &$this->_oModule->_oConfig->CNF;
  169. // get content data and profile info
  170. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  171. if (!$aContentInfo)
  172. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  173. // check access
  174. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  175. return MsgBox($sMsg);
  176. return $aContentInfo[$CNF['FIELD_TEXT']];
  177. }
  178. /**
  179. * @return main content text
  180. */
  181. public function viewDataEntry ($iContentId)
  182. {
  183. // get content data and profile info
  184. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  185. if (!$aContentInfo)
  186. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  187. // check access
  188. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  189. return MsgBox($sMsg);
  190. return $this->_oModule->_oTemplate->entryText($aContentInfo);
  191. }
  192. /**
  193. * @return array of profile object, profile info and content info
  194. */
  195. protected function _getProfileAndContentData ($iContentId)
  196. {
  197. $CNF = &$this->_oModule->_oConfig->CNF;
  198. $aContentInfo = array();
  199. $oProfile = false;
  200. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  201. if (!$aContentInfo)
  202. return array (false, false);
  203. $oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
  204. if (!$oProfile) {
  205. bx_import('BxDolProfileUndefined');
  206. $oProfile = BxDolProfileUndefined::getInstance();
  207. }
  208. return array ($oProfile, $aContentInfo);
  209. }
  210. }
  211. /** @} */