Passed
Push — master ( f4108d...932680 )
by John
01:48
created

ManageUltimateMenu::ListButtons()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 160
Code Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 106
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 160
rs 8

How to fix   Long Method   

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
	public 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
	public function ManageUltimateMenu()
48
	{
49
		// Get rid of all of em!
50
		if (!empty($_POST['removeAll']))
51
		{
52
			checkSession();
53
			$this->um->deleteallButtons();
54
			$this->um->rebuildMenu();
55
			redirectexit('action=admin;area=umen');
56
		}
57
		// User pressed the 'remove selection button'.
58
		elseif (!empty($_POST['removeButtons']) && !empty($_POST['remove']) && is_array($_POST['remove']))
59
		{
60
			checkSession();
61
62
			// Make sure every entry is a proper integer.
63
			foreach ($_POST['remove'] as $index => $page_id)
64
				$_POST['remove'][(int) $index] = (int) $page_id;
65
66
			// Delete the page(s)!
67
			$this->um->deleteButton($_POST['remove']);
68
			$this->um->rebuildMenu();
69
			redirectexit('action=admin;area=umen');
70
		}
71
		// Changing the status?
72
		elseif (isset($_POST['save']))
73
		{
74
			checkSession();
75
			$this->um->updateButton($_POST);
76
			$this->um->rebuildMenu();
77
			redirectexit('action=admin;area=umen');
78
		}
79
		// New item?
80
		elseif (isset($_POST['new']))
81
			redirectexit('action=admin;area=umen;sa=addbutton');
82
83
		ListButtons();
0 ignored issues
show
Bug introduced by
The function ListButtons 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

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