comparison forum/Sources/Subs-Categories.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 contains the functions to add, modify, remove, collapse and expand
18 categories.
19
20 void modifyCategory(int category_id, array catOptions)
21 - general function to modify the settings and position of a category.
22 - used by ManageBoards.php to change the settings of a category.
23
24 int createCategory(array catOptions)
25 - general function to create a new category and set its position.
26 - allows (almost) the same options as the modifyCat() function.
27 - returns the ID of the newly created category.
28
29 void deleteCategories(array categories_to_remove, moveChildrenTo = null)
30 - general function to delete one or more categories.
31 - allows to move all boards in the categories to a different category
32 before deleting them.
33 - if moveChildrenTo is set to null, all boards inside the given
34 categorieswill be deleted.
35 - deletes all information that's associated with the given categories.
36 - updates the statistics to reflect the new situation.
37
38 void collapseCategories(array categories, string new_status, array members = null, bool check_collapsable = true)
39 - collapses or expands one or more categories for one or more members.
40 - if members is null, the category is collapsed/expanded for all members.
41 - allows three changes to the status: 'expand', 'collapse' and 'toggle'.
42 - if check_collapsable is set, only category allowed to be collapsed,
43 will be collapsed.
44 */
45
46 // Edit the position and properties of a category.
47 function modifyCategory($category_id, $catOptions)
48 {
49 global $sourcedir, $smcFunc;
50
51 $catUpdates = array();
52 $catParameters = array();
53
54 // Wanna change the categories position?
55 if (isset($catOptions['move_after']))
56 {
57 // Store all categories in the proper order.
58 $cats = array();
59 $cat_order = array();
60
61 // Setting 'move_after' to '0' moves the category to the top.
62 if ($catOptions['move_after'] == 0)
63 $cats[] = $category_id;
64
65 // Grab the categories sorted by cat_order.
66 $request = $smcFunc['db_query']('', '
67 SELECT id_cat, cat_order
68 FROM {db_prefix}categories
69 ORDER BY cat_order',
70 array(
71 )
72 );
73 while ($row = $smcFunc['db_fetch_assoc']($request))
74 {
75 if ($row['id_cat'] != $category_id)
76 $cats[] = $row['id_cat'];
77 if ($row['id_cat'] == $catOptions['move_after'])
78 $cats[] = $category_id;
79 $cat_order[$row['id_cat']] = $row['cat_order'];
80 }
81 $smcFunc['db_free_result']($request);
82
83 // Set the new order for the categories.
84 foreach ($cats as $index => $cat)
85 if ($index != $cat_order[$cat])
86 $smcFunc['db_query']('', '
87 UPDATE {db_prefix}categories
88 SET cat_order = {int:new_order}
89 WHERE id_cat = {int:current_category}',
90 array(
91 'new_order' => $index,
92 'current_category' => $cat,
93 )
94 );
95
96 // If the category order changed, so did the board order.
97 require_once($sourcedir . '/Subs-Boards.php');
98 reorderBoards();
99 }
100
101 if (isset($catOptions['cat_name']))
102 {
103 $catUpdates[] = 'name = {string:cat_name}';
104 $catParameters['cat_name'] = $catOptions['cat_name'];
105 }
106
107 // Can a user collapse this category or is it too important?
108 if (isset($catOptions['is_collapsible']))
109 {
110 $catUpdates[] = 'can_collapse = {int:is_collapsible}';
111 $catParameters['is_collapsible'] = $catOptions['is_collapsible'] ? 1 : 0;
112 }
113
114 // Do the updates (if any).
115 if (!empty($catUpdates))
116 {
117 $smcFunc['db_query']('', '
118 UPDATE {db_prefix}categories
119 SET
120 ' . implode(',
121 ', $catUpdates) . '
122 WHERE id_cat = {int:current_category}',
123 array_merge($catParameters, array(
124 'current_category' => $category_id,
125 ))
126 );
127
128 if (empty($catOptions['dont_log']))
129 logAction('edit_cat', array('catname' => isset($catOptions['cat_name']) ? $catOptions['cat_name'] : $category_id), 'admin');
130 }
131 }
132
133 // Create a new category.
134 function createCategory($catOptions)
135 {
136 global $smcFunc;
137
138 // Check required values.
139 if (!isset($catOptions['cat_name']) || trim($catOptions['cat_name']) == '')
140 trigger_error('createCategory(): A category name is required', E_USER_ERROR);
141
142 // Set default values.
143 if (!isset($catOptions['move_after']))
144 $catOptions['move_after'] = 0;
145 if (!isset($catOptions['is_collapsible']))
146 $catOptions['is_collapsible'] = true;
147 // Don't log an edit right after.
148 $catOptions['dont_log'] = true;
149
150 // Add the category to the database.
151 $smcFunc['db_insert']('',
152 '{db_prefix}categories',
153 array(
154 'name' => 'string-48',
155 ),
156 array(
157 $catOptions['cat_name'],
158 ),
159 array('id_cat')
160 );
161
162 // Grab the new category ID.
163 $category_id = $smcFunc['db_insert_id']('{db_prefix}categories', 'id_cat');
164
165 // Set the given properties to the newly created category.
166 modifyCategory($category_id, $catOptions);
167
168 logAction('add_cat', array('catname' => $catOptions['cat_name']), 'admin');
169
170 // Return the database ID of the category.
171 return $category_id;
172 }
173
174 // Remove one or more categories.
175 function deleteCategories($categories, $moveBoardsTo = null)
176 {
177 global $sourcedir, $smcFunc, $cat_tree;
178
179 require_once($sourcedir . '/Subs-Boards.php');
180
181 getBoardTree();
182
183 // With no category set to move the boards to, delete them all.
184 if ($moveBoardsTo === null)
185 {
186 $request = $smcFunc['db_query']('', '
187 SELECT id_board
188 FROM {db_prefix}boards
189 WHERE id_cat IN ({array_int:category_list})',
190 array(
191 'category_list' => $categories,
192 )
193 );
194 $boards_inside = array();
195 while ($row = $smcFunc['db_fetch_assoc']($request))
196 $boards_inside[] = $row['id_board'];
197 $smcFunc['db_free_result']($request);
198
199 if (!empty($boards_inside))
200 deleteBoards($boards_inside, null);
201 }
202
203 // Make sure the safe category is really safe.
204 elseif (in_array($moveBoardsTo, $categories))
205 trigger_error('deleteCategories(): You cannot move the boards to a category that\'s being deleted', E_USER_ERROR);
206
207 // Move the boards inside the categories to a safe category.
208 else
209 $smcFunc['db_query']('', '
210 UPDATE {db_prefix}boards
211 SET id_cat = {int:new_parent_cat}
212 WHERE id_cat IN ({array_int:category_list})',
213 array(
214 'category_list' => $categories,
215 'new_parent_cat' => $moveBoardsTo,
216 )
217 );
218
219 // Noone will ever be able to collapse these categories anymore.
220 $smcFunc['db_query']('', '
221 DELETE FROM {db_prefix}collapsed_categories
222 WHERE id_cat IN ({array_int:category_list})',
223 array(
224 'category_list' => $categories,
225 )
226 );
227
228 // Do the deletion of the category itself
229 $smcFunc['db_query']('', '
230 DELETE FROM {db_prefix}categories
231 WHERE id_cat IN ({array_int:category_list})',
232 array(
233 'category_list' => $categories,
234 )
235 );
236
237 // Log what we've done.
238 foreach ($categories as $category)
239 logAction('delete_cat', array('catname' => $cat_tree[$category]['node']['name']), 'admin');
240
241 // Get all boards back into the right order.
242 reorderBoards();
243 }
244
245 // Collapse, expand or toggle one or more categories for one or more members.
246 function collapseCategories($categories, $new_status, $members = null, $check_collapsable = true)
247 {
248 global $smcFunc;
249
250 // Collapse or expand the categories.
251 if ($new_status === 'collapse' || $new_status === 'expand')
252 {
253 $smcFunc['db_query']('', '
254 DELETE FROM {db_prefix}collapsed_categories
255 WHERE id_cat IN ({array_int:category_list})' . ($members === null ? '' : '
256 AND id_member IN ({array_int:member_list})'),
257 array(
258 'category_list' => $categories,
259 'member_list' => $members,
260 )
261 );
262
263 if ($new_status === 'collapse')
264 $smcFunc['db_query']('', '
265 INSERT INTO {db_prefix}collapsed_categories
266 (id_cat, id_member)
267 SELECT c.id_cat, mem.id_member
268 FROM {db_prefix}categories AS c
269 INNER JOIN {db_prefix}members AS mem ON (' . ($members === null ? '1=1' : '
270 mem.id_member IN ({array_int:member_list})') . ')
271 WHERE c.id_cat IN ({array_int:category_list})' . ($check_collapsable ? '
272 AND c.can_collapse = {int:is_collapsible}' : ''),
273 array(
274 'member_list' => $members,
275 'category_list' => $categories,
276 'is_collapsible' => 1,
277 )
278 );
279 }
280
281 // Toggle the categories: collapsed get expanded and expanded get collapsed.
282 elseif ($new_status === 'toggle')
283 {
284 // Get the current state of the categories.
285 $updates = array(
286 'insert' => array(),
287 'remove' => array(),
288 );
289 $request = $smcFunc['db_query']('', '
290 SELECT mem.id_member, c.id_cat, IFNULL(cc.id_cat, 0) AS is_collapsed, c.can_collapse
291 FROM {db_prefix}members AS mem
292 INNER JOIN {db_prefix}categories AS c ON (c.id_cat IN ({array_int:category_list}))
293 LEFT JOIN {db_prefix}collapsed_categories AS cc ON (cc.id_cat = c.id_cat AND cc.id_member = mem.id_member)
294 ' . ($members === null ? '' : '
295 WHERE mem.id_member IN ({array_int:member_list})'),
296 array(
297 'category_list' => $categories,
298 'member_list' => $members,
299 )
300 );
301 while ($row = $smcFunc['db_fetch_assoc']($request))
302 {
303 if (empty($row['is_collapsed']) && (!empty($row['can_collapse']) || !$check_collapsable))
304 $updates['insert'][] = array($row['id_member'], $row['id_cat']);
305 elseif (!empty($row['is_collapsed']))
306 $updates['remove'][] = '(id_member = ' . $row['id_member'] . ' AND id_cat = ' . $row['id_cat'] . ')';
307 }
308 $smcFunc['db_free_result']($request);
309
310 // Collapse the ones that were originally expanded...
311 if (!empty($updates['insert']))
312 $smcFunc['db_insert']('replace',
313 '{db_prefix}collapsed_categories',
314 array(
315 'id_cat' => 'int', 'id_member' => 'int',
316 ),
317 $updates['insert'],
318 array('id_cat', 'id_member')
319 );
320
321 // And expand the ones that were originally collapsed.
322 if (!empty($updates['remove']))
323 $smcFunc['db_query']('', '
324 DELETE FROM {db_prefix}collapsed_categories
325 WHERE ' . implode(' OR ', $updates['remove']),
326 array(
327 )
328 );
329 }
330 }
331
332 ?>