BxForumModule.php 19 KB

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