Passed
Push — master ( 932680...d062ce )
by John
01:56
created

UltimateMenu::listGroups()   B

Complexity

Conditions 11
Paths 36

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 33
nc 36
nop 5
dl 0
loc 48
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package   Ultimate Menu mod
5
 * @version   1.1
6
 * @author    John Rayes <[email protected]>
7
 * @copyright Copyright (c) 2014, John Rayes
8
 * @license   http://opensource.org/licenses/MIT MIT
9
 */
10
11
class UltimateMenu
12
{
13
	/**
14
	 * Gets all membergroups and filters them according to the parameters.
15
	 *
16
	 * @param int[] $checked    comma-seperated list of all id_groups to be checked (have a mark in the checkbox). Default
17
	 *                           is an empty array.
18
	 * @param int[] $disallowed comma-seperated list of all id_groups that are skipped. Default is an empty array.
19
	 * @param bool  $inherited  whether or not to filter out the inherited groups. Default is false.
20
	 *
21
	 * @return array all the membergroups filtered according to the parameters; empty array if something went wrong.
22
	 */
23
	public function listGroups($checked, $disallowed = '', $inherited = false, $permission = null, $board_id = null)
24
	{
25
		global $modSettings, $smcFunc, $sourcedir, $txt;
26
27
		loadLanguage('ManageBoards');
28
		$groups = array();
29
		if (!in_array(-1, $disallowed))
0 ignored issues
show
Bug introduced by
It seems like $disallowed can also be of type string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
		if (!in_array(-1, /** @scrutinizer ignore-type */ $disallowed))
Loading history...
30
			$groups[-1] = array(
31
				'id' => -1,
32
				'name' => $txt['parent_guests_only'],
33
				'checked' => in_array(-1, $checked) || in_array(-3, $checked),
34
				'is_post_group' => false,
35
			);
36
		if (!in_array(0, $disallowed))
37
			$groups[0] = array(
38
				'id' => 0,
39
				'name' => $txt['parent_members_only'],
40
				'checked' => in_array(0, $checked) || in_array(-3, $checked),
41
				'is_post_group' => false,
42
			);
43
		$request = $smcFunc['db_query']('', '
44
			SELECT
45
				group_name, id_group, min_posts
46
			FROM {db_prefix}membergroups
47
			WHERE id_group > {int:is_zero}' . (!$inherited ? '
48
				AND id_parent = {int:not_inherited}' : '') . (!$inherited && empty($modSettings['permission_enable_postgroups']) ? '
49
				AND min_posts = {int:min_posts}' : ''),
50
			array(
51
				'is_zero' => 0,
52
				'not_inherited' => -2,
53
				'min_posts' => -1,
54
			)
55
		);
56
		while ($row = $smcFunc['db_fetch_assoc']($request))
57
		{
58
			if (!in_array($row['id_group'], $disallowed))
59
				$groups[$row['id_group']] = array(
60
					'id' => $row['id_group'],
61
					'name' => trim($row['group_name']),
62
					'checked' => in_array($row['id_group'], $checked) || in_array(-3, $checked),
63
					'is_post_group' => $row['min_posts'] != -1,
64
				);
65
		}
66
		$smcFunc['db_free_result']($request);
67
68
		asort($groups);
69
70
		return $groups;
71
	}
72
73
	/**
74
	 * Loads all buttons from the db
75
	 *
76
	 * @return string[]
77
	 */
78
	public function total_getMenu()
79
	{
80
		global $smcFunc;
81
82
		$request = $smcFunc['db_query']('', '
83
			SELECT
84
				id_button, name, target, type, position, link, status, permissions, parent
85
			FROM {db_prefix}um_menu'
86
		);
87
		$buttons = array();
88
		while ($row = $smcFunc['db_fetch_assoc']($request))
89
			$buttons[] = $row;
90
91
		return $buttons;
92
	}
93
94
	/**
95
	 * Createlist callback, used to display um entries
96
	 *
97
	 * @param int    $start
98
	 * @param int    $items_per_page
99
	 * @param string $sort
100
	 *
101
	 * @return string[]
102
	 */
103
	public function list_getMenu($start, $items_per_page, $sort)
104
	{
105
		global $smcFunc;
106
107
		$request = $smcFunc['db_query']('', '
108
			SELECT
109
				id_button, name, target, type, position, link, status, parent
110
			FROM {db_prefix}um_menu
111
			ORDER BY {raw:sort}
112
			LIMIT {int:offset}, {int:limit}',
113
			array(
114
				'sort' => $sort,
115
				'offset' => $start,
116
				'limit' => $items_per_page,
117
			)
118
		);
119
		$buttons = array();
120
		while ($row = $smcFunc['db_fetch_assoc']($request))
121
			$buttons[] = $row;
122
123
		return $buttons;
124
	}
125
126
	/**
127
	 * Createlist callback to determine the number of buttons
128
	 * 
129
	 * @return int
130
	 */
131
	public function list_getNumButtons()
132
	{
133
		global $smcFunc;
134
135
		$request = $smcFunc['db_query']('', '
136
			SELECT COUNT(*)
137
			FROM {db_prefix}um_menu'
138
		);
139
		list ($numButtons) = $smcFunc['db_fetch_row']($request);
140
		$smcFunc['db_free_result']($request);
141
142
		return $numButtons;
143
	}
144
145
	/**
146
	 * Sets the serialized array of buttons into settings
147
	 *
148
	 * Called whenever the menu structure is updated in the ACP
149
	 */
150
	public function rebuildMenu()
151
	{
152
		global $smcFunc;
153
154
		$request = $smcFunc['db_query']('', '
155
			SELECT *
156
			FROM {db_prefix}um_menu'
157
		);
158
		$buttons = array();
159
		while ($row = $smcFunc['db_fetch_assoc']($request))
160
			$buttons['um_button_' . $row['id_button']] = json_encode($row);
161
		$smcFunc['db_free_result']($request);
162
		updateSettings(
163
			array(
164
				'um_count' => count($buttons),
165
			) + $buttons
166
		);
167
	}
168
169
	/**
170
	 * Removes menu item(s) from the um system
171
	 *
172
	 * @param int[] $ids
173
	 */
174
	public function deleteButton(array $ids)
175
	{
176
		global $smcFunc;
177
178
		$smcFunc['db_query']('', '
179
			DELETE FROM {db_prefix}um_menu
180
			WHERE id_button IN ({array_int:button_list})',
181
			array(
182
				'button_list' => $ids,
183
			)
184
		);
185
	}
186
187
	/**
188
	 * Changes the status of a button from active to inactive
189
	 *
190
	 * @param array $updates
191
	 */
192
	public function updateButton(array $updates)
193
	{
194
		global $smcFunc;
195
196
		foreach ($this->total_getMenu() as $item)
197
		{
198
			$status = !empty($updates['status'][$item['id_button']]) ? 'active' : 'inactive';
199
			if ($status != $item['status'])
200
				$smcFunc['db_query']('', '
201
					UPDATE {db_prefix}um_menu
202
					SET status = {string:status}
203
					WHERE id_button = {int:item}',
204
					array(
205
						'status' => $status,
206
						'item' => $item['id_button'],
207
					)
208
				);
209
		}
210
	}
211
212
	/**
213
	 * Checks if there is an existing um id with the same name before saving
214
	 *
215
	 * @param int    $id
216
	 * @param string $name
217
	 *
218
	 * @return int
219
	 */
220
	public function checkButton($id, $name)
221
	{
222
		global $smcFunc;
223
224
		$request = $smcFunc['db_query']('', '
225
			SELECT id_button
226
			FROM {db_prefix}um_menu
227
			WHERE name = {string:name}
228
				AND id_button != {int:id}',
229
			array(
230
				'name' => $name,
231
				'id' => $id ?: 0,
232
			)
233
		);
234
		$check = $smcFunc['db_num_rows']($request);
235
		$smcFunc['db_free_result']($request);
236
237
		return $check;
238
	}
239
240
	/**
241
	 * Saves a new or updates an existing button
242
	 */
243
	public function saveButton(array $menu_entry)
244
	{
245
		global $smcFunc;
246
247
		if (!empty($menu_entry['id']))
248
		{
249
			$smcFunc['db_query']('', '
250
				UPDATE {db_prefix}um_menu
251
				SET
252
					name = {string:name},
253
					type = {string:type},
254
					target = {string:target},
255
					position = {string:position},
256
					link = {string:link},
257
					status = {string:status},
258
					permissions = {string:permissions},
259
					parent = {string:parent}
260
				WHERE id_button = {int:id}',
261
				array(
262
					'id' => $menu_entry['id'],
263
					'name' => $menu_entry['name'],
264
					'type' => $menu_entry['type'],
265
					'target' => $menu_entry['target'],
266
					'position' => $menu_entry['position'],
267
					'link' => $menu_entry['link'],
268
					'status' => $menu_entry['status'],
269
					'permissions' => implode(',', array_filter($menu_entry['permissions'], 'strlen')),
270
					'parent' => $menu_entry['parent'],
271
				)
272
			);
273
		}
274
		else
275
		{
276
			$smcFunc['db_insert'](
277
				'insert',
278
				'{db_prefix}um_menu',
279
				array(
280
					'name' => 'string',
281
					'type' => 'string',
282
					'target' => 'string',
283
					'position' => 'string',
284
					'link' => 'string',
285
					'status' => 'string',
286
					'permissions' => 'string',
287
					'parent' => 'string',
288
				),
289
				array(
290
					$menu_entry['name'],
291
					$menu_entry['type'],
292
					$menu_entry['target'],
293
					$menu_entry['position'],
294
					$menu_entry['link'],
295
					$menu_entry['status'],
296
					implode(',', array_filter($menu_entry['permissions'], 'strlen')),
297
					$menu_entry['parent'],
298
				),
299
				array('id_button')
300
			);
301
		}
302
	}
303
304
	/**
305
	 * Fetch a specific button
306
	 *
307
	 * @param int $id
308
	 *
309
	 * @return array
310
	 */
311
	public function fetchButton($id)
312
	{
313
		global $smcFunc;
314
315
		$request = $smcFunc['db_query']('', '
316
			SELECT
317
				id_button, name, target, type, position, link, status, permissions, parent
318
			FROM {db_prefix}um_menu
319
			WHERE id_button = {int:button}',
320
			array(
321
				'button' => $id,
322
			)
323
		);
324
		$row = $smcFunc['db_fetch_assoc']($request);
325
		$smcFunc['db_free_result']($request);
326
327
		return array(
328
			'id' => $row['id_button'],
329
			'name' => $row['name'],
330
			'target' => $row['target'],
331
			'type' => $row['type'],
332
			'position' => $row['position'],
333
			'permissions' => explode(',', $row['permissions']),
334
			'link' => $row['link'],
335
			'status' => $row['status'],
336
			'parent' => $row['parent'],
337
		);
338
	}
339
340
	/**
341
	 * Removes all buttons
342
	 */
343
	public function deleteallButtons()
344
	{
345
		global $smcFunc;
346
347
		$smcFunc['db_query']('', '
348
			TRUNCATE {db_prefix}um_menu'
349
		);
350
	}
351
352
	/**
353
	 * Fetches the names of all SMF menu buttons.
354
	 *
355
	 * @return array
356
	 */
357
	public function getButtonNames()
358
	{
359
		global $context;
360
361
		$button_names = [];
362
		foreach ($context['menu_buttons'] as $button_index => $button_data)
363
		{
364
			$button_names[$button_index] = $button_data['title'];
365
366
			if (!empty($button_data['sub_buttons']))
367
			{
368
				foreach ($button_data['sub_buttons'] as $child_button => $child_button_data)
369
					$button_names[$child_button] = $child_button_data['title'];
370
371
				if (!empty($child_button_data['sub_buttons']))
372
					foreach ($child_button_data['sub_buttons'] as $grand_child_button => $grand_child_button_data)
373
						$button_names[$grand_child_button] = $grand_child_button_data['title'];
374
			}
375
		}
376
377
		return $button_names;
378
	}
379
}