Passed
Pull Request — release-2.1 (#6262)
by Jeremy
04:04
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
c 6
b 6
f 0
nc 7
nop 1
dl 0
loc 18
rs 9.6111
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
			if ($row['id_cat'] == $catOptions['move_after'])
61
				$cats[] = $category_id;
62
			$cat_order[$row['id_cat']] = $row['cat_order'];
63
		}
64
		$smcFunc['db_free_result']($request);
65
66
		// Set the new order for the categories.
67
		foreach ($cats as $index => $cat)
68
			if ($index != $cat_order[$cat])
69
				$smcFunc['db_query']('', '
70
					UPDATE {db_prefix}categories
71
					SET cat_order = {int:new_order}
72
					WHERE id_cat = {int:current_category}',
73
					array(
74
						'new_order' => $index,
75
						'current_category' => $cat,
76
					)
77
				);
78
79
		// If the category order changed, so did the board order.
80
		require_once($sourcedir . '/Subs-Boards.php');
81
		reorderBoards();
82
	}
83
84
	if (isset($catOptions['cat_name']))
85
	{
86
		$catUpdates[] = 'name = {string:cat_name}';
87
		$catParameters['cat_name'] = $catOptions['cat_name'];
88
	}
89
90
	if (isset($catOptions['cat_desc']))
91
	{
92
		$catUpdates[] = 'description = {string:cat_desc}';
93
		$catParameters['cat_desc'] = $catOptions['cat_desc'];
94
	}
95
96
	// Can a user collapse this category or is it too important?
97
	if (isset($catOptions['is_collapsible']))
98
	{
99
		$catUpdates[] = 'can_collapse = {int:is_collapsible}';
100
		$catParameters['is_collapsible'] = $catOptions['is_collapsible'] ? 1 : 0;
101
	}
102
103
	$cat_id = $category_id;
104
	call_integration_hook('integrate_modify_category', array($cat_id, &$catUpdates, &$catParameters));
105
106
	// Do the updates (if any).
107
	if (!empty($catUpdates))
108
	{
109
		$smcFunc['db_query']('', '
110
			UPDATE {db_prefix}categories
111
			SET
112
				' . implode(',
113
				', $catUpdates) . '
114
			WHERE id_cat = {int:current_category}',
115
			array_merge($catParameters, array(
116
				'current_category' => $category_id,
117
			))
118
		);
119
120
		if (empty($catOptions['dont_log']))
121
			logAction('edit_cat', array('catname' => isset($catOptions['cat_name']) ? $catOptions['cat_name'] : $category_id), 'admin');
122
	}
123
}
124
125
/**
126
 * Create a new category.
127
 * general function to create a new category and set its position.
128
 * allows (almost) the same options as the modifyCat() function.
129
 * returns the ID of the newly created category.
130
 *
131
 * @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'
132
 */
133
function createCategory($catOptions)
134
{
135
	global $smcFunc;
136
137
	// Check required values.
138
	if (!isset($catOptions['cat_name']) || trim($catOptions['cat_name']) == '')
139
		trigger_error('createCategory(): A category name is required', E_USER_ERROR);
140
141
	// Set default values.
142
	if (!isset($catOptions['cat_desc']))
143
		$catOptions['cat_desc'] = '';
144
	if (!isset($catOptions['move_after']))
145
		$catOptions['move_after'] = 0;
146
	if (!isset($catOptions['is_collapsible']))
147
		$catOptions['is_collapsible'] = true;
148
	// Don't log an edit right after.
149
	$catOptions['dont_log'] = true;
150
151
	$cat_columns = array(
152
		'name' => 'string-48',
153
		'description' => 'string',
154
	);
155
	$cat_parameters = array(
156
		$catOptions['cat_name'],
157
		$catOptions['cat_desc'],
158
	);
159
160
	call_integration_hook('integrate_create_category', array(&$catOptions, &$cat_columns, &$cat_parameters));
161
162
	// Add the category to the database.
163
	$category_id = $smcFunc['db_insert']('',
164
		'{db_prefix}categories',
165
		$cat_columns,
166
		$cat_parameters,
167
		array('id_cat'),
168
		1
169
	);
170
171
	// Set the given properties to the newly created category.
172
	modifyCategory($category_id, $catOptions);
173
174
	logAction('add_cat', array('catname' => $catOptions['cat_name']), 'admin');
175
176
	// Return the database ID of the category.
177
	return $category_id;
178
}
179
180
/**
181
 * Remove one or more categories.
182
 * general function to delete one or more categories.
183
 * allows to move all boards in the categories to a different category before deleting them.
184
 * if moveChildrenTo is set to null, all boards inside the given categories will be deleted.
185
 * deletes all information that's associated with the given categories.
186
 * updates the statistics to reflect the new situation.
187
 *
188
 * @param array $categories The IDs of the categories to delete
189
 * @param int $moveBoardsTo The ID of the category to move any boards to or null to delete the boards
190
 */
191
function deleteCategories($categories, $moveBoardsTo = null)
192
{
193
	global $sourcedir, $smcFunc, $cat_tree;
194
195
	require_once($sourcedir . '/Subs-Boards.php');
196
197
	getBoardTree();
198
199
	call_integration_hook('integrate_delete_category', array($categories, &$moveBoardsTo));
200
201
	// With no category set to move the boards to, delete them all.
202
	if ($moveBoardsTo === null)
203
	{
204
		$request = $smcFunc['db_query']('', '
205
			SELECT id_board
206
			FROM {db_prefix}boards
207
			WHERE id_cat IN ({array_int:category_list})',
208
			array(
209
				'category_list' => $categories,
210
			)
211
		);
212
		$boards_inside = array();
213
		while ($row = $smcFunc['db_fetch_assoc']($request))
214
			$boards_inside[] = $row['id_board'];
215
		$smcFunc['db_free_result']($request);
216
217
		if (!empty($boards_inside))
218
			deleteBoards($boards_inside, null);
219
	}
220
221
	// Make sure the safe category is really safe.
222
	elseif (in_array($moveBoardsTo, $categories))
223
		trigger_error('deleteCategories(): You cannot move the boards to a category that\'s being deleted', E_USER_ERROR);
224
225
	// Move the boards inside the categories to a safe category.
226
	else
227
		$smcFunc['db_query']('', '
228
			UPDATE {db_prefix}boards
229
			SET id_cat = {int:new_parent_cat}
230
			WHERE id_cat IN ({array_int:category_list})',
231
			array(
232
				'category_list' => $categories,
233
				'new_parent_cat' => $moveBoardsTo,
234
			)
235
		);
236
237
	// Do the deletion of the category itself
238
	$smcFunc['db_query']('', '
239
		DELETE FROM {db_prefix}categories
240
		WHERE id_cat IN ({array_int:category_list})',
241
		array(
242
			'category_list' => $categories,
243
		)
244
	);
245
246
	// Log what we've done.
247
	foreach ($categories as $category)
248
		logAction('delete_cat', array('catname' => $cat_tree[$category]['node']['name']), 'admin');
249
250
	// Get all boards back into the right order.
251
	reorderBoards();
252
}
253
254
?>