ManagePortalMenus_Controller::list_spCountMenus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @package SimplePortal ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
13
/**
14
 * SimplePortal Menus Administration controller class.
15
 * This class handles the adding/editing of menus
16
 *
17
 * @todo not complete, do not use
18
 */
19
class ManagePortalMenus_Controller extends Action_Controller
20
{
21
	/**
22
	 * The starting point for the controller, called before all others
23
	 */
24
	public function action_index()
25
	{
26
		global $context, $txt;
27
28
		// Admin or at least manage menu permissions
29
		if (!allowedTo('sp_admin'))
30
		{
31
			isAllowedTo('sp_manage_menus');
32
		}
33
34
		// Going to need these
35
		require_once(SUBSDIR . '/PortalAdmin.subs.php');
36
		loadTemplate('PortalAdminMenus');
37
38
		$subActions = array(
39
			'listmainitem' => array($this, 'action_sportal_admin_menus_main_item_list'),
40
			'addmainitem' => array($this, 'action_sportal_admin_menus_main_item_edit'),
41
			'editmainitem' => array($this, 'action_sportal_admin_menus_main_item_edit'),
42
			'deletemainitem' => array($this, 'action_sportal_admin_menus_main_item_delete'),
43
44
			'listcustommenu' => array($this, 'action_sportal_admin_menus_custom_menu_list'),
45
			'addcustommenu' => array($this, 'action_sportal_admin_menus_custom_menu_edit'),
46
			'editcustommenu' => array($this, 'action_sportal_admin_menus_custom_menu_edit'),
47
			'deletecustommenu' => array($this, 'action_sportal_admin_menus_custom_menu_delete'),
48
49
			'listcustomitem' => array($this, 'action_sportal_admin_menus_custom_item_list'),
50
			'addcustomitem' => array($this, 'action_sportal_admin_menus_custom_item_edit'),
51
			'editcustomitem' => array($this, 'action_sportal_admin_menus_custom_item_edit'),
52
			'deletecustomitem' => array($this, 'action_sportal_admin_menus_custom_item_delete'),
53
		);
54
55
		// Start up the controller, provide a hook since we can
56
		$action = new Action('portal_menus');
57
58
		// Set up the tabs
59
		$context[$context['admin_menu_name']]['tab_data'] = array(
60
			'title' => $txt['sp_admin_menus_title'],
61
			'help' => 'sp_MenusArea',
62
			'description' => $txt['sp_admin_menus_desc'],
63
			'tabs' => array(
64
				//	'listmainitem' => array(),
65
				//	'addmainitem' => array(),
66
				'listcustommenu' => array(),
67
				'addcustommenu' => array(),
68
			),
69
		);
70
71
		// Default to list the categories
72
		$subAction = $action->initialize($subActions, 'listcustommenu');
73
		$context['sub_action'] = $subAction;
74
75
		// Extra tab in the right cases
76
		if ($context['sub_action'] === 'listcustomitem' && !empty($_REQUEST['menu_id']))
77
		{
78
			$context[$context['admin_menu_name']]['tab_data']['tabs']['addcustomitem'] = array(
79
				'add_params' => ';menu_id=' . $_REQUEST['menu_id'],
80
			);
81
		}
82
83
		// Call the right function for this sub-action.
84
		$action->dispatch($subAction);
85
	}
86
87
	/**
88
	 * List the custom menus in the system
89
	 */
90
	public function action_sportal_admin_menus_custom_menu_list()
91
	{
92
		global $context, $scripturl, $txt, $modSettings;
93
94
		// Want to remove some menus
95
		if (!empty($_POST['remove_menus']) && !empty($_POST['remove']) && is_array($_POST['remove']))
96
		{
97
			checkSession();
98
99
			$remove_ids = array();
100
			foreach ($_POST['remove'] as $index => $menu_id)
101
			{
102
				$remove_ids[(int) $index] = (int) $menu_id;
103
			}
104
105
			sp_remove_menu($remove_ids);
106
		}
107
108
		// Build the list option array to display the menus
109
		$listOptions = array(
110
			'id' => 'portal_menus',
111
			'title' => $txt['sp_admin_menus_custom_menu_list'],
112
			'items_per_page' => $modSettings['defaultMaxMessages'],
113
			'no_items_label' => $txt['sp_error_no_custom_menus'],
114
			'base_href' => $scripturl . '?action=admin;area=portalmenus;sa=listcustommenu;',
115
			'default_sort_col' => 'name',
116
			'get_items' => array(
117
				'function' => array($this, 'list_spLoadMenus'),
118
			),
119
			'get_count' => array(
120
				'function' => array($this, 'list_spCountMenus'),
121
			),
122
			'columns' => array(
123
				'name' => array(
124
					'header' => array(
125
						'value' => $txt['sp_admin_menus_col_name'],
126
					),
127
					'data' => array(
128
						'db' => 'name',
129
					),
130
					'sort' => array(
131
						'default' => 'cm.name ASC',
132
						'reverse' => 'cm.name DESC',
133
					),
134
				),
135
				'items' => array(
136
					'header' => array(
137
						'value' => $txt['sp_admin_menus_col_items'],
138
					),
139
					'data' => array(
140
						'db' => 'items',
141
					),
142
					'sort' => array(
143
						'default' => 'items',
144
						'reverse' => 'items DESC',
145
					),
146
				),
147
				'action' => array(
148
					'header' => array(
149
						'value' => $txt['sp_admin_menus_col_actions'],
150
						'class' => ' grid8 centertext',
151
					),
152
					'data' => array(
153
						'sprintf' => array(
154
							'format' => '
155
								<a href="' . $scripturl . '?action=admin;area=portalmenus;sa=addcustomitem;menu_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '">' . sp_embed_image('add') . '</a>
156
								<a href="' . $scripturl . '?action=admin;area=portalmenus;sa=listcustomitem;menu_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '">' . sp_embed_image('items') . '</a>
157
						 		<a href="' . $scripturl . '?action=admin;area=portalmenus;sa=editcustommenu;menu_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '">' . sp_embed_image('modify') . '</a>
158
								<a href="' . $scripturl . '?action=admin;area=portalmenus;sa=deletecustommenu;menu_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(\'' . $txt['sp_admin_menus_menu_delete_confirm'] . '\');">' . sp_embed_image('delete') . '</a>',
159
							'params' => array(
160
								'id' => true,
161
							)
162
						),
163
						'class' => 'centertext nowrap',
164
					),
165
				),
166
			),
167
			'form' => array(
168
				'href' => $scripturl . '?action=admin;area=portalmenus;sa=listcustommenu',
169
				'include_sort' => true,
170
				'include_start' => true,
171
				'hidden_fields' => array(
172
					$context['session_var'] => $context['session_id'],
173
				),
174
			),
175
		);
176
177
		// Set the context values
178
		$context['page_title'] = $txt['sp_admin_menus_custom_menu_list'];
179
		$context['sub_template'] = 'show_list';
180
		$context['default_list'] = 'portal_menus';
181
182
		// Create the list.
183
		require_once(SUBSDIR . '/GenericList.class.php');
184
		createList($listOptions);
185
	}
186
187
	/**
188
	 * Returns the number of menus in the system
189
	 * Callback for createList()
190
	 */
191
	public function list_spCountMenus()
192
	{
193
		return sp_menu_count();
194
	}
195
196
	/**
197
	 * Returns an array of menus, passthru really
198
	 * Callback for createList()
199
	 *
200
	 * @param int $start
201
	 * @param int $items_per_page
202
	 * @param string $sort
203
	 *
204
	 * @return array
205
	 */
206
	public function list_spLoadMenus($start, $items_per_page, $sort)
207
	{
208
		return sp_custom_menu_items($start, $items_per_page, $sort);
209
	}
210
211
	/**
212
	 * Create or edit a menu
213
	 */
214
	public function action_sportal_admin_menus_custom_menu_edit()
215
	{
216
		global $context, $txt;
217
218
		// New menu or existing menu
219
		$is_new = empty($_REQUEST['menu_id']);
220
221
		// Saving the edit/add
222
		if (!empty($_POST['submit']))
223
		{
224
			checkSession();
225
226
			if (!isset($_POST['name']) || Util::htmltrim(Util::htmlspecialchars($_POST['name'], ENT_QUOTES)) === '')
227
			{
228
				throw new Elk_Exception('sp_error_menu_name_empty', false);
229
			}
230
231
			$menu_info = array(
232
				'id' => (int) $_POST['menu_id'],
233
				'name' => Util::htmlspecialchars($_POST['name'], ENT_QUOTES),
234
			);
235
236
			$menu_info['id'] = sp_add_menu($menu_info, $is_new);
237
238
			redirectexit('action=admin;area=portalmenus;sa=listcustommenu');
239
		}
240
241
		// Not saving so set up for the template display
242
		if ($is_new)
243
		{
244
			$context['menu'] = array(
245
				'id' => 0,
246
				'name' => $txt['sp_menus_default_custom_menu_name'],
247
			);
248
		}
249
		else
250
		{
251
			$menu_id = (int) $_REQUEST['menu_id'];
252
			$context['menu'] = sportal_get_custom_menus($menu_id);
253
		}
254
255
		// Final template bits
256
		$context['page_title'] = $is_new ? $txt['sp_admin_menus_custom_menu_add'] : $txt['sp_admin_menus_custom_menu_edit'];
257
		$context['sub_template'] = 'menus_custom_menu_edit';
258
	}
259
260
	/**
261
	 * Delete a custom menu and its items
262
	 */
263
	public function action_sportal_admin_menus_custom_menu_delete()
264
	{
265
		checkSession('get');
266
267
		$menu_id = !empty($_REQUEST['menu_id']) ? (int) $_REQUEST['menu_id'] : 0;
268
269
		sp_remove_menu_items($menu_id);
270
		sp_remove_menu($menu_id);
271
272
		redirectexit('action=admin;area=portalmenus;sa=listcustommenu');
273
	}
274
275
	/**
276
	 * List the items contained in a custom menu
277
	 */
278
	public function action_sportal_admin_menus_custom_item_list()
279
	{
280
		global $context, $scripturl, $txt, $modSettings;
281
282
		// Want to remove some items from a menu?
283
		if (!empty($_POST['remove_items']) && !empty($_POST['remove']) && is_array($_POST['remove']))
284
		{
285
			checkSession();
286
287
			$remove = array();
288
			foreach ($_POST['remove'] as $index => $item_id)
289
			{
290
				$remove[(int) $index] = (int) $item_id;
291
			}
292
293
			sp_remove_menu_items($remove);
294
		}
295
296
		$menu_id = !empty($_REQUEST['menu_id']) ? (int) $_REQUEST['menu_id'] : 0;
297
		$context['menu'] = sportal_get_custom_menus($menu_id);
298
299
		if (empty($context['menu']))
300
		{
301
			throw new Elk_Exception('error_sp_menu_not_found', false);
302
		}
303
304
		// Build the list option array to display the custom items in this custom menu
305
		$listOptions = array(
306
			'id' => 'portal_items',
307
			'title' => $txt['sp_admin_menus_custom_item_list'],
308
			'items_per_page' => $modSettings['defaultMaxMessages'],
309
			'no_items_label' => $txt['sp_error_no_custom_menus'],
310
			'base_href' => $scripturl . '?action=admin;area=portalmenus;sa=listcustomitem;',
311
			'default_sort_col' => 'title',
312
			'get_items' => array(
313
				'function' => array($this, 'list_sp_menu_item'),
314
				'params' => array(
315
					$menu_id,
316
				),
317
			),
318
			'get_count' => array(
319
				'function' => array($this, 'list_sp_menu_item_count'),
320
				'params' => array(
321
					$menu_id,
322
				),
323
			),
324
			'columns' => array(
325
				'title' => array(
326
					'header' => array(
327
						'value' => $txt['sp_admin_menus_col_title'],
328
					),
329
					'data' => array(
330
						'db' => 'title',
331
					),
332
					'sort' => array(
333
						'default' => 'title ASC',
334
						'reverse' => 'title DESC',
335
					),
336
				),
337
				'namespace' => array(
338
					'header' => array(
339
						'value' => $txt['sp_admin_menus_col_namespace'],
340
					),
341
					'data' => array(
342
						'db' => 'namespace',
343
					),
344
					'sort' => array(
345
						'default' => 'namespace ASC',
346
						'reverse' => 'namespace DESC',
347
					),
348
				),
349
				'target' => array(
350
					'header' => array(
351
						'value' => $txt['sp_admin_menus_col_target'],
352
					),
353
					'data' => array(
354
						'db' => 'target',
355
					),
356
					'sort' => array(
357
						'default' => 'target',
358
						'reverse' => 'target DESC',
359
					),
360
				),
361
				'action' => array(
362
					'header' => array(
363
						'value' => $txt['sp_admin_menus_col_actions'],
364
						'class' => ' grid8 centertext',
365
					),
366
					'data' => array(
367
						'sprintf' => array(
368
							'format' => '
369
								<a href="' . $scripturl . '?action=admin;area=portalmenus;sa=editcustomitem;menu_id=%1$s;item_id=%2$s;' . $context['session_var'] . '=' . $context['session_id'] . '">' . sp_embed_image('modify') . '</a>
370
								<a href="' . $scripturl . '?action=admin;area=portalmenus;sa=deletecustomitem;menu_id=%1$s;item_id=%2$s;' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(\'' . $txt['sp_admin_menus_item_delete_confirm'] . '\');">' . sp_embed_image('delete') . '</a>',
371
							'params' => array(
372
								'menu' => true,
373
								'id' => true,
374
							)
375
						),
376
						'class' => 'centertext nowrap',
377
					),
378
				),
379
				'check' => array(
380
					'header' => array(
381
						'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
382
						'class' => 'centertext',
383
					),
384
					'data' => array(
385
						'function' => function ($row) {
386
							return '<input type="checkbox" name="remove[]" value="' . $row['id'] . '" class="input_check" />';
387
						},
388
						'class' => 'centertext',
389
					),
390
				),
391
			),
392
			'form' => array(
393
				'href' => $scripturl . '?action=admin;area=portalmenus;sa=listcustomitem;menu_id=' . $menu_id,
394
				'include_sort' => true,
395
				'include_start' => true,
396
				'hidden_fields' => array(
397
					$context['session_var'] => $context['session_id'],
398
				),
399
			),
400
			'additional_rows' => array(
401
				array(
402
					'position' => 'below_table_data',
403
					'value' => '
404
						<input type="submit" name="remove_items" value="' . $txt['sp_admin_items_remove'] . '" class="right_submit" />',
405
				),
406
			),
407
		);
408
409
		// Set the context values
410
		$context['page_title'] = $txt['sp_admin_menus_custom_item_list'];
411
		$context['sub_template'] = 'show_list';
412
		$context['default_list'] = 'portal_items';
413
414
		// Create the list.
415
		require_once(SUBSDIR . '/GenericList.class.php');
416
		createList($listOptions);
417
	}
418
419
	/**
420
	 * Returns the number of menus in the system
421
	 * Callback for createList()
422
	 *
423
	 * @param int $menu_id
424
	 *
425
	 * @return int
426
	 */
427
	public function list_sp_menu_item_count($menu_id)
428
	{
429
		return sp_menu_item_count($menu_id);
430
	}
431
432
	/**
433
	 * Returns an array of menus, passthru really
434
	 * Callback for createList()
435
	 *
436
	 * @param int $start
437
	 * @param int $items_per_page
438
	 * @param string $sort
439
	 * @param int $menu_id
440
	 *
441
	 * @return array
442
	 */
443
	public function list_sp_menu_item($start, $items_per_page, $sort, $menu_id)
444
	{
445
		return sp_menu_items($start, $items_per_page, $sort, $menu_id);
446
	}
447
448
	/**
449
	 * Add or edit menu items
450
	 */
451
	public function action_sportal_admin_menus_custom_item_edit()
452
	{
453
		global $context, $txt;
454
455
		$menu_id = !empty($_REQUEST['menu_id']) ? (int) $_REQUEST['menu_id'] : 0;
456
		$context['menu'] = sportal_get_custom_menus($menu_id);
457
458
		// No menu, no further
459
		if (empty($context['menu']))
460
		{
461
			throw new Elk_Exception('error_sp_menu_not_found', false);
462
		}
463
464
		// Need to know if we are adding or editing
465
		$is_new = empty($_REQUEST['item_id']);
466
467
		// Saving the form.
468
		if (!empty($_POST['submit']))
469
		{
470
			checkSession();
471
472
			// Use our standard validation functions in a few spots
473
			require_once(SUBSDIR . '/DataValidator.class.php');
474
			$validator = new Data_Validator();
475
476
			// Clean and Review the post data for compliance
477
			$validator->sanitation_rules(array(
478
				'title' => 'Util::htmltrim|Util::htmlspecialchars',
479
				'namespace' => 'Util::htmltrim|Util::htmlspecialchars',
480
				'item_id' => 'intval',
481
				'url' => 'Util::htmlspecialchars',
482
				'target' => 'intval',
483
			));
484
			$validator->validation_rules(array(
485
				'title' => 'required',
486
				'namespace' => 'alpha_numeric|required',
487
				'item_id' => 'required',
488
			));
489
			$validator->text_replacements(array(
490
				'title' => $txt['sp_error_item_title_empty'],
491
				'namespace' => $txt['sp_error_item_namespace_empty'],
492
			));
493
494
			// If you messed this up, back you go
495
			if (!$validator->validate($_POST))
496
			{
497
				// @todo, should set  ErrorContext::context and display in template instead
498
				foreach ($validator->validation_errors() as $id => $error)
499
				{
500
					throw new Elk_Exception($error, false);
501
				}
502
			}
503
504
			// Can't have the same name in the same menu twice
505
			$has_duplicate = sp_menu_check_duplicate_items($validator->item_id, $validator->namespace);
506
			if (!empty($has_duplicate))
507
			{
508
				throw new Elk_Exception('sp_error_item_namespace_duplicate', false);
509
			}
510
511
			// Can't have a simple numeric namespace
512
			if (preg_replace('~[0-9]+~', '', $validator->namespace) === '')
513
			{
514
				throw new Elk_Exception('sp_error_item_namespace_numeric', false);
515
			}
516
517
			$item_info = array(
518
				'id' => $validator->item_id,
519
				'id_menu' => $context['menu']['id'],
520
				'namespace' => $validator->namespace,
521
				'title' => $validator->title,
522
				'href' => $validator->url,
523
				'target' => $validator->target,
524
			);
525
526
			// Adjust the url for the link type
527
			$link_type = !empty($_POST['link_type']) ? $_POST['link_type'] : '';
528
			$link_item = !empty($_POST['link_item']) ? $_POST['link_item'] : '';
529
			if ($link_type !== 'custom')
530
			{
531
				if (preg_match('~^\d+|[A-Za-z0-9_\-]+$~', $link_item, $match))
532
				{
533
					$link_item_id = $match[0];
534
				}
535
				else
536
				{
537
					throw new Elk_Exception('sp_error_item_link_item_invalid', false);
538
				}
539
540
				switch ($link_type)
541
				{
542
					case 'action':
543
					case 'page':
544
					case 'category':
545
					case 'article':
546
						$item_info['href'] = '$scripturl?' . $link_type . '=' . $link_item_id;
547
						break;
548
					case 'board':
549
						$item_info['href'] = '$scripturl?' . $link_type . '=' . $link_item_id . '.0';
550
						break;
551
				}
552
			}
553
554
			// Add or update the item
555
			sp_add_menu_item($item_info, $is_new);
556
557
			redirectexit('action=admin;area=portalmenus;sa=listcustomitem;menu_id=' . $context['menu']['id']);
558
		}
559
560
		// Prepare the items for the template
561
		if ($is_new)
562
		{
563
			$context['item'] = array(
564
				'id' => 0,
565
				'namespace' => 'item' . random_int(1, 5000),
566
				'title' => $txt['sp_menus_default_menu_item_name'],
567
				'url' => '',
568
				'target' => 0,
569
			);
570
		}
571
		// Not new so fetch what we know about the item
572
		else
573
		{
574
			$_REQUEST['item_id'] = (int) $_REQUEST['item_id'];
575
			$context['item'] = sportal_get_menu_items($_REQUEST['item_id']);
576
		}
577
578
		// Menu items
579
		$context['items']['action'] = array(
580
			'portal' => $txt['sp-portal'],
581
			'forum' => $txt['sp-forum'],
582
			'recent' => $txt['recent_posts'],
583
			'unread' => $txt['unread_topics_visit'],
584
			'unreadreplies' => $txt['unread_replies'],
585
			'profile' => $txt['profile'],
586
			'pm' => $txt['pm_short'],
587
			'calendar' => $txt['calendar'],
588
			'admin' => $txt['admin'],
589
			'login' => $txt['login'],
590
			'register' => $txt['register'],
591
			'post' => $txt['post'],
592
			'stats' => $txt['forum_stats'],
593
			'search' => $txt['search'],
594
			'mlist' => $txt['members_list'],
595
			'moderate' => $txt['moderate'],
596
			'help' => $txt['help'],
597
			'who' => $txt['who_title'],
598
		);
599
600
		$context['items'] = array_merge($context['items'], sp_block_template_helpers());
601
		$context['page_title'] = $is_new ? $txt['sp_admin_menus_custom_item_add'] : $txt['sp_admin_menus_custom_item_edit'];
602
		$context['sub_template'] = 'menus_custom_item_edit';
603
	}
604
605
	/**
606
	 * Remove an item from a menu
607
	 */
608
	public function action_sportal_admin_menus_custom_item_delete()
609
	{
610
		checkSession('get');
611
612
		$menu_id = !empty($_REQUEST['menu_id']) ? (int) $_REQUEST['menu_id'] : 0;
613
		$item_id = !empty($_REQUEST['item_id']) ? (int) $_REQUEST['item_id'] : 0;
614
615
		sp_remove_menu_items($item_id);
616
617
		redirectexit('action=admin;area=portalmenus;sa=listcustomitem;menu_id=' . $menu_id);
618
	}
619
}
620