BxForumModule.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Forum Forum
  7. * @ingroup UnaModules
  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, $bShowHeader = 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), $bShowHeader);
  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 serviceBrowseIndex($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true, $bShowHeader = true)
  83. {
  84. $sType = 'index';
  85. if($sUnitView != 'table')
  86. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  87. return $this->_serviceBrowseTable(array(
  88. 'type' => $sType,
  89. 'per_page' => (int)$this->_oDb->getParam('bx_forum_per_page_index')
  90. ), $bShowHeader);
  91. }
  92. public function serviceBrowseAuthor ($iProfileId = 0, $aParams = array())
  93. {
  94. if(!$iProfileId)
  95. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  96. if(!$iProfileId)
  97. return '';
  98. return $this->_serviceBrowseTable(array(
  99. 'type' => 'author',
  100. 'author' => $iProfileId,
  101. 'where' => array('fld' => 'author', 'val' => $iProfileId, 'opr' => '='),
  102. 'per_page' => (int)$this->_oDb->getParam('bx_forum_per_page_profile')
  103. ), false);
  104. }
  105. public function serviceBrowseFavorite ($iProfileId = 0, $aParams = array())
  106. {
  107. $CNF = &$this->_oConfig->CNF;
  108. $oProfile = null;
  109. if((int)$iProfileId)
  110. $oProfile = BxDolProfile::getInstance($iProfileId);
  111. if(!$oProfile && bx_get('profile_id') !== false)
  112. $oProfile = BxDolProfile:: getInstance(bx_process_input(bx_get('profile_id'), BX_DATA_INT));
  113. if(!$oProfile)
  114. $oProfile = BxDolProfile::getInstance();
  115. if(!$oProfile)
  116. return '';
  117. $iProfileAuthor = $oProfile->id();
  118. $oFavorite = $this->getObjectFavorite();
  119. if(!$oFavorite->isPublic() && $iProfileAuthor != bx_get_logged_profile_id())
  120. return '';
  121. $aConditions = $oFavorite->getConditionsTrack($CNF['TABLE_ENTRIES'], 'id', $iProfileAuthor);
  122. if(empty($aConditions) || !is_array($aConditions))
  123. return '';
  124. $aJoinGroup = array('grp' => true, 'cnds' => array());
  125. if(!empty($aConditions['join']))
  126. foreach($aConditions['join'] as $aCondition)
  127. $aJoinGroup['cnds'][] = array(
  128. 'tp' => $aCondition['type'],
  129. 'tbl1' => $aCondition['table'],
  130. 'fld1' => $aCondition['onField'],
  131. 'tbl2' => $aCondition['mainTable'],
  132. 'fld2' => $aCondition['mainField']
  133. );
  134. $aWhereGroup = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
  135. if(!empty($aConditions['restriction']))
  136. foreach($aConditions['restriction'] as $aCondition)
  137. $aWhereGroup['cnds'][] = array(
  138. 'tbl' => (!empty($aCondition['table']) ? $aCondition['table'] : ''),
  139. 'fld' => $aCondition['field'],
  140. 'val' => $aCondition['value'],
  141. 'opr' => $aCondition['operator']
  142. );
  143. return $this->_serviceBrowseTable(array(
  144. 'grid' => $CNF['OBJECT_GRID_FAVORITE'],
  145. 'type' => 'favorite',
  146. 'author' => $iProfileId,
  147. 'join' => $aJoinGroup,
  148. 'where' => $aWhereGroup,
  149. 'per_page' => (int)$this->_oDb->getParam('bx_forum_per_page_profile')
  150. ), false);
  151. }
  152. public function serviceBrowseCategory($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  153. {
  154. $sType = 'category';
  155. $iCategory = bx_process_input(bx_get('category'), BX_DATA_INT);
  156. $aCategory = $this->_oDb->getCategories(array('type' => 'by_category', 'category' => $iCategory));
  157. if(!empty($aCategory['visible_for_levels']) && !BxDolAcl::getInstance()->isMemberLevelInSet($aCategory['visible_for_levels']))
  158. return $bEmptyMessage ? MsgBox(_t('_sys_txt_access_denied')) : '';
  159. if($sUnitView != 'table')
  160. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  161. return $this->_serviceBrowseTable(array('type' => $sType, 'where' => array('fld' => 'cat', 'val' => $iCategory, 'opr' => '=')));
  162. }
  163. public function serviceBrowseKeyword($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  164. {
  165. $sType = 'keyword';
  166. $sKeyword = bx_process_input(bx_get('keyword'));
  167. if($sUnitView != 'table')
  168. return $this->_serviceBrowse($sType, $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  169. return $this->_serviceBrowseTable(array('type' => $sType, 'where' => $this->_getSearchKeywordDescriptor('#' . $sKeyword)));
  170. }
  171. public function serviceBrowseSearchResults($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  172. {
  173. $sType = 'search';
  174. $aAutors = bx_process_input(bx_get('author'));
  175. $iCategory = bx_process_input(bx_get('category'), BX_DATA_INT);
  176. $sKeyword = bx_process_input(bx_get('keyword'));
  177. $aCategory = $this->_oDb->getCategories(array('type' => 'by_category', 'category' => $iCategory));
  178. if(!empty($aCategory['visible_for_levels']) && !BxDolAcl::getInstance()->isMemberLevelInSet($aCategory['visible_for_levels']))
  179. return $bEmptyMessage ? MsgBox(_t('_sys_txt_access_denied')) : '';
  180. if($sUnitView != 'table')
  181. return $this->_serviceBrowse('', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  182. $aWhereGroupAnd = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
  183. if(!empty($aAutors) && is_array($aAutors))
  184. $aWhereGroupAnd['cnds'][] = array('fld' => 'author', 'val' => $aAutors, 'opr' => 'IN');
  185. if(!empty($iCategory))
  186. $aWhereGroupAnd['cnds'][] = array('fld' => 'cat', 'val' => $iCategory, 'opr' => '=');
  187. if(!empty($sKeyword))
  188. $aWhereGroupAnd['cnds'][] = $this->_getSearchKeywordDescriptor($sKeyword);
  189. return $this->_serviceBrowseTable(array('type' => $sType, 'where' => $aWhereGroupAnd), false);
  190. }
  191. /**
  192. * Get number of discussions for some profile
  193. * @param $iProfileId - profile to get discussions for, if omitted then currently logged in profile is used
  194. * @return integer
  195. */
  196. public function serviceGetDiscussionsNum ($iProfileId = 0)
  197. {
  198. if (!$iProfileId)
  199. $iProfileId = bx_get_logged_profile_id();
  200. return $this->_oDb->getEntriesNumByAuthor((int)$iProfileId);
  201. }
  202. /**
  203. * Get number of unreplied discussions for some profile
  204. * @param $iProfileId - profile to get unreplied discussions for, if omitted then currently logged is profile is used
  205. * @return integer
  206. */
  207. public function serviceGetUnrepliedDiscussionsNum ($iProfileId = 0)
  208. {
  209. if (!$iProfileId)
  210. $iProfileId = bx_get_logged_profile_id();
  211. return $this->_oDb->getUnrepliedDiscussionsNum((int)$iProfileId);
  212. }
  213. /**
  214. * Update last comment time and author
  215. */
  216. public function serviceTriggerCommentPost ($iContentId, $iProfileId, $iCommentId, $iTimestamp = 0, $sCommentText = '')
  217. {
  218. $CNF = $this->_oConfig->CNF;
  219. $iContentId = (int)$iContentId;
  220. if(!$iContentId)
  221. return false;
  222. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  223. if(!$aContentInfo)
  224. return false;
  225. if(!$iTimestamp)
  226. $iTimestamp = time();
  227. if(!$this->_oDb->updateLastCommentTimeProfile((int)$iContentId, (int)$iProfileId, $iCommentId, $iTimestamp))
  228. return false;
  229. // send notification to author
  230. if($iProfileId != $aContentInfo[$CNF['FIELD_AUTHOR']]) {
  231. $oProfile = BxDolProfile::getInstance($iProfileId);
  232. if($oProfile)
  233. sendMailTemplate('bx_forum_new_reply', 0, $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  234. 'SenderDisplayName' => $oProfile->getDisplayName(),
  235. 'SenderUrl' => $oProfile->getUrl(),
  236. 'Message' => $sCommentText,
  237. ), BX_EMAIL_NOTIFY);
  238. }
  239. return true;
  240. }
  241. /**
  242. * Entry collaborators block
  243. */
  244. public function serviceEntityParticipants ($iContentId = 0)
  245. {
  246. if (!$iContentId)
  247. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  248. if (!$iContentId)
  249. return false;
  250. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  251. if (!$aContentInfo)
  252. return false;
  253. return $this->_oTemplate->entryParticipants ($aContentInfo, 5, 'right');
  254. }
  255. public function serviceSearch()
  256. {
  257. $CNF = $this->_oConfig->CNF;
  258. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_SEARCH'], $CNF['OBJECT_FORM_SEARCH_DISPLAY_FULL'], $this->_oTemplate);
  259. $oForm->initChecker();
  260. return $oForm->getCode();
  261. }
  262. /**
  263. * No thumbs for discussions
  264. */
  265. public function checkAllowedSetThumb()
  266. {
  267. return _t('_sys_txt_access_denied');
  268. }
  269. public function checkAllowedSubscribe(&$aDataEntry, $isPerformAction = false)
  270. {
  271. $CNF = &$this->_oConfig->CNF;
  272. $sMsg = $this->checkAllowedView($aDataEntry);
  273. if($sMsg !== CHECK_ACTION_RESULT_ALLOWED)
  274. return $sMsg;
  275. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $CNF['OBJECT_CONNECTION_SUBSCRIBERS'], false, false);
  276. }
  277. public function checkAllowedUnsubscribe(&$aDataEntry, $isPerformAction = false)
  278. {
  279. $CNF = &$this->_oConfig->CNF;
  280. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $CNF['OBJECT_CONNECTION_SUBSCRIBERS'], false, true);
  281. }
  282. public function checkAllowedStickAnyEntry($aDataEntry, $isPerformAction = false)
  283. {
  284. $CNF = &$this->_oConfig->CNF;
  285. if((int)$aDataEntry[$CNF['FIELD_STICK']] != 0)
  286. return false;
  287. return $this->_checkAllowedAction('stick any entry', $aDataEntry, $isPerformAction);
  288. }
  289. public function checkAllowedUnstickAnyEntry($aDataEntry, $isPerformAction = false)
  290. {
  291. $CNF = $this->_oConfig->CNF;
  292. if((int)$aDataEntry[$CNF['FIELD_STICK']] != 1)
  293. return false;
  294. return $this->_checkAllowedAction('stick any entry', $aDataEntry, $isPerformAction);
  295. }
  296. public function checkAllowedLockAnyEntry($aDataEntry, $isPerformAction = false)
  297. {
  298. $CNF = $this->_oConfig->CNF;
  299. if((int)$aDataEntry[$CNF['FIELD_LOCK']] != 0)
  300. return false;
  301. return $this->_checkAllowedAction('lock any entry', $aDataEntry, $isPerformAction);
  302. }
  303. public function checkAllowedUnlockAnyEntry($aDataEntry, $isPerformAction = false)
  304. {
  305. $CNF = $this->_oConfig->CNF;
  306. if((int)$aDataEntry[$CNF['FIELD_LOCK']] != 1)
  307. return false;
  308. return $this->_checkAllowedAction('lock any entry', $aDataEntry, $isPerformAction);
  309. }
  310. public function checkAllowedHideAnyEntry($aDataEntry, $isPerformAction = false)
  311. {
  312. $CNF = $this->_oConfig->CNF;
  313. if($aDataEntry[$CNF['FIELD_STATUS_ADMIN']] == 'hidden')
  314. return false;
  315. return $this->_checkAllowedAction('hide any entry', $aDataEntry, $isPerformAction);
  316. }
  317. public function checkAllowedUnhideAnyEntry($aDataEntry, $isPerformAction = false)
  318. {
  319. $CNF = &$this->_oConfig->CNF;
  320. if($aDataEntry[$CNF['FIELD_STATUS_ADMIN']] != 'hidden')
  321. return false;
  322. return $this->_checkAllowedAction('hide any entry', $aDataEntry, $isPerformAction);
  323. }
  324. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult)
  325. {
  326. $CNF = &$this->_oConfig->CNF;
  327. if(!$this->_iProfileId)
  328. return _t('_sys_txt_access_denied');
  329. $isConnected = BxDolConnection::getObjectInstance($sObjConnection)->isConnected($this->_iProfileId, $aDataEntry[$CNF['FIELD_ID']], $isMutual);
  330. if($isInvertResult)
  331. $isConnected = !$isConnected;
  332. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  333. }
  334. /**
  335. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  336. */
  337. protected function _checkAllowedAction($sAction, $aDataEntry, $isPerformAction = false)
  338. {
  339. if($this->_isModerator($isPerformAction))
  340. return CHECK_ACTION_RESULT_ALLOWED;
  341. $aCheck = checkActionModule($this->_iProfileId, $sAction, $this->getName(), $isPerformAction);
  342. if($aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  343. return CHECK_ACTION_RESULT_ALLOWED;
  344. return _t('_sys_txt_access_denied');
  345. }
  346. protected function _serviceBrowseTable($aParams, $isDisplayHeader = true)
  347. {
  348. $sGrid = $this->_oConfig->CNF['OBJECT_GRID'];
  349. if(!empty($aParams['grid'])) {
  350. $sGrid = $aParams['grid'];
  351. unset($aParams['grid']);
  352. }
  353. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  354. if(!$oGrid)
  355. return false;
  356. $oGrid->setBrowseParams($aParams);
  357. return $oGrid->getCode($isDisplayHeader);
  358. }
  359. protected function _getSearchKeywordDescriptor($sKeyword)
  360. {
  361. $aWhereGroupOr = array('grp' => true, 'opr' => 'OR', 'cnds' => array(
  362. array('fld' => 'title', 'val' => $sKeyword, 'opr' => 'LIKE'),
  363. array('fld' => 'text', 'val' => $sKeyword, 'opr' => 'LIKE')
  364. ));
  365. $aEntriesIds = $this->_oDb->getComments(array('type' => 'entries_keyword_search', 'keyword' => $sKeyword));
  366. if(!empty($aEntriesIds) && is_array($aEntriesIds))
  367. $aWhereGroupOr['cnds'][] = array('fld' => 'id', 'val' => $aEntriesIds, 'opr' => 'IN');
  368. return $aWhereGroupOr;
  369. }
  370. }
  371. /** @} */