Passed
Push — 5.x ( 63f35e...64edde )
by Jeroen
13:37 queued 12s
created

AdminHeader::moveUtilities()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 20
rs 9.9332
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' => 'security',
258
			'text' => elgg_echo('admin:security'),
259
			'href' => 'admin/security',
260
			'priority' => 30,
261
			'parent_name' => 'configure',
262
		]);
263
		
264
		$return[] = \ElggMenuItem::factory([
265
			'name' => 'configure_utilities:maintenance',
266
			'text' => elgg_echo('admin:configure_utilities:maintenance'),
267
			'href' => 'admin/configure_utilities/maintenance',
268
			'priority' => 40,
269
			'parent_name' => 'configure',
270
		]);
271
		
272
		$return[] = \ElggMenuItem::factory([
273
			'name' => 'configure_utilities:robots',
274
			'text' => elgg_echo('admin:configure_utilities:robots'),
275
			'href' => 'admin/configure_utilities/robots',
276
			'priority' => 50,
277
			'parent_name' => 'configure',
278
		]);
279
						
280
		return $return;
281
	}
282
	
283
	/**
284
	 * Add the utilities section to the admin page menu
285
	 *
286
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
287
	 *
288
	 * @return void|MenuItems
289
	 */
290
	public static function registerAdminUtilities(\Elgg\Event $event) {
291
		if (!elgg_is_admin_logged_in()) {
292
			return;
293
		}
294
		
295
		/* @var $return MenuItems */
296
		$return = $event->getValue();
297
		
298
		$return[] = \ElggMenuItem::factory([
299
			'name' => 'utilities',
300
			'text' => elgg_echo('menu:page:header:utilities'),
301
			'href' => false,
302
			'priority' => 30,
303
		]);
304
305
		$return[] = \ElggMenuItem::factory([
306
			'name' => 'configure_utilities:menu_items',
307
			'text' => elgg_echo('admin:configure_utilities:menu_items'),
308
			'href' => 'admin/configure_utilities/menu_items',
309
			'parent_name' => 'utilities',
310
		]);
311
		
312
		return $return;
313
	}
314
	
315
	/**
316
	 * Register menu items for default widgets
317
	 *
318
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
319
	 *
320
	 * @return void|MenuItems
321
	 */
322
	public static function registerAdminDefaultWidgets(\Elgg\Event $event) {
323
		if (!elgg_is_admin_logged_in()) {
324
			return;
325
		}
326
		
327
		if (empty(elgg_trigger_event_results('get_list', 'default_widgets', [], []))) {
328
			return;
329
		}
330
		
331
		/* @var $return MenuItems */
332
		$return = $event->getValue();
333
		
334
		$return[] = \ElggMenuItem::factory([
335
			'name' => 'default_widgets',
336
			'text' => elgg_echo('admin:configure_utilities:default_widgets'),
337
			'href' => 'admin/configure_utilities/default_widgets',
338
			'parent_name' => 'utilities',
339
		]);
340
		
341
		return $return;
342
	}
343
	
344
	/**
345
	 * Add the information section to the admin page menu
346
	 *
347
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
348
	 *
349
	 * @return void|MenuItems
350
	 */
351
	public static function registerAdminInformation(\Elgg\Event $event) {
352
		if (!elgg_is_admin_logged_in()) {
353
			return;
354
		}
355
		
356
		/* @var $return MenuItems */
357
		$return = $event->getValue();
358
		
359
		$return[] = \ElggMenuItem::factory([
360
			'name' => 'information',
361
			'text' => elgg_echo('menu:page:header:information'),
362
			'href' => false,
363
			'priority' => 40,
364
		]);
365
		
366
		$return[] = \ElggMenuItem::factory([
367
			'name' => 'server',
368
			'text' => elgg_echo('admin:server'),
369
			'href' => 'admin/server',
370
			'parent_name' => 'information',
371
			'priority' => 50,
372
		]);
373
		
374
		$return[] = \ElggMenuItem::factory([
375
			'name' => 'information:security',
376
			'text' => elgg_echo('admin:security'),
377
			'href' => 'admin/security/information',
378
			'parent_name' => 'information',
379
			'priority' => 60,
380
		]);
381
		
382
		$return[] = \ElggMenuItem::factory([
383
			'name' => 'information:performance',
384
			'text' => elgg_echo('admin:performance'),
385
			'href' => 'admin/performance',
386
			'parent_name' => 'information',
387
			'priority' => 70,
388
		]);
389
		
390
		$return[] = \ElggMenuItem::factory([
391
			'name' => 'statistics',
392
			'text' => elgg_echo('admin:statistics'),
393
			'href' => 'admin/statistics',
394
			'parent_name' => 'information',
395
			'priority' => 80,
396
		]);
397
		
398
		$return[] = \ElggMenuItem::factory([
399
			'name' => 'cron',
400
			'text' => elgg_echo('admin:cron'),
401
			'href' => 'admin/cron',
402
			'parent_name' => 'information',
403
			'priority' => 90,
404
		]);
405
		
406
		return $return;
407
	}
408
	
409
	/**
410
	 * Moves utility menu items to the new section
411
	 *
412
	 * @param \Elgg\Event $event 'register', 'menu:admin_header'
413
	 *
414
	 * @return void|MenuItems
415
	 */
416
	public static function moveUtilities(\Elgg\Event $event) {
417
		if (!elgg_is_admin_logged_in()) {
418
			return;
419
		}
420
		
421
		/* @var $return MenuItems */
422
		$return = $event->getValue();
423
		
424
		/* @var $menu_item \ElggMenuItem */
425
		foreach ($return as $menu_item) {
426
			if (in_array($menu_item->getParentName(), ['administer_utilities', 'configure_utilities'])) {
0 ignored issues
show
Bug introduced by
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

426
			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...
Bug introduced by
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

426
			if (in_array($menu_item->/** @scrutinizer ignore-call */ getParentName(), ['administer_utilities', 'configure_utilities'])) {
Loading history...
427
				$message = 'The menu item ' . $menu_item->getName() . ' is using a deprecated parent.';
0 ignored issues
show
Bug introduced by
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

427
				$message = 'The menu item ' . $menu_item->/** @scrutinizer ignore-call */ getName() . ' is using a deprecated parent.';
Loading history...
428
				$message .= ' Utilities have been moved to a dedicated top menu item (utilities) in the admin header.';
429
				
430
				elgg_deprecated_notice($message, '5.1');
431
				
432
				$menu_item->setParentName('utilities');
0 ignored issues
show
Bug introduced by
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

432
				$menu_item->/** @scrutinizer ignore-call */ 
433
                setParentName('utilities');
Loading history...
433
			}
434
		}
435
		
436
		return $return;
437
	}
438
}
439