Passed
Push — master ( ba823b...756562 )
by Jeroen
06:16
created

elgg_push_entity_breadcrumbs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 2
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg navigation library
4
 * Functions for managing menus and other navigational elements
5
 *
6
 * Breadcrumbs
7
 * Elgg uses a breadcrumb stack. The page handlers (controllers in MVC terms)
8
 * push the breadcrumb links onto the stack. @see elgg_push_breadcrumb()
9
 *
10
 *
11
 * Pagination
12
 * Automatically handled by Elgg when using elgg_list_entities* functions.
13
 * @see elgg_list_entities()
14
 *
15
 *
16
 * Tabs
17
 * @see navigation/tabs view
18
 *
19
 *
20
 * Menus
21
 * Elgg uses a single interface to manage its menus. Menu items are added with
22
 * {@link elgg_register_menu_item()}. This is generally used for menus that
23
 * appear only once per page. For dynamic menus (such as the hover
24
 * menu for user's avatar), a plugin hook is emitted when the menu is being
25
 * created. The hook is 'register', 'menu:<menu_name>'. For more details on this,
26
 * @see elgg_view_menu().
27
 *
28
 * Menus supported by the Elgg core
29
 * Standard menus:
30
 *     site   Site navigation shown on every page.
31
 *     page   Page menu usually shown in a sidebar. Uses Elgg's context.
32
 *     topbar Topbar menu shown on every page. The default has two sections.
33
 *     footer Like the topbar but in the footer.
34
 *
35
 * Dynamic menus (also called just-in-time menus):
36
 *     user_hover  Avatar hover menu. The user entity is passed as a parameter.
37
 *     entity      The set of links shown in the summary of an entity.
38
 *     river       Links shown on river items.
39
 *     owner_block Links shown for a user or group in their owner block.
40
 *     filter      The tab filter for content (all, mine, friends)
41
 *     title       The buttons shown next to a content title.
42
 *     longtext    The links shown above the input/longtext view.
43
 *     login       Menu of links at bottom of login box
44
 *
45
 * @package    Elgg.Core
46
 * @subpackage Navigation
47
 */
48
49
/**
50
 * Register an item for an Elgg menu
51
 *
52
 * @warning Generally you should not use this in response to the plugin hook:
53
 * 'register', 'menu:<menu_name>'. If you do, you may end up with many incorrect
54
 * links on a dynamic menu.
55
 *
56
 * @warning A menu item's name must be unique per menu. If more than one menu
57
 * item with the same name are registered, the last menu item takes priority.
58
 *
59
 * @see elgg_view_menu() for the plugin hooks available for modifying a menu as
60
 * it is being rendered.
61
 *
62
 * @see ElggMenuItem::factory() is used to turn an array value of $menu_item into an
63
 * ElggMenuItem object.
64
 *
65
 * @param string $menu_name The name of the menu: site, page, userhover,
66
 *                          userprofile, groupprofile, or any custom menu
67
 * @param mixed  $menu_item A \ElggMenuItem object or an array of options in format:
68
 *                          name        => STR  Menu item identifier (required)
69
 *                          text        => STR  Menu item display text as HTML (required)
70
 *                          href        => STR  Menu item URL (required)
71
 *                                              false = do not create a link.
72
 *                                              null = current URL.
73
 *                                              "" = current URL.
74
 *                                              "/" = site home page.
75
 *                                              @warning If href is false, the <a> tag will
76
 *                                              not appear, so the link_class will not apply. If you
77
 *                                              put <a> tags in manually through the 'text' option
78
 *                                              the default CSS selector .elgg-menu-$menu > li > a
79
 *                                              may affect formatting. Wrap in a <span> if it does.)
80
 *                          contexts    => ARR  Page context strings
81
 *                          section     => STR  Menu section identifier
82
 *                          title       => STR  Menu item tooltip
83
 *                          selected    => BOOL Is this menu item currently selected
84
 *                          parent_name => STR  Identifier of the parent menu item
85
 *                          link_class  => STR  A class or classes for the <a> tag
86
 *                          item_class  => STR  A class or classes for the <li> tag
87
 *                          deps     => STR  One or more AMD modules to require
88
 *
89
 *                          Additional options that the view output/url takes can be
90
 *							passed in the array. Custom options can be added by using
91
 *							the 'data' key with the	value being an associative array.
92
 *
93
 * @return bool False if the item could not be added
94
 * @since 1.8.0
95
 */
96
function elgg_register_menu_item($menu_name, $menu_item) {
97 34
	if (is_array($menu_item)) {
98 34
		$options = $menu_item;
99 34
		$menu_item = \ElggMenuItem::factory($options);
100 34
		if (!$menu_item) {
0 ignored issues
show
introduced by Ismayil Khayredinov
The condition ! $menu_item can never be false.
Loading history...
101
			$menu_item_name = elgg_extract('name', $options, 'MISSING NAME');
102
			elgg_log("Unable to add menu item '{$menu_item_name}' to '$menu_name' menu", 'WARNING');
103
			return false;
104
		}
105
	}
106
107 34
	if (!$menu_item instanceof ElggMenuItem) {
108
		elgg_log('Second argument of elgg_register_menu_item() must be an instance of '
109
			. 'ElggMenuItem or an array of menu item factory options', 'ERROR');
110
		return false;
111
	}
112
113 34
	$menus = _elgg_config()->menus;
114 34
	if (!$menus) {
115 21
		$menus = [];
116
	}
117
118 34
	$menus[$menu_name][] = $menu_item;
119 34
	_elgg_config()->menus = $menus;
120
121 34
	return true;
122
}
123
124
/**
125
 * Remove an item from a menu
126
 *
127
 * @param string $menu_name The name of the menu
128
 * @param string $item_name The unique identifier for this menu item
129
 *
130
 * @return \ElggMenuItem|null
131
 * @since 1.8.0
132
 */
133
function elgg_unregister_menu_item($menu_name, $item_name) {
134
	$menus = _elgg_config()->menus;
135
	if (!$menus) {
136
		return null;
137
	}
138
139
	foreach ($menus[$menu_name] as $index => $menu_object) {
140
		/* @var \ElggMenuItem $menu_object */
141
		if ($menu_object instanceof ElggMenuItem && $menu_object->getName() == $item_name) {
142
			$item = $menus[$menu_name][$index];
143
			unset($menus[$menu_name][$index]);
144
			elgg_set_config('menus', $menus);
145
			return $item;
146
		}
147
	}
148
149
	return null;
150
}
151
152
/**
153
 * Check if a menu item has been registered
154
 *
155
 * @param string $menu_name The name of the menu
156
 * @param string $item_name The unique identifier for this menu item
157
 *
158
 * @return bool
159
 * @since 1.8.0
160
 */
161
function elgg_is_menu_item_registered($menu_name, $item_name) {
162
	$menus = _elgg_config()->menus;
163
	if (!$menus) {
164
		return false;
165
	}
166
167
	if (!isset($menus[$menu_name])) {
168
		return false;
169
	}
170
171
	foreach ($menus[$menu_name] as $menu_object) {
172
		/* @var \ElggMenuItem $menu_object */
173
		if ($menu_object->getName() == $item_name) {
174
			return true;
175
		}
176
	}
177
178
	return false;
179
}
180
181
/**
182
 * Get a menu item registered for a menu
183
 *
184
 * @param string $menu_name The name of the menu
185
 * @param string $item_name The unique identifier for this menu item
186
 *
187
 * @return ElggMenuItem|null
188
 * @since 1.9.0
189
 */
190
function elgg_get_menu_item($menu_name, $item_name) {
191
	$menus = _elgg_config()->menus;
192
	if (!$menus) {
193
		return null;
194
	}
195
196
	if (!isset($menus[$menu_name])) {
197
		return null;
198
	}
199
200
	foreach ($menus[$menu_name] as $index => $menu_object) {
201
		/* @var \ElggMenuItem $menu_object */
202
		if ($menu_object->getName() == $item_name) {
203
			return $menus[$menu_name][$index];
204
		}
205
	}
206
207
	return null;
208
}
209
210
/**
211
 * Convenience function for registering a button to the title menu
212
 *
213
 * The URL must be $handler/$name/$guid where $guid is the guid of the page owner.
214
 * The label of the button is "$handler:$name" so that must be defined in a
215
 * language file.
216
 *
217
 * This is used primarily to support adding an add content button
218
 *
219
 * @param string $handler        The handler to use or null to autodetect from context
220
 * @param string $name           Name of the button (defaults to 'add')
221
 * @param string $entity_type    Optional entity type to be added (used to verify canWriteToContainer permission)
222
 * @param string $entity_subtype Optional entity subtype to be added (used to verify canWriteToContainer permission)
223
 * @return void
224
 * @since 1.8.0
225
 */
226
function elgg_register_title_button($handler = null, $name = 'add', $entity_type = 'all', $entity_subtype = 'all') {
227
	
228
	if (!$handler) {
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression $handler of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
229
		$handler = elgg_get_context();
230
	}
231
232
	$owner = elgg_get_page_owner_entity();
233
	if (!$owner) {
0 ignored issues
show
introduced by Ismayil Khayredinov
The condition ! $owner can never be false.
Loading history...
234
		// noone owns the page so this is probably an all site list page
235
		$owner = elgg_get_logged_in_user_entity();
236
	}
237
	
238
	if (($name === 'add') && ($owner instanceof ElggUser)) {
239
		// make sure the add link goes to the current logged in user, not the page owner
240
		$logged_in_user = elgg_get_logged_in_user_entity();
241
		if (!empty($logged_in_user) && ($logged_in_user->guid !== $owner->guid)) {
242
			// change the 'owner' for the link to the current logged in user
243
			$owner = $logged_in_user;
244
		}
245
	}
246
	
247
	// do we have an owner and is the current user allowed to create content here
248
	if (!$owner || !$owner->canWriteToContainer(0, $entity_type, $entity_subtype)) {
249
		return;
250
	}
251
252
	// register the title menu item
253
	elgg_register_menu_item('title', [
254
		'name' => $name,
255
		'icon' => $name === 'add' ? 'plus' : '',
256
		'href' => "$handler/$name/$owner->guid",
257
		'text' => elgg_echo("$handler:$name"),
258
		'link_class' => 'elgg-button elgg-button-action',
259
	]);
260
}
261
262
/**
263
 * Adds a breadcrumb to the breadcrumbs stack.
264
 *
265
 * See elgg_get_breadcrumbs() and the navigation/breadcrumbs view.
266
 *
267
 * @param string $text The title to display. During rendering this is HTML encoded.
268
 * @param string $href Optional. The href for the title. During rendering links are
269
 *                     normalized via elgg_normalize_url().
270
 *
271
 * @return void
272
 * @since 1.8.0
273
 * @see elgg_get_breadcrumbs()
274
 */
275
function elgg_push_breadcrumb($text, $href = null) {
276
	$breadcrumbs = (array) _elgg_config()->breadcrumbs;
277
	
278
	$breadcrumbs[] = [
279
		'text' => $text,
280
		'href' => $href,
281
	];
282
	
283
	elgg_set_config('breadcrumbs', $breadcrumbs);
284
}
285
286
/**
287
 * Removes last breadcrumb entry.
288
 *
289
 * @return array popped breadcrumb array or empty array
290
 * @since 1.8.0
291
 */
292
function elgg_pop_breadcrumb() {
293
	$breadcrumbs = (array) _elgg_config()->breadcrumbs;
294
295
	if (empty($breadcrumbs)) {
296
		return [];
297
	}
298
299
	$popped = array_pop($breadcrumbs);
300
	elgg_set_config('breadcrumbs', $breadcrumbs);
301
302
	return $popped;
303
}
304
305
/**
306
 * Returns all breadcrumbs as an array
307
 * <code>
308
 * [
309
 *    [
310
 *       'text' => 'Breadcrumb title',
311
 *       'href' => '/path/to/page',
312
 *    ]
313
 * ]
314
 * </code>
315
 *
316
 * Breadcrumbs are filtered through the plugin hook [prepare, breadcrumbs] before
317
 * being returned.
318
 *
319
 * @param array $breadcrumbs An array of breadcrumbs
320
 *                           If set, will override breadcrumbs in the stack
321
 * @return array
322
 * @since 1.8.0
323
 * @see elgg_prepare_breadcrumbs()
324
 */
325
function elgg_get_breadcrumbs(array $breadcrumbs = null) {
326
	if (!isset($breadcrumbs)) {
327
		// if no crumbs set, still allow hook to populate it
328
		$breadcrumbs = (array) _elgg_config()->breadcrumbs;
329
	}
330
331
	if (!is_array($breadcrumbs)) {
0 ignored issues
show
introduced by Ismayil Khayredinov
The condition ! is_array($breadcrumbs) can never be true.
Loading history...
332
		_elgg_services()->logger->error(__FUNCTION__ . ' expects breadcrumbs as an array');
333
		$breadcrumbs = [];
334
	}
335
	
336
	$params = [
337
		'breadcrumbs' => $breadcrumbs,
338
	];
339
340
	$params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
341
	$params['segments'] = _elgg_services()->request->getUrlSegments();
342
	array_shift($params['segments']);
343
344
	$breadcrumbs = elgg_trigger_plugin_hook('prepare', 'breadcrumbs', $params, $breadcrumbs);
345
	if (!is_array($breadcrumbs)) {
0 ignored issues
show
introduced by Steve Clay
The condition ! is_array($breadcrumbs) can never be true.
Loading history...
346
		_elgg_services()->logger->error('"prepare, breadcrumbs" hook must return an array of breadcrumbs');
347
		return [];
348
	}
349
	
350
	foreach ($breadcrumbs as $key => $breadcrumb) {
351
		$text = elgg_extract('text', $breadcrumb, elgg_extract('title', $breadcrumb));
352
		if (isset($breadcrumb['link'])) {
353
			elgg_deprecated_notice("Breadcrumb [{$text}] requires 'href' instead of 'link' set in the configuration", '3.0');
354
			
355
			$breadcrumbs[$key]['href'] = $breadcrumb['link'];
356
			unset($breadcrumbs[$key]['link']);
357
		}
358
		if (isset($breadcrumb['title'])) {
359
			elgg_deprecated_notice("Breadcrumb [{$text}] requires 'text' instead of 'title' set in the configuration", '3.0');
360
			
361
			$breadcrumbs[$key]['text'] = $breadcrumb['title'];
362
			unset($breadcrumbs[$key]['title']);
363
		}
364
		
365
		// adds name for usage in menu items
366
		if (!isset($breadcrumb['name'])) {
367
			$breadcrumbs[$key]['name'] = $key;
368
		}
369
	}
370
371
	return $breadcrumbs;
372
}
373
374
/**
375
 * Prepare breadcrumbs before display. This turns titles into 100-character excerpts, and also
376
 * removes the last crumb if it's not a link.
377
 *
378
 * @param string $hook        "prepare"
379
 * @param string $type        "breadcrumbs"
380
 * @param array  $breadcrumbs Breadcrumbs to be altered
381
 * @param array  $params      Hook parameters
382
 *
383
 * @return array
384
 * @since 1.11
385
 */
386
function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, $params) {
3 ignored issues
show
Unused Code introduced by Steve Clay
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

386
function elgg_prepare_breadcrumbs(/** @scrutinizer ignore-unused */ $hook, $type, $breadcrumbs, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Steve Clay
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

386
function elgg_prepare_breadcrumbs($hook, /** @scrutinizer ignore-unused */ $type, $breadcrumbs, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Steve Clay
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

386
function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
387
	// remove last crumb if not a link
388
	$last_crumb = end($breadcrumbs);
389
	if (empty($last_crumb['href'])) {
390
		array_pop($breadcrumbs);
391
	}
392
393
	// apply excerpt to text
394
	foreach (array_keys($breadcrumbs) as $i) {
395
		$breadcrumbs[$i]['text'] = elgg_get_excerpt($breadcrumbs[$i]['text'], 100);
396
	}
397
	return $breadcrumbs;
398
}
399
400
/**
401
 * Resolves and pushes entity breadcrumbs based on named routes
402
 *
403
 * @param ElggEntity $entity    Entity
404
 * @param bool       $link_self Use entity link in the last crumb
405
 *
406
 * @return void
407
 */
408
function elgg_push_entity_breadcrumbs(ElggEntity $entity, $link_self = true) {
409
410
	$container = $entity->getContainerEntity() ? : null;
411
	elgg_push_collection_breadcrumbs($entity->type, $entity->subtype, $container);
412
413
	$entity_url = $link_self ? $entity->getURL() : null;
414
	elgg_push_breadcrumb($entity->getDisplayName(), $entity_url);
415
}
416
417
/**
418
 * Resolves and pushes collection breadcrumbs for a container
419
 *
420
 * @param string          $entity_type    Entity type in the collection
421
 * @param string          $entity_subtype Entity subtype in the collection
422
 * @param ElggEntity|null $container      Container/page owner entity
423
 * @param bool            $friends        Collection belongs to container's friends?
424
 *
425
 * @return void
426
 */
427
function elgg_push_collection_breadcrumbs($entity_type, $entity_subtype, ElggEntity $container = null, $friends = false) {
428
429
	if ($container) {
430
		elgg_push_breadcrumb($container->getDisplayName(), $container->getURL());
431
432
		if ($friends) {
433
			$collection_route = "collection:$entity_type:$entity_subtype:friends";
434
		} else if ($container instanceof ElggUser) {
435
			$collection_route = "collection:$entity_type:$entity_subtype:owner";
436
		} else if ($container instanceof ElggGroup) {
437
			$collection_route = "collection:$entity_type:$entity_subtype:group";
438
		} else {
439
			$collection_route = "collection:$entity_type:$entity_subtype:container";
440
		}
441
442
		$parameters = _elgg_services()->router->resolveRouteParameters($collection_route, $container);
443
		if ($parameters) {
0 ignored issues
show
introduced by Ismayil Khayredinov
The condition $parameters can never be true.
Loading history...
444
			$label = elgg_echo("item:$entity_type:$entity_subtype");
445
			if ($friends) {
446
				$label = elgg_echo('collection:friends', [$label]);
447
			}
448
			$collection_url = elgg_generate_url($collection_route, $parameters);
449
			elgg_push_breadcrumb($label, $collection_url);
450
		}
451
	} else {
452
		$label = elgg_echo("item:$entity_type:$entity_subtype");
453
		$collection_url = elgg_generate_url("collection:$entity_type:$entity_subtype:all");
454
		elgg_push_breadcrumb($label, $collection_url);
455
	}
456
}
457
458
/**
459
 * Returns default filter tabs (All, Mine, Friends) for the user
460
 *
461
 * @param string   $context  Context to be used to prefix tab URLs
462
 * @param string   $selected Name of the selected tab
463 1
 * @param ElggUser $user     User who owns the layout (defaults to logged in user)
464
 * @param array    $vars     Additional vars
465 1
 * @return ElggMenuItem[]
466
 * @since 2.3
467
 */
468
function elgg_get_filter_tabs($context = null, $selected = null, ElggUser $user = null, array $vars = []) {
469
470
	if (!isset($selected)) {
471
		$selected = 'all';
472
	}
473
474
	if (!$user) {
475 1
		$user = elgg_get_logged_in_user_entity();
476
	}
477
478
	$items = [];
479
	if ($user) {
480
		$items[] = ElggMenuItem::factory([
481
			'name' => 'all',
482
			'text' => elgg_echo('all'),
483 1
			'href' => (isset($vars['all_link'])) ? $vars['all_link'] : "$context/all",
484
			'selected' => ($selected == 'all'),
485
			'priority' => 200,
486
		]);
487
		$items[] = ElggMenuItem::factory([
488
			'name' => 'mine',
489
			'text' => elgg_echo('mine'),
490
			'href' => (isset($vars['mine_link'])) ? $vars['mine_link'] : "$context/owner/{$user->username}",
491
			'selected' => ($selected == 'mine'),
492
			'priority' => 300,
493
		]);
494
	}
495
496
	$params = [
497
		'selected' => $selected,
498
		'user' => $user,
499
		'vars' => $vars,
500
	];
501
	$items = _elgg_services()->hooks->trigger('filter_tabs', $context, $params, $items);
502 1
503
	return $items;
504 1
}
505
506
/**
507 1
 * Init site menu
508 1
 *
509 1
 * Registers custom menu items
510 1
 *
511
 * @param string         $hook   'register'
512
 * @param string         $type   'menu:site'
513
 * @param ElggMenuItem[] $return Menu
514 1
 * @param array          $params Hook params
515 1
 *
516
 * @return ElggMenuItem[]
517 1
 *
518 1
 * @access private
519
 */
520
function _elgg_site_menu_init($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by Cash Costello
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

520
function _elgg_site_menu_init(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

520
function _elgg_site_menu_init($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

520
function _elgg_site_menu_init($hook, $type, $return, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
521
	$custom_menu_items = elgg_get_config('site_custom_menu_items');
522 1
523
	if ($custom_menu_items) {
524 1
		// add custom menu items
525 1
		$n = 1;
526 1
		foreach ($custom_menu_items as $title => $url) {
527
			$item = new ElggMenuItem("custom$n", $title, $url);
528
			$return[] = $item;
529 1
			$n++;
530
		}
531
	}
532
533
	if (elgg_is_logged_in() && elgg_is_active_plugin('dashboard')) {
534 1
		$return[] = ElggMenuItem::factory([
535 1
			'name' => 'dashboard',
536 1
			'text' => elgg_echo('dashboard'),
537 1
			'href' => 'dashboard',
538
		]);
539 1
	}
540
541
	return $return;
542
}
543
544 1
/**
545
 * Set up the site menu
546 1
 *
547
 * Handles default, featured, and custom menu items
548 1
 *
549
 * @param string         $hook   'prepare'
550 1
 * @param string         $type   'menu:site'
551 1
 * @param ElggMenuItem[] $return current return value
552 1
 * @param array          $params supplied params
553
 *
554
 * @return ElggMenuItem[]
555 1
 *
556 1
 * @access private
557 1
 */
558 1
function _elgg_site_menu_setup($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by Cash Costello
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

558
function _elgg_site_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Jeroen Dalsem
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

558
function _elgg_site_menu_setup($hook, $type, $return, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

558
function _elgg_site_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
559 1
560 1
	$featured_menu_names = array_values((array) elgg_get_config('site_featured_menu_names'));
561 1
562
	$registered = $return['default'];
563
	/* @var ElggMenuItem[] $registered */
564 1
565 1
	$has_selected = false;
566
	$priority = 500;
567
	foreach ($registered as &$item) {
568 1
		if (in_array($item->getName(), $featured_menu_names)) {
569
			$featured_index = array_search($item->getName(), $featured_menu_names);
570 1
			$item->setPriority($featured_index);
0 ignored issues
show
Bug introduced by Ismayil Khayredinov
It seems like $featured_index can also be of type string and false; however, parameter $priority of ElggMenuItem::setPriority() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

570
			$item->setPriority(/** @scrutinizer ignore-type */ $featured_index);
Loading history...
571
		} else {
572
			$item->setPriority($priority);
573 1
			$priority++;
574
		}
575 1
		if ($item->getSelected()) {
576
			$has_selected = true;
577
		}
578
	}
579
580
	if (!$has_selected) {
581
		$is_selected = function ($item) {
582
			$current_url = current_page_url();
583
			if (strpos($item->getHref(), elgg_get_site_url()) === 0) {
584
				if ($item->getName() == elgg_get_context()) {
585
					return true;
586
				}
587
				if ($item->getHref() == $current_url) {
588
					return true;
589
				}
590 2
			}
591
592 2
			return false;
593 1
		};
594 1
		foreach ($registered as &$item) {
595 1
			if ($is_selected($item)) {
596 1
				$item->setSelected(true);
597 1
				break;
598
			}
599 1
		}
600
	}
601
602
	usort($registered, [\ElggMenuBuilder::class, 'compareByPriority']);
603
604 2
	$max_display_items = 5;
605 2
606
	$num_menu_items = count($registered);
607
608
	$more = [];
609
	if ($max_display_items && $num_menu_items > ($max_display_items + 1)) {
610
		$more = array_splice($registered, $max_display_items);
611
	}
612
613 2
	if (!empty($more)) {
614
		$dropdown = ElggMenuItem::factory([
615
			'name' => 'more',
616
			'href' => 'javascript:void(0);',
617
			'text' => elgg_echo('more'),
618
			'icon_alt' => 'angle-down',
619
			'priority' => 999,
620
		]);
621
622
		foreach ($more as &$item) {
623
			$item->setParentName('more');
624
		}
625
626
		$dropdown->setChildren($more);
627
628
		$registered[] = $dropdown;
629 1
	}
630 1
631
	$return['default'] = $registered;
632
633
	return $return;
0 ignored issues
show
Bug Best Practice introduced by Cash Costello
The expression return $return returns an array which contains values of type ElggMenuItem[] which are incompatible with the documented value type ElggMenuItem.
Loading history...
634 1
}
635 1
636 1
/**
637
 * Prepare page menu
638
 * Sets the display child menu option to "toggle" if not set
639
 * Recursively marks parents of the selected item as selected (expanded)
640
 *
641
 * @param \Elgg\Hook $hook 'prepare', 'menu:page'
642
 *
643
 * @return ElggMenuItem[]
644
 *
645
 * @access private
646
 */
647
function _elgg_page_menu_setup(\Elgg\Hook $hook) {
648
	$menu = $hook->getValue();
649
650
	foreach ($menu as $section => $menu_items) {
651
		foreach ($menu_items as $menu_item) {
652
			if ($menu_item instanceof ElggMenuItem) {
653
				$child_menu_vars = $menu_item->getChildMenuOptions();
654
				if (empty($child_menu_vars['display'])) {
655
					$child_menu_vars['display'] = 'toggle';
656
				}
657
				$menu_item->setChildMenuOptions($child_menu_vars);
658
			}
659
		}
660
	}
661
662
	$selected_item = $hook->getParam('selected_item');
663
	if ($selected_item instanceof \ElggMenuItem) {
664
		$parent = $selected_item->getParent();
665
		while ($parent instanceof \ElggMenuItem) {
0 ignored issues
show
introduced by Ismayil Khayredinov
The condition $parent instanceof ElggMenuItem can never be false since $parent is always a sub-type of ElggMenuItem.
Loading history...
666
			$parent->setSelected();
667
			$parent = $parent->getParent();
668
		}
669
	}
670
671
	return $menu;
672
}
673
674
/**
675
 * Entity menu is list of links and info on any entity
676
 *
677
 * @param string         $hook   'register'
678
 * @param string         $type   'menu:entity'
679
 * @param ElggMenuItem[] $return current return value
680
 * @param array          $params supplied params
681 2
 *
682
 * @return void|ElggMenuItem[]
683 2
 *
684 2
 * @access private
685
 */
686
function _elgg_entity_menu_setup($hook, $type, $return, $params) {
2 ignored issues
show
Unused Code introduced by Cash Costello
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

686
function _elgg_entity_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

686
function _elgg_entity_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
687
	$entity = elgg_extract('entity', $params);
688 2
	if (!($entity instanceof \ElggObject)) {
689 2
		return;
690 2
	}
691 2
692
	$handler = elgg_extract('handler', $params, false);
693 2
	if ($handler) {
694
		elgg_deprecated_notice("Using 'handler' in entity menu parameters is deprecated. Use named routes instead.", '3.0');
695 2
696 2
		$edit_url = "$handler/edit/{$entity->guid}";
697 2
698
		if (elgg_action_exists("$handler/delete")) {
699
			$action = "$handler/delete";
700
		} else {
701 2
			$action = "entity/delete";
702
		}
703 2
		$delete_url = elgg_generate_action_url($action, [
704
			'guid' => $entity->guid,
705
		]);
706
	} else {
707 2
		$edit_url = elgg_generate_entity_url($entity, 'edit');
708
		$delete_url = elgg_generate_action_url('entity/delete', [
709
			'guid' => $entity->guid,
710
		]);
711
	}
712
713
	if ($entity->canEdit() && $edit_url) {
714
		$return[] = \ElggMenuItem::factory([
715
			'name' => 'edit',
716
			'icon' => 'edit',
717
			'text' => elgg_echo('edit'),
718
			'title' => elgg_echo('edit:this'),
719
			'href' => $edit_url,
720 1
			'priority' => 900,
721 1
		]);
722
	}
723
724
	if ($entity->canDelete() && $delete_url) {
725 1
		$return[] = \ElggMenuItem::factory([
726
			'name' => 'delete',
727
			'icon' => 'delete',
728 1
			'text' => elgg_echo('delete'),
729 1
			'title' => elgg_echo('delete:this'),
730 1
			'href' => $delete_url,
731 1
			'confirm' => elgg_echo('deleteconfirm'),
732 1
			'priority' => 950,
733
		]);
734
	}
735 1
736 1
	return $return;
737 1
}
738
739 1
/**
740 1
 * Moves default menu items into a dropdown
741
 *
742
 * @param \Elgg\Hook $hook 'prepare', 'menu:entity'|'menu:river'
743
 *
744
 * @return void|ElggMenuItem[]
745
 *
746
 * @access private
747
 */
748
function _elgg_menu_transform_to_dropdown(\Elgg\Hook $hook) {
749
	$result = $hook->getValue();
750
	
751
	$items = elgg_extract('default', $result);
752
	if (empty($items)) {
753 1
		return;
754 1
	}
755 1
		
756
	$result['default'] = [
757 1
		\ElggMenuItem::factory([
758 1
			'name' => 'entity-menu-toggle',
759
			'icon' => 'ellipsis-v',
760
			'href' => false,
761
			'text' => '',
762
			'child_menu' => [
763
				'display' => 'dropdown',
764
				'data-position' => json_encode([
765
					'at' => 'right bottom',
766
					'my' => 'right top',
767
					'collision' => 'fit fit',
768
				]),
769
				'class' => "elgg-{$hook->getParam('name')}-dropdown-menu",
770
			],
771 1
			'children' => $items,
772
		]),
773
	];
774
	
775
	return $result;
776
}
777
778
/**
779
 * Entity navigation menu is previous/next link for an entity
780
 *
781
 * @param \Elgg\Hook $hook 'register' 'menu:entity_navigation'
782
 *
783
 * @return void|ElggMenuItem[]
784
 *
785
 * @access private
786
 */
787
function _elgg_entity_navigation_menu_setup(\Elgg\Hook $hook) {
788
	$entity = $hook->getEntityParam();
789
	if (!$entity) {
790
		return;
791
	}
792
793
	$return = $hook->getValue();
794
795
	$options = [
796
		'type' => $entity->getType(),
797
		'subtype' => $entity->getSubtype(),
798
		'container_guid' => $entity->container_guid,
799
		'wheres' => ["e.guid != {$entity->guid}"],
800
		'limit' => 1,
801
	];
802
803
	$previous_options = $options;
804
	$previous_options['created_time_upper'] = $entity->time_created;
805
	$previous_options['order_by'] = 'e.time_created DESC, e.guid DESC';
806
807
	$previous = elgg_get_entities($previous_options);
808
	if ($previous) {
809
		$previous = $previous[0];
810
		$return[] = \ElggMenuItem::factory([
811
			'name' => 'previous',
812
			'text' => elgg_echo('previous'),
813
			'href' => $previous->getUrl(),
814
			'title' => $previous->getDisplayName(),
815
			'icon' => 'angle-double-left',
816
			'link_class' => 'elgg-button elgg-button-outline',
817
			'priority' => 100,
818
		]);
819
	}
820
	
821
	$next_options = $options;
822
	$next_options['created_time_lower'] = $entity->time_created;
823
	$next_options['order_by'] = 'e.time_created ASC, e.guid ASC';
824
	
825
	$next = elgg_get_entities($next_options);
826
	if ($next) {
827
		$next = $next[0];
828
		$return[] = \ElggMenuItem::factory([
829
			'name' => 'next',
830
			'text' => elgg_echo('next'),
831
			'href' => $next->getUrl(),
832
			'title' => $next->getDisplayName(),
833
			'icon_alt' => 'angle-double-right',
834
			'link_class' => 'elgg-button elgg-button-outline',
835
			'priority' => 800,
836
		]);
837 1
	}
838 1
	
839 1
	return $return;
840 1
}
841 1
842 1
/**
843
 * Widget menu is a set of widget controls
844
 *
845
 * @param string         $hook   'register'
846 1
 * @param string         $type   'menu:widget'
847 1
 * @param ElggMenuItem[] $return current return value
848 1
 * @param array          $params supplied params
849 1
 *
850 1
 * @return void|ElggMenuItem[]
851
 *
852
 * @access private
853 1
 */
854
function _elgg_widget_menu_setup($hook, $type, $return, $params) {
2 ignored issues
show
Unused Code introduced by Cash Costello
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

854
function _elgg_widget_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

854
function _elgg_widget_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
855
856
	$widget = elgg_extract('entity', $params);
857
	if (!($widget instanceof \ElggWidget)) {
858
		return;
859
	}
860
	
861
	if ($widget->canDelete()) {
862
		$return[] = \ElggMenuItem::factory([
863
			'name' => 'delete',
864
			'text' => elgg_view_icon('delete-alt'),
865
			'title' => elgg_echo('widget:delete', [$widget->getTitle()]),
866
			'href' => "action/widgets/delete?widget_guid=$widget->guid",
867
			'is_action' => true,
868
			'link_class' => 'elgg-widget-delete-button',
869
			'id' => "elgg-widget-delete-button-$widget->guid",
870 1
			'data-elgg-widget-type' => $widget->handler,
871 1
			'priority' => 900,
872
		]);
873
	}
874
	
875
	$show_edit = elgg_extract('show_edit', $params, $widget->canEdit());
876
	if ($show_edit) {
877
		$return[] = \ElggMenuItem::factory([
878
			'name' => 'settings',
879
			'text' => elgg_view_icon('settings-alt'),
880
			'title' => elgg_echo('widget:edit'),
881
			'href' => "#widget-edit-$widget->guid",
882
			'link_class' => "elgg-widget-edit-button",
883
			'rel' => 'toggle',
884
			'priority' => 800,
885
		]);
886
	}
887
888
	return $return;
889
}
890
891
/**
892
 * Add the register and forgot password links to login menu
893
 *
894
 * @param string         $hook   'register'
895
 * @param string         $type   'menu:login'
896
 * @param ElggMenuItem[] $return current return value
897
 * @param array          $params supplied params
898
 *
899
 * @return ElggMenuItem[]
900
 *
901
 * @access private
902
 */
903 31
function _elgg_login_menu_setup($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by Cash Costello
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

903
function _elgg_login_menu_setup($hook, $type, $return, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

903
function _elgg_login_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Cash Costello
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

903
function _elgg_login_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
904
905 31
	if (_elgg_config()->allow_registration) {
906 31
		$return[] = \ElggMenuItem::factory([
907
			'name' => 'register',
908 31
			'href' => elgg_get_registration_url(),
909
			'text' => elgg_echo('register'),
910 31
			'link_class' => 'registration_link',
911 31
		]);
912 31
	}
913 31
914 31
	$return[] = \ElggMenuItem::factory([
915 31
		'name' => 'forgotpassword',
916 31
		'href' => elgg_generate_url('account:password:reset'),
917
		'text' => elgg_echo('user:password:lost'),
918 31
		'link_class' => 'forgot_link',
919
	]);
920 31
921 31
	return $return;
922 31
}
923 31
924 31
/**
925 31
 * Add the RSS link to the menu
926 31
 *
927 31
 * @param string         $hook   'register'
928
 * @param string         $type   'menu:footer'
929
 * @param ElggMenuItem[] $return current return value
930
 * @param array          $params supplied params
931 31
 *
932
 * @return void|ElggMenuItem[]
933
 *
934
 * @access private
935 31
 */
936 31
function _elgg_rss_menu_setup($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by Jeroen Dalsem
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

936
function _elgg_rss_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Jeroen Dalsem
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

936
function _elgg_rss_menu_setup($hook, $type, $return, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Jeroen Dalsem
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

936
function _elgg_rss_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
937
938
	if (!elgg_is_logged_in()) {
939
		return;
940
	}
941
	
942
	if (!_elgg_has_rss_link()) {
943
		return;
944
	}
945
	
946
	if (_elgg_config()->disable_rss) {
947
		return;
948
	}
949
950
	$return[] = ElggMenuItem::factory([
951
		'name' => 'rss',
952
		'text' => elgg_echo('feed:rss'),
953
		'icon' => 'rss',
954
		'href' => elgg_http_add_url_query_elements(current_page_url(), [
955
			'view' => 'rss',
956
		]),
957
		'title' => elgg_echo('feed:rss:title'),
958
	]);
959
960
	return $return;
961
}
962 18
963 18
/**
964
 * Navigation initialization
965
 *
966
 * @return void
967
 *
968
 * @access private
969
 */
970
function _elgg_nav_init() {
971
	elgg_register_plugin_hook_handler('prepare', 'breadcrumbs', 'elgg_prepare_breadcrumbs');
972
973
	elgg_register_plugin_hook_handler('prepare', 'menu:site', '_elgg_site_menu_setup', 999);
974