Elgg  Version 3.0
ElggMenuItem.php
Go to the documentation of this file.
1 <?php
2 
4 
19 
23  protected $data = [
24  // string Identifier of the menu
25  'name' => '',
26 
27  // array Page contexts this menu item should appear on
28  'contexts' => ['all'],
29 
30  // string Menu section identifier
31  'section' => 'default',
32 
33  // int Smaller priorities float to the top
34  'priority' => 100,
35 
36  // bool Is this the currently selected menu item (null for autodetection)
37  'selected' => null,
38 
39  // string Identifier of this item's parent
40  'parent_name' => '',
41 
42  // \ElggMenuItem The parent object or null
43  'parent' => null,
44 
45  // array Array of children objects or empty array
46  'children' => [],
47 
48  // array An array of options for child menu of the parent item
49  'child_menu' => [],
50 
51  // array Classes to apply to the li tag
52  'itemClass' => [],
53 
54  // array Classes to apply to the anchor tag
55  'linkClass' => [],
56 
57  // array AMD modules required by this menu item
58  'deps' => []
59  ];
60 
64  protected $text;
65 
69  protected $href = null;
70 
74  protected $title = false;
75 
79  protected $confirm = '';
80 
81 
89  public function __construct($name, $text, $href) {
90  $this->text = $text;
91  if ($href) {
92  $this->href = elgg_normalize_url($href);
93  } else {
94  $this->href = $href;
95  }
96 
97  $this->data['name'] = $name;
98  }
99 
132  public static function factory($options) {
133  if (!isset($options['name']) || !isset($options['text'])) {
134  elgg_log(__METHOD__ . ': $options "name" and "text" are required.', 'ERROR');
135  return null;
136  }
137  if (!isset($options['href'])) {
138  $options['href'] = '';
139  }
140 
141  $item = new \ElggMenuItem($options['name'], $options['text'], $options['href']);
142  unset($options['name']);
143  unset($options['text']);
144  unset($options['href']);
145 
146  // special catch in case someone uses context rather than contexts
147  if (isset($options['context'])) {
148  $options['contexts'] = $options['context'];
149  unset($options['context']);
150  }
151 
152  // make sure contexts is set correctly
153  if (isset($options['contexts'])) {
154  $item->setContext($options['contexts']);
155  unset($options['contexts']);
156  }
157 
158  if (isset($options['link_class'])) {
159  $item->setLinkClass($options['link_class']);
160  unset($options['link_class']);
161  }
162 
163  if (isset($options['item_class'])) {
164  $item->setItemClass($options['item_class']);
165  unset($options['item_class']);
166  }
167 
168  if (isset($options['data']) && is_array($options['data'])) {
169  $item->setData($options['data']);
170  unset($options['data']);
171  }
172 
173  foreach ($options as $key => $value) {
174  if (array_key_exists($key, $item->data)) {
175  $item->data[$key] = $value;
176  } else {
177  $item->$key = $value;
178  }
179  }
180 
181  return $item;
182  }
183 
194  public function setData($key, $value = null) {
195  if (is_array($key)) {
196  $this->data = array_merge($this->data, $key);
197  } else {
198  $this->data[$key] = $value;
199  }
200  }
201 
208  public function getData($key) {
209  if (isset($this->data[$key])) {
210  return $this->data[$key];
211  } else {
212  return null;
213  }
214  }
215 
222  public function setName($name) {
223  $this->data['name'] = $name;
224  }
225 
231  public function getName() {
232  return $this->data['name'];
233  }
234 
241  public function setText($text) {
242  $this->text = $text;
243  }
244 
250  public function getText() {
251  return $this->text;
252  }
253 
261  public function setHref($href) {
262  $this->href = $href;
263  }
264 
270  public function getHref() {
271  return $this->href;
272  }
273 
280  public function setContext($contexts) {
281  if (is_string($contexts)) {
282  $contexts = [$contexts];
283  }
284  $this->data['contexts'] = $contexts;
285  }
286 
292  public function getContext() {
293  return $this->data['contexts'];
294  }
295 
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 
327  public function setSelected($state = true) {
328  $this->data['selected'] = $state;
329  }
330 
336  public function getSelected() {
337  if (isset($this->data['selected'])) {
338  return $this->data['selected'];
339  }
340 
342  }
343 
350  public function setTooltip($text) {
351  $this->title = $text;
352  }
353 
359  public function getTooltip() {
360  return $this->title;
361  }
362 
369  public function setConfirmText($text) {
370  $this->confirm = $text;
371  }
372 
378  public function getConfirmText() {
379  return $this->confirm;
380  }
381 
388  public function setLinkClass($class) {
389  if (!is_array($class)) {
390  $this->data['linkClass'] = [$class];
391  } else {
392  $this->data['linkClass'] = $class;
393  }
394  }
395 
401  public function getLinkClass() {
402  return implode(' ', $this->data['linkClass']);
403  }
404 
411  public function addLinkClass($class) {
412  $this->addClass($this->data['linkClass'], $class);
413  }
414 
421  public function setDeps($modules) {
422  $this->data['deps'] = (array) $modules;
423  }
424 
430  public function getDeps() {
431  $modules = (array) $this->data['deps'];
432  return array_filter($modules, function($m) {
433  return is_string($m) && !empty($m);
434  });
435  }
436 
443  public function addDeps($modules) {
444  $current = $this->getDeps();
445  $this->setDeps($current + (array) $modules);
446  }
447 
454  public function setChildMenuOptions(array $options = []) {
455  $this->data['child_menu'] = $options;
456  }
457 
463  public function getChildMenuOptions() {
464  return $this->data['child_menu'];
465  }
466 
473  public function setItemClass($class) {
474  if (!is_array($class)) {
475  $this->data['itemClass'] = [$class];
476  } else {
477  $this->data['itemClass'] = $class;
478  }
479  }
480 
486  public function getItemClass() {
487  // allow people to specify name with underscores and colons
488  $name = preg_replace('/[^a-z0-9\-]/i', '-', strtolower($this->getName()));
489 
490  $class = implode(' ', $this->data['itemClass']);
491  if ($class) {
492  return "elgg-menu-item-$name $class";
493  } else {
494  return "elgg-menu-item-$name";
495  }
496  }
497 
505  public function addItemClass($class) {
506  $this->addClass($this->data['itemClass'], $class);
507  }
508 
509  // @codingStandardsIgnoreStart
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  // @codingStandardsIgnoreEnd
525 
532  public function setPriority(int $priority) {
533  $this->data['priority'] = $priority;
534  }
535 
541  public function getPriority() {
542  return (int) $this->data['priority'];
543  }
544 
551  public function setSection($section) {
552  $this->data['section'] = $section;
553  }
554 
560  public function getSection() {
561  return $this->data['section'];
562  }
563 
570  public function setParentName($name) {
571  $this->data['parent_name'] = $name;
572  }
573 
579  public function getParentName() {
580  return $this->data['parent_name'];
581  }
582 
591  public function setParent($parent) {
592  $this->data['parent'] = $parent;
593  }
594 
602  public function getParent() {
603  return $this->data['parent'];
604  }
605 
614  public function addChild($item) {
615  $this->data['children'][] = $item;
616  }
617 
626  public function setChildren($children) {
627  $this->data['children'] = $children;
628  }
629 
637  public function getChildren() {
638  return $this->data['children'];
639  }
640 
650  public function sortChildren($sortFunction) {
651  foreach ($this->data['children'] as $key => $node) {
652  $node->data['original_order'] = $key;
653  $node->sortChildren($sortFunction);
654  }
655  usort($this->data['children'], $sortFunction);
656  }
657 
664  public function getValues() {
665  $values = get_object_vars($this);
666  unset($values['data']);
667 
668  return $values;
669  }
670 
675  public function getId() {
676  return $this->getName();
677  }
678 }
getChildMenuOptions()
Returns child menu options for parent items.
$modules
Boot all the plugins and trigger the [init, system] hook.
Definition: init.js.php:8
$context
Definition: add.php:8
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
getId()
Get unique item identifier within a collection.
elgg_normalize_url($url)
Definition: output.php:186
setParentName($name)
Set the parent identifier.
if(!$items) $item
Definition: delete.php:13
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.
getLinkClass()
Get the anchor classes as text.
getContext()
Get an array of context strings.
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:94
$children
Definition: item.php:20
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.
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:991
$class
Definition: field.php:29
getTooltip()
Get the tool tip text.
setContext($contexts)
Set the contexts that this menu item is available for.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
setItemClass($class)
Set the li classes.
addItemClass($class)
Add a li class.
addChild($item)
Add a child menu item.
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:238
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:55
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.
$value
Definition: debugging.php:7
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.