Subs-BoardIndex.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file currently only contains one function to collect the data needed to
  15. show a list of boards for the board index and the message index.
  16. array getBoardIndex(array boardIndexOptions)
  17. - Fetches a list of boards and (optional) categories including
  18. statistical information, child boards and moderators.
  19. - Used by both the board index (main data) and the message index (child
  20. boards).
  21. - Depending on the include_categories setting returns an associative
  22. array with categories->boards->child_boards or an associative array
  23. with boards->child_boards.
  24. */
  25. function getBoardIndex($boardIndexOptions)
  26. {
  27. global $smcFunc, $scripturl, $user_info, $modSettings, $txt;
  28. global $settings, $context;
  29. // For performance, track the latest post while going through the boards.
  30. if (!empty($boardIndexOptions['set_latest_post']))
  31. $latest_post = array(
  32. 'timestamp' => 0,
  33. 'ref' => 0,
  34. );
  35. // Find all boards and categories, as well as related information. This will be sorted by the natural order of boards and categories, which we control.
  36. $result_boards = $smcFunc['db_query']('boardindex_fetch_boards', '
  37. SELECT' . ($boardIndexOptions['include_categories'] ? '
  38. c.id_cat, c.name AS cat_name,' : '') . '
  39. b.id_board, b.name AS board_name, b.description,
  40. CASE WHEN b.redirect != {string:blank_string} THEN 1 ELSE 0 END AS is_redirect,
  41. b.num_posts, b.num_topics, b.unapproved_posts, b.unapproved_topics, b.id_parent,
  42. IFNULL(m.poster_time, 0) AS poster_time, IFNULL(mem.member_name, m.poster_name) AS poster_name,
  43. m.subject, m.id_topic, IFNULL(mem.real_name, m.poster_name) AS real_name,
  44. ' . ($user_info['is_guest'] ? ' 1 AS is_read, 0 AS new_from,' : '
  45. (IFNULL(lb.id_msg, 0) >= b.id_msg_updated) AS is_read, IFNULL(lb.id_msg, -1) + 1 AS new_from,' . ($boardIndexOptions['include_categories'] ? '
  46. c.can_collapse, IFNULL(cc.id_member, 0) AS is_collapsed,' : '')) . '
  47. IFNULL(mem.id_member, 0) AS id_member, m.id_msg,
  48. IFNULL(mods_mem.id_member, 0) AS id_moderator, mods_mem.real_name AS mod_real_name
  49. FROM {db_prefix}boards AS b' . ($boardIndexOptions['include_categories'] ? '
  50. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)' : '') . '
  51. LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = b.id_last_msg)
  52. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . ($user_info['is_guest'] ? '' : '
  53. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})' . ($boardIndexOptions['include_categories'] ? '
  54. LEFT JOIN {db_prefix}collapsed_categories AS cc ON (cc.id_cat = c.id_cat AND cc.id_member = {int:current_member})' : '')) . '
  55. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = b.id_board)
  56. LEFT JOIN {db_prefix}members AS mods_mem ON (mods_mem.id_member = mods.id_member)
  57. WHERE {query_see_board}' . (empty($boardIndexOptions['countChildPosts']) ? (empty($boardIndexOptions['base_level']) ? '' : '
  58. AND b.child_level >= {int:child_level}') : '
  59. AND b.child_level BETWEEN ' . $boardIndexOptions['base_level'] . ' AND ' . ($boardIndexOptions['base_level'] + 1)),
  60. array(
  61. 'current_member' => $user_info['id'],
  62. 'child_level' => $boardIndexOptions['base_level'],
  63. 'blank_string' => '',
  64. )
  65. );
  66. // Start with an empty array.
  67. if ($boardIndexOptions['include_categories'])
  68. $categories = array();
  69. else
  70. $this_category = array();
  71. // Run through the categories and boards (or only boards)....
  72. while ($row_board = $smcFunc['db_fetch_assoc']($result_boards))
  73. {
  74. // Perhaps we are ignoring this board?
  75. $ignoreThisBoard = in_array($row_board['id_board'], $user_info['ignoreboards']);
  76. $row_board['is_read'] = !empty($row_board['is_read']) || $ignoreThisBoard ? '1' : '0';
  77. if ($boardIndexOptions['include_categories'])
  78. {
  79. // Haven't set this category yet.
  80. if (empty($categories[$row_board['id_cat']]))
  81. {
  82. $categories[$row_board['id_cat']] = array(
  83. 'id' => $row_board['id_cat'],
  84. 'name' => $row_board['cat_name'],
  85. 'is_collapsed' => isset($row_board['can_collapse']) && $row_board['can_collapse'] == 1 && $row_board['is_collapsed'] > 0,
  86. 'can_collapse' => isset($row_board['can_collapse']) && $row_board['can_collapse'] == 1,
  87. 'collapse_href' => isset($row_board['can_collapse']) ? $scripturl . '?action=collapse;c=' . $row_board['id_cat'] . ';sa=' . ($row_board['is_collapsed'] > 0 ? 'expand;' : 'collapse;') . $context['session_var'] . '=' . $context['session_id'] . '#c' . $row_board['id_cat'] : '',
  88. 'collapse_image' => isset($row_board['can_collapse']) ? '<img src="' . $settings['images_url'] . '/' . $context['theme_variant_url'] . ($row_board['is_collapsed'] > 0 ? 'expand.gif" alt="+"' : 'collapse.gif" alt="-"') . ' />' : '',
  89. 'href' => $scripturl . '#c' . $row_board['id_cat'],
  90. 'boards' => array(),
  91. 'new' => false
  92. );
  93. $categories[$row_board['id_cat']]['link'] = '<a id="c' . $row_board['id_cat'] . '"></a>' . ($categories[$row_board['id_cat']]['can_collapse'] ? '<a href="' . $categories[$row_board['id_cat']]['collapse_href'] . '">' . $row_board['cat_name'] . '</a>' : $row_board['cat_name']);
  94. }
  95. // If this board has new posts in it (and isn't the recycle bin!) then the category is new.
  96. if (empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $row_board['id_board'])
  97. $categories[$row_board['id_cat']]['new'] |= empty($row_board['is_read']) && $row_board['poster_name'] != '';
  98. // Avoid showing category unread link where it only has redirection boards.
  99. $categories[$row_board['id_cat']]['show_unread'] = !empty($categories[$row_board['id_cat']]['show_unread']) ? 1 : !$row_board['is_redirect'];
  100. // Collapsed category - don't do any of this.
  101. if ($categories[$row_board['id_cat']]['is_collapsed'])
  102. continue;
  103. // Let's save some typing. Climbing the array might be slower, anyhow.
  104. $this_category = &$categories[$row_board['id_cat']]['boards'];
  105. }
  106. // This is a parent board.
  107. if ($row_board['id_parent'] == $boardIndexOptions['parent_id'])
  108. {
  109. // Is this a new board, or just another moderator?
  110. if (!isset($this_category[$row_board['id_board']]))
  111. {
  112. // Not a child.
  113. $isChild = false;
  114. $this_category[$row_board['id_board']] = array(
  115. 'new' => empty($row_board['is_read']),
  116. 'id' => $row_board['id_board'],
  117. 'name' => $row_board['board_name'],
  118. 'description' => $row_board['description'],
  119. 'moderators' => array(),
  120. 'link_moderators' => array(),
  121. 'children' => array(),
  122. 'link_children' => array(),
  123. 'children_new' => false,
  124. 'topics' => $row_board['num_topics'],
  125. 'posts' => $row_board['num_posts'],
  126. 'is_redirect' => $row_board['is_redirect'],
  127. 'unapproved_topics' => $row_board['unapproved_topics'],
  128. 'unapproved_posts' => $row_board['unapproved_posts'] - $row_board['unapproved_topics'],
  129. 'can_approve_posts' => !empty($user_info['mod_cache']['ap']) && ($user_info['mod_cache']['ap'] == array(0) || in_array($row_board['id_board'], $user_info['mod_cache']['ap'])),
  130. 'href' => $scripturl . '?board=' . $row_board['id_board'] . '.0',
  131. 'link' => '<a href="' . $scripturl . '?board=' . $row_board['id_board'] . '.0">' . $row_board['board_name'] . '</a>'
  132. );
  133. }
  134. if (!empty($row_board['id_moderator']))
  135. {
  136. $this_category[$row_board['id_board']]['moderators'][$row_board['id_moderator']] = array(
  137. 'id' => $row_board['id_moderator'],
  138. 'name' => $row_board['mod_real_name'],
  139. 'href' => $scripturl . '?action=profile;u=' . $row_board['id_moderator'],
  140. 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_board['id_moderator'] . '" title="' . $txt['board_moderator'] . '">' . $row_board['mod_real_name'] . '</a>'
  141. );
  142. $this_category[$row_board['id_board']]['link_moderators'][] = '<a href="' . $scripturl . '?action=profile;u=' . $row_board['id_moderator'] . '" title="' . $txt['board_moderator'] . '">' . $row_board['mod_real_name'] . '</a>';
  143. }
  144. }
  145. // Found a child board.... make sure we've found its parent and the child hasn't been set already.
  146. elseif (isset($this_category[$row_board['id_parent']]['children']) && !isset($this_category[$row_board['id_parent']]['children'][$row_board['id_board']]))
  147. {
  148. // A valid child!
  149. $isChild = true;
  150. $this_category[$row_board['id_parent']]['children'][$row_board['id_board']] = array(
  151. 'id' => $row_board['id_board'],
  152. 'name' => $row_board['board_name'],
  153. 'description' => $row_board['description'],
  154. 'new' => empty($row_board['is_read']) && $row_board['poster_name'] != '',
  155. 'topics' => $row_board['num_topics'],
  156. 'posts' => $row_board['num_posts'],
  157. 'is_redirect' => $row_board['is_redirect'],
  158. 'unapproved_topics' => $row_board['unapproved_topics'],
  159. 'unapproved_posts' => $row_board['unapproved_posts'] - $row_board['unapproved_topics'],
  160. 'can_approve_posts' => !empty($user_info['mod_cache']['ap']) && ($user_info['mod_cache']['ap'] == array(0) || in_array($row_board['id_board'], $user_info['mod_cache']['ap'])),
  161. 'href' => $scripturl . '?board=' . $row_board['id_board'] . '.0',
  162. 'link' => '<a href="' . $scripturl . '?board=' . $row_board['id_board'] . '.0">' . $row_board['board_name'] . '</a>'
  163. );
  164. // Counting child board posts is... slow :/.
  165. if (!empty($boardIndexOptions['countChildPosts']) && !$row_board['is_redirect'])
  166. {
  167. $this_category[$row_board['id_parent']]['posts'] += $row_board['num_posts'];
  168. $this_category[$row_board['id_parent']]['topics'] += $row_board['num_topics'];
  169. }
  170. // Does this board contain new boards?
  171. $this_category[$row_board['id_parent']]['children_new'] |= empty($row_board['is_read']);
  172. // This is easier to use in many cases for the theme....
  173. $this_category[$row_board['id_parent']]['link_children'][] = &$this_category[$row_board['id_parent']]['children'][$row_board['id_board']]['link'];
  174. }
  175. // Child of a child... just add it on...
  176. elseif (!empty($boardIndexOptions['countChildPosts']))
  177. {
  178. if (!isset($parent_map))
  179. $parent_map = array();
  180. if (!isset($parent_map[$row_board['id_parent']]))
  181. foreach ($this_category as $id => $board)
  182. {
  183. if (!isset($board['children'][$row_board['id_parent']]))
  184. continue;
  185. $parent_map[$row_board['id_parent']] = array(&$this_category[$id], &$this_category[$id]['children'][$row_board['id_parent']]);
  186. $parent_map[$row_board['id_board']] = array(&$this_category[$id], &$this_category[$id]['children'][$row_board['id_parent']]);
  187. break;
  188. }
  189. if (isset($parent_map[$row_board['id_parent']]) && !$row_board['is_redirect'])
  190. {
  191. $parent_map[$row_board['id_parent']][0]['posts'] += $row_board['num_posts'];
  192. $parent_map[$row_board['id_parent']][0]['topics'] += $row_board['num_topics'];
  193. $parent_map[$row_board['id_parent']][1]['posts'] += $row_board['num_posts'];
  194. $parent_map[$row_board['id_parent']][1]['topics'] += $row_board['num_topics'];
  195. continue;
  196. }
  197. continue;
  198. }
  199. // Found a child of a child - skip.
  200. else
  201. continue;
  202. // Prepare the subject, and make sure it's not too long.
  203. censorText($row_board['subject']);
  204. $row_board['short_subject'] = shorten_subject($row_board['subject'], 24);
  205. $this_last_post = array(
  206. 'id' => $row_board['id_msg'],
  207. 'time' => $row_board['poster_time'] > 0 ? timeformat($row_board['poster_time']) : $txt['not_applicable'],
  208. 'timestamp' => forum_time(true, $row_board['poster_time']),
  209. 'subject' => $row_board['short_subject'],
  210. 'member' => array(
  211. 'id' => $row_board['id_member'],
  212. 'username' => $row_board['poster_name'] != '' ? $row_board['poster_name'] : $txt['not_applicable'],
  213. 'name' => $row_board['real_name'],
  214. 'href' => $row_board['poster_name'] != '' && !empty($row_board['id_member']) ? $scripturl . '?action=profile;u=' . $row_board['id_member'] : '',
  215. 'link' => $row_board['poster_name'] != '' ? (!empty($row_board['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row_board['id_member'] . '">' . $row_board['real_name'] . '</a>' : $row_board['real_name']) : $txt['not_applicable'],
  216. ),
  217. 'start' => 'msg' . $row_board['new_from'],
  218. 'topic' => $row_board['id_topic']
  219. );
  220. // Provide the href and link.
  221. if ($row_board['subject'] != '')
  222. {
  223. $this_last_post['href'] = $scripturl . '?topic=' . $row_board['id_topic'] . '.msg' . ($user_info['is_guest'] ? $row_board['id_msg'] : $row_board['new_from']) . (empty($row_board['is_read']) ? ';boardseen' : '') . '#new';
  224. $this_last_post['link'] = '<a href="' . $this_last_post['href'] . '" title="' . $row_board['subject'] . '">' . $row_board['short_subject'] . '</a>';
  225. }
  226. else
  227. {
  228. $this_last_post['href'] = '';
  229. $this_last_post['link'] = $txt['not_applicable'];
  230. }
  231. // Set the last post in the parent board.
  232. if ($row_board['id_parent'] == $boardIndexOptions['parent_id'] || ($isChild && !empty($row_board['poster_time']) && $this_category[$row_board['id_parent']]['last_post']['timestamp'] < forum_time(true, $row_board['poster_time'])))
  233. $this_category[$isChild ? $row_board['id_parent'] : $row_board['id_board']]['last_post'] = $this_last_post;
  234. // Just in the child...?
  235. if ($isChild)
  236. {
  237. $this_category[$row_board['id_parent']]['children'][$row_board['id_board']]['last_post'] = $this_last_post;
  238. // If there are no posts in this board, it really can't be new...
  239. $this_category[$row_board['id_parent']]['children'][$row_board['id_board']]['new'] &= $row_board['poster_name'] != '';
  240. }
  241. // No last post for this board? It's not new then, is it..?
  242. elseif ($row_board['poster_name'] == '')
  243. $this_category[$row_board['id_board']]['new'] = false;
  244. // Determine a global most recent topic.
  245. if (!empty($boardIndexOptions['set_latest_post']) && !empty($row_board['poster_time']) && $row_board['poster_time'] > $latest_post['timestamp'] && !$ignoreThisBoard)
  246. $latest_post = array(
  247. 'timestamp' => $row_board['poster_time'],
  248. 'ref' => &$this_category[$isChild ? $row_board['id_parent'] : $row_board['id_board']]['last_post'],
  249. );
  250. }
  251. $smcFunc['db_free_result']($result_boards);
  252. // By now we should know the most recent post...if we wanna know it that is.
  253. if (!empty($boardIndexOptions['set_latest_post']) && !empty($latest_post['ref']))
  254. $context['latest_post'] = $latest_post['ref'];
  255. return $boardIndexOptions['include_categories'] ? $categories : $this_category;
  256. }
  257. ?>