BxForumModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. public function actionAjaxGetAuthors()
  53. {
  54. $aResult = BxDolService::call('system', 'profiles_search', array(bx_get('term')), 'TemplServiceProfiles');
  55. header('Content-Type:text/javascript; charset=utf-8');
  56. echo json_encode($aResult);
  57. }
  58. /**
  59. * Service methods
  60. */
  61. public function serviceBrowseNew ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  62. {
  63. $sType = 'new';
  64. if($sUnitView != 'table')
  65. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  66. return $this->_serviceBrowseTable(array('type' => $sType));
  67. }
  68. public function serviceBrowseLatest($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  69. {
  70. $sType = 'latest';
  71. if($sUnitView != 'table')
  72. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  73. return $this->_serviceBrowseTable(array('type' => $sType));
  74. }
  75. public function serviceBrowseTop($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  76. {
  77. $sType = 'top';
  78. if($sUnitView != 'table')
  79. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  80. return $this->_serviceBrowseTable(array('type' => $sType));
  81. }
  82. public function serviceBrowseAuthor ($iProfileId = 0, $aParams = array())
  83. {
  84. if(!$iProfileId)
  85. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  86. if(!$iProfileId)
  87. return '';
  88. return $this->_serviceBrowseTable(array('type' => 'author', 'where' => array('fld' => 'author', 'val' => $iProfileId, 'opr' => '=')), false);
  89. }
  90. public function serviceBrowseCategory($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  91. {
  92. $sType = 'category';
  93. $iCategory = bx_process_input(bx_get('category'), BX_DATA_INT);
  94. $aCategory = $this->_oDb->getCategories(array('type' => 'by_category', 'category' => $iCategory));
  95. if(!empty($aCategory['visible_for_levels']) && !BxDolAcl::getInstance()->isMemberLevelInSet($aCategory['visible_for_levels']))
  96. return $bEmptyMessage ? MsgBox(_t('_sys_txt_access_denied')) : '';
  97. if($sUnitView != 'table')
  98. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  99. return $this->_serviceBrowseTable(array('type' => $sType, 'where' => array('fld' => 'cat', 'val' => $iCategory, 'opr' => '=')));
  100. }
  101. public function serviceBrowseKeywords()
  102. {
  103. $CNF = $this->_oConfig->CNF;
  104. $aSrvParams = array($CNF['OBJECT_METATAGS'], $this->_oConfig->getName(), array(
  105. 'browse_url' => bx_append_url_params(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_KEYWORD_ENTRIES']), array('keyword' => '{keyword}'))
  106. ));
  107. return BxDolService::call('system', 'keywords_cloud', $aSrvParams, 'TemplServiceMetatags');
  108. }
  109. public function serviceBrowseKeyword($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  110. {
  111. $sType = 'keyword';
  112. $sKeyword = bx_process_input(bx_get('keyword'));
  113. if($sUnitView != 'table')
  114. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  115. return $this->_serviceBrowseTable(array('type' => $sType, 'where' => $this->_getSearchKeywordDescriptor('#' . $sKeyword)));
  116. }
  117. public function serviceBrowseSearchResults($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  118. {
  119. $sType = 'search';
  120. $aAutors = bx_process_input(bx_get('author'));
  121. $iCategory = bx_process_input(bx_get('category'), BX_DATA_INT);
  122. $sKeyword = bx_process_input(bx_get('keyword'));
  123. $aCategory = $this->_oDb->getCategories(array('type' => 'by_category', 'category' => $iCategory));
  124. if(!empty($aCategory['visible_for_levels']) && !BxDolAcl::getInstance()->isMemberLevelInSet($aCategory['visible_for_levels']))
  125. return $bEmptyMessage ? MsgBox(_t('_sys_txt_access_denied')) : '';
  126. if($sUnitView != 'table')
  127. return $this->_serviceBrowse('', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  128. $aWhereGroupAnd = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
  129. if(!empty($aAutors) && is_array($aAutors))
  130. $aWhereGroupAnd['cnds'][] = array('fld' => 'author', 'val' => $aAutors, 'opr' => 'IN');
  131. if(!empty($iCategory))
  132. $aWhereGroupAnd['cnds'][] = array('fld' => 'cat', 'val' => $iCategory, 'opr' => '=');
  133. if(!empty($sKeyword))
  134. $aWhereGroupAnd['cnds'][] = $this->_getSearchKeywordDescriptor($sKeyword);
  135. return $this->_serviceBrowseTable(array('type' => $sType, 'where' => $aWhereGroupAnd), false);
  136. }
  137. /**
  138. * Get number of unread messages for spme profile
  139. * @param $iProfileId - profile to get unread messages for, if omitted then currently logged is profile is used
  140. * @return integer
  141. */
  142. public function serviceGetUnrepliedDiscussionsNum ($iProfileId = 0)
  143. {
  144. if (!$iProfileId)
  145. $iProfileId = bx_get_logged_profile_id();
  146. return $this->_oDb->getUnrepliedDiscussionsNum((int)$iProfileId);
  147. }
  148. /**
  149. * Update last comment time and author
  150. */
  151. public function serviceTriggerCommentPost ($iContentId, $iProfileId, $iCommentId, $iTimestamp = 0, $sCommentText = '')
  152. {
  153. $CNF = $this->_oConfig->CNF;
  154. $iContentId = (int)$iContentId;
  155. if(!$iContentId)
  156. return false;
  157. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  158. if(!$aContentInfo)
  159. return false;
  160. if(!$iTimestamp)
  161. $iTimestamp = time();
  162. if(!$this->_oDb->updateLastCommentTimeProfile((int)$iContentId, (int)$iProfileId, $iCommentId, $iTimestamp))
  163. return false;
  164. // send notification to author
  165. if($iProfileId != $aContentInfo[$CNF['FIELD_AUTHOR']]) {
  166. $oProfile = BxDolProfile::getInstance($iProfileId);
  167. if($oProfile)
  168. sendMailTemplate('bx_forum_new_reply', 0, $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  169. 'SenderDisplayName' => $oProfile->getDisplayName(),
  170. 'SenderUrl' => $oProfile->getUrl(),
  171. 'Message' => $sCommentText,
  172. ), BX_EMAIL_NOTIFY);
  173. }
  174. return true;
  175. }
  176. public function serviceEntityBreadcrumb ($iContentId = 0)
  177. {
  178. if (!$iContentId)
  179. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  180. if (!$iContentId)
  181. return false;
  182. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  183. if (!$aContentInfo)
  184. return false;
  185. return $this->_oTemplate->entryBreadcrumb($aContentInfo);
  186. }
  187. /**
  188. * Entry collaborators block
  189. */
  190. public function serviceEntityParticipants ($iContentId = 0)
  191. {
  192. if (!$iContentId)
  193. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  194. if (!$iContentId)
  195. return false;
  196. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  197. if (!$aContentInfo)
  198. return false;
  199. return $this->_oTemplate->entryParticipants ($aContentInfo, 5, 'right');
  200. }
  201. /**
  202. * No social sharing for private discussions
  203. */
  204. public function serviceEntitySocialSharing($iContentId = 0)
  205. {
  206. return '';
  207. }
  208. public function serviceSearch()
  209. {
  210. $CNF = $this->_oConfig->CNF;
  211. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_SEARCH'], $CNF['OBJECT_FORM_SEARCH_DISPLAY_FULL'], $this->_oTemplate);
  212. $oForm->initChecker();
  213. return $oForm->getCode();
  214. }
  215. /**
  216. * No thumbs for discussions
  217. */
  218. public function checkAllowedSetThumb()
  219. {
  220. return _t('_sys_txt_access_denied');
  221. }
  222. public function checkAllowedStickAnyEntry($aDataEntry, $isPerformAction = false)
  223. {
  224. $CNF = $this->_oConfig->CNF;
  225. if((int)$aDataEntry[$CNF['FIELD_STICK']] != 0)
  226. return false;
  227. return $this->_checkAllowedAction('stick any entry', $aDataEntry, $isPerformAction);
  228. }
  229. public function checkAllowedUnstickAnyEntry($aDataEntry, $isPerformAction = false)
  230. {
  231. $CNF = $this->_oConfig->CNF;
  232. if((int)$aDataEntry[$CNF['FIELD_STICK']] != 1)
  233. return false;
  234. return $this->_checkAllowedAction('stick any entry', $aDataEntry, $isPerformAction);
  235. }
  236. public function checkAllowedLockAnyEntry($aDataEntry, $isPerformAction = false)
  237. {
  238. $CNF = $this->_oConfig->CNF;
  239. if((int)$aDataEntry[$CNF['FIELD_LOCK']] != 0)
  240. return false;
  241. return $this->_checkAllowedAction('lock any entry', $aDataEntry, $isPerformAction);
  242. }
  243. public function checkAllowedUnlockAnyEntry($aDataEntry, $isPerformAction = false)
  244. {
  245. $CNF = $this->_oConfig->CNF;
  246. if((int)$aDataEntry[$CNF['FIELD_LOCK']] != 1)
  247. return false;
  248. return $this->_checkAllowedAction('lock any entry', $aDataEntry, $isPerformAction);
  249. }
  250. public function checkAllowedHideAnyEntry($aDataEntry, $isPerformAction = false)
  251. {
  252. $CNF = $this->_oConfig->CNF;
  253. if($aDataEntry[$CNF['FIELD_STATUS_ADMIN']] == 'hidden')
  254. return false;
  255. return $this->_checkAllowedAction('hide any entry', $aDataEntry, $isPerformAction);
  256. }
  257. public function checkAllowedUnhideAnyEntry($aDataEntry, $isPerformAction = false)
  258. {
  259. $CNF = $this->_oConfig->CNF;
  260. if($aDataEntry[$CNF['FIELD_STATUS_ADMIN']] != 'hidden')
  261. return false;
  262. return $this->_checkAllowedAction('hide any entry', $aDataEntry, $isPerformAction);
  263. }
  264. /**
  265. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  266. */
  267. protected function _checkAllowedAction($sAction, $aDataEntry, $isPerformAction = false)
  268. {
  269. if($this->_isModerator($isPerformAction))
  270. return CHECK_ACTION_RESULT_ALLOWED;
  271. $aCheck = checkActionModule($this->_iProfileId, $sAction, $this->getName(), $isPerformAction);
  272. if($aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  273. return CHECK_ACTION_RESULT_ALLOWED;
  274. return _t('_sys_txt_access_denied');
  275. }
  276. protected function _serviceBrowseTable($aParams, $isDisplayHeader = true)
  277. {
  278. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  279. if(!$oGrid)
  280. return false;
  281. $oGrid->setBrowseParams($aParams);
  282. return $oGrid->getCode($isDisplayHeader);
  283. }
  284. protected function _getSearchKeywordDescriptor($sKeyword)
  285. {
  286. $aWhereGroupOr = array('grp' => true, 'opr' => 'OR', 'cnds' => array(
  287. array('fld' => 'title', 'val' => $sKeyword, 'opr' => 'LIKE'),
  288. array('fld' => 'text', 'val' => $sKeyword, 'opr' => 'LIKE')
  289. ));
  290. $aEntriesIds = $this->_oDb->getComments(array('type' => 'entries_keyword_search', 'keyword' => $sKeyword));
  291. if(!empty($aEntriesIds) && is_array($aEntriesIds))
  292. $aWhereGroupOr['cnds'][] = array('fld' => 'id', 'val' => $aEntriesIds, 'opr' => 'IN');
  293. return $aWhereGroupOr;
  294. }
  295. }
  296. /** @} */