um_load_menu()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
c 3
b 0
f 0
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 8.8333
cc 7
nc 16
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @package   Ultimate Menu mod
7
 * @version   2.0.2
8
 * @author    John Rayes <[email protected]>
9
 * @copyright Copyright (c) 2014, John Rayes
10
 * @license   http://opensource.org/licenses/MIT MIT
11
 */
12
13
function um_load_menu(&$menu_buttons): void
14
{
15
	global $smcFunc, $user_info, $scripturl, $modSettings;
16
17
	// Make damn sure we ALWAYS load last. Priority: 100!
18 4
	if (substr($modSettings['integrate_menu_buttons'], -12) !== 'um_load_menu')
19
	{
20 3
		remove_integration_function('integrate_menu_buttons', 'um_load_menu');
21 3
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
22
	}
23
24 4
	for ($i = 1; $i <= ($modSettings['um_count'] ?? 0); $i++)
25
	{
26 3
		$key = 'um_button_' . $i;
27
28 3
		if (!isset($modSettings[$key]))
29 3
			continue;
30 3
		$row = json_decode($modSettings[$key], true);
31 3
		$temp_menu = [
32 3
			'title' => $row['name'],
33 3
			'href' => ($row['type'] == 'forum' ? $scripturl . '?' : '') . $row['link'],
34 3
			'target' => $row['target'],
35 3
			'show' => (allowedTo('admin_forum') || array_intersect($user_info['groups'], $row['groups']) != []) && $row['active'],
36
		];
37
38 3
		recursive_button($temp_menu, $menu_buttons, $row['parent'], $row['position'], $key);
39
	}
40
}
41
42
function um_replay_menu(&$menu_buttons)
43
{
44
	global $context;
45
46 1
	$context['replayed_menu_buttons'] = $menu_buttons;
47
}
48
49
function recursive_button(array $needle, array &$haystack, $insertion_point, $where, $key): void
50
{
51 9
	foreach ($haystack as $area => &$info)
52 9
		if ($area == $insertion_point)
53 7
			switch ($where)
54
			{
55 7
				case 'before':
56 4
				case 'after':
57 7
					insert_button([$key => $needle], $haystack, $insertion_point, $where);
58 7
					break 2;
59
60 4
				case 'child_of':
61 4
					$info['sub_buttons'][$key] = $needle;
62 4
					break 2;
63
			}
64 7
		elseif (!empty($info['sub_buttons']))
65 4
			recursive_button($needle, $info['sub_buttons'], $insertion_point, $where, $key);
66
}
67
68
function insert_button(array $needle, array &$haystack, $insertion_point, $where = 'after'): void
69
{
70 7
	$offset = 0;
71
72 7
	foreach ($haystack as $area => $dummy)
73 7
		if (++$offset && $area == $insertion_point)
74 7
			break;
75
76 7
	if ($where == 'before')
77 7
		$offset--;
78
79 7
	$haystack = array_slice($haystack, 0, $offset, true) + $needle + array_slice($haystack, $offset, null, true);
80
}
81
82
function um_admin_areas(&$admin_areas): void
83
{
84
	global $txt;
85
86
	loadLanguage('ManageUltimateMenu');
87
	$admin_areas['config']['areas']['umen'] = [
88
		'label' => $txt['um_admin_menu'],
89
		'file' => 'ManageUltimateMenu.php',
90
		'function' => function (): void
91
		{
92
			global $sourcedir;
93
94
			loadTemplate('ManageUltimateMenu');
95
			require_once $sourcedir . '/Class-UltimateMenu.php';
96
			(new ManageUltimateMenu($_GET['sa'] ?? ''));
97
		},
98
		'icon' => 'umen.png',
99
		'subsections' => [
100
			'manmenu' => [$txt['um_admin_manage_menu'], ''],
101
			'addbutton' => [$txt['um_admin_add_button'], ''],
102
		],
103
	];
104
}