Passed
Push — master ( 31fdb3...e945a6 )
by John
01:52
created

UltimateMenu::checkButton()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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