Elgg  Version 3.0
Service.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menu;
4 
7 use Elgg\Config;
9 use ElggMenuItem;
10 
14 class Service {
15 
19  private $hooks;
20 
24  private $config;
25 
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  $items = $this->prepareMenuItems(elgg_extract('items', $params, []));
61  unset($params['items']);
62 
63  $registered_items = elgg_extract($name, $this->config->menus);
64  if (is_array($registered_items)) {
65  $items->merge($registered_items);
66  }
67 
68  $params['name'] = $name;
69 
70  $params = $this->hooks->trigger('parameters', "menu:$name", $params, $params);
71 
72  if (!isset($params['sort_by'])) {
73  $params['sort_by'] = 'priority';
74  }
75 
76  $items = $this->hooks->trigger('register', "menu:$name", $params, $items);
77 
78  return new UnpreparedMenu($params, $items);
79  }
80 
88  public function prepareMenu(UnpreparedMenu $menu) {
89  $name = $menu->getName();
90  $params = $menu->getParams();
91  $sort_by = $menu->getSortBy();
92 
93  $builder = new ElggMenuBuilder($menu->getItems());
94  $params['menu'] = $builder->getMenu($sort_by);
95  $params['selected_item'] = $builder->getSelected();
96 
97  $params['menu'] = $this->hooks->trigger('prepare', "menu:$name", $params, $params['menu']);
98 
99  return new Menu($params);
100  }
101 
114  public function combineMenus(array $names = [], array $params = [], $new_name = '') {
115  if (!$new_name) {
116  $new_name = implode('__', $names);
117  }
118 
119  $all_items = new MenuItems();
120 
121  foreach ($names as $name) {
122  $items = $this->getUnpreparedMenu($name, $params)->getItems();
123 
124  foreach ($items as $item) {
125  $section = $item->getSection();
126  if ($section == 'default') {
127  $item->setSection($name);
128  }
129  $item->setData('menu_name', $name);
130 
131  $all_items->add($item);
132  }
133  }
134 
135  $params['name'] = $new_name;
136 
137  return new UnpreparedMenu($params, $all_items);
138  }
139 
147  public function prepareMenuItems($items = []) {
148  $prepared_items = new MenuItems();
149 
150  foreach ($items as $item) {
151  if (is_array($item)) {
152  $options = $item;
154  }
155 
156  if (!$item instanceof ElggMenuItem) {
157  continue;
158  }
159 
160  $prepared_items->add($item);
161  }
162 
163  return $prepared_items;
164  }
165 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
prepareMenuItems($items=[])
Prepare menu items.
Definition: Service.php:147
getSortBy()
Get the designated (or default) sort strategy.
if(!$items) $item
Definition: delete.php:13
__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
$menu
Admin sidebar – just outputs the page menus.
Definition: sidebar.php:6
$items
Definition: delete.php:8
getUnpreparedMenu($name, array $params=[])
Build an unprepared menu.
Definition: Service.php:59
$options
Elgg admin footer.
Definition: footer.php:6
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:14
getName()
Get the menu name.
combineMenus(array $names=[], array $params=[], $new_name= '')
Combine several menus into one.
Definition: Service.php:114
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:1131
getItems()
Get the menu items.
A collection of menu items.
Definition: MenuItems.php:10
A complete menu, sorted, filtered by the "prepare" hook, and split into sections. ...
Definition: Menu.php:10
prepareMenu(UnpreparedMenu $menu)
Split a menu into sections, and pass it through the "prepare" hook.
Definition: Service.php:88