Issues (10)

engine/classes/Elgg/Menus/AdminHeader.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace Elgg\Menus;
4
5
use Elgg\Menu\MenuItems;
6
use Elgg\Menu\PreparedMenu;
7
8
/**
9
 * Register menu items for the admin_header menu
10
 *
11
 * @since 4.0
12
 * @internal
13
 */
14
class AdminHeader {
15
	
16
	/**
17
	 * Add the default menu items
18
	 *
19
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
20
	 *
21
	 * @return void|MenuItems
22
	 */
23
	public static function register(\Elgg\Event $event) {
24
		if (!elgg_is_admin_logged_in()) {
25
			return;
26
		}
27
		
28
		/* @var $return MenuItems */
29
		$return = $event->getValue();
30
		
31
		$admin = elgg_get_logged_in_user_entity();
32
		
33
		// link back to the site
34
		$return[] = \ElggMenuItem::factory([
35
			'name' => 'view_site',
36
			'icon' => 'home',
37
			'text' => elgg_echo('admin:view_site'),
38
			'href' => elgg_get_site_url(),
39
			'parent_name' => 'account',
40
			'priority' => 100,
41
			'section' => 'alt',
42
		]);
43
		
44
		// logout action
45
		$return[] = \ElggMenuItem::factory([
46
			'name' => 'admin_logout',
47
			'icon' => 'sign-out-alt',
48
			'text' => elgg_echo('logout'),
49
			'href' => elgg_generate_action_url('logout'),
50
			'parent_name' => 'account',
51
			'priority' => 1000,
52
			'section' => 'alt',
53
		]);
54
		
55
		// link to admin profile
56
		$return[] = \ElggMenuItem::factory([
57
			'name' => 'account',
58
			'text' => elgg_echo('account'),
59
			'href' => false,
60
			'icon' => elgg_view('output/img', [
61
				'src' => $admin->getIconURL('small'),
62
				'alt' => $admin->getDisplayName(),
63
			]),
64
			'link_class' => 'elgg-avatar-small',
65
			'section' => 'alt',
66
		]);
67
		
68
		return $return;
69
	}
70
	
71
	/**
72
	 * Add the maintenance link
73
	 *
74
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
75
	 *
76
	 * @return void|MenuItems
77
	 */
78
	public static function registerMaintenance(\Elgg\Event $event) {
79
		if (!elgg_is_admin_logged_in()) {
80
			return;
81
		}
82
		
83
		if (!elgg_get_config('elgg_maintenance_mode')) {
84
			return;
85
		}
86
		
87
		/* @var $return MenuItems */
88
		$return = $event->getValue();
89
		
90
		$return[] = \ElggMenuItem::factory([
91
			'name' => 'maintenance',
92
			'icon' => 'warning',
93
			'text' => elgg_echo('admin:configure_utilities:maintenance'),
94
			'href' => 'admin/configure_utilities/maintenance',
95
			'link_class' => 'elgg-maintenance-mode-warning',
96
			'priority' => 700,
97
		]);
98
		
99
		return $return;
100
	}
101
	
102
	/**
103
	 * Add the administer section to the admin header menu
104
	 *
105
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
106
	 *
107
	 * @return void|MenuItems
108
	 */
109
	public static function registerAdminAdminister(\Elgg\Event $event) {
110
		if (!elgg_is_admin_logged_in()) {
111
			return;
112
		}
113
		
114
		/* @var $return MenuItems */
115
		$return = $event->getValue();
116
		
117
		$return[] = \ElggMenuItem::factory([
118
			'name' => 'administer',
119
			'text' => elgg_echo('menu:page:header:administer'),
120
			'href' => false,
121
			'priority' => 10,
122
		]);
123
		
124
		$return[] = \ElggMenuItem::factory([
125
			'name' => 'dashboard',
126
			'text' => elgg_echo('admin:dashboard'),
127
			'href' => 'admin',
128
			'priority' => 10,
129
			'parent_name' => 'administer',
130
		]);
131
		
132
		$return[] = \ElggMenuItem::factory([
133
			'name' => 'plugins',
134
			'text' => elgg_echo('admin:plugins'),
135
			'href' => elgg_generate_url('admin', ['segments' => 'plugins']),
136
			'priority' => 30,
137
			'parent_name' => 'administer',
138
		]);
139
		
140
		$return[] = \ElggMenuItem::factory([
141
			'name' => 'users',
142
			'text' => elgg_echo('admin:users'),
143
			'href' => 'admin/users',
144
			'priority' => 40,
145
			'parent_name' => 'administer',
146
		]);
147
		
148
		$return[] = \ElggMenuItem::factory([
149
			'name' => 'upgrades',
150
			'text' => elgg_echo('admin:upgrades'),
151
			'href' => 'admin/upgrades',
152
			'priority' => 600,
153
			'parent_name' => 'administer',
154
		]);
155
				
156
		return $return;
157
	}
158
	
159
	/**
160
	 * Prepare the users menu item in the administer section on admin pages
161
	 *
162
	 * @param \Elgg\Event $event 'prepare', 'menu:admin_header'
163
	 *
164
	 * @return PreparedMenu|null
165
	 */
166
	public static function prepareAdminAdministerUsersChildren(\Elgg\Event $event): ?PreparedMenu {
167
		if (!elgg_is_admin_logged_in()) {
168
			return null;
169
		}
170
		
171
		/* @var $result PreparedMenu */
172
		$result = $event->getValue();
173
		
174
		$default = $result->getSection('default');
175
		
176
		/* @var $administer \ElggMenuItem */
177
		$administer = $default->get('administer');
178
		if (!$administer instanceof \ElggMenuItem || empty($administer->getChildren())) {
179
			return null;
180
		}
181
		
182
		/* @var $users \ElggMenuItem */
183
		$users = null;
184
		foreach ($administer->getChildren() as $child) {
185
			if ($child->getID() === 'users') {
186
				$users = $child;
187
				break;
188
			}
189
		}
190
		
191
		if (!$users instanceof \ElggMenuItem || empty($users->getChildren())) {
192
			return null;
193
		}
194
		
195
		$children = $users->getChildren();
196
		
197
		$selected = $users->getSelected();
198
		array_unshift($children, \ElggMenuItem::factory([
199
			'name' => 'users:all',
200
			'text' => elgg_echo('all'),
201
			'href' => 'admin/users',
202
			'parent_name' => 'users',
203
			'priority' => 1,
204
			'selected' => $selected,
205
		]));
206
		$users->setChildren($children);
207
		
208
		if ($selected) {
209
			$users->addItemClass('elgg-has-selected-child');
210
			$users->addItemClass('elgg-state-selected');
211
		}
212
		
213
		$users->setHref(false);
214
		
215
		return $result;
216
	}
217
	
218
	/**
219
	 * Add the configure section to the admin page menu
220
	 *
221
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
222
	 *
223
	 * @return void|MenuItems
224
	 */
225
	public static function registerAdminConfigure(\Elgg\Event $event) {
226
		if (!elgg_is_admin_logged_in()) {
227
			return;
228
		}
229
		
230
		/* @var $return MenuItems */
231
		$return = $event->getValue();
232
		
233
		$return[] = \ElggMenuItem::factory([
234
			'name' => 'configure',
235
			'text' => elgg_echo('menu:page:header:configure'),
236
			'href' => false,
237
			'priority' => 20,
238
		]);
239
		
240
		$return[] = \ElggMenuItem::factory([
241
			'name' => 'settings:basic',
242
			'text' => elgg_echo('admin:site_settings'),
243
			'href' => 'admin/site_settings',
244
			'priority' => 10,
245
			'parent_name' => 'configure',
246
		]);
247
		
248
		$return[] = \ElggMenuItem::factory([
249
			'name' => 'settings:icons',
250
			'text' => elgg_echo('admin:site_icons'),
251
			'href' => 'admin/site_icons',
252
			'priority' => 20,
253
			'parent_name' => 'configure',
254
		]);
255
		
256
		$return[] = \ElggMenuItem::factory([
257
			'name' => 'settings:theme',
258
			'text' => elgg_echo('admin:theme'),
259
			'href' => 'admin/theme',
260
			'priority' => 25,
261
			'parent_name' => 'configure',
262
		]);
263
		
264
		$return[] = \ElggMenuItem::factory([
265
			'name' => 'security',
266
			'text' => elgg_echo('admin:security'),
267
			'href' => 'admin/security',
268
			'priority' => 30,
269
			'parent_name' => 'configure',
270
		]);
271
		
272
		$return[] = \ElggMenuItem::factory([
273
			'name' => 'configure_utilities:maintenance',
274
			'text' => elgg_echo('admin:configure_utilities:maintenance'),
275
			'href' => 'admin/configure_utilities/maintenance',
276
			'priority' => 40,
277
			'parent_name' => 'configure',
278
		]);
279
		
280
		$return[] = \ElggMenuItem::factory([
281
			'name' => 'configure_utilities:robots',
282
			'text' => elgg_echo('admin:configure_utilities:robots'),
283
			'href' => 'admin/configure_utilities/robots',
284
			'priority' => 50,
285
			'parent_name' => 'configure',
286
		]);
287
						
288
		return $return;
289
	}
290
	
291
	/**
292
	 * Add the utilities section to the admin page menu
293
	 *
294
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
295
	 *
296
	 * @return void|MenuItems
297
	 */
298
	public static function registerAdminUtilities(\Elgg\Event $event) {
299
		if (!elgg_is_admin_logged_in()) {
300
			return;
301
		}
302
		
303
		/* @var $return MenuItems */
304
		$return = $event->getValue();
305
		
306
		$return[] = \ElggMenuItem::factory([
307
			'name' => 'utilities',
308
			'text' => elgg_echo('menu:page:header:utilities'),
309
			'href' => false,
310
			'priority' => 30,
311
		]);
312
313
		$return[] = \ElggMenuItem::factory([
314
			'name' => 'configure_utilities:menu_items',
315
			'text' => elgg_echo('admin:configure_utilities:menu_items'),
316
			'href' => 'admin/configure_utilities/menu_items',
317
			'parent_name' => 'utilities',
318
		]);
319
		
320
		return $return;
321
	}
322
	
323
	/**
324
	 * Register menu items for default widgets
325
	 *
326
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
327
	 *
328
	 * @return void|MenuItems
329
	 */
330
	public static function registerAdminDefaultWidgets(\Elgg\Event $event) {
331
		if (!elgg_is_admin_logged_in()) {
332
			return;
333
		}
334
		
335
		if (empty(elgg_trigger_event_results('get_list', 'default_widgets', [], []))) {
336
			return;
337
		}
338
		
339
		/* @var $return MenuItems */
340
		$return = $event->getValue();
341
		
342
		$return[] = \ElggMenuItem::factory([
343
			'name' => 'default_widgets',
344
			'text' => elgg_echo('admin:configure_utilities:default_widgets'),
345
			'href' => 'admin/configure_utilities/default_widgets',
346
			'parent_name' => 'utilities',
347
		]);
348
		
349
		return $return;
350
	}
351
	
352
	/**
353
	 * Add the information section to the admin page menu
354
	 *
355
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
356
	 *
357
	 * @return void|MenuItems
358
	 */
359
	public static function registerAdminInformation(\Elgg\Event $event) {
360
		if (!elgg_is_admin_logged_in()) {
361
			return;
362
		}
363
		
364
		/* @var $return MenuItems */
365
		$return = $event->getValue();
366
		
367
		$return[] = \ElggMenuItem::factory([
368
			'name' => 'information',
369
			'text' => elgg_echo('menu:page:header:information'),
370
			'href' => false,
371
			'priority' => 40,
372
		]);
373
		
374
		$return[] = \ElggMenuItem::factory([
375
			'name' => 'server',
376
			'text' => elgg_echo('admin:server'),
377
			'href' => 'admin/server',
378
			'parent_name' => 'information',
379
			'priority' => 50,
380
		]);
381
		
382
		$return[] = \ElggMenuItem::factory([
383
			'name' => 'information:security',
384
			'text' => elgg_echo('admin:security'),
385
			'href' => 'admin/security/information',
386
			'parent_name' => 'information',
387
			'priority' => 60,
388
		]);
389
		
390
		$return[] = \ElggMenuItem::factory([
391
			'name' => 'information:performance',
392
			'text' => elgg_echo('admin:performance'),
393
			'href' => 'admin/performance',
394
			'parent_name' => 'information',
395
			'priority' => 70,
396
		]);
397
		
398
		$return[] = \ElggMenuItem::factory([
399
			'name' => 'statistics',
400
			'text' => elgg_echo('admin:statistics'),
401
			'href' => 'admin/statistics',
402
			'parent_name' => 'information',
403
			'priority' => 80,
404
		]);
405
		
406
		$return[] = \ElggMenuItem::factory([
407
			'name' => 'cron',
408
			'text' => elgg_echo('admin:cron'),
409
			'href' => 'admin/cron',
410
			'parent_name' => 'information',
411
			'priority' => 90,
412
		]);
413
		
414
		return $return;
415
	}
416
	
417
	/**
418
	 * Moves utility menu items to the new section
419
	 *
420
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
421
	 *
422
	 * @return void|MenuItems
423
	 */
424
	public static function moveUtilities(\Elgg\Event $event) {
425
		if (!elgg_is_admin_logged_in()) {
426
			return;
427
		}
428
		
429
		/* @var $return MenuItems */
430
		$return = $event->getValue();
431
		
432
		/* @var $menu_item \ElggMenuItem */
433
		foreach ($return as $menu_item) {
434
			if (in_array($menu_item->getParentName(), ['administer_utilities', 'configure_utilities'])) {
0 ignored issues
show
The method getParentName() does not exist on Elgg\Collections\CollectionItemInterface. It seems like you code against a sub-type of Elgg\Collections\CollectionItemInterface such as ElggMenuItem. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

434
			if (in_array($menu_item->/** @scrutinizer ignore-call */ getParentName(), ['administer_utilities', 'configure_utilities'])) {
Loading history...
The method getParentName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

434
			if (in_array($menu_item->/** @scrutinizer ignore-call */ getParentName(), ['administer_utilities', 'configure_utilities'])) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
435
				$message = 'The menu item ' . $menu_item->getName() . ' is using a deprecated parent.';
0 ignored issues
show
The method getName() does not exist on Elgg\Collections\CollectionItemInterface. It seems like you code against a sub-type of Elgg\Collections\CollectionItemInterface such as ElggMenuItem. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

435
				$message = 'The menu item ' . $menu_item->/** @scrutinizer ignore-call */ getName() . ' is using a deprecated parent.';
Loading history...
436
				$message .= ' Utilities have been moved to a dedicated top menu item (utilities) in the admin header.';
437
				
438
				elgg_deprecated_notice($message, '5.1');
439
				
440
				$menu_item->setParentName('utilities');
0 ignored issues
show
The method setParentName() does not exist on Elgg\Collections\CollectionItemInterface. It seems like you code against a sub-type of Elgg\Collections\CollectionItemInterface such as ElggMenuItem. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

440
				$menu_item->/** @scrutinizer ignore-call */ 
441
                setParentName('utilities');
Loading history...
441
			}
442
		}
443
		
444
		return $return;
445
	}
446
}
447