Passed
Push — master ( 4c81df...f4108d )
by John
01:57
created

ManageUltimateMenu::SaveButton()   C

Complexity

Conditions 14
Paths 37

Size

Total Lines 122
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 76
c 3
b 0
f 0
dl 0
loc 122
rs 5.5369
cc 14
nc 37
nop 0

How to fix   Long Method    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
 * @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
					),
107
					'sort' => array(
108
						'default' => 'name',
109
						'reverse' => 'name DESC',
110
					),
111
				),
112
				'type' => array(
113
					'header' => array(
114
						'value' => $txt['um_menu_button_type'],
115
					),
116
					'data' => array(
117
						'function' => function($rowData) use ($txt)
118
						{
119
							return $txt['um_menu_' . $rowData['type'] . '_link'];
120
						},
121
					),
122
					'sort' => array(
123
						'default' => 'type',
124
						'reverse' => 'type DESC',
125
					),
126
				),
127
				'position' => array(
128
					'header' => array(
129
						'value' => $txt['um_menu_button_position'],
130
					),
131
					'data' => array(
132
						'function' => function($rowData) use ($txt, $button_names)
133
						{
134
							return sprintf(
135
								'%s %s',
136
								$txt['um_menu_' . $rowData['position']],
137
								isset($button_names[$rowData['parent']])
138
									? $button_names[$rowData['parent']]
139
									: ucwords($rowData['parent'])
140
							);
141
						},
142
					),
143
					'sort' => array(
144
						'default' => 'position',
145
						'reverse' => 'position DESC',
146
					),
147
				),
148
				'link' => array(
149
					'header' => array(
150
						'value' => $txt['um_menu_button_link'],
151
					),
152
					'data' => array(
153
						'db_htmlsafe' => 'link',
154
					),
155
					'sort' => array(
156
						'default' => 'link',
157
						'reverse' => 'link DESC',
158
					),
159
				),
160
				'status' => array(
161
					'header' => array(
162
						'value' => $txt['um_menu_button_active'],
163
						'class' => 'centertext',
164
					),
165
					'data' => array(
166
						'function' => function($rowData)
167
						{
168
							return sprintf(
169
								'<input type="checkbox" name="status[%1$s]" id="status_%1$s" value="%1$s"%2$s />',
170
								$rowData['id_button'],
171
								$rowData['status'] == 'inactive' ? '' : ' checked="checked"'
172
							);
173
						},
174
						'class' => 'centertext',
175
					),
176
					'sort' => array(
177
						'default' => 'status',
178
						'reverse' => 'status DESC',
179
					),
180
				),
181
				'actions' => array(
182
					'header' => array(
183
						'value' => $txt['um_menu_actions'],
184
						'class' => 'centertext',
185
					),
186
					'data' => array(
187
						'function' => function($rowData) use ($txt)
188
						{
189
							return sprintf(
190
								'<a href="%s?action=admin;area=umen;sa=addbutton;edit;in=%d">%s</a>',
191
								$scripturl,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $scripturl seems to be never defined.
Loading history...
192
								$rowData['id_button'],
193
								$txt['modify']
194
							);
195
						},
196
						'class' => 'centertext',
197
					),
198
				),
199
				'check' => array(
200
					'header' => array(
201
						'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
202
						'class' => 'centertext',
203
					),
204
					'data' => array(
205
						'sprintf' => array(
206
							'format' => '<input type="checkbox" name="remove[]" value="%d" class="input_check" />',
207
							'params' => array(
208
								'id_button' => false,
209
							),
210
						),
211
						'class' => 'centertext',
212
					),
213
				),
214
			),
215
			'form' => array(
216
				'href' => $scripturl . '?action=admin;area=umen;sa=manmenu',
217
			),
218
			'additional_rows' => array(
219
				array(
220
					'position' => 'below_table_data',
221
					'value' => '
222
						<input type="submit" name="removeButtons" value="' . $txt['um_menu_remove_selected'] . '" onclick="return confirm(\'' . $txt['um_menu_remove_confirm'] . '\');" class="button_submit" />
223
						<input type="submit" name="removeAll" value="' . $txt['um_menu_remove_all'] . '" onclick="return confirm(\'' . $txt['um_menu_remove_all_confirm'] . '\');" class="button_submit" />
224
						<input type="submit" name="new" value="' . $txt['um_admin_add_button'] . '" class="button_submit" />
225
						<input type="submit" name="save" value="' . $txt['save'] . '" class="button_submit" />',
226
					'class' => 'righttext',
227
				),
228
			),
229
		);
230
		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...
231
		createList($listOptions);
232
		$context['sub_template'] = 'show_list';
233
		$context['default_list'] = 'menu_list';
234
	}
235
236
	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...
237
	{
238
		global $context, $txt;
239
240
		if (isset($_POST['submit']))
241
		{
242
			$post_errors = array();
243
			$required_fields = array(
244
				'name',
245
				'link',
246
				'parent',
247
			);
248
			$member_groups = array_column($this->um->list_groups('-3', 1), 'id');
249
			$button_names = $this->um->getButtonNames();
250
			$args = array(
251
				'in' => FILTER_VALIDATE_INT,
252
				'name' => FILTER_UNSAFE_RAW,
253
				'position' => array(
254
					'filter' => FILTER_CALLBACK,
255
					'options' => function ($v)
256
					{
257
						return in_array($v, ['before', 'child_of', 'after']) ? $v : false;
258
					},
259
				),
260
				'parent' => array(
261
					'filter' => FILTER_CALLBACK,
262
					'options' => function ($v) use ($button_names)
263
					{
264
						return isset($button_names[$v]) ? $v : false;
265
					},
266
				),
267
				'type' => array(
268
					'filter' => FILTER_CALLBACK,
269
					'options' => function ($v)
270
					{
271
						return in_array($v, ['forum', 'external']) ? $v : false;
272
					},
273
				),
274
				'link' => FILTER_UNSAFE_RAW,
275
				'permissions' => array(
276
					'filter' => FILTER_CALLBACK,
277
					'flags' => FILTER_REQUIRE_ARRAY,
278
					'options' => function ($v) use ($member_groups)
279
					{
280
						return in_array($v, $member_groups) ? $v : false;
281
					},
282
				),
283
				'status' => array(
284
					'filter' => FILTER_CALLBACK,
285
					'options' => function ($v)
286
					{
287
						return in_array($v, ['active', 'inactive']) ? $v : false;
288
					},
289
				),
290
				'target' => array(
291
					'filter' => FILTER_CALLBACK,
292
					'options' => function ($v)
293
					{
294
						return in_array($v, ['_self', '_blank']) ? $v : false;
295
					},
296
				),
297
			);
298
299
			// Make sure we grab all of the content
300
			$menu_entry = array_replace(
301
				array(
302
					'target' => '_self',
303
					'type' => 'forum',
304
					'position' => 'before',
305
					'status' => 'active',
306
					'parent' => 'home',
307
				),
308
				filter_input_array(INPUT_POST, $args)
309
			);
310
311
			// These fields are required!
312
			foreach ($required_fields as $required_field)
313
				if (empty($menu_entry[$required_field]))
314
					$post_errors[$required_field] = 'um_menu_empty_' . $required_field;
315
316
			// Stop making numeric names!
317
			if (is_numeric($menu_entry['name']))
318
				$post_errors['name'] = 'um_menu_numeric';
319
320
			// Let's make sure you're not trying to make a name that's already taken.
321
			$check = $this->um->checkButton($menu_entry['id'], $menu_entry['name']);
322
			if ($check > 0)
323
				$post_errors['name'] = 'um_menu_mysql';
324
325
			// I see you made it to the final stage, my young padawan.
326
			if (empty($post_errors))
327
			{
328
				$this->um->saveButton($menu_entry);
329
				$this->um->rebuildMenu();
330
331
332
				// Before we leave, we must clear the cache. See, SMF
333
				// caches its menu at level 2 or higher.
334
				clean_cache('menu_buttons');
335
336
				redirectexit('action=admin;area=umen');
337
			}
338
			else
339
			{
340
				$context['page_title'] = $txt['um_menu_edit_title'];
341
				$context['post_error'] = $post_errors;
342
				$context['error_title'] = empty($menu_entry['id'])
343
					? 'um_menu_errors_create'
344
					: 'um_menu_errors_modify';
345
				$context['button_data'] = array(
346
					'name' => $menu_entry['name'],
347
					'type' => $menu_entry['type'],
348
					'target' => $menu_entry['target'],
349
					'position' => $menu_entry['position'],
350
					'link' => $menu_entry['link'],
351
					'parent' => $menu_entry['parent'],
352
					'permissions' => $this->um->list_groups(
353
						implode(',', array_filter($menu_entry['permissions'], 'strlen')),
354
						1
355
					),
356
					'status' => $menu_entry['status'],
357
					'id' => $menu_entry['id'],
358
				);
359
			}
360
		}
361
	}
362
363
	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...
364
	{
365
		global $context, $txt;
366
367
		if (isset($_GET['in']))
368
		{
369
			$row = $this->um->fetchButton($_GET['in']);
370
371
			$context['button_data'] = array(
372
				'id' => $_GET['in'],
373
				'name' => $row['name'],
374
				'target' => $row['target'],
375
				'type' => $row['type'],
376
				'position' => $row['position'],
377
				'permissions' => $this->um->list_groups($row['permissions'], 1),
378
				'link' => $row['link'],
379
				'status' => $row['status'],
380
				'parent' => $row['parent'],
381
			);
382
		}
383
		else
384
		{
385
			$context['button_data'] = array(
386
				'name' => '',
387
				'link' => '',
388
				'target' => '_self',
389
				'type' => 'forum',
390
				'position' => 'before',
391
				'status' => 'active',
392
				'permissions' => $this->um->list_groups('-3', 1),
393
				'parent' => 'home',
394
				'id' => 0,
395
			);
396
397
			$context['page_title'] = $txt['um_menu_add_title'];
398
		}
399
	}
400
}
401