Passed
Push — master ( da58ee...4c81df )
by John
02:17
created

UltimateMenu::rebuildMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 2
nc 2
nop 0
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 string $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 string $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
	 * @param null   $permission
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $permission is correct as it would always require null to be passed?
Loading history...
21
	 * @param null   $board_id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $board_id is correct as it would always require null to be passed?
Loading history...
22
	 *
23
	 * @return array all the membergroups filtered according to the parameters; empty array if something went wrong.
24
	 */
25
	function list_groups($checked, $disallowed = '', $inherited = false, $permission = null, $board_id = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
	{
27
		global $modSettings, $smcFunc, $sourcedir, $txt;
28
29
		// We'll need this for loading up the names of each group.
30
		if (!loadLanguage('ManageBoards'))
31
			loadLanguage('ManageBoards');
32
33
		$checked = explode(',', $checked);
34
		$disallowed = explode(',', $disallowed);
35
36
		// Are we also looking up permissions?
37
		if ($permission !== null)
0 ignored issues
show
introduced by
The condition $permission !== null is always false.
Loading history...
38
		{
39
			require_once($sourcedir . '/Subs-Members.php');
40
			$member_groups = groupsAllowedTo($permission, $board_id);
41
			$disallowed = array_diff(array_keys(list_groups(-3)), $member_groups['allowed']);
0 ignored issues
show
Bug introduced by
The function list_groups was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

41
			$disallowed = array_diff(array_keys(/** @scrutinizer ignore-call */ list_groups(-3)), $member_groups['allowed']);
Loading history...
42
		}
43
44
		$groups = array();
45
		if (!in_array(-1, $disallowed))
46
			$groups[-1] = array(
47
				'id' => -1,
48
				'name' => $txt['parent_guests_only'],
49
				'checked' => in_array(-1, $checked) || in_array(-3, $checked),
50
				'is_post_group' => false,
51
			);
52
		if (!in_array(0, $disallowed))
53
			$groups[0] = array(
54
				'id' => 0,
55
				'name' => $txt['parent_members_only'],
56
				'checked' => in_array(0, $checked) || in_array(-3, $checked),
57
				'is_post_group' => false,
58
			);
59
		$request = $smcFunc['db_query']('', '
60
			SELECT
61
				group_name, id_group, min_posts
62
			FROM {db_prefix}membergroups
63
			WHERE id_group > {int:is_zero}' . (!$inherited ? '
64
				AND id_parent = {int:not_inherited}' : '') . (!$inherited && empty($modSettings['permission_enable_postgroups']) ? '
65
				AND min_posts = {int:min_posts}' : ''),
66
			array(
67
				'is_zero' => 0,
68
				'not_inherited' => -2,
69
				'min_posts' => -1,
70
			)
71
		);
72
		while ($row = $this->db->fetch_assoc($request))
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
73
		{
74
			if (!in_array($row['id_group'], $disallowed))
75
				$groups[(int) $row['id_group']] = array(
76
					'id' => $row['id_group'],
77
					'name' => trim($row['group_name']),
78
					'checked' => in_array($row['id_group'], $checked) || in_array(-3, $checked),
79
					'is_post_group' => $row['min_posts'] != -1,
80
				);
81
		}
82
		$this->db->free_result($request);
83
84
		asort($groups);
85
86
		return $groups;
87
	}
88
89
	/**
90
	 * Loads all buttons from the db
91
	 *
92
	 * @return string[]
93
	 */
94
	function total_getMenu()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
95
	{
96
		global $smcFunc;
97
98
		$request = $smcFunc['db_query']('', '
99
			SELECT
100
				id_button, name, target, type, position, link, status, permissions, parent
101
			FROM {db_prefix}um_menu'
102
		);
103
		$buttons = array();
104
		while ($row = $this->db->fetch_assoc($request))
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
105
			$buttons[] = $row;
106
107
		return $buttons;
108
	}
109
110
	/**
111
	 * Createlist callback, used to display um entries
112
	 *
113
	 * @param int    $start
114
	 * @param int    $items_per_page
115
	 * @param string $sort
116
	 *
117
	 * @return string[]
118
	 */
119
	function list_getMenu($start, $items_per_page, $sort)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
120
	{
121
		global $smcFunc;
122
123
		$request = $smcFunc['db_query']('', '
124
			SELECT
125
				id_button, name, target, type, position, link, status, parent
126
			FROM {db_prefix}um_menu
127
			ORDER BY {raw:sort}
128
			LIMIT {int:offset}, {int:limit}',
129
			array(
130
				'sort' => $sort,
131
				'offset' => $start,
132
				'limit' => $items_per_page,
133
			)
134
		);
135
		$buttons = array();
136
		while ($row = $this->db->fetch_assoc($request))
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
137
			$buttons[] = $row;
138
139
		return $buttons;
140
	}
141
142
	/**
143
	 * Createlist callback to determine the number of buttons
144
	 * 
145
	 * @return int
146
	 */
147
	function list_getNumButtons()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
148
	{
149
		global $smcFunc;
150
151
		$request = $smcFunc['db_query']('', '
152
			SELECT COUNT(*)
153
			FROM {db_prefix}um_menu'
154
		);
155
		list ($numButtons) = $this->db->fetch_row($request);
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
156
		$this->db->free_result($request);
157
158
		return $numButtons;
159
	}
160
161
	/**
162
	 * Sets the serialized array of buttons into settings
163
	 *
164
	 * Called whenever the menu structure is updated in the ACP
165
	 */
166
	function rebuildMenu()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
167
	{
168
		global $smcFunc;
169
170
		$request = $smcFunc['db_query']('', '
171
			SELECT *
172
			FROM {db_prefix}um_menu'
173
		);
174
		$buttons = array();
175
		while ($row = $this->db->fetch_assoc($request))
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
176
			$buttons['um_button_' . $row['id_button']] = json_encode($row);
177
		$this->db->free_result($request);
178
		updateSettings(
179
			array(
180
				'um_count' => count($buttons),
181
			) + $buttons
182
		);
183
	}
184
185
	/**
186
	 * Removes menu item(s) from the um system
187
	 *
188
	 * @param int[] $ids
189
	 */
190
	function deleteButton($ids)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
191
	{
192
		global $smcFunc;
193
194
		$smcFunc['db_query']('', '
195
			DELETE FROM {db_prefix}um_menu
196
			WHERE id_button IN ({array_int:button_list})',
197
			array(
198
				'button_list' => $ids,
199
			)
200
		);
201
	}
202
203
	/**
204
	 * Changes the status of a button from active to inactive
205
	 *
206
	 * @param int    $id
207
	 * @param string $updates
208
	 */
209
	function updateButton($id, $updates)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
210
	{
211
		global $smcFunc;
212
213
		foreach ($this->total_getMenu() as $item)
214
		{
215
			$status = !empty($updates['status'][$item['id_button']]) ? 'active' : 'inactive';
216
			if ($status != $item['status'])
217
				$smcFunc['db_query']('', '
218
					UPDATE {db_prefix}um_menu
219
					SET status = {string:status}
220
					WHERE id_button = {int:item}',
221
					array(
222
						'status' => $status,
223
						'item' => $id,
224
					)
225
				);
226
		}
227
	}
228
229
	/**
230
	 * Checks if there is an existing um id with the same name before saving
231
	 *
232
	 * @param int    $id
233
	 * @param string $name
234
	 *
235
	 * @return int
236
	 */
237
	function checkButton($id, $name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
238
	{
239
		global $smcFunc;
240
241
		$request = $smcFunc['db_query']('', '
242
			SELECT id_button
243
			FROM {db_prefix}um_menu
244
			WHERE name = {string:name}
245
				AND id_button != {int:id}',
246
			array(
247
				'name' => $name,
248
				'id' => $id ?: 0,
249
			)
250
		);
251
		$check = $this->db->num_rows($request);
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
252
		$this->db->free_result($request);
253
254
		return $check;
255
	}
256
257
	/**
258
	 * Saves a new or updates an existing button
259
	 */
260
	function saveButton($menu_entry)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
261
	{
262
		global $smcFunc;
263
264
		if (!empty($menu_entry['id']))
265
		{
266
			$request = $smcFunc['db_query']('', '
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
267
				UPDATE {db_prefix}um_menu
268
				SET
269
					name = {string:name},
270
					type = {string:type},
271
					target = {string:target},
272
					position = {string:position},
273
					link = {string:link},
274
					status = {string:status},
275
					permissions = {string:permissions},
276
					parent = {string:parent}
277
				WHERE id_button = {int:id}',
278
				array(
279
					'id' => $menu_entry['id'],
280
					'name' => $menu_entry['name'],
281
					'type' => $menu_entry['type'],
282
					'target' => $menu_entry['target'],
283
					'position' => $menu_entry['position'],
284
					'link' => $menu_entry['link'],
285
					'status' => $menu_entry['status'],
286
					'permissions' => implode(',', array_filter($menu_entry['permissions'], 'strlen')),
287
					'parent' => $menu_entry['parent'],
288
				)
289
			);
290
		}
291
		else
292
		{
293
			$this->db->insert(
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
294
				'insert',
295
				'{db_prefix}um_menu',
296
				array(
297
					'name' => 'string',
298
					'type' => 'string',
299
					'target' => 'string',
300
					'position' => 'string',
301
					'link' => 'string',
302
					'status' => 'string',
303
					'permissions' => 'string',
304
					'parent' => 'string',
305
				),
306
				array(
307
					$menu_entry['name'],
308
					$menu_entry['type'],
309
					$menu_entry['target'],
310
					$menu_entry['position'],
311
					$menu_entry['link'],
312
					$menu_entry['status'],
313
					implode(',', array_filter($menu_entry['permissions'], 'strlen')),
314
					$menu_entry['parent'],
315
				),
316
				array('id_button')
317
			);
318
		}
319
	}
320
321
	/**
322
	 * Fetch a specific button
323
	 *
324
	 * @param int $id
325
	 *
326
	 * @return mixed[]
327
	 */
328
	function fetchButton($id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
329
	{
330
		global $smcFunc;
331
332
		$request = $smcFunc['db_query']('', '
333
			SELECT
334
				name, target, type, position, link, status, permissions, parent
335
			FROM {db_prefix}um_menu
336
			WHERE id_button = {int:button}',
337
			array(
338
				'button' => $id,
339
			)
340
		);
341
		$row = $this->db->fetch_assoc($request);
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on UltimateMenu. Did you maybe forget to declare it?
Loading history...
342
		$this->db->free_result($request);
343
344
		return $row;
345
	}
346
347
	/**
348
	 * Removes all buttons
349
	 */
350
	function deleteallButtons()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
351
	{
352
		global $smcFunc;
353
354
		$smcFunc['db_query']('', '
355
			TRUNCATE {db_prefix}um_menu'
356
		);
357
	}
358
359
	/**
360
	 * Fetches the names of all SMF menu buttons.
361
	 *
362
	 * @return string[]
363
	 */
364
	function getButtonNames()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
365
	{
366
		global $context;
367
368
		$button_names = [];
369
		foreach ($context['menu_buttons'] as $button_index => $button_data)
370
		{
371
			$button_names[$button_index] = $button_data['title'];
372
373
			if (!empty($button_data['sub_buttons']))
374
			{
375
				foreach ($button_data['sub_buttons'] as $child_button => $child_button_data)
376
					$button_names[$child_button] = $child_button_data['title'];
377
378
				if (!empty($child_button_data['sub_buttons']))
379
					foreach ($child_button_data['sub_buttons'] as $grand_child_button => $grand_child_button_data)
380
						$button_names[$grand_child_button] = $grand_child_button_data['title'];
381
			}
382
		}
383
384
		return $button_names;
385
	}
386
}