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

ManageUltimateMenu   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 331
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
eloc 210
c 1
b 0
f 0
dl 0
loc 331
rs 9.84

4 Methods

Rating   Name   Duplication   Size   Complexity  
A PrepareContext() 0 35 2
A __construct() 0 31 3
F SaveButton() 0 77 17
C ManageUltimateMenu() 0 175 10
1
<?php
2
/**
3
 * @package   Ultimate Menu mod
4
 * @version   1.1
5
 * @author    John Rayes <[email protected]>
6
 * @copyright Copyright (c) 2014, John Rayes
7
 * @license   http://opensource.org/licenses/MIT MIT
8
 */
9
10
class ManageUltimateMenu
11
{
12
	private $um;
13
14
	function __construct()
15
	{
16
		global $context, $sourcedir, $txt;
17
18
		isAllowedTo('admin_forum');
19
20
		$context['page_title'] = $txt['admin_menu_title'];
21
		$context[$context['admin_menu_name']]['tab_data'] = array(
22
			'title' => $txt['admin_menu'],
23
			'description' => $txt['admin_menu_desc'],
24
			'tabs' => array(
25
				'manmenu' => array(
26
					'description' => $txt['admin_manage_menu_desc'],
27
				),
28
				'addbutton' => array(
29
					'description' => $txt['admin_menu_add_button_desc'],
30
				),
31
			),
32
		);
33
		loadTemplate('ManageUltimateMenu');
34
		require_once $sourcedir . '/Class-UltimateMenu.php';
35
		$this->um = new UltimateMenu;
36
37
		$subActions = array(
38
			'manmenu' => 'ManageUltimateMenu',
39
			'addbutton' => 'PrepareContext',
40
			'savebutton' => 'SaveButton',
41
		);
42
		if (!isset($_GET['sa']) || !isset($subActions[$_GET['sa']]))
43
			$_GET['sa'] = 'manmenu';
44
		$this->$subActions[$_GET['sa']]();
45
	}
46
47
	function ManageUltimateMenu()
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...
48
	{
49
		global $context, $txt, $scripturl;
50
51
		// Get rid of all of em!
52
		if (!empty($_POST['removeAll']))
53
		{
54
			checkSession();
55
			$this->um->deleteallButtons();
56
			$this->um->rebuildMenu();
57
			redirectexit('action=admin;area=umen');
58
		}
59
		// User pressed the 'remove selection button'.
60
		elseif (!empty($_POST['removeButtons']) && !empty($_POST['remove']) && is_array($_POST['remove']))
61
		{
62
			checkSession();
63
64
			// Make sure every entry is a proper integer.
65
			foreach ($_POST['remove'] as $index => $page_id)
66
				$_POST['remove'][(int) $index] = (int) $page_id;
67
68
			// Delete the page(s)!
69
			$this->um->deleteButton($_POST['remove']);
70
			$this->um->rebuildMenu();
71
			redirectexit('action=admin;area=umen');
72
		}
73
		// Changing the status?
74
		elseif (isset($_POST['save']))
75
		{
76
			checkSession();
77
			$this->um->updateButton($_POST);
78
			$this->um->rebuildMenu();
79
			redirectexit('action=admin;area=umen');
80
		}
81
		// New item?
82
		elseif (isset($_POST['new']))
83
			redirectexit('action=admin;area=umen;sa=addbutton');
84
85
		$button_names = $this->um->getButtonNames();
86
		$listOptions = array(
87
			'id' => 'menu_list',
88
			'items_per_page' => 20,
89
			'base_href' => $scripturl . '?action=admin;area=umen;sa=manmenu',
90
			'default_sort_col' => 'name',
91
			'default_sort_dir' => 'desc',
92
			'get_items' => array(
93
				'function' => array($this->um, 'list_getMenu'),
94
			),
95
			'get_count' => array(
96
				'function' => array($this->um, 'list_getNumButtons'),
97
			),
98
			'no_items_label' => $txt['um_menu_no_buttons'],
99
			'columns' => array(
100
				'name' => array(
101
					'header' => array(
102
						'value' => $txt['um_menu_button_name'],
103
					),
104
					'data' => array(
105
						'db_htmlsafe' => 'name',
106
						'class' => 'centertext',
107
					),
108
					'sort' => array(
109
						'default' => 'name',
110
						'reverse' => 'name DESC',
111
					),
112
				),
113
				'type' => array(
114
					'header' => array(
115
						'value' => $txt['um_menu_button_type'],
116
					),
117
					'data' => array(
118
						'function' => function($rowData) use ($txt)
119
						{
120
							return $txt[$rowData['type'] . '_link'];
121
						},
122
						'class' => 'centertext',
123
					),
124
					'sort' => array(
125
						'default' => 'type',
126
						'reverse' => 'type DESC',
127
					),
128
				),
129
				'poition' => array(
130
					'header' => array(
131
						'value' => $txt['um_menu_button_position'],
132
					),
133
					'data' => array(
134
						'function' => function($rowData) use ($txt, $button_names)
135
						{
136
							return $txt['mboards_order_' . $rowData['position']] . ' ' . (isset($button_names[$rowData['parent']]) ? $button_names[$rowData['parent']] : ucwords($rowData['parent']));
137
						},
138
						'class' => 'centertext',
139
					),
140
					'sort' => array(
141
						'default' => 'position',
142
						'reverse' => 'position DESC',
143
					),
144
				),
145
				'link' => array(
146
					'header' => array(
147
						'value' => $txt['um_menu_button_link'],
148
					),
149
					'data' => array(
150
						'db_htmlsafe' => 'link',
151
						'class' => 'centertext',
152
					),
153
					'sort' => array(
154
						'default' => 'link',
155
						'reverse' => 'link DESC',
156
					),
157
				),
158
				'status' => array(
159
					'header' => array(
160
						'value' => $txt['um_menu_button_active'],
161
					),
162
					'data' => array(
163
						'function' => function($rowData) use ($txt)
0 ignored issues
show
Unused Code introduced by
The import $txt is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
164
						{
165
							return sprintf('<input type="checkbox" name="status[%1$s]" id="status_%1$s" value="%1$s"%2$s />', $rowData['id_button'], $rowData['status'] == 'inactive' ? '' : ' checked="checked"');
166
						},
167
						'class' => 'centertext',
168
					),
169
					'sort' => array(
170
						'default' => 'status',
171
						'reverse' => 'status DESC',
172
					),
173
				),
174
				'actions' => array(
175
					'header' => array(
176
						'value' => $txt['um_menu_actions'],
177
					),
178
					'data' => array(
179
						'sprintf' => array(
180
							'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=admin;area=umen;sa=addbutton;edit;in=%1$d">' . $txt['modify'] . '</a>',
181
							'params' => array(
182
								'id_button' => false,
183
							),
184
						),
185
						'class' => 'centertext',
186
					),
187
				),
188
				'check' => array(
189
					'header' => array(
190
						'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
191
					),
192
					'data' => array(
193
						'sprintf' => array(
194
							'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
195
							'params' => array(
196
								'id_button' => false,
197
							),
198
						),
199
						'class' => 'centertext',
200
					),
201
				),
202
			),
203
			'form' => array(
204
				'href' => $scripturl . '?action=admin;area=umen;sa=manmenu',
205
			),
206
			'additional_rows' => array(
207
				array(
208
					'position' => 'below_table_data',
209
					'value' => '
210
						<input type="submit" name="removeButtons" value="' . $txt['um_menu_remove_selected'] . '" onclick="return confirm(\'' . $txt['um_menu_remove_confirm'] . '\');" class="button_submit" />
211
						<input type="submit" name="removeAll" value="' . $txt['um_menu_remove_all'] . '" onclick="return confirm(\'' . $txt['um_menu_remove_all_confirm'] . '\');" class="button_submit" />
212
						<input type="submit" name="new" value="' . $txt['um_admin_add_button'] . '" class="button_submit" />
213
						<input type="submit" name="save" value="' . $txt['save'] . '" class="button_submit" />',
214
					'class' => 'righttext',
215
				),
216
			),
217
		);
218
		require_once $sourcedir . '/Subs-List.php';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sourcedir seems to be never defined.
Loading history...
219
		createList($listOptions);
220
		$context['sub_template'] = 'show_list';
221
		$context['default_list'] = 'menu_list';
222
	}
223
224
	function SaveButton()
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...
225
	{
226
		global $context, $smcFunc, $txt, $sourcedir;
227
228
		// It's expected to be present.
229
		$context['user']['unread_messages'] = 0;
230
231
		// Load SMF's default menu context
232
		setupMenuContext();
233
234
		if (isset($_REQUEST['submit']))
235
		{
236
			$post_errors = array();
237
			$required_fields = array(
238
				'name',
239
				'link',
240
				'parent',
241
			);
242
243
			// Make sure we grab all of the content
244
			$id = isset($_REQUEST['in']) ? (int) $_REQUEST['in'] : 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
245
			$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';
246
			$position = isset($_REQUEST['position']) ? $_REQUEST['position'] : 'before';
0 ignored issues
show
Unused Code introduced by
The assignment to $position is dead and can be removed.
Loading history...
247
			$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'forum';
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
248
			$link = isset($_REQUEST['link']) ? $_REQUEST['link'] : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $link is dead and can be removed.
Loading history...
249
			$permissions = isset($_REQUEST['permissions']) ? implode(',', array_intersect($_REQUEST['permissions'], array_keys(list_groups(-3, 1)))) : '1';
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

249
			$permissions = isset($_REQUEST['permissions']) ? implode(',', array_intersect($_REQUEST['permissions'], array_keys(/** @scrutinizer ignore-call */ list_groups(-3, 1)))) : '1';
Loading history...
Unused Code introduced by
The assignment to $permissions is dead and can be removed.
Loading history...
250
			$status = isset($_REQUEST['status']) ? $_REQUEST['status'] : 'active';
0 ignored issues
show
Unused Code introduced by
The assignment to $status is dead and can be removed.
Loading history...
251
			$parent = isset($_REQUEST['parent']) ? $_REQUEST['parent'] : 'home';
0 ignored issues
show
Unused Code introduced by
The assignment to $parent is dead and can be removed.
Loading history...
252
			$target = isset($_REQUEST['target']) ? $_REQUEST['target'] : '_self';
0 ignored issues
show
Unused Code introduced by
The assignment to $target is dead and can be removed.
Loading history...
253
254
			// These fields are required!
255
			foreach ($required_fields as $required_field)
256
				if ($_POST[$required_field] == '')
257
					$post_errors[$required_field] = 'um_menu_empty_' . $required_field;
258
259
			// Stop making numeric names!
260
			if (is_numeric($name))
261
				$post_errors['name'] = 'um_menu_numeric';
262
263
			// Let's make sure you're not trying to make a name that's already taken.
264
			$check = $this->um->checkButton($menu_entry['id'], $menu_entry['name']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $menu_entry seems to be never defined.
Loading history...
265
			if ($check > 0)
266
				$post_errors['name'] = 'um_menu_mysql';
267
268
			// I see you made it to the final stage, my young padawan.
269
			if (empty($post_errors))
270
			{
271
				$this->um->saveButton($menu_entry);
272
				$this->um->rebuildMenu();
273
274
275
				// Before we leave, we must clear the cache. See, SMF
276
				// caches its menu at level 2 or higher.
277
				clean_cache('menu_buttons');
278
279
				redirectexit('action=admin;area=umen');
280
			}
281
			else
282
			{
283
				$context['page_title'] = $txt['um_menu_edit_title'];
284
				$context['post_error'] = $post_errors;
285
				$context['error_title'] = empty($menu_entry['id'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $menu_entry seems to never exist and therefore empty should always be true.
Loading history...
286
					? 'um_menu_errors_create'
287
					: 'um_menu_errors_modify';
288
				$context['button_data'] = array(
289
					'name' => $menu_entry['name'],
290
					'type' => $menu_entry['type'],
291
					'target' => $menu_entry['target'],
292
					'position' => $menu_entry['position'],
293
					'link' => $menu_entry['link'],
294
					'parent' => $menu_entry['parent'],
295
					'permissions' => $this->um->list_groups(
296
						implode(',', array_filter($menu_entry['permissions'], 'strlen')),
297
						1
298
					),
299
					'status' => $menu_entry['status'],
300
					'id' => $menu_entry['id'],
301
				);
302
			}
303
		}
304
	}
305
306
	function PrepareContext()
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...
307
	{
308
		global $context, $txt;
309
310
		if (isset($_GET['in']))
311
		{
312
			$row = $this->um->fetchButton($_GET['in']);
313
314
			$context['button_data'] = array(
315
				'id' => $_GET['in'],
316
				'name' => $row['name'],
317
				'target' => $row['target'],
318
				'type' => $row['type'],
319
				'position' => $row['position'],
320
				'permissions' => $this->um->list_groups($row['permissions'], 1),
321
				'link' => $row['link'],
322
				'status' => $row['status'],
323
				'parent' => $row['parent'],
324
			);
325
		}
326
		else
327
		{
328
			$context['button_data'] = array(
329
				'name' => '',
330
				'link' => '',
331
				'target' => '_self',
332
				'type' => 'forum',
333
				'position' => 'before',
334
				'status' => 'active',
335
				'permissions' => $this->um->list_groups('-3', 1),
336
				'parent' => 'home',
337
				'id' => 0,
338
			);
339
340
			$context['page_title'] = $txt['um_menu_add_title'];
341
		}
342
	}
343
}
344