Elgg  Version 5.1
ElggMenuItem.php
Go to the documentation of this file.
1 <?php
13 #[\AllowDynamicProperties]
15 
19  protected $data = [
20  // string Identifier of the menu
21  'name' => '',
22 
23  // array Page contexts this menu item should appear on
24  'contexts' => ['all'],
25 
26  // string Menu section identifier
27  'section' => 'default',
28 
29  // int Smaller priorities float to the top
30  'priority' => 100,
31 
32  // bool Is this the currently selected menu item (null for autodetection)
33  'selected' => null,
34 
35  // string Identifier of this item's parent
36  'parent_name' => '',
37 
38  // boolean Indicator to control if menu item should show if it has no children
39  'show_with_empty_children' => true,
40 
41  // \ElggMenuItem The parent object or null
42  'parent' => null,
43 
44  // array Array of children objects or empty array
45  'children' => [],
46 
47  // array An array of options for child menu of the parent item
48  'child_menu' => [],
49 
50  // array Classes to apply to the li tag
51  'itemClass' => [],
52 
53  // array Classes to apply to the anchor tag
54  'linkClass' => [],
55 
56  // array AMD modules required by this menu item
57  'deps' => [],
58 
59  // which view should be used to output the menu item contents
60  'item_contents_view' => null,
61  ];
62 
66  protected $text;
67 
71  protected $href = null;
72 
76  protected $title = false;
77 
81  protected $confirm = '';
82 
90  public function __construct($name, $text, $href) {
91  $this->text = $text;
92  if ($href) {
93  $this->href = elgg_normalize_url($href);
94  } else {
95  $this->href = $href;
96  }
97 
98  $this->data['name'] = $name;
99  }
100 
130  public static function factory(array $options): \ElggMenuItem {
131  if (!isset($options['name']) || !isset($options['text'])) {
132  throw new \Elgg\Exceptions\InvalidArgumentException(__METHOD__ . ': $options "name" and "text" are required.');
133  }
134 
135  if (!isset($options['href'])) {
136  $options['href'] = '';
137  }
138 
139  $item = new \ElggMenuItem($options['name'], $options['text'], $options['href']);
140  unset($options['name']);
141  unset($options['text']);
142  unset($options['href']);
143 
144  // special catch in case someone uses context rather than contexts
145  if (isset($options['context'])) {
146  $options['contexts'] = $options['context'];
147  unset($options['context']);
148  }
149 
150  // make sure contexts is set correctly
151  if (isset($options['contexts'])) {
152  $item->setContext($options['contexts']);
153  unset($options['contexts']);
154  }
155 
156  if (isset($options['link_class'])) {
157  $item->setLinkClass($options['link_class']);
158  unset($options['link_class']);
159  }
160 
161  if (isset($options['item_class'])) {
162  $item->setItemClass($options['item_class']);
163  unset($options['item_class']);
164  }
165 
166  if (isset($options['data']) && is_array($options['data'])) {
167  $item->setData($options['data']);
168  unset($options['data']);
169  }
170 
171  foreach ($options as $key => $value) {
172  if (array_key_exists($key, $item->data)) {
173  $item->data[$key] = $value;
174  } else {
175  $item->$key = $value;
176  }
177  }
178 
179  return $item;
180  }
181 
193  public function setData($key, $value = null) {
194  if (is_array($key)) {
195  $this->data = array_merge($this->data, $key);
196  } else {
197  $this->data[$key] = $value;
198  }
199  }
200 
208  public function getData($key) {
209  return $this->data[$key] ?? null;
210  }
211 
219  public function setName($name) {
220  $this->data['name'] = $name;
221  }
222 
228  public function getName() {
229  return $this->data['name'];
230  }
231 
238  public function setText($text) {
239  $this->text = $text;
240  }
241 
247  public function getText() {
248  return $this->text;
249  }
250 
258  public function setHref($href) {
259  $this->href = $href;
260  }
261 
267  public function getHref() {
268  return $this->href;
269  }
270 
278  public function setContext($contexts) {
279  if (is_string($contexts)) {
280  $contexts = [$contexts];
281  }
282 
283  $this->data['contexts'] = $contexts;
284  }
285 
291  public function getContext() {
292  return $this->data['contexts'];
293  }
294 
303  public function inContext($context = '') {
304  if (in_array('all', $this->data['contexts'])) {
305  return true;
306  }
307 
308  if ($context) {
309  return in_array($context, $this->data['contexts']);
310  }
311 
312  foreach ($this->data['contexts'] as $context) {
313  if (elgg_in_context($context)) {
314  return true;
315  }
316  }
317 
318  return false;
319  }
320 
328  public function setSelected($state = true) {
329  $this->data['selected'] = $state;
330  }
331 
337  public function getSelected() {
338  if (isset($this->data['selected'])) {
339  return $this->data['selected'];
340  }
341 
342  $href = $this->getHref();
343  if ($href === false) {
344  return false;
345  }
346 
348  }
349 
357  public function setTooltip($text) {
358  $this->title = $text;
359  }
360 
366  public function getTooltip() {
367  return $this->title;
368  }
369 
377  public function setConfirmText($text) {
378  $this->confirm = $text;
379  }
380 
386  public function getConfirmText() {
387  return $this->confirm;
388  }
389 
397  public function setLinkClass($class) {
398  if (!is_array($class)) {
399  $this->data['linkClass'] = [$class];
400  } else {
401  $this->data['linkClass'] = $class;
402  }
403  }
404 
410  public function getLinkClass() {
411  return implode(' ', $this->data['linkClass']);
412  }
413 
421  public function addLinkClass($class) {
422  $this->addClass($this->data['linkClass'], $class);
423  }
424 
432  public function setDeps($modules) {
433  $this->data['deps'] = (array) $modules;
434  }
435 
441  public function getDeps() {
442  $modules = (array) $this->data['deps'];
443  return array_filter($modules, function($m) {
444  return is_string($m) && !empty($m);
445  });
446  }
447 
455  public function addDeps($modules) {
456  $current = $this->getDeps();
457  $this->setDeps($current + (array) $modules);
458  }
459 
467  public function setChildMenuOptions(array $options = []) {
468  $this->data['child_menu'] = $options;
469  }
470 
476  public function getChildMenuOptions() {
477  return $this->data['child_menu'];
478  }
479 
487  public function setItemClass($class) {
488  if (!is_array($class)) {
489  $this->data['itemClass'] = [$class];
490  } else {
491  $this->data['itemClass'] = $class;
492  }
493  }
494 
500  public function getItemClass() {
501  // allow people to specify name with underscores and colons
502  $name = preg_replace('/[^a-z0-9\-]/i', '-', strtolower($this->getName()));
503 
504  $class = implode(' ', $this->data['itemClass']);
505  if ($class) {
506  return "elgg-menu-item-$name $class";
507  } else {
508  return "elgg-menu-item-$name";
509  }
510  }
511 
520  public function addItemClass($class) {
521  $this->addClass($this->data['itemClass'], $class);
522  }
523 
532  protected function addClass(array &$current, $additional) {
533  if (!is_array($additional)) {
534  $current[] = $additional;
535  } else {
536  $current = array_merge($current, $additional);
537  }
538  }
539 
547  public function setPriority(int $priority) {
548  $this->data['priority'] = $priority;
549  }
550 
556  public function getPriority() {
557  return (int) $this->data['priority'];
558  }
559 
567  public function setSection($section) {
568  $this->data['section'] = $section;
569  }
570 
576  public function getSection() {
577  return $this->data['section'];
578  }
579 
587  public function setParentName($name) {
588  $this->data['parent_name'] = $name;
589  }
590 
596  public function getParentName() {
597  return $this->data['parent_name'];
598  }
599 
609  public function setParent($parent) {
610  $this->data['parent'] = $parent;
611  }
612 
620  public function getParent() {
621  return $this->data['parent'];
622  }
623 
633  public function addChild($item) {
634  $this->data['children'][] = $item;
635  }
636 
646  public function setChildren($children) {
647  $this->data['children'] = $children;
648  }
649 
657  public function getChildren() {
658  return $this->data['children'];
659  }
660 
670  public function sortChildren($sortFunction) {
671  foreach ($this->data['children'] as $key => $node) {
672  $node->data['original_order'] = $key;
673  $node->sortChildren($sortFunction);
674  }
675 
676  usort($this->data['children'], $sortFunction);
677  }
678 
685  public function getValues() {
686  $values = get_object_vars($this);
687  unset($values['data']);
688 
689  return $values;
690  }
691 
697  public function getID() {
698  return $this->getName();
699  }
700 
709  public function setItemContentsView(string $view_name): void {
710  $this->data['item_contents_view'] = $view_name;
711  }
712 
719  public function getItemContentsView(): ?string {
720  return $this->data['item_contents_view'];
721  }
722 
729  public function hasItemContentsView(): bool {
730  return isset($this->data['item_contents_view']);
731  }
732 }
if(!$items) $item
Definition: delete.php:13
getChildMenuOptions()
Returns child menu options for parent items.
$context
Definition: add.php:8
getID()
Get unique item identifier within a collection.
setParentName($name)
Set the parent identifier.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getItemClass()
Get the li classes as text.
setText($text)
Set the display text of the menu item.
getData($key)
Get stored data.
setLinkClass($class)
Set the anchor class.
addDeps($modules)
Add required AMD modules.
getChildren()
Get the children menu items.
elgg_in_context(string $context)
Check if this context exists anywhere in the stack.
Definition: context.php:78
setData($key, $value=null)
Set a data key/value pair or a set of key/value pairs.
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
setItemContentsView(string $view_name)
Set the item contents view.
getLinkClass()
Get the anchor classes as text.
getContext()
Get an array of context strings.
$children
Definition: item.php:17
static factory(array $options)
Create an ElggMenuItem from an associative array.
setPriority(int $priority)
Set the priority of the menu item.
$options
Elgg admin footer.
Definition: footer.php:6
Elgg Menu Item.
setChildren($children)
Set the menu item&#39;s children.
$value
Definition: generic.php:51
$class
Definition: summary.php:44
inContext($context= '')
Should this menu item be used given the current context.
getConfirmText()
Get the confirm text.
getTooltip()
Get the tool tip text.
setContext($contexts)
Set the contexts that this menu item is available for.
setItemClass($class)
Set the li classes.
addItemClass($class)
Add a li class.
getItemContentsView()
Get the item contents view.
addChild($item)
Add a child menu item.
hasItemContentsView()
Check if the menu items has a contents view set.
elgg_get_current_url()
Returns the current page&#39;s complete URL.
getSection()
Get the section identifier.
getPriority()
Get the priority of the menu item.
setName($name)
Set the identifier of the menu item.
setParent($parent)
Set the parent menu item.
setHref($href)
Set the URL of the menu item.
setSelected($state=true)
Set the selected flag.
elgg_http_url_is_identical(string $url1, string $url2, array $ignore_params=['offset', 'limit'])
Test if two URLs are functionally identical.
Definition: elgglib.php:199
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
getText()
Get the display text of the menu item.
addClass(array &$current, $additional)
Add additional classes.
setConfirmText($text)
Set the confirm text shown when link is clicked.
getName()
Get the identifier of the menu item.
setTooltip($text)
Set the tool tip text.
getDeps()
Get required AMD modules.
sortChildren($sortFunction)
Sort the children.
getSelected()
Get selected state.
elgg_normalize_url(string $url)
Definition: output.php:163
setChildMenuOptions(array $options=[])
Set child menu options for a parent item.
getParent()
Get the parent menu item.
setSection($section)
Set the section identifier.
setDeps($modules)
Set required AMD modules.
getParentName()
Get the parent identifier.
$priority
__construct($name, $text, $href)
constructor
getValues()
Get all the values for this menu item.
getHref()
Get the URL of the menu item.
addLinkClass($class)
Add a link class.