Completed
Pull Request — development (#3050)
by John
23:37
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 18
CRAP Score 5

Importance

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