Completed
Pull Request — development (#3050)
by John
08:57
created

Menu.subs.php ➔ createMenu()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 4
nop 2
dl 0
loc 31
ccs 0
cts 0
cp 0
crap 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file contains a standard way of displaying side/drop down menus.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:    2011 Simple Machines (http://www.simplemachines.org)
12
 * license:    BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 2.0 dev
15
 *
16
 */
17
18
use ElkArte\Menu\Menu;
19
use ElkArte\Menu\MenuArea;
20
use ElkArte\Menu\MenuSection;
21
use ElkArte\Menu\MenuSubsection;
22
23
/**
24
 * Create a menu.
25
 *
26
 * @depreciated since 2.0 use the menu object
27
 *
28
 * developers don't remove this until 3.0-dev
29
 *
30
 * @param array $menuData
31
 * @param array $menuOptions
32
 *
33
 * @return array
34
 */
35
function createMenu(array $menuData, array $menuOptions = []): array
36
{
37
	global $context;
38
39
	$menu = new Menu();
40
	$menu->addOptions($menuOptions);
41
	foreach ($menuData as $section_id => $section)
42
	{
43
		$newAreas = ['areas' => []];
44
		foreach ($section['areas'] as $area_id => $area)
45
		{
46
			$newSubsections = ['subsections' => []];
47
			if (!empty($area['subsections']))
48
			{
49
				foreach ($area['subsections'] as $sa => $sub)
50
				{
51
					$newSubsections['subsections'][$sa] = MenuSubsection::buildFromArray($sub);
52
					unset($area['subsections']);
53
				}
54
			}
55
			$newAreas['areas'][$area_id] = MenuArea::buildFromArray($area + $newSubsections);
56
		}
57
		unset($section['areas']);
58
		$menu->addSection($section_id, MenuSection::buildFromArray($section + $newAreas));
0 ignored issues
show
Compatibility introduced by
\ElkArte\Menu\MenuSectio...y($section + $newAreas) of type object<ElkArte\Menu\MenuItem> is not a sub-type of object<ElkArte\Menu\MenuSection>. It seems like you assume a child class of the class ElkArte\Menu\MenuItem to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
59
	}
60
	$include_data = $menu->prepareMenu();
61
	$menu->setContext();
62
	$context['menu_data_' . $context['max_menu_id']]['object'] = $menu;
63
64
	return $include_data;
65
}
66
67 42
/**
68
 * Delete a menu.
69 42
 *
70
 * @depreciated since 2.0 use the menu object
71
 *
72 42
 * developers don't remove this until 3.0-dev
73
 *
74
 * @param string $menu_id
75 42
 *
76
 * @return boolean
77
 */
78 42
function destroyMenu($menu_id = 'last'): bool
79 42
{
80
	global $context;
81
82 42
	$menu_name =
83 42
		$menu_id === 'last'
84
		&& isset($context['max_menu_id'], $context['menu_data_' . $context['max_menu_id']])
85
			? 'menu_data_' . $context['max_menu_id']
86 42
			: 'menu_data_' . $menu_id;
87
88
	if (!isset($context['max_menu_id'], $context[$menu_name]))
89 42
	{
90 32
		return false;
91
	}
92
	else
93 42
	{
94 42
		// Decrement the pointer if this is the final menu in the series.
95 40 View Code Duplication
		if ($menu_id == 'last' || $menu_id == $context['max_menu_id'])
96 36
		{
97
			$context['max_menu_id'] = max($context['max_menu_id'] - 1, 0);
98
		}
99 42
100 40
		// Remove the template layer if this was the only menu left.
101
		if ($context['max_menu_id'] == 0)
102 42
		{
103
			theme()->getLayers()->remove($context[$menu_name]['layer_name']);
104 42
		}
105
106
		unset($context[$menu_name]);
107 42
108
		return true;
109
	}
110 42
}
111 36
112
/**
113 42
 * Call the function or method for the selected menu item.
114 28
 *
115
 * @depreciated since 2.0 use the menu object
116 36
 *
117 24
 * developers don't remove this until 3.0-dev
118
 *
119
 * @param array $selectedMenu
120
 */
121 36
function callMenu(array $selectedMenu): void
122
{
123 24
	global $context;
124
125
	$action = new Action();
126 42
	$action->initialize(['action' => $selectedMenu]);
127
	$action->dispatch('action');
128
	$context['menu_data_' . $context['max_menu_id']]['object']->prepareTabData();
129 42
}
130 28
131
/**
132 42
 * Loads up all the default menu entries available.
133 28
 *
134
 * @return array
135 42
 */
136 28
function loadDefaultMenuButtons(): array
137 6
{
138 6
	global $scripturl, $txt, $context, $user_info, $modSettings;
139 4
140 36
	$buttons = [
141 2
		'home' => [
142 28
			'title' => $txt['community'],
143
			'href' => $scripturl,
144
			'data-icon' => 'i-home',
145 42
			'show' => true,
146 28
			'sub_buttons' => [
147
				'help' => [
148 42
					'title' => $txt['help'],
149 28
					'href' => $scripturl . '?action=help',
150
					'show' => true,
151
				],
152 42
				'search' => [
153 28
					'title' => $txt['search'],
154 30
					'href' => $scripturl . '?action=search',
155 30
					'show' => $context['allow_search'],
156 20
				],
157
				'calendar' => [
158
					'title' => $txt['calendar'],
159 42
					'href' => $scripturl . '?action=calendar',
160 28
					'show' => $context['allow_calendar'],
161
				],
162 42
				'memberlist' => [
163 28
					'title' => $txt['members_title'],
164 42
					'href' => $scripturl . '?action=memberlist',
165 28
					'show' => $context['allow_memberlist'],
166
				],
167 42
				'recent' => [
168 28
					'title' => $txt['recent_posts'],
169
					'href' => $scripturl . '?action=recent',
170 42
					'show' => true,
171 42
				],
172 28
				'like_stats' => array(
173
					'title' => $txt['like_post_stats'],
174
					'href' => $scripturl . '?action=likes;sa=likestats',
175 42
					'show' => !empty($modSettings['likes_enabled']) && allowedTo('like_posts_stats'),
176
				),
177
				'contact' => array(
178 42
					'title' => $txt['contact'],
179 30
					'href' => $scripturl . '?action=register;sa=contact',
180
					'show' => $user_info['is_guest'] && !empty($modSettings['enable_contactform']) && $modSettings['enable_contactform'] == 'menu',
181
				),
182 42
			],
183 40
		],
184
	];
185 6
186
	// Will change title correctly if user is either a mod or an admin.
187
	// Button highlighting works properly too (see current action stuffz).
188 42
	if ($context['allow_admin'])
189 28
	{
190 42
		$buttons['admin'] = [
191 42
			'title' => $context['current_action'] !== 'moderate' ? $txt['admin'] : $txt['moderate'],
192 42
			'counter' => 'grand_total',
193
			'href' => $scripturl . '?action=admin',
194 42
			'data-icon' => 'i-cog',
195 28
			'show' => true,
196 42
			'sub_buttons' => [
197 42
				'admin_center' => [
198
					'title' => $txt['admin_center'],
199 42
					'href' => $scripturl . '?action=admin',
200 42
					'show' => $context['allow_admin'],
201 28
				],
202
				'featuresettings' => [
203
					'title' => $txt['modSettings_title'],
204 42
					'href' => $scripturl . '?action=admin;area=featuresettings',
205 40
					'show' => allowedTo('admin_forum'),
206
				],
207
				'packages' => [
208 42
					'title' => $txt['package'],
209 28
					'href' => $scripturl . '?action=admin;area=packages',
210
					'show' => allowedTo('admin_forum'),
211 6
				],
212 4
				'permissions' => [
213
					'title' => $txt['edit_permissions'],
214
					'href' => $scripturl . '?action=admin;area=permissions',
215 6
					'show' => allowedTo('manage_permissions'),
216 4
				],
217
				'errorlog' => [
218 6
					'title' => $txt['errlog'],
219
					'href' => $scripturl . '?action=admin;area=logs;sa=errorlog;desc',
220
					'show' => allowedTo('admin_forum') && !empty($modSettings['enableErrorLogging']),
221
				],
222 6
				'moderate_sub' => [
223
					'title' => $txt['moderate'],
224 4
					'counter' => 'grand_total',
225
					'href' => $scripturl . '?action=moderate',
226
					'show' => $context['allow_moderation_center'],
227 42
					'sub_buttons' => [
228 28
						'reports' => [
229
							'title' => $txt['mc_reported_posts'],
230
							'counter' => 'reports',
231 42
							'href' => $scripturl . '?action=moderate;area=reports',
232 28
							'show' => !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1',
233
						],
234
						'modlog' => [
235 42
							'title' => $txt['modlog_view'],
236 28
							'href' => $scripturl . '?action=moderate;area=modlog',
237 42
							'show' => !empty($modSettings['modlog_enabled']) && !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1',
238 42
						],
239
						'attachments' => [
240 42
							'title' => $txt['mc_unapproved_attachments'],
241 30
							'counter' => 'attachments',
242 28
							'href' => $scripturl . '?action=moderate;area=attachmod;sa=attachments',
243 28
							'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']),
244 28
						],
245 28
						'poststopics' => [
246
							'title' => $txt['mc_unapproved_poststopics'],
247
							'counter' => 'postmod',
248
							'href' => $scripturl . '?action=moderate;area=postmod;sa=posts',
249 42
							'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']),
250 28
						],
251
						'postbyemail' => [
252 42
							'title' => $txt['mc_emailerror'],
253
							'counter' => 'emailmod',
254
							'href' => $scripturl . '?action=admin;area=maillist;sa=emaillist',
255 42
							'show' => !empty($modSettings['maillist_enabled']) && allowedTo('approve_emails'),
256
						],
257 42
					],
258
				],
259
			],
260 42
		];
261 28
	}
262
	else
263 42
	{
264
		$buttons['admin'] = [
265 12
			'title' => $txt['moderate'],
266 12
			'counter' => 'grand_total',
267 22
			'href' => $scripturl . '?action=moderate',
268 8
			'data-icon' => 'i-cog',
269 28
			'show' => $context['allow_moderation_center'],
270 28
			'sub_buttons' => [
271 28
				'reports' => [
272
					'title' => $txt['mc_reported_posts'],
273 42
					'counter' => 'reports',
274 28
					'href' => $scripturl . '?action=moderate;area=reports',
275 6
					'show' => !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1',
276 4
				],
277
				'modlog' => [
278
					'title' => $txt['modlog_view'],
279 42
					'href' => $scripturl . '?action=moderate;area=modlog',
280
					'show' => !empty($modSettings['modlog_enabled']) && !empty($user_info['mod_cache']) && $user_info['mod_cache']['bq'] != '0=1',
281
				],
282 42
				'attachments' => [
283 28
					'title' => $txt['mc_unapproved_attachments'],
284 42
					'counter' => 'attachments',
285 42
					'href' => $scripturl . '?action=moderate;area=attachmod;sa=attachments',
286 42
					'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']),
287 30
				],
288
				'poststopics' => [
289 42
					'title' => $txt['mc_unapproved_poststopics'],
290 42
					'counter' => 'postmod',
291
					'href' => $scripturl . '?action=moderate;area=postmod;sa=posts',
292 42
					'show' => $modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap']),
293 28
				],
294 42
				'postbyemail' => [
295 42
					'title' => $txt['mc_emailerror'],
296
					'counter' => 'emailmod',
297 28
					'href' => $scripturl . '?action=admin;area=maillist;sa=emaillist',
298 28
					'show' => !empty($modSettings['maillist_enabled']) && allowedTo('approve_emails'),
299
				],
300
			],
301 42
		];
302 28
	}
303
304
	$buttons += [
305 42
		'profile' => [
306 28
			'title' => !empty($modSettings['displayMemberNames']) ? $user_info['name'] : $txt['account_short'],
307
			'href' => $scripturl . '?action=profile',
308
			'data-icon' => 'i-account',
309
			'show' => $context['allow_edit_profile'],
310
			'sub_buttons' => [
311
				'account' => [
312
					'title' => $txt['account'],
313
					'href' => $scripturl . '?action=profile;area=account',
314
					'show' => allowedTo(['profile_identity_any', 'profile_identity_own', 'manage_membergroups']),
315
				],
316 42
				'drafts' => [
317 28
					'title' => $txt['mydrafts'],
318 42
					'href' => $scripturl . '?action=profile;area=showdrafts',
319 42
					'show' => !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']) && allowedTo(
320 28
							'post_draft'
321
						),
322
				],
323
				'forumprofile' => [
324
					'title' => $txt['forumprofile'],
325 42
					'href' => $scripturl . '?action=profile;area=forumprofile',
326 42
					'show' => allowedTo(['profile_extra_any', 'profile_extra_own']),
327 42
				],
328
				'theme' => [
329
					'title' => $txt['theme'],
330 42
					'href' => $scripturl . '?action=profile;area=theme',
331 28
					'show' => allowedTo(['profile_extra_any', 'profile_extra_own', 'profile_extra_any']),
332
				],
333
				'logout' => [
334
					'title' => $txt['logout'],
335 42
					'href' => $scripturl . '?action=logout',
336 42
					'show' => !$user_info['is_guest'],
337 42
				],
338 42
			],
339
		],
340
		// @todo Look at doing something here, to provide instant access to inbox when using click menus.
341 42
		// @todo A small pop-up anchor seems like the obvious way to handle it. ;)
342
		'pm' => [
343
			'title' => $txt['pm_short'],
344
			'counter' => 'unread_messages',
345
			'href' => $scripturl . '?action=pm',
346
			'data-icon' => ($context['user']['unread_messages'] ? 'i-envelope' : 'i-envelope-blank'),
347
			'show' => $context['allow_pm'],
348
			'sub_buttons' => [
349
				'pm_read' => [
350
					'title' => $txt['pm_menu_read'],
351
					'href' => $scripturl . '?action=pm',
352 30
					'show' => allowedTo('pm_read'),
353
				],
354 30
				'pm_send' => [
355 30
					'title' => $txt['pm_menu_send'],
356 20
					'href' => $scripturl . '?action=pm;sa=send',
357
					'show' => allowedTo('pm_send'),
358 30
				],
359
			],
360 30
		],
361 30
		'mentions' => [
362
			'title' => $txt['mention'],
363
			'counter' => 'mentions',
364
			'href' => $scripturl . '?action=mentions',
365
			'data-icon' => ($context['user']['mentions'] ? 'i-bell' : 'i-bell-blank'),
366
			'show' => !$user_info['is_guest'] && !empty($modSettings['mentions_enabled']),
367
		],
368
		// The old language string made no sense, and was too long.
369
		// "New posts" is better, because there are probably a pile
370
		// of old unread posts, and they wont be reached from this button.
371
		'unread' => [
372
			'title' => $txt['view_unread_category'],
373
			'href' => $scripturl . '?action=unread',
374
			'data-icon' => 'i-comments',
375
			'show' => !$user_info['is_guest'],
376
		],
377 6
		// The old language string made no sense, and was too long.
378 4
		// "New replies" is better, because there are "updated topics"
379
		// that the user has never posted in and doesn't care about.
380
		'unreadreplies' => [
381 6
			'title' => $txt['view_replies_category'],
382
			'href' => $scripturl . '?action=unreadreplies',
383
			'data-icon' => 'i-comments-blank',
384 6
			'show' => !$user_info['is_guest'],
385
		],
386 6
		'login' => [
387 4
			'title' => $txt['login'],
388
			'href' => $scripturl . '?action=login',
389
			'data-icon' => 'i-sign-in',
390
			'show' => $user_info['is_guest'],
391
		],
392
		'register' => [
393 6
			'title' => $txt['register'],
394
			'href' => $scripturl . '?action=register',
395
			'data-icon' => 'i-register',
396
			'show' => $user_info['is_guest'] && $context['can_register'],
397
		],
398
	];
399
400
	return $buttons;
401
}