Elgg  Version 2.3
Service.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Menu;
3 
8 
12 class Service {
13 
17  private $hooks;
18 
22  private $config;
23 
32  public function __construct(PluginHooksService $hooks, Config $config) {
33  $this->hooks = $hooks;
34  $this->config = $config;
35  }
36 
47  public function getMenu($name, array $params = []) {
48  return $this->prepareMenu($this->getUnpreparedMenu($name, $params));
49  }
50 
59  public function getUnpreparedMenu($name, array $params = []) {
60  $menus = $this->config->getVolatile('menus');
61 
62  $items = $this->prepareMenuItems(elgg_extract('items', $params, []));
63  unset($params['items']);
64 
65  if ($menus && isset($menus[$name])) {
66  $registered_items = elgg_extract($name, $menus, []);
67  $items = array_merge($items, $registered_items);
68  }
69 
70  $params['name'] = $name;
71 
72  $params = $this->hooks->trigger('parameters', "menu:$name", $params, $params);
73 
74  if (!isset($params['sort_by'])) {
75  $params['sort_by'] = 'text';
76  }
77 
78  $items = $this->hooks->trigger('register', "menu:$name", $params, $items);
79 
80  return new UnpreparedMenu($params, $items);
81  }
82 
90  public function prepareMenu(UnpreparedMenu $menu) {
91  $name = $menu->getName();
92  $params = $menu->getParams();
93  $sort_by = $menu->getSortBy();
94 
95  $builder = new ElggMenuBuilder($menu->getItems());
96  $params['menu'] = $builder->getMenu($sort_by);
97  $params['selected_item'] = $builder->getSelected();
98 
99  $params['menu'] = $this->hooks->trigger('prepare', "menu:$name", $params, $params['menu']);
100 
101  return new Menu($params);
102  }
103 
116  function combineMenus(array $names = [], array $params = [], $new_name = '') {
117  if (!$new_name) {
118  $new_name = implode('__' , $names);
119  }
120 
121  $all_items = [];
122  foreach ($names as $name) {
123  $items = $this->getUnpreparedMenu($name, $params)->getItems();
124 
125  foreach ($items as $item) {
126  $section = $item->getSection();
127  if ($section == 'default') {
128  $item->setSection($name);
129  }
130  $item->setData('menu_name', $name);
131  $all_items[] = $item;
132  }
133  }
134 
135  $params['name'] = $new_name;
136 
137  return new UnpreparedMenu($params, $all_items);
138  }
139 
146  public function prepareMenuItems(array $items = []) {
147  $prepared_items = [];
148 
149  foreach ($items as $item) {
150  if (is_array($item)) {
151  $options = $item;
153  }
154 
155  if (!$item instanceof ElggMenuItem) {
156  continue;
157  }
158 
159  $prepared_items[] = $item;
160  }
161 
162  return $prepared_items;
163  }
164 }
prepareMenuItems(array $items=[])
Prepare menu items.
Definition: Service.php:146
getSortBy()
Get the designated (or default) sort strategy.
if(!$items) $item
Definition: delete.php:17
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
__construct(PluginHooksService $hooks, Config $config)
Constructor.
Definition: Service.php:32
getMenu($name, array $params=[])
Build a full menu, pulling items from configuration and the "register" menu hooks.
Definition: Service.php:47
Access to configuration values.
Definition: Config.php:11
$items
Definition: delete.php:11
getUnpreparedMenu($name, array $params=[])
Build an unprepared menu.
Definition: Service.php:59
$options
Elgg admin footer.
Definition: footer.php:6
$params
Definition: login.php:72
getParams()
Get the menu parameters.
static factory($options)
Create an ElggMenuItem from an associative array.
getSelected()
Get the selected menu item.
Linear set of menu items collected from configuration and the "register" hook.
Methods to construct and prepare menus for rendering.
Definition: Service.php:12
getName()
Get the menu name.
combineMenus(array $names=[], array $params=[], $new_name= '')
Combine several menus into one.
Definition: Service.php:116
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1375
getItems()
Get the menu items.
$menu
Definition: default.php:19
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
A complete menu, sorted, filtered by the "prepare" hook, and split into sections. ...
Definition: Menu.php:11
prepareMenu(UnpreparedMenu $menu)
Split a menu into sections, and pass it through the "prepare" hook.
Definition: Service.php:90