Elgg  Version 4.3
ElggMenuItem.php
Go to the documentation of this file.
1 <?php
14 
18  protected $data = [
19  // string Identifier of the menu
20  'name' => '',
21 
22  // array Page contexts this menu item should appear on
23  'contexts' => ['all'],
24 
25  // string Menu section identifier
26  'section' => 'default',
27 
28  // int Smaller priorities float to the top
29  'priority' => 100,
30 
31  // bool Is this the currently selected menu item (null for autodetection)
32  'selected' => null,
33 
34  // string Identifier of this item's parent
35  'parent_name' => '',
36 
37  // boolean Indicator to control if menu item should show if it has no children
38  'show_with_empty_children' => true,
39 
40  // \ElggMenuItem The parent object or null
41  'parent' => null,
42 
43  // array Array of children objects or empty array
44  'children' => [],
45 
46  // array An array of options for child menu of the parent item
47  'child_menu' => [],
48 
49  // array Classes to apply to the li tag
50  'itemClass' => [],
51 
52  // array Classes to apply to the anchor tag
53  'linkClass' => [],
54 
55  // array AMD modules required by this menu item
56  'deps' => [],
57 
58  // which view should be used to output the menu item contents
59  'item_contents_view' => null,
60  ];
61 
65  protected $text;
66 
70  protected $href = null;
71 
75  protected $title = false;
76 
80  protected $confirm = '';
81 
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 
134  public static function factory($options) {
135  if (!isset($options['name']) || !isset($options['text'])) {
136  elgg_log(__METHOD__ . ': $options "name" and "text" are required.', 'ERROR');
137  return null;
138  }
139  if (!isset($options['href'])) {
140  $options['href'] = '';
141  }
142 
143  $item = new \ElggMenuItem($options['name'], $options['text'], $options['href']);
144  unset($options['name']);
145  unset($options['text']);
146  unset($options['href']);
147 
148  // special catch in case someone uses context rather than contexts
149  if (isset($options['context'])) {
150  $options['contexts'] = $options['context'];
151  unset($options['context']);
152  }
153 
154  // make sure contexts is set correctly
155  if (isset($options['contexts'])) {
156  $item->setContext($options['contexts']);
157  unset($options['contexts']);
158  }
159 
160  if (isset($options['link_class'])) {
161  $item->setLinkClass($options['link_class']);
162  unset($options['link_class']);
163  }
164 
165  if (isset($options['item_class'])) {
166  $item->setItemClass($options['item_class']);
167  unset($options['item_class']);
168  }
169 
170  if (isset($options['data']) && is_array($options['data'])) {
171  $item->setData($options['data']);
172  unset($options['data']);
173  }
174 
175  foreach ($options as $key => $value) {
176  if (array_key_exists($key, $item->data)) {
177  $item->data[$key] = $value;
178  } else {
179  $item->$key = $value;
180  }
181  }
182 
183  return $item;
184  }
185 
196  public function setData($key, $value = null) {
197  if (is_array($key)) {
198  $this->data = array_merge($this->data, $key);
199  } else {
200  $this->data[$key] = $value;
201  }
202  }
203 
210  public function getData($key) {
211  if (isset($this->data[$key])) {
212  return $this->data[$key];
213  } else {
214  return null;
215  }
216  }
217 
224  public function setName($name) {
225  $this->data['name'] = $name;
226  }
227 
233  public function getName() {
234  return $this->data['name'];
235  }
236 
243  public function setText($text) {
244  $this->text = $text;
245  }
246 
252  public function getText() {
253  return $this->text;
254  }
255 
262  public function setHref($href) {
263  $this->href = $href;
264  }
265 
271  public function getHref() {
272  return $this->href;
273  }
274 
281  public function setContext($contexts) {
282  if (is_string($contexts)) {
283  $contexts = [$contexts];
284  }
285  $this->data['contexts'] = $contexts;
286  }
287 
293  public function getContext() {
294  return $this->data['contexts'];
295  }
296 
304  public function inContext($context = '') {
305  if (in_array('all', $this->data['contexts'])) {
306  return true;
307  }
308 
309  if ($context) {
310  return in_array($context, $this->data['contexts']);
311  }
312 
313  foreach ($this->data['contexts'] as $context) {
314  if (elgg_in_context($context)) {
315  return true;
316  }
317  }
318 
319  return false;
320  }
321 
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 
343  }
344 
351  public function setTooltip($text) {
352  $this->title = $text;
353  }
354 
360  public function getTooltip() {
361  return $this->title;
362  }
363 
370  public function setConfirmText($text) {
371  $this->confirm = $text;
372  }
373 
379  public function getConfirmText() {
380  return $this->confirm;
381  }
382 
389  public function setLinkClass($class) {
390  if (!is_array($class)) {
391  $this->data['linkClass'] = [$class];
392  } else {
393  $this->data['linkClass'] = $class;
394  }
395  }
396 
402  public function getLinkClass() {
403  return implode(' ', $this->data['linkClass']);
404  }
405 
412  public function addLinkClass($class) {
413  $this->addClass($this->data['linkClass'], $class);
414  }
415 
422  public function setDeps($modules) {
423  $this->data['deps'] = (array) $modules;
424  }
425 
431  public function getDeps() {
432  $modules = (array) $this->data['deps'];
433  return array_filter($modules, function($m) {
434  return is_string($m) && !empty($m);
435  });
436  }
437 
444  public function addDeps($modules) {
445  $current = $this->getDeps();
446  $this->setDeps($current + (array) $modules);
447  }
448 
455  public function setChildMenuOptions(array $options = []) {
456  $this->data['child_menu'] = $options;
457  }
458 
464  public function getChildMenuOptions() {
465  return $this->data['child_menu'];
466  }
467 
474  public function setItemClass($class) {
475  if (!is_array($class)) {
476  $this->data['itemClass'] = [$class];
477  } else {
478  $this->data['itemClass'] = $class;
479  }
480  }
481 
487  public function getItemClass() {
488  // allow people to specify name with underscores and colons
489  $name = preg_replace('/[^a-z0-9\-]/i', '-', strtolower($this->getName()));
490 
491  $class = implode(' ', $this->data['itemClass']);
492  if ($class) {
493  return "elgg-menu-item-$name $class";
494  } else {
495  return "elgg-menu-item-$name";
496  }
497  }
498 
506  public function addItemClass($class) {
507  $this->addClass($this->data['itemClass'], $class);
508  }
509 
517  protected function addClass(array &$current, $additional) {
518  if (!is_array($additional)) {
519  $current[] = $additional;
520  } else {
521  $current = array_merge($current, $additional);
522  }
523  }
524 
531  public function setPriority(int $priority) {
532  $this->data['priority'] = $priority;
533  }
534 
540  public function getPriority() {
541  return (int) $this->data['priority'];
542  }
543 
550  public function setSection($section) {
551  $this->data['section'] = $section;
552  }
553 
559  public function getSection() {
560  return $this->data['section'];
561  }
562 
569  public function setParentName($name) {
570  $this->data['parent_name'] = $name;
571  }
572 
578  public function getParentName() {
579  return $this->data['parent_name'];
580  }
581 
590  public function setParent($parent) {
591  $this->data['parent'] = $parent;
592  }
593 
601  public function getParent() {
602  return $this->data['parent'];
603  }
604 
613  public function addChild($item) {
614  $this->data['children'][] = $item;
615  }
616 
625  public function setChildren($children) {
626  $this->data['children'] = $children;
627  }
628 
636  public function getChildren() {
637  return $this->data['children'];
638  }
639 
649  public function sortChildren($sortFunction) {
650  foreach ($this->data['children'] as $key => $node) {
651  $node->data['original_order'] = $key;
652  $node->sortChildren($sortFunction);
653  }
654  usort($this->data['children'], $sortFunction);
655  }
656 
663  public function getValues() {
664  $values = get_object_vars($this);
665  unset($values['data']);
666 
667  return $values;
668  }
669 
674  public function getID() {
675  return $this->getName();
676  }
677 
686  public function setItemContentsView(string $view_name): void {
687  $this->data['item_contents_view'] = $view_name;
688  }
689 
696  public function getItemContentsView(): ?string {
697  return $this->data['item_contents_view'];
698  }
699 
706  public function hasItemContentsView(): bool {
707  return isset($this->data['item_contents_view']);
708  }
709 }
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.
elgg_normalize_url($url)
Definition: output.php:153
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.
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
setPriority(int $priority)
Set the priority of the menu item.
$options
Elgg admin footer.
Definition: footer.php:6
setChildren($children)
Set the menu item&#39;s children.
$value
Definition: generic.php:51
$class
Definition: summary.php:44
static factory($options)
Create an ElggMenuItem from an associative array.
inContext($context= '')
Should this menu item be used given the current context.
getConfirmText()
Get the confirm text.
elgg_http_url_is_identical($url1, $url2, $ignore_params=['offset', 'limit'])
Test if two URLs are functionally identical.
Definition: elgglib.php:499
getTooltip()
Get the tool tip text.
setContext($contexts)
Set the contexts that this menu item is available for.
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: context.php:78
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
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.
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.
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.