BxForumModule.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 Forum Forum
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. class BxForumModule extends BxBaseModTextModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. public function sortParticipants ($aParticipants, $iProfileIdLastComment, $iProfileIdAuthor, $iProfileIdCurrent = 0)
  18. {
  19. if (!$iProfileIdCurrent)
  20. $iProfileIdCurrent = bx_get_logged_profile_id();
  21. $aMoveUp = array($iProfileIdCurrent, $iProfileIdLastComment, $iProfileIdAuthor);
  22. asort($aParticipants, SORT_NUMERIC);
  23. foreach ($aMoveUp as $iProfileId) {
  24. if (!isset($aParticipants[$iProfileId]))
  25. continue;
  26. $a = array($iProfileId => $aParticipants[$iProfileId]);
  27. unset($aParticipants[$iProfileId]);
  28. $aParticipants = $a + $aParticipants;
  29. }
  30. return $aParticipants;
  31. }
  32. /**
  33. * Action methods
  34. */
  35. public function actionUpdateStatus($sAction = '', $iContentId = 0)
  36. {
  37. if(empty($sAction) && bx_get('action') !== false)
  38. $sAction = bx_process_input(bx_get('action'));
  39. if(empty($iContentId) && bx_get('id') !== false)
  40. $iContentId = (int)bx_get('id');
  41. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  42. if(empty($aContentInfo) || !is_array($aContentInfo))
  43. return echoJson(array('code' => 1, 'message' => _t('_bx_forum_err_not_found')));
  44. $sMethodCheck = 'checkAllowed' . bx_gen_method_name($sAction) . 'AnyEntry';
  45. $sResult = $this->$sMethodCheck($aContentInfo);
  46. if($sResult !== CHECK_ACTION_RESULT_ALLOWED)
  47. return echoJson(array('code' => 2, 'message' => $sResult));
  48. if(!$this->_oDb->updateStatus($sAction, $aContentInfo))
  49. return echoJson(array('code' => 3, 'message' => _t('_error occured')));
  50. echoJson(array('code' => 0, 'id' => $iContentId, 'reload' => 1));
  51. }
  52. /**
  53. * Service methods
  54. */
  55. public function serviceBrowseNew ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  56. {
  57. $sType = 'new';
  58. if($sUnitView != 'table')
  59. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  60. return $this->_serviceBrowseTable(array('type' => $sType));
  61. }
  62. public function serviceBrowseLatest($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  63. {
  64. $sType = 'latest';
  65. if($sUnitView != 'table')
  66. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  67. return $this->_serviceBrowseTable(array('type' => $sType));
  68. }
  69. public function serviceBrowseTop($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  70. {
  71. $sType = 'top';
  72. if($sUnitView != 'table')
  73. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  74. return $this->_serviceBrowseTable(array('type' => $sType));
  75. }
  76. public function serviceBrowseCategories($bEmptyMessage = true)
  77. {
  78. return $this->_oTemplate->getCategories($bEmptyMessage);
  79. }
  80. public function serviceBrowseCategory($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  81. {
  82. $sType = 'category';
  83. $iCategory = bx_process_input(bx_get('category'), BX_DATA_INT);
  84. $aCategory = $this->_oDb->getCategories(array('type' => 'by_category', 'category' => $iCategory));
  85. if(!empty($aCategory['visible_for_levels']) && !BxDolAcl::getInstance()->isMemberLevelInSet($aCategory['visible_for_levels']))
  86. return $bEmptyMessage ? MsgBox(_t('_sys_txt_access_denied')) : '';
  87. if($sUnitView != 'table')
  88. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  89. return $this->_serviceBrowseTable(array('type' => $sType, 'where' => array('cat' => $iCategory)));
  90. }
  91. /**
  92. * Get number of unread messages for spme profile
  93. * @param $iProfileId - profile to get unread messages for, if omitted then currently logged is profile is used
  94. * @return integer
  95. */
  96. public function serviceGetUnrepliedDiscussionsNum ($iProfileId = 0)
  97. {
  98. if (!$iProfileId)
  99. $iProfileId = bx_get_logged_profile_id();
  100. return $this->_oDb->getUnrepliedDiscussionsNum((int)$iProfileId);
  101. }
  102. /**
  103. * Update last comment time and author
  104. */
  105. public function serviceTriggerCommentPost ($iContentId, $iProfileId, $iCommentId, $iTimestamp = 0, $sCommentText = '')
  106. {
  107. $CNT = $this->_oConfig->CNF;
  108. $iContentId = (int)$iContentId;
  109. if(!$iContentId)
  110. return false;
  111. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  112. if(!$aContentInfo)
  113. return false;
  114. if(!$iTimestamp)
  115. $iTimestamp = time();
  116. if(!$this->_oDb->updateLastCommentTimeProfile((int)$iContentId, (int)$iProfileId, $iCommentId, $iTimestamp))
  117. return false;
  118. // send notification to author
  119. if($iProfileId != $aContentInfo[$CNT['FIELD_AUTHOR']]) {
  120. $oProfile = BxDolProfile::getInstance($iProfileId);
  121. if($oProfile)
  122. sendMailTemplate('bx_forum_new_reply', 0, $aContentInfo[$CNT['FIELD_AUTHOR']], array(
  123. 'SenderDisplayName' => $oProfile->getDisplayName(),
  124. 'SenderUrl' => $oProfile->getUrl(),
  125. 'Message' => $sCommentText,
  126. ), BX_EMAIL_NOTIFY);
  127. }
  128. return true;
  129. }
  130. public function serviceEntityBreadcrumb ($iContentId = 0)
  131. {
  132. if (!$iContentId)
  133. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  134. if (!$iContentId)
  135. return false;
  136. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  137. if (!$aContentInfo)
  138. return false;
  139. return $this->_oTemplate->entryBreadcrumb($aContentInfo);
  140. }
  141. /**
  142. * Entry collaborators block
  143. */
  144. public function serviceEntityParticipants ($iContentId = 0)
  145. {
  146. if (!$iContentId)
  147. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  148. if (!$iContentId)
  149. return false;
  150. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  151. if (!$aContentInfo)
  152. return false;
  153. return $this->_oTemplate->entryParticipants ($aContentInfo, 5, 'right');
  154. }
  155. /**
  156. * No social sharing for private discussions
  157. */
  158. public function serviceEntitySocialSharing($iContentId = 0)
  159. {
  160. return '';
  161. }
  162. /**
  163. * No thumbs for discussions
  164. */
  165. public function checkAllowedSetThumb()
  166. {
  167. return _t('_sys_txt_access_denied');
  168. }
  169. public function checkAllowedStickAnyEntry($aDataEntry, $isPerformAction = false)
  170. {
  171. $CNF = $this->_oConfig->CNF;
  172. if((int)$aDataEntry[$CNF['FIELD_STICK']] != 0)
  173. return false;
  174. return $this->_checkAllowedAction('stick any entry', $aDataEntry, $isPerformAction);
  175. }
  176. public function checkAllowedUnstickAnyEntry($aDataEntry, $isPerformAction = false)
  177. {
  178. $CNF = $this->_oConfig->CNF;
  179. if((int)$aDataEntry[$CNF['FIELD_STICK']] != 1)
  180. return false;
  181. return $this->_checkAllowedAction('stick any entry', $aDataEntry, $isPerformAction);
  182. }
  183. public function checkAllowedLockAnyEntry($aDataEntry, $isPerformAction = false)
  184. {
  185. $CNF = $this->_oConfig->CNF;
  186. if((int)$aDataEntry[$CNF['FIELD_LOCK']] != 0)
  187. return false;
  188. return $this->_checkAllowedAction('lock any entry', $aDataEntry, $isPerformAction);
  189. }
  190. public function checkAllowedUnlockAnyEntry($aDataEntry, $isPerformAction = false)
  191. {
  192. $CNF = $this->_oConfig->CNF;
  193. if((int)$aDataEntry[$CNF['FIELD_LOCK']] != 1)
  194. return false;
  195. return $this->_checkAllowedAction('lock any entry', $aDataEntry, $isPerformAction);
  196. }
  197. public function checkAllowedHideAnyEntry($aDataEntry, $isPerformAction = false)
  198. {
  199. $CNF = $this->_oConfig->CNF;
  200. if($aDataEntry[$CNF['FIELD_STATUS_ADMIN']] == 'hidden')
  201. return false;
  202. return $this->_checkAllowedAction('hide any entry', $aDataEntry, $isPerformAction);
  203. }
  204. public function checkAllowedUnhideAnyEntry($aDataEntry, $isPerformAction = false)
  205. {
  206. $CNF = $this->_oConfig->CNF;
  207. if($aDataEntry[$CNF['FIELD_STATUS_ADMIN']] != 'hidden')
  208. return false;
  209. return $this->_checkAllowedAction('hide any entry', $aDataEntry, $isPerformAction);
  210. }
  211. /**
  212. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  213. */
  214. protected function _checkAllowedAction($sAction, $aDataEntry, $isPerformAction = false)
  215. {
  216. if($this->_isModerator($isPerformAction))
  217. return CHECK_ACTION_RESULT_ALLOWED;
  218. $aCheck = checkActionModule($this->_iProfileId, $sAction, $this->getName(), $isPerformAction);
  219. if($aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  220. return CHECK_ACTION_RESULT_ALLOWED;
  221. return _t('_sys_txt_access_denied');
  222. }
  223. protected function _serviceBrowseTable($aParams)
  224. {
  225. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  226. if(!$oGrid)
  227. return false;
  228. $oGrid->setBrowseParams($aParams);
  229. return $oGrid->getCode();
  230. }
  231. }
  232. /** @} */