Elgg  Version master
Service.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menu;
4 
5 use Elgg\Config;
7 
11 class Service {
12 
16  protected $menus = [];
17 
24  public function __construct(protected EventsService $events, protected Config $config) {
25  }
26 
37  public function getMenu(string $name, array $params = []): Menu {
38  return $this->prepareMenu($this->getUnpreparedMenu($name, $params));
39  }
40 
49  public function getUnpreparedMenu(string $name, array $params = []): UnpreparedMenu {
50  $items = $this->prepareMenuItems(elgg_extract('items', $params, []));
51  unset($params['items']);
52 
53  $registered_items = elgg_extract($name, $this->menus);
54  if (is_array($registered_items)) {
55  $items->merge($registered_items);
56  }
57 
58  $params['name'] = $name;
59 
60  $params = $this->events->triggerResults('parameters', "menu:{$name}", $params, $params);
61 
62  if (!isset($params['sort_by'])) {
63  $params['sort_by'] = 'priority';
64  }
65 
66  // trigger specific menu events
67  $entity = elgg_extract('entity', $params);
68  if ($entity instanceof \ElggEntity) {
69  $items = $this->events->triggerResults('register', "menu:{$name}:{$entity->type}:{$entity->subtype}", $params, $items);
70  }
71 
72  $annotation = elgg_extract('annotation', $params);
73  if ($annotation instanceof \ElggAnnotation) {
74  $items = $this->events->triggerResults('register', "menu:{$name}:{$annotation->getType()}:{$annotation->getSubtype()}", $params, $items);
75  }
76 
77  $relationship = elgg_extract('relationship', $params);
78  if ($relationship instanceof \ElggRelationship) {
79  $items = $this->events->triggerResults('register', "menu:{$name}:{$relationship->getType()}:{$relationship->getSubtype()}", $params, $items);
80  }
81 
82  // trigger generic menu event
83  $items = $this->events->triggerResults('register', "menu:{$name}", $params, $items);
84 
85  return new UnpreparedMenu($params, $items);
86  }
87 
95  public function prepareMenu(UnpreparedMenu $menu): Menu {
96  $name = $menu->getName();
97  $params = $menu->getParams();
98  $sort_by = $menu->getSortBy();
99  $selected_menu_item_name = elgg_extract('selected_item_name', $params, '');
100 
101  $builder = new \ElggMenuBuilder($menu->getItems());
102  $builder->setSelected($selected_menu_item_name);
103 
104  $params['menu'] = $builder->getMenu($sort_by);
105  $params['selected_item'] = $builder->getSelected();
106 
107  // trigger specific menu events
108  $entity = elgg_extract('entity', $params);
109  if ($entity instanceof \ElggEntity) {
110  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$entity->type}:{$entity->subtype}", $params, $params['menu']);
111  }
112 
113  $annotation = elgg_extract('annotation', $params);
114  if ($annotation instanceof \ElggAnnotation) {
115  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$annotation->getType()}:{$annotation->getSubtype()}", $params, $params['menu']);
116  }
117 
118  $relationship = elgg_extract('relationship', $params);
119  if ($relationship instanceof \ElggRelationship) {
120  $params['menu'] = $this->events->triggerResults('prepare', "menu:{$name}:{$relationship->getType()}:{$relationship->getSubtype()}", $params, $params['menu']);
121  }
122 
123  // trigger generic menu event
124  $params['menu'] = $this->events->triggerResults('prepare', "menu:$name", $params, $params['menu']);
125 
126  $params['menu'] = $this->prepareVerticalMenu($params['menu'], $params);
127  $params['menu'] = $this->prepareDropdownMenu($params['menu'], $params);
128  $params['menu'] = $this->prepareSelectedParents($params['menu'], $params);
129  $params['menu'] = $this->prepareItemContentsView($params['menu'], $params);
130 
131  return new Menu($params);
132  }
133 
143  if (elgg_extract('prepare_vertical', $params) !== true) {
144  return $menu;
145  }
146 
147  $prepare = function(\ElggMenuItem $menu_item) use (&$prepare) {
148  $child_menu_vars = $menu_item->getChildMenuOptions();
149  if (empty($child_menu_vars['display'])) {
150  $child_menu_vars['display'] = 'toggle';
151  }
152 
153  $menu_item->setChildMenuOptions($child_menu_vars);
154 
155  foreach ($menu_item->getChildren() as $child_menu_item) {
156  $prepare($child_menu_item);
157  }
158  };
159 
160  /* @var $section MenuSection */
161  foreach ($menu as $section) {
162  /* @var $menu_item \ElggMenuItem */
163  foreach ($section as $menu_item) {
164  $prepare($menu_item);
165  }
166  }
167 
168  return $menu;
169  }
170 
180  $selected_item = elgg_extract('selected_item', $params);
181  if (!$selected_item instanceof \ElggMenuItem) {
182  return $menu;
183  }
184 
185  $parent = $selected_item->getParent();
186  while ($parent instanceof \ElggMenuItem) {
187  $parent->setSelected();
188  $parent->addItemClass('elgg-has-selected-child');
189  $parent = $parent->getParent();
190  }
191 
192  return $menu;
193  }
194 
204  if (elgg_extract('prepare_dropdown', $params) !== true) {
205  return $menu;
206  }
207 
208  $items = $menu->getItems('default');
209  if (empty($items)) {
210  return $menu;
211  }
212 
213  $menu_name = elgg_extract('name', $params);
214  $menu->getSection('default')->fill([
216  'name' => 'entity-menu-toggle',
217  'icon' => 'ellipsis-v',
218  'href' => false,
219  'text' => '',
220  'title' => elgg_echo('more'),
221  'child_menu' => [
222  'display' => 'dropdown',
223  'data-position' => json_encode([
224  'at' => 'right bottom',
225  'my' => 'right top',
226  'collision' => 'fit fit',
227  ]),
228  'class' => "elgg-{$menu_name}-dropdown-menu",
229  ],
230  'children' => $items,
231  ]),
232  ]);
233 
234  return $menu;
235  }
236 
247  $item_contents_view = elgg_extract('item_contents_view', $params, 'navigation/menu/elements/item/url');
248 
249  $prepare = function(\ElggMenuItem $menu_item) use (&$prepare, $item_contents_view) {
250  if (!$menu_item->hasItemContentsView()) {
251  $menu_item->setItemContentsView($item_contents_view);
252  }
253 
254  foreach ($menu_item->getChildren() as $child_menu_item) {
255  $prepare($child_menu_item);
256  }
257  };
258 
259  /* @var $section MenuSection */
260  foreach ($menu as $section) {
261  /* @var $menu_item \ElggMenuItem */
262  foreach ($section as $menu_item) {
263  $prepare($menu_item);
264  }
265  }
266 
267  return $menu;
268  }
269 
282  public function combineMenus(array $names = [], array $params = [], string $new_name = ''): UnpreparedMenu {
283  if (!$new_name) {
284  $new_name = implode('__', $names);
285  }
286 
287  $all_items = new MenuItems();
288 
289  foreach ($names as $name) {
290  $items = $this->getUnpreparedMenu($name, $params)->getItems();
291 
292  /* @var $item \ElggMenuItem */
293  foreach ($items as $item) {
294  $section = $item->getSection();
295  if ($section === 'default') {
296  $item->setSection($name);
297  }
298 
299  $item->setData('menu_name', $name);
300 
301  $all_items->add($item);
302  }
303  }
304 
305  $params['name'] = $new_name;
306 
307  return new UnpreparedMenu($params, $all_items);
308  }
309 
317  public function prepareMenuItems(array $items = []): MenuItems {
318  $prepared_items = new MenuItems();
319 
320  foreach ($items as $item) {
321  if (is_array($item)) {
322  $options = $item;
324  }
325 
326  if (!$item instanceof \ElggMenuItem) {
327  continue;
328  }
329 
330  $prepared_items->add($item);
331  }
332 
333  return $prepared_items;
334  }
335 
345  public function registerMenuItem(string $menu_name, \ElggMenuItem $menu_item): void {
346  $this->menus[$menu_name][] = $menu_item;
347  }
348 
358  public function unregisterMenuItem(string $menu_name, string $item_name): ?\ElggMenuItem {
359  if (!isset($this->menus[$menu_name])) {
360  return null;
361  }
362 
363  foreach ($this->menus[$menu_name] as $index => $menu_item) {
364  if ($menu_item->getName() === $item_name) {
365  $item = $this->menus[$menu_name][$index];
366  unset($this->menus[$menu_name][$index]);
367  return $item;
368  }
369  }
370 
371  return null;
372  }
373 
381  public function getAllMenus(): array {
382  return $this->menus;
383  }
384 }
if(!$items) $item
Definition: delete.php:13
prepareDropdownMenu(PreparedMenu $menu, array $params)
Prepares a dropdown menu.
Definition: Service.php:203
getUnpreparedMenu(string $name, array $params=[])
Build an unprepared menu.
Definition: Service.php:49
$params
Saves global plugin settings.
Definition: save.php:13
prepareMenuItems(array $items=[])
Prepare menu items.
Definition: Service.php:317
getSortBy()
Get the designated (or default) sort strategy.
if($id< 1) $annotation
Definition: delete.php:11
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$relationship
Elgg default relationship view.
Definition: default.php:10
Entity Annotation.
Events service.
getItems($section_id)
Get items in a section.
getAllMenus()
Returns all registered menu items.
Definition: Service.php:381
static factory(array $options)
Create an ElggMenuItem from an associative array.
getMenu(string $name, array $params=[])
Build a full menu, pulling items from configuration and the "register" menu events.
Definition: Service.php:37
prepareItemContentsView(PreparedMenu $menu, array $params)
Set a content view for each menu item based on the default for the menu.
Definition: Service.php:246
Elgg Menu Item.
$items
Definition: delete.php:8
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
getParams()
Get the menu parameters.
prepareSelectedParents(PreparedMenu $menu, array $params)
Marks parents of selected items also as selected.
Definition: Service.php:179
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
Linear set of menu items collected from configuration and the "register" event.
Methods to construct and prepare menus for rendering.
Definition: Service.php:11
getSection($id)
Get menu section.
prepareVerticalMenu(PreparedMenu $menu, array $params)
Prepares a vertical menu by setting the display child menu option to "toggle" if not set...
Definition: Service.php:142
__construct(protected EventsService $events, protected Config $config)
Constructor.
Definition: Service.php:24
$entity
Definition: reset.php:8
getName()
Get the menu name.
Represents a menu that has been broken down into sections, with menu hierarchy trees setup...
$menu
Form body for setting up site menu.
Definition: save.php:7
combineMenus(array $names=[], array $params=[], string $new_name= '')
Combine several menus into one.
Definition: Service.php:282
unregisterMenuItem(string $menu_name, string $item_name)
Remove an item from a menu.
Definition: Service.php:358
getItems()
Get the menu items.
$index
Definition: gallery.php:40
registerMenuItem(string $menu_name,\ElggMenuItem $menu_item)
Register a menu item.
Definition: Service.php:345
A collection of menu items.
Definition: MenuItems.php:10
A complete menu, sorted, filtered by the "prepare" event, and split into sections.
Definition: Menu.php:10
prepareMenu(UnpreparedMenu $menu)
Split a menu into sections, and pass it through the "prepare" event.
Definition: Service.php:95