Passed
Push — master ( e945a6...9c54c2 )
by John
02:19
created

ManageUltimateMenu::EditButton()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 27
rs 9.584
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' => 'AddButton',
40
			'editbutton' => 'EditButton',
41
			'savebutton' => 'SaveButton',
42
		);
43
		if (!isset($_GET['sa']) || !isset($subActions[$_GET['sa']]))
44
			$_GET['sa'] = 'manmenu';
45
		$this->{$subActions[$_GET['sa']]}();
46
	}
47
48
	public function ManageUltimateMenu()
49
	{
50
		// Get rid of all of em!
51
		if (!empty($_POST['removeAll']))
52
		{
53
			checkSession();
54
			$this->um->deleteallButtons();
55
			$this->um->rebuildMenu();
56
			redirectexit('action=admin;area=umen');
57
		}
58
		// User pressed the 'remove selection button'.
59
		elseif (isset($_POST['removeButtons'], $_POST['remove']) && is_array($_POST['remove']))
60
		{
61
			checkSession();
62
			$this->um->deleteButton(array_filter($_POST['remove'], 'ctype_digit'));
63
			$this->um->rebuildMenu();
64
			redirectexit('action=admin;area=umen');
65
		}
66
		// Changing the status?
67
		elseif (isset($_POST['save']))
68
		{
69
			checkSession();
70
			$this->um->updateButton($_POST);
71
			$this->um->rebuildMenu();
72
			redirectexit('action=admin;area=umen');
73
		}
74
		// New item?
75
		elseif (isset($_POST['new']))
76
			redirectexit('action=admin;area=umen;sa=addbutton');
77
78
		$this->ListButtons();
79
	}
80
81
	public function ListButtons()
82
	{
83
		global $context, $txt, $scripturl, $sourcedir;
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']][1]
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 ($scripturl, $txt)
188
						{
189
							return sprintf(
190
								'<a href="%s?action=admin;area=umen;sa=editbutton;in=%d">%s</a>',
191
								$scripturl,
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' => sprintf('
222
						<input type="submit" name="removeButtons" value="%s" onclick="return confirm(\'%s\');" class="button_submit" />
223
						<input type="submit" name="removeAll" value="%s" onclick="return confirm(\'%s\');" class="button_submit" />
224
						<input type="submit" name="new" value="%s" class="button_submit" />
225
						<input type="submit" name="save" value="%s" class="button_submit" />',
226
						$txt['um_menu_remove_selected'],
227
						$txt['um_menu_remove_confirm'],
228
						$txt['um_menu_remove_all'],
229
						$txt['um_menu_remove_all_confirm'],
230
						$txt['um_admin_add_button'],
231
						$txt['save']
232
					),
233
					'class' => 'righttext',
234
				),
235
			),
236
		);
237
		require_once $sourcedir . '/Subs-List.php';
238
		createList($listOptions);
239
		$context['sub_template'] = 'show_list';
240
		$context['default_list'] = 'menu_list';
241
	}
242
243
	public function SaveButton()
244
	{
245
		global $context, $txt;
246
247
		if (isset($_POST['submit']))
248
		{
249
			checkSession();
250
			$post_errors = array();
251
			$required_fields = array(
252
				'name',
253
				'link',
254
				'parent',
255
			);
256
			$member_groups = $this->um->listGroups([-3]);
257
			$button_names = $this->um->getButtonNames();
258
			$args = array(
259
				'in' => FILTER_VALIDATE_INT,
260
				'name' => FILTER_UNSAFE_RAW,
261
				'position' => array(
262
					'filter' => FILTER_CALLBACK,
263
					'options' => function ($v)
264
					{
265
						return in_array($v, ['before', 'child_of', 'after']) ? $v : false;
266
					},
267
				),
268
				'parent' => array(
269
					'filter' => FILTER_CALLBACK,
270
					'options' => function ($v) use ($button_names)
271
					{
272
						return isset($button_names[$v]) ? $v : false;
273
					},
274
				),
275
				'type' => array(
276
					'filter' => FILTER_CALLBACK,
277
					'options' => function ($v)
278
					{
279
						return in_array($v, ['forum', 'external']) ? $v : false;
280
					},
281
				),
282
				'link' => FILTER_UNSAFE_RAW,
283
				'permissions' => array(
284
					'filter' => FILTER_CALLBACK,
285
					'flags' => FILTER_REQUIRE_ARRAY,
286
					'options' => function ($v) use ($member_groups)
287
					{
288
						return isset($member_groups[$v]) ? $v : false;
289
					},
290
				),
291
				'status' => array(
292
					'filter' => FILTER_CALLBACK,
293
					'options' => function ($v)
294
					{
295
						return in_array($v, ['active', 'inactive']) ? $v : false;
296
					},
297
				),
298
				'target' => array(
299
					'filter' => FILTER_CALLBACK,
300
					'options' => function ($v)
301
					{
302
						return in_array($v, ['_self', '_blank']) ? $v : false;
303
					},
304
				),
305
			);
306
307
			// Make sure we grab all of the content
308
			$menu_entry = array_replace(
309
				array(
310
					'target' => '_self',
311
					'type' => 'forum',
312
					'position' => 'before',
313
					'status' => 'active',
314
					'parent' => 'home',
315
				),
316
				filter_input_array(INPUT_POST, $args)
317
			);
318
319
			// These fields are required!
320
			foreach ($required_fields as $required_field)
321
				if (empty($menu_entry[$required_field]))
322
					$post_errors[$required_field] = 'um_menu_empty_' . $required_field;
323
324
			// Stop making numeric names!
325
			if (is_numeric($menu_entry['name']))
326
				$post_errors['name'] = 'um_menu_numeric';
327
328
			// Let's make sure you're not trying to make a name that's already taken.
329
			if (!empty($this->um->checkButton($menu_entry['in'], $menu_entry['name'])))
330
				$post_errors['name'] = 'um_menu_mysql';
331
332
			// I see you made it to the final stage, my young padawan.
333
			if (empty($post_errors))
334
			{
335
				$this->um->saveButton($menu_entry);
336
				$this->um->rebuildMenu();
337
338
				// Before we leave, we must clear the cache. See, SMF
339
				// caches its menu at level 2 or higher.
340
				clean_cache('menu_buttons');
341
342
				redirectexit('action=admin;area=umen');
343
			}
344
			else
345
			{
346
				$context['page_title'] = $txt['um_menu_edit_title'];
347
				$context['button_names'] = $button_names;
348
				$context['post_error'] = $post_errors;
349
				$context['error_title'] = empty($menu_entry['in'])
350
					? 'um_menu_errors_create'
351
					: 'um_menu_errors_modify';
352
				$context['button_data'] = array(
353
					'name' => $menu_entry['name'],
354
					'type' => $menu_entry['type'],
355
					'target' => $menu_entry['target'],
356
					'position' => $menu_entry['position'],
357
					'link' => $menu_entry['link'],
358
					'parent' => $menu_entry['parent'],
359
					'permissions' => $this->um->listGroups(
360
						array_filter($menu_entry['permissions'], 'strlen')
361
					),
362
					'status' => $menu_entry['status'],
363
					'id' => $menu_entry['in'],
364
				);
365
				$context['all_groups_checked'] = empty(array_diff_key(
366
					$context['button_data']['permissions'],
367
					array_flip(array_filter($menu_entry['permissions'], 'strlen'))
368
				));
369
				$context['template_layers'][] = 'form';
370
				$context['template_layers'][] = 'errors';
371
			}
372
		}
373
		else
374
			fatal_lang_error('no_access', false);
375
	}
376
377
	public function EditButton()
378
	{
379
		global $context, $txt;
380
381
		if (isset($_GET['in']))
382
			$row = $this->um->fetchButton($_GET['in']);
383
		elseif (empty($row))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $row seems to never exist and therefore empty should always be true.
Loading history...
384
			fatal_lang_error('no_access', false);
385
386
		$context['button_data'] = array(
387
			'id' => $row['id'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $row does not seem to be defined for all execution paths leading up to this point.
Loading history...
388
			'name' => $row['name'],
389
			'target' => $row['target'],
390
			'type' => $row['type'],
391
			'position' => $row['position'],
392
			'permissions' => $this->um->listGroups($row['permissions']),
393
			'link' => $row['link'],
394
			'status' => $row['status'],
395
			'parent' => $row['parent'],
396
		);
397
		$context['all_groups_checked'] = empty(array_diff_key(
398
			$context['button_data']['permissions'],
399
			array_flip($row['permissions'])
400
		));
401
		$context['page_title'] = $txt['um_menu_edit_title'];
402
		$context['button_names'] = $this->um->getButtonNames();
403
		$context['template_layers'][] = 'form';
404
	}
405
406
	public function AddButton()
407
	{
408
		global $context, $txt;
409
410
		$context['button_data'] = array(
411
			'name' => '',
412
			'link' => '',
413
			'target' => '_self',
414
			'type' => 'forum',
415
			'position' => 'before',
416
			'status' => 'active',
417
			'permissions' => $this->um->listGroups([-3]),
418
			'parent' => 'home',
419
			'id' => 0,
420
		);
421
		$context['all_groups_checked'] = true;
422
		$context['page_title'] = $txt['um_menu_add_title'];
423
		$context['button_names'] = $this->um->getButtonNames();
424
		$context['template_layers'][] = 'form';
425
	}
426
}
427