Passed
Push — release-2.1 ( 493a4c...4017f8 )
by Mathias
09:59 queued 10s
created

setCategoryParsedDescription()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 6 Features 0
Metric Value
cc 5
eloc 10
nop 1
dl 0
loc 18
rs 9.6111
c 6
b 6
f 0
nc 7
1
<?php
2
3
/**
4
 * This file contains the functions to add, modify, remove, collapse and expand categories.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2020 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC3
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Edit the position and properties of a category.
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
 * @param int $category_id The ID of the category
25
 * @param array $catOptions An array containing data and options related to the category
26
 */
27
function modifyCategory($category_id, $catOptions)
28
{
29
	global $sourcedir, $smcFunc;
30
31
	$catUpdates = array();
32
	$catParameters = array();
33
34
	$cat_id = $category_id;
35
	call_integration_hook('integrate_pre_modify_category', array($cat_id, &$catOptions));
36
37
	// Wanna change the categories position?
38
	if (isset($catOptions['move_after']))
39
	{
40
		// Store all categories in the proper order.
41
		$cats = array();
42
		$cat_order = array();
43
44
		// Setting 'move_after' to '0' moves the category to the top.
45
		if ($catOptions['move_after'] == 0)
46
			$cats[] = $category_id;
47
48
		// Grab the categories sorted by cat_order.
49
		$request = $smcFunc['db_query']('', '
50
			SELECT id_cat, cat_order
51
			FROM {db_prefix}categories
52
			ORDER BY cat_order',
53
			array(
54
			)
55
		);
56
		while ($row = $smcFunc['db_fetch_assoc']($request))
57
		{
58
			if ($row['id_cat'] != $category_id)
59
				$cats[] = $row['id_cat'];
60
61
			if ($row['id_cat'] == $catOptions['move_after'])
62
				$cats[] = $category_id;
63
64
			$cat_order[$row['id_cat']] = $row['cat_order'];
65
		}
66
		$smcFunc['db_free_result']($request);
67
68
		// Set the new order for the categories.
69
		foreach ($cats as $index => $cat)
70
			if ($index != $cat_order[$cat])
71
				$smcFunc['db_query']('', '
72
					UPDATE {db_prefix}categories
73
					SET cat_order = {int:new_order}
74
					WHERE id_cat = {int:current_category}',
75
					array(
76
						'new_order' => $index,
77
						'current_category' => $cat,
78
					)
79
				);
80
81
		// If the category order changed, so did the board order.
82
		require_once($sourcedir . '/Subs-Boards.php');
83
		reorderBoards();
84
	}
85
86
	if (isset($catOptions['cat_name']))
87
	{
88
		$catUpdates[] = 'name = {string:cat_name}';
89
		$catParameters['cat_name'] = $catOptions['cat_name'];
90
	}
91
92
	if (isset($catOptions['cat_desc']))
93
	{
94
		$catUpdates[] = 'description = {string:cat_desc}';
95
		$catParameters['cat_desc'] = $catOptions['cat_desc'];
96
97
		setCategoryParsedDescription(array(
98
			$category_id => $catOptions['cat_desc']
99
		));
100
	}
101
102
	// Can a user collapse this category or is it too important?
103
	if (isset($catOptions['is_collapsible']))
104
	{
105
		$catUpdates[] = 'can_collapse = {int:is_collapsible}';
106
		$catParameters['is_collapsible'] = $catOptions['is_collapsible'] ? 1 : 0;
107
	}
108
109
	$cat_id = $category_id;
110
	call_integration_hook('integrate_modify_category', array($cat_id, &$catUpdates, &$catParameters));
111
112
	// Do the updates (if any).
113
	if (!empty($catUpdates))
114
	{
115
		$smcFunc['db_query']('', '
116
			UPDATE {db_prefix}categories
117
			SET
118
				' . implode(',
119
				', $catUpdates) . '
120
			WHERE id_cat = {int:current_category}',
121
			array_merge($catParameters, array(
122
				'current_category' => $category_id,
123
			))
124
		);
125
126
		if (empty($catOptions['dont_log']))
127
			logAction('edit_cat', array('catname' => isset($catOptions['cat_name']) ? $catOptions['cat_name'] : $category_id), 'admin');
128
	}
129
}
130
131
/**
132
 * Create a new category.
133
 * general function to create a new category and set its position.
134
 * allows (almost) the same options as the modifyCat() function.
135
 * returns the ID of the newly created category.
136
 *
137
 * @param array $catOptions An array of data and settings related to the new category. Should have at least 'cat_name' and can also have 'cat_desc', 'move_after' and 'is_collapsable'
138
 * @return mixed
139
 */
140
function createCategory($catOptions)
141
{
142
	global $smcFunc;
143
144
	// Check required values.
145
	if (!isset($catOptions['cat_name']) || trim($catOptions['cat_name']) == '')
146
		trigger_error('createCategory(): A category name is required', E_USER_ERROR);
147
148
	// Set default values.
149
	if (!isset($catOptions['cat_desc']))
150
		$catOptions['cat_desc'] = '';
151
152
	if (!isset($catOptions['move_after']))
153
		$catOptions['move_after'] = 0;
154
155
	if (!isset($catOptions['is_collapsible']))
156
		$catOptions['is_collapsible'] = true;
157
158
	// Don't log an edit right after.
159
	$catOptions['dont_log'] = true;
160
161
	$cat_columns = array(
162
		'name' => 'string-48',
163
		'description' => 'string',
164
	);
165
	$cat_parameters = array(
166
		$catOptions['cat_name'],
167
		$catOptions['cat_desc'],
168
	);
169
170
	call_integration_hook('integrate_create_category', array(&$catOptions, &$cat_columns, &$cat_parameters));
171
172
	// Add the category to the database.
173
	$category_id = $smcFunc['db_insert']('',
174
		'{db_prefix}categories',
175
		$cat_columns,
176
		$cat_parameters,
177
		array('id_cat'),
178
		1
179
	);
180
181
	// Set the given properties to the newly created category.
182
	modifyCategory($category_id, $catOptions);
183
184
	logAction('add_cat', array('catname' => $catOptions['cat_name']), 'admin');
185
186
	// Return the database ID of the category.
187
	return $category_id;
188
}
189
190
/**
191
 * Remove one or more categories.
192
 * general function to delete one or more categories.
193
 * allows to move all boards in the categories to a different category before deleting them.
194
 * if moveChildrenTo is set to null, all boards inside the given categories will be deleted.
195
 * deletes all information that's associated with the given categories.
196
 * updates the statistics to reflect the new situation.
197
 *
198
 * @param array $categories The IDs of the categories to delete
199
 * @param int $moveBoardsTo The ID of the category to move any boards to or null to delete the boards
200
 */
201
function deleteCategories($categories, $moveBoardsTo = null)
202
{
203
	global $sourcedir, $smcFunc, $cat_tree;
204
205
	require_once($sourcedir . '/Subs-Boards.php');
206
207
	getBoardTree();
208
209
	call_integration_hook('integrate_delete_category', array($categories, &$moveBoardsTo));
210
211
	// With no category set to move the boards to, delete them all.
212
	if ($moveBoardsTo === null)
213
	{
214
		$request = $smcFunc['db_query']('', '
215
			SELECT id_board
216
			FROM {db_prefix}boards
217
			WHERE id_cat IN ({array_int:category_list})',
218
			array(
219
				'category_list' => $categories,
220
			)
221
		);
222
		$boards_inside = array();
223
		while ($row = $smcFunc['db_fetch_assoc']($request))
224
			$boards_inside[] = $row['id_board'];
225
		$smcFunc['db_free_result']($request);
226
227
		if (!empty($boards_inside))
228
			deleteBoards($boards_inside, null);
229
	}
230
231
	// Make sure the safe category is really safe.
232
	elseif (in_array($moveBoardsTo, $categories))
233
		trigger_error('deleteCategories(): You cannot move the boards to a category that\'s being deleted', E_USER_ERROR);
234
235
	// Move the boards inside the categories to a safe category.
236
	else
237
		$smcFunc['db_query']('', '
238
			UPDATE {db_prefix}boards
239
			SET id_cat = {int:new_parent_cat}
240
			WHERE id_cat IN ({array_int:category_list})',
241
			array(
242
				'category_list' => $categories,
243
				'new_parent_cat' => $moveBoardsTo,
244
			)
245
		);
246
247
	// Do the deletion of the category itself
248
	$smcFunc['db_query']('', '
249
		DELETE FROM {db_prefix}categories
250
		WHERE id_cat IN ({array_int:category_list})',
251
		array(
252
			'category_list' => $categories,
253
		)
254
	);
255
256
	// Log what we've done.
257
	foreach ($categories as $category)
258
		logAction('delete_cat', array('catname' => $cat_tree[$category]['node']['name']), 'admin');
259
260
	// Get all boards back into the right order.
261
	reorderBoards();
262
}
263
264
function setCategoryParsedDescription($category_info = array())
265
{
266
	global $cache_enable, $context;
267
268
	if (empty($category_info))
269
		return $category_info;
270
271
	// Get the data we already parsed
272
	$already_parsed_categories = getCategoriesParsedDescription();
273
274
	foreach ($category_info as $category_id => $category_description)
275
	$already_parsed_categories[$category_id] = !empty($category_description) ?
276
		parse_bbc($category_description, false, '', $context['description_allowed_tags']) : '';
277
278
	if (!empty($cache_enable))
279
		cache_put_data('parsed_category_descriptions', $already_parsed_categories, 864000);
280
281
	return $already_parsed_categories;
282
}
283
284
function getCategoriesParsedDescription()
285
{
286
	global $cache_enable;
287
288
	if (empty($cache_enable))
289
		return array();
290
291
	return cache_get_data('parsed_category_descriptions', 864000);
292
}
293
294
?>