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