Elgg  Version 1.11
navigation.php
Go to the documentation of this file.
1 <?php
92 function elgg_register_menu_item($menu_name, $menu_item) {
94 
95  if (!isset($CONFIG->menus[$menu_name])) {
96  $CONFIG->menus[$menu_name] = array();
97  }
98 
99  if (is_array($menu_item)) {
100  $item = \ElggMenuItem::factory($menu_item);
101  if (!$item) {
102  elgg_log("Unable to add menu item '{$menu_item['name']}' to '$menu_name' menu", 'WARNING');
103  return false;
104  }
105  } else {
106  $item = $menu_item;
107  }
108 
109  $CONFIG->menus[$menu_name][] = $item;
110  return true;
111 }
112 
122 function elgg_unregister_menu_item($menu_name, $item_name) {
123  global $CONFIG;
124 
125  if (!isset($CONFIG->menus[$menu_name])) {
126  return null;
127  }
128 
129  foreach ($CONFIG->menus[$menu_name] as $index => $menu_object) {
130  /* @var \ElggMenuItem $menu_object */
131  if ($menu_object->getName() == $item_name) {
132  $item = $CONFIG->menus[$menu_name][$index];
133  unset($CONFIG->menus[$menu_name][$index]);
134  return $item;
135  }
136  }
137 
138  return null;
139 }
140 
150 function elgg_is_menu_item_registered($menu_name, $item_name) {
151  global $CONFIG;
152 
153  if (!isset($CONFIG->menus[$menu_name])) {
154  return false;
155  }
156 
157  foreach ($CONFIG->menus[$menu_name] as $menu_object) {
158  /* @var \ElggMenuItem $menu_object */
159  if ($menu_object->getName() == $item_name) {
160  return true;
161  }
162  }
163 
164  return false;
165 }
166 
176 function elgg_get_menu_item($menu_name, $item_name) {
177  global $CONFIG;
178 
179  if (!isset($CONFIG->menus[$menu_name])) {
180  return null;
181  }
182 
183  foreach ($CONFIG->menus[$menu_name] as $index => $menu_object) {
184  /* @var \ElggMenuItem $menu_object */
185  if ($menu_object->getName() == $item_name) {
186  return $CONFIG->menus[$menu_name][$index];
187  }
188  }
189 
190  return null;
191 }
192 
207 function elgg_register_title_button($handler = null, $name = 'add') {
208  if (elgg_is_logged_in()) {
209 
210  if (!$handler) {
212  }
213 
215  if (!$owner) {
216  // no owns the page so this is probably an all site list page
218  }
219  if ($owner && $owner->canWriteToContainer()) {
220  $guid = $owner->getGUID();
221  elgg_register_menu_item('title', array(
222  'name' => $name,
223  'href' => "$handler/$name/$guid",
224  'text' => elgg_echo("$handler:$name"),
225  'link_class' => 'elgg-button elgg-button-action',
226  ));
227  }
228  }
229 }
230 
244 function elgg_push_breadcrumb($title, $link = null) {
245  global $CONFIG;
246  if (!isset($CONFIG->breadcrumbs)) {
247  $CONFIG->breadcrumbs = array();
248  }
249 
250  $CONFIG->breadcrumbs[] = array('title' => $title, 'link' => $link);
251 }
252 
260  global $CONFIG;
261 
262  if (empty($CONFIG->breadcrumbs) || !is_array($CONFIG->breadcrumbs)) {
263  return array();
264  }
265  return array_pop($CONFIG->breadcrumbs);
266 }
267 
279  global $CONFIG;
280 
281  // if no crumbs set, still allow hook to populate it
282  if (isset($CONFIG->breadcrumbs) && is_array($CONFIG->breadcrumbs)) {
283  $breadcrumbs = $CONFIG->breadcrumbs;
284  } else {
285  $breadcrumbs = array();
286  }
287 
288  $params = array(
289  'breadcrumbs' => $breadcrumbs,
290  );
291  $breadcrumbs = elgg_trigger_plugin_hook('prepare', 'breadcrumbs', $params, $breadcrumbs);
292  if (!is_array($breadcrumbs)) {
293  return array();
294  }
295 
296  return $breadcrumbs;
297 }
298 
311 function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, $params) {
312  foreach (array_keys($breadcrumbs) as $i) {
313  $breadcrumbs[$i]['title'] = elgg_get_excerpt($breadcrumbs[$i]['title'], 100);
314  }
315  return $breadcrumbs;
316 }
317 
326 
327  $featured_menu_names = elgg_get_config('site_featured_menu_names');
328  $custom_menu_items = elgg_get_config('site_custom_menu_items');
330  // we have featured or custom menu items
331 
332  $registered = $return['default'];
333  /* @var \ElggMenuItem[] $registered */
334 
335  // set up featured menu items
336  $featured = array();
337  foreach ($featured_menu_names as $name) {
338  foreach ($registered as $index => $item) {
339  if ($item->getName() == $name) {
340  $featured[] = $item;
341  unset($registered[$index]);
342  }
343  }
344  }
345 
346  // add custom menu items
347  $n = 1;
348  foreach ($custom_menu_items as $title => $url) {
349  $item = new \ElggMenuItem("custom$n", $title, $url);
350  $featured[] = $item;
351  $n++;
352  }
353 
354  $return['default'] = $featured;
355  if (count($registered) > 0) {
356  $return['more'] = $registered;
357  }
358  } else {
359  // no featured menu items set
360  $max_display_items = 5;
361 
362  // the first n are shown, rest added to more list
363  // if only one item on more menu, stick it with the rest
364  $num_menu_items = count($return['default']);
365  if ($num_menu_items > ($max_display_items + 1)) {
366  $return['more'] = array_splice($return['default'], $max_display_items);
367  }
368  }
369 
370  // check if we have anything selected
371  $selected = false;
372  foreach ($return as $section) {
373  /* @var \ElggMenuItem[] $section */
374 
375  foreach ($section as $item) {
376  if ($item->getSelected()) {
377  $selected = true;
378  break 2;
379  }
380  }
381  }
382 
383  if (!$selected) {
384  // nothing selected, match name to context or match url
385  $current_url = current_page_url();
386  foreach ($return as $section_name => $section) {
387  foreach ($section as $key => $item) {
388  // only highlight internal links
389  if (strpos($item->getHref(), elgg_get_site_url()) === 0) {
390  if ($item->getName() == elgg_get_context()) {
391  $return[$section_name][$key]->setSelected(true);
392  break 2;
393  }
394  if ($item->getHref() == $current_url) {
395  $return[$section_name][$key]->setSelected(true);
396  break 2;
397  }
398  }
399  }
400  }
401  }
402 
403  return $return;
404 }
405 
411  if (elgg_is_logged_in()) {
412  $item = $params['item'];
413  /* @var \ElggRiverItem $item */
414  $object = $item->getObjectEntity();
415  // add comment link but annotations cannot be commented on
416  if ($item->annotation_id == 0) {
417  if ($object->canComment()) {
418  $options = array(
419  'name' => 'comment',
420  'href' => "#comments-add-$object->guid",
421  'text' => elgg_view_icon('speech-bubble'),
422  'title' => elgg_echo('comment:this'),
423  'rel' => 'toggle',
424  'priority' => 50,
425  );
427  }
428  }
429 
430  if (elgg_is_admin_logged_in()) {
431  $options = array(
432  'name' => 'delete',
433  'href' => elgg_add_action_tokens_to_url("action/river/delete?id=$item->id"),
434  'text' => elgg_view_icon('delete'),
435  'title' => elgg_echo('river:delete'),
436  'confirm' => elgg_echo('deleteconfirm'),
437  'priority' => 200,
438  );
440  }
441  }
442 
443  return $return;
444 }
445 
451  if (elgg_in_context('widgets')) {
452  return $return;
453  }
454 
455  $entity = $params['entity'];
456  /* @var \ElggEntity $entity */
457  $handler = elgg_extract('handler', $params, false);
458 
459  // access
460  if (elgg_is_logged_in()) {
461  $access = elgg_view('output/access', array('entity' => $entity));
462  $options = array(
463  'name' => 'access',
464  'text' => $access,
465  'href' => false,
466  'priority' => 100,
467  );
469  }
470 
471  if ($entity->canEdit() && $handler) {
472  // edit link
473  $options = array(
474  'name' => 'edit',
475  'text' => elgg_echo('edit'),
476  'title' => elgg_echo('edit:this'),
477  'href' => "$handler/edit/{$entity->getGUID()}",
478  'priority' => 200,
479  );
481 
482  // delete link
483  $options = array(
484  'name' => 'delete',
485  'text' => elgg_view_icon('delete'),
486  'title' => elgg_echo('delete:this'),
487  'href' => "action/$handler/delete?guid={$entity->getGUID()}",
488  'confirm' => elgg_echo('deleteconfirm'),
489  'priority' => 300,
490  );
492  }
493 
494  return $return;
495 }
496 
502 
503  $widget = $params['entity'];
504  /* @var \ElggWidget $widget */
505  $show_edit = elgg_extract('show_edit', $params, true);
506 
507  $collapse = array(
508  'name' => 'collapse',
509  'text' => ' ',
510  'href' => "#elgg-widget-content-$widget->guid",
511  'link_class' => 'elgg-widget-collapse-button',
512  'rel' => 'toggle',
513  'priority' => 1,
514  );
515  $return[] = \ElggMenuItem::factory($collapse);
516 
517  if ($widget->canEdit()) {
518  $delete = array(
519  'name' => 'delete',
520  'text' => elgg_view_icon('delete-alt'),
521  'title' => elgg_echo('widget:delete', array($widget->getTitle())),
522  'href' => "action/widgets/delete?widget_guid=$widget->guid",
523  'is_action' => true,
524  'link_class' => 'elgg-widget-delete-button',
525  'id' => "elgg-widget-delete-button-$widget->guid",
526  'data-elgg-widget-type' => $widget->handler,
527  'priority' => 900,
528  );
529  $return[] = \ElggMenuItem::factory($delete);
530 
531  if ($show_edit) {
532  $edit = array(
533  'name' => 'settings',
534  'text' => elgg_view_icon('settings-alt'),
535  'title' => elgg_echo('widget:edit'),
536  'href' => "#widget-edit-$widget->guid",
537  'link_class' => "elgg-widget-edit-button",
538  'rel' => 'toggle',
539  'priority' => 800,
540  );
541  $return[] = \ElggMenuItem::factory($edit);
542  }
543  }
544 
545  return $return;
546 }
547 
553 
554  if (elgg_get_config('allow_registration')) {
556  'name' => 'register',
557  'href' => 'register',
558  'text' => elgg_echo('register'),
559  'link_class' => 'registration_link',
560  ));
561  }
562 
564  'name' => 'forgotpassword',
565  'href' => 'forgotpassword',
566  'text' => elgg_echo('user:password:lost'),
567  'link_class' => 'forgot_link',
568  ));
569 
570  return $return;
571 }
572 
573 
578 function _elgg_nav_init() {
579  elgg_register_plugin_hook_handler('prepare', 'breadcrumbs', 'elgg_prepare_breadcrumbs');
580 
581  elgg_register_plugin_hook_handler('prepare', 'menu:site', '_elgg_site_menu_setup');
582  elgg_register_plugin_hook_handler('register', 'menu:river', '_elgg_river_menu_setup');
583  elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_entity_menu_setup');
584  elgg_register_plugin_hook_handler('register', 'menu:widget', '_elgg_widget_menu_setup');
585  elgg_register_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');
586 
587  elgg_register_plugin_hook_handler('public_pages', 'walled_garden', '_elgg_nav_public_pages');
588 
590  'name' => 'powered',
591  'text' => elgg_echo("elgg:powered"),
592  'href' => 'http://elgg.org',
593  'title' => 'Elgg ' . elgg_get_version(true),
594  'section' => 'meta',
595  )));
596 
597  elgg_register_ajax_view('navigation/menu/user_hover/contents');
598 }
599 
612 function _elgg_nav_public_pages($hook_name, $entity_type, $return_value, $params) {
613  if (is_array($return_value)) {
614  $return_value[] = 'navigation/menu/user_hover/contents';
615  }
616 
617  return $return_value;
618 }
619 
620 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
621  $events->registerHandler('init', 'system', '_elgg_nav_init');
622 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$custom_menu_items
Definition: save.php:21
$n
Profile fields.
Definition: list.php:9
elgg_push_breadcrumb($title, $link=null)
Adds a breadcrumb to the breadcrumbs stack.
Definition: navigation.php:244
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:51
elgg_unregister_menu_item($menu_name, $item_name)
Remove an item from a menu.
Definition: navigation.php:122
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:1059
_elgg_login_menu_setup($hook, $type, $return, $params)
Add the register and forgot password links to login menu private.
Definition: navigation.php:552
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:60
elgg_view_icon($name, $vars=array())
View one of the elgg sprite icons.
Definition: views.php:1403
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:65
$object
Definition: upgrade.php:12
$widget
Definition: delete.php:9
elgg_get_menu_item($menu_name, $item_name)
Get a menu item registered for a menu.
Definition: navigation.php:176
$return
Definition: opendd.php:15
$guid
Removes an admin notice.
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1246
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:703
_elgg_widget_menu_setup($hook, $type, $return, $params)
Widget menu is a set of widget controls private.
Definition: navigation.php:501
$url
Definition: exceptions.php:24
$title
Definition: save.php:24
$params
Definition: login.php:72
$options
Definition: index.php:14
$featured_menu_names
Definition: save.php:16
static factory($options)
Create an ElggMenuItem from an associative array.
elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, $params)
Hook handler to turn titles into 100-character excerpts.
Definition: navigation.php:311
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$owner
Definition: crop.php:8
$key
Definition: summary.php:34
$item
Definition: item.php:12
global $CONFIG
$num_menu_items
Definition: save.php:20
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:250
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:775
elgg_get_breadcrumbs()
Returns all breadcrumbs as an array of array(&#39;title&#39; => &#39;Title&#39;, &#39;link&#39; => &#39;URL&#39;) ...
Definition: navigation.php:278
elgg_register_title_button($handler=null, $name= 'add')
Convenience function for registering a button to the title menu.
Definition: navigation.php:207
elgg_get_context()
Get the current context.
Definition: pageowner.php:213
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:53
$type
Definition: add.php:8
elgg_register_menu_item($menu_name, $menu_item)
Register an item for an Elgg menu.
Definition: navigation.php:92
elgg_view($view, $vars=array(), $bypass=false, $ignored=false, $viewtype= '')
Return a parsed view.
Definition: views.php:354
_elgg_entity_menu_setup($hook, $type, $return, $params)
Entity menu is list of links and info on any entity private.
Definition: navigation.php:450
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:933
_elgg_site_menu_setup($hook, $type, $return, $params)
Set up the site menu.
Definition: navigation.php:325
elgg_get_version($human_readable=false)
Get the current Elgg version information.
Definition: elgglib.php:976
_elgg_nav_init()
Navigation initialization private.
Definition: navigation.php:578
elgg_register_ajax_view($view)
Register a view to be available for ajax calls.
Definition: views.php:208
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:72
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
$handler
Definition: add.php:10
$entity
Definition: delete.php:10
_elgg_nav_public_pages($hook_name, $entity_type, $return_value, $params)
Extend public pages.
Definition: navigation.php:612
_elgg_river_menu_setup($hook, $type, $return, $params)
Add the comment and like links to river actions menu private.
Definition: navigation.php:410
elgg_pop_breadcrumb()
Removes last breadcrumb entry.
Definition: navigation.php:259
elgg_is_menu_item_registered($menu_name, $item_name)
Check if a menu item has been registered.
Definition: navigation.php:150
$access
Definition: save.php:15