comparison forum/Sources/Subs-BoardIndex.php @ 76:e3e11437ecea website

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