PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Sources/Subs-BoardIndex.php

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