BxNotesNoteForms.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 Notes Notes
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolProfileForms');
  12. bx_import('BxDolProfile');
  13. bx_import('BxDolForm');
  14. /**
  15. * Note forms functions
  16. */
  17. class BxNotesNoteForms extends BxDolProfileForms {
  18. protected $_oModule;
  19. public function __construct($oModule) {
  20. parent::__construct();
  21. $this->_oModule = $oModule;
  22. }
  23. /**
  24. * @return add data html
  25. */
  26. public function addDataForm () {
  27. // check access
  28. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->isAllowedAdd()))
  29. return MsgBox($sMsg);
  30. // check and display form
  31. $oForm = BxDolForm::getObjectInstance('bx_notes', 'bx_notes_note_add');
  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('_bx_notes_txt_error_note_creation'));
  45. }
  46. /* TODO: status
  47. // approve note if auto-approval is enabled and profile status is 'pending'
  48. $sStatus = $oProfile->getStatus();
  49. $isAutoApprove = getParam('bx_notes_autoapproval') ? true : false;
  50. if ($sStatus == BX_PROFILE_STATUS_PENDING && $isAutoApprove)
  51. $oProfile->approve(BX_PROFILE_ACTION_AUTO);
  52. */
  53. // perform action
  54. $this->_oModule->isAllowedAdd(true);
  55. // alert
  56. bx_alert($this->_oModule->getName(), 'added', $iContentId);
  57. // redirect
  58. $this->_redirectAndExit('page.php?i=view-note&id=' . $iContentId);
  59. }
  60. /**
  61. * @return edit data html
  62. */
  63. public function editDataForm ($iContentId, $sDisplay = 'bx_notes_note_edit') {
  64. // get content data and profile info
  65. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  66. if (!$aContentInfo)
  67. return MsgBox(_t('_bx_notes_txt_error_note_is_not_defined'));
  68. // check access
  69. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->isAllowedEdit($aContentInfo)))
  70. return MsgBox($sMsg);
  71. // check and display form
  72. $oForm = BxDolForm::getObjectInstance('bx_notes', $sDisplay);
  73. if (!$oForm)
  74. return MsgBox(_t('_sys_txt_error_occured'));
  75. $oForm->initChecker($aContentInfo);
  76. if (!$oForm->isSubmittedAndValid())
  77. return $oForm->getCode();
  78. // update data in the DB
  79. $aTrackTextFieldsChanges = null;
  80. /* TODO: status
  81. $isAutoApprove = getParam('bx_notes_autoapproval') ? true : false;
  82. if (!$isAutoApprove && $oProfile->isActive()))
  83. $aTrackTextFieldsChanges = array ();
  84. */
  85. if (!$oForm->update ($aContentInfo['id'], array(), $aTrackTextFieldsChanges)) {
  86. if (!$oForm->isValid())
  87. return $oForm->getCode();
  88. else
  89. return MsgBox(_t('_bx_notes_txt_error_note_update'));
  90. }
  91. // change profile to 'pending' only if profile is 'active'
  92. if (/*!$isAutoApprove &&*/ $oProfile->isActive() && !empty($aTrackTextFieldsChanges['changed_fields']))
  93. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO);
  94. // perform action
  95. $this->_oModule->isAllowedEdit($aContentInfo, true);
  96. // create an alert
  97. bx_alert($this->_oModule->getName(), 'edited', $aContentInfo['id']);
  98. // redirect
  99. $this->_redirectAndExit('page.php?i=view-note&id=' . $aContentInfo['id']);
  100. }
  101. /**
  102. * @return delete data html
  103. */
  104. public function deleteDataForm ($iContentId, $sDisplay = 'bx_notes_note_delete') {
  105. // get content data and profile info
  106. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  107. if (!$aContentInfo)
  108. return MsgBox(_t('_bx_notes_txt_error_note_is_not_defined'));
  109. // check access
  110. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->isAllowedDelete($aContentInfo)))
  111. return MsgBox($sMsg);
  112. // check and display form
  113. $oForm = BxDolForm::getObjectInstance('bx_notes', $sDisplay);
  114. if (!$oForm)
  115. return MsgBox(_t('_sys_txt_error_occured'));
  116. $oForm->initChecker($aContentInfo);
  117. if (!$oForm->isSubmittedAndValid())
  118. return $oForm->getCode();
  119. if (!$oForm->delete ($aContentInfo['id'])) {
  120. if (!$oForm->isValid())
  121. return $oForm->getCode();
  122. else
  123. return MsgBox(_t('_bx_notes_txt_error_note_delete'));
  124. }
  125. // perform action
  126. $this->_oModule->isAllowedDelete($aContentInfo, true);
  127. // create an alert
  128. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo['id']);
  129. // redirect
  130. $this->_redirectAndExit('page.php?i=notes-home');
  131. }
  132. /**
  133. * @return view data html
  134. */
  135. public function viewDataForm ($iContentId) {
  136. // get content data and profile info
  137. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  138. if (!$aContentInfo)
  139. return MsgBox(_t('_bx_notes_txt_error_note_is_not_defined'));
  140. // check access
  141. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->isAllowedView($aContentInfo)))
  142. return MsgBox($sMsg);
  143. // get form
  144. $oForm = BxDolForm::getObjectInstance('bx_notes', 'bx_notes_note_view');
  145. if (!$oForm)
  146. return MsgBox(_t('_sys_txt_error_occured'));
  147. // perform action
  148. $this->_oModule->isAllowedView($aContentInfo);
  149. // display profile
  150. $oForm->initChecker($aContentInfo);
  151. return $oForm->getCode();
  152. }
  153. /**
  154. * @return main content text
  155. */
  156. public function viewDataText ($iContentId) {
  157. // get content data and profile info
  158. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  159. if (!$aContentInfo)
  160. return MsgBox(_t('_bx_notes_txt_error_note_is_not_defined'));
  161. // check access
  162. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->isAllowedView($aContentInfo)))
  163. return MsgBox($sMsg);
  164. return $aContentInfo[BxNotesConfig::$FIELD_TEXT];
  165. }
  166. /**
  167. * @return main content text
  168. */
  169. public function viewDataEntry ($iContentId) {
  170. // get content data and profile info
  171. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  172. if (!$aContentInfo)
  173. return MsgBox(_t('_bx_notes_txt_error_note_is_not_defined'));
  174. // check access
  175. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->isAllowedView($aContentInfo)))
  176. return MsgBox($sMsg);
  177. return $this->_oModule->_oTemplate->entryText($aContentInfo);
  178. }
  179. /**
  180. * @return array of profile object, profile info and content info
  181. */
  182. protected function _getProfileAndContentData ($iContentId) {
  183. $aContentInfo = array();
  184. $oProfile = false;
  185. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  186. if (!$aContentInfo)
  187. return array (false, false, false);
  188. $oProfile = BxDolProfile::getInstance($aContentInfo['author']);
  189. if (!$oProfile) {
  190. bx_import('BxDolProfileUndefined');
  191. $oProfile = BxDolProfileUndefined::getInstance();
  192. }
  193. return array ($oProfile, $aContentInfo);
  194. }
  195. }
  196. /** @} */