Passed
Push — master ( 71950d...a9671b )
by John
01:51
created

um_replay_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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