Elgg  Version 2.3
ElggMenuItem.php
Go to the documentation of this file.
1 <?php
15 class ElggMenuItem {
16 
20  protected $data = array(
21  // string Identifier of the menu
22  'name' => '',
23 
24  // array Page contexts this menu item should appear on
25  'contexts' => array('all'),
26 
27  // string Menu section identifier
28  'section' => 'default',
29 
30  // int Smaller priorities float to the top
31  'priority' => 100,
32 
33  // bool Is this the currently selected menu item
34  'selected' => false,
35 
36  // string Identifier of this item's parent
37  'parent_name' => '',
38 
39  // \ElggMenuItem The parent object or null
40  'parent' => null,
41 
42  // array Array of children objects or empty array
43  'children' => array(),
44 
45  // array Classes to apply to the li tag
46  'itemClass' => array(),
47 
48  // array Classes to apply to the anchor tag
49  'linkClass' => array(),
50 
51  // array AMD modules required by this menu item
52  'deps' => array()
53  );
54 
58  protected $text;
59 
63  protected $href = null;
64 
68  protected $title = false;
69 
73  protected $confirm = '';
74 
75 
83  public function __construct($name, $text, $href) {
84  $this->text = $text;
85  if ($href) {
86  $this->href = elgg_normalize_url($href);
87  } else {
88  $this->href = $href;
89  }
90 
91  $this->data['name'] = $name;
92  }
93 
125  public static function factory($options) {
126  if (!isset($options['name']) || !isset($options['text'])) {
127  elgg_log(__METHOD__ . ': $options "name" and "text" are required.', 'ERROR');
128  return null;
129  }
130  if (!isset($options['href'])) {
131  $options['href'] = '';
132  }
133 
134  $item = new \ElggMenuItem($options['name'], $options['text'], $options['href']);
135  unset($options['name']);
136  unset($options['text']);
137  unset($options['href']);
138 
139  // special catch in case someone uses context rather than contexts
140  if (isset($options['context'])) {
141  $options['contexts'] = $options['context'];
142  unset($options['context']);
143  }
144 
145  // make sure contexts is set correctly
146  if (isset($options['contexts'])) {
147  $item->setContext($options['contexts']);
148  unset($options['contexts']);
149  }
150 
151  if (isset($options['link_class'])) {
152  $item->setLinkClass($options['link_class']);
153  unset($options['link_class']);
154  } elseif (isset($options['class'])) {
155  elgg_deprecated_notice("\ElggMenuItem::factory() does not accept 'class' key anymore, use 'link_class' instead", 1.9);
156  $item->setLinkClass($options['class']);
157  unset($options['class']);
158  }
159 
160  if (isset($options['item_class'])) {
161  $item->setItemClass($options['item_class']);
162  unset($options['item_class']);
163  }
164 
165  if (isset($options['data']) && is_array($options['data'])) {
166  $item->setData($options['data']);
167  unset($options['data']);
168  }
169 
170  foreach ($options as $key => $value) {
171  if (isset($item->data[$key])) {
172  $item->data[$key] = $value;
173  } else {
174  $item->$key = $value;
175  }
176  }
177 
178  return $item;
179  }
180 
191  public function setData($key, $value = null) {
192  if (is_array($key)) {
193  $this->data = array_merge($this->data, $key);
194  } else {
195  $this->data[$key] = $value;
196  }
197  }
198 
205  public function getData($key) {
206  if (isset($this->data[$key])) {
207  return $this->data[$key];
208  } else {
209  return null;
210  }
211  }
212 
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 
277  public function setContext($contexts) {
278  if (is_string($contexts)) {
279  $contexts = array($contexts);
280  }
281  $this->data['contexts'] = $contexts;
282  }
283 
289  public function getContext() {
290  return $this->data['contexts'];
291  }
292 
300  public function inContext($context = '') {
301  if (in_array('all', $this->data['contexts'])) {
302  return true;
303  }
304 
305  if ($context) {
306  return in_array($context, $this->data['contexts']);
307  }
308 
309  foreach ($this->data['contexts'] as $context) {
310  if (elgg_in_context($context)) {
311  return true;
312  }
313  }
314  return false;
315  }
316 
323  public function setSelected($state = true) {
324  $this->data['selected'] = $state;
325  }
326 
332  public function getSelected() {
333  return $this->data['selected'];
334  }
335 
342  public function setTooltip($text) {
343  $this->title = $text;
344  }
345 
351  public function getTooltip() {
352  return $this->title;
353  }
354 
361  public function setConfirmText($text) {
362  $this->confirm = $text;
363  }
364 
370  public function getConfirmText() {
371  return $this->confirm;
372  }
373 
380  public function setLinkClass($class) {
381  if (!is_array($class)) {
382  $this->data['linkClass'] = array($class);
383  } else {
384  $this->data['linkClass'] = $class;
385  }
386  }
387 
393  public function getLinkClass() {
394  return implode(' ', $this->data['linkClass']);
395  }
396 
403  public function addLinkClass($class) {
404  $this->addClass($this->data['linkClass'], $class);
405  }
406 
413  public function setDeps($modules) {
414  $this->data['deps'] = (array) $modules;
415  }
416 
422  public function getDeps() {
423  $modules = (array) $this->data['deps'];
424  return array_filter($modules, function($m) {
425  return is_string($m) && !empty($m);
426  });
427  }
428 
435  public function addDeps($modules) {
436  $current = $this->getDeps();
437  $this->setDeps($current + (array) $modules);
438  }
439 
446  public function setItemClass($class) {
447  if (!is_array($class)) {
448  $this->data['itemClass'] = array($class);
449  } else {
450  $this->data['itemClass'] = $class;
451  }
452  }
453 
459  public function getItemClass() {
460  // allow people to specify name with underscores and colons
461  $name = strtolower($this->getName());
462  $name = str_replace('_', '-', $name);
463  $name = str_replace(':', '-', $name);
464  $name = str_replace(' ', '-', $name);
465 
466  $class = implode(' ', $this->data['itemClass']);
467  if ($class) {
468  return "elgg-menu-item-$name $class";
469  } else {
470  return "elgg-menu-item-$name";
471  }
472  }
473 
481  public function addItemClass($class) {
482  $this->addClass($this->data['itemClass'], $class);
483  }
484 
485  // @codingStandardsIgnoreStart
493  protected function addClass(array &$current, $additional) {
494  if (!is_array($additional)) {
495  $current[] = $additional;
496  } else {
497  $current = array_merge($current, $additional);
498  }
499  }
500  // @codingStandardsIgnoreEnd
501 
502 
510  public function setWeight($priority) {
511  elgg_deprecated_notice("\ElggMenuItem::setWeight() deprecated by \ElggMenuItem::setPriority()", 1.9);
512  $this->data['priority'] = $priority;
513  }
514 
521  public function getWeight() {
522  elgg_deprecated_notice("\ElggMenuItem::getWeight() deprecated by \ElggMenuItem::getPriority()", 1.9);
523  return $this->data['priority'];
524  }
525 
532  public function setPriority($priority) {
533  $this->data['priority'] = $priority;
534  }
535 
541  public function getPriority() {
542  return $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 
592  public function setParent($parent) {
593  $this->data['parent'] = $parent;
594  }
595 
604  public function getParent() {
605  return $this->data['parent'];
606  }
607 
617  public function addChild($item) {
618  $this->data['children'][] = $item;
619  }
620 
630  public function setChildren($children) {
631  $this->data['children'] = $children;
632  }
633 
642  public function getChildren() {
643  return $this->data['children'];
644  }
645 
655  public function sortChildren($sortFunction) {
656  foreach ($this->data['children'] as $key => $node) {
657  $this->data['children'][$key]->data['original_order'] = $key;
658  }
659  usort($this->data['children'], $sortFunction);
660  }
661 
668  public function getValues() {
669  $values = get_object_vars($this);
670  unset($values['data']);
671 
672  return $values;
673  }
674 
682  public function getContent(array $vars = array()) {
683  elgg_deprecated_notice("\ElggMenuItem::getContent() deprecated by elgg_view_menu_item()", 1.9);
684  return elgg_view_menu_item($this, $vars);
685  }
686 }
$context
Definition: add.php:11
$options
Elgg admin footer.
Definition: footer.php:6
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
ui datepicker title
Definition: admin.css.php:662
elgg input text
Definition: admin.css.php:505
setChildren($children)
Set the menu item's children.
setPriority($priority)
Set the priority of the menu item.
addItemClass($class)
Add a li class.
setWeight($priority)
Set the priority of the menu item.
setConfirmText($text)
Set the confirm text shown when link is clicked.
addClass(array &$current, $additional)
Add additional classes.
setData($key, $value=null)
Set a data key/value pair or a set of key/value pairs.
addChild($item)
Add a child menu item.
getContent(array $vars=array())
Get the menu item content (usually a link)
getItemClass()
Get the li classes as text.
getTooltip()
Get the tool tip text.
getDeps()
Get required AMD modules.
inContext($context='')
Should this menu item be used given the current context.
setParent($parent)
Set the parent menu item.
getHref()
Get the URL of the menu item.
addDeps($modules)
Add required AMD modules.
setDeps($modules)
Set required AMD modules.
setParentName($name)
Set the parent identifier.
__construct($name, $text, $href)
\ElggMenuItem constructor
addLinkClass($class)
Add a link class.
setItemClass($class)
Set the li classes.
getName()
Get the identifier of the menu item.
sortChildren($sortFunction)
Sort the children.
setLinkClass($class)
Set the anchor class.
static factory($options)
Create an ElggMenuItem from an associative array.
getSection()
Get the section identifier.
setSection($section)
Set the section identifier.
setText($text)
Set the display text of the menu item.
getLinkClass()
Get the anchor classes as text.
getWeight()
Get the priority of the menu item.
getChildren()
Get the children menu items.
getParentName()
Get the parent identifier.
getParent()
Get the parent menu item.
getValues()
Get all the values for this menu item.
getConfirmText()
Get the confirm text.
getSelected()
Get selected state.
setHref($href)
Set the URL of the menu item.
getText()
Get the display text of the menu item.
setContext($contexts)
Set the contexts that this menu item is available for.
setName($name)
Set the identifier of the menu item.
getContext()
Get an array of context strings.
getData($key)
Get stored data.
setTooltip($text)
Set the tool tip text.
getPriority()
Get the priority of the menu item.
setSelected($state=true)
Set the selected flag.
$contexts
Definition: contents.php:13
$state
Definition: contents.php:4
if($item->getSelected()) $children
Definition: item.php:21
elgg_log($message, $level='NOTICE')
Display or log a message.
Definition: elgglib.php:1028
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
$class
Definition: field.php:20
$modules
Boot all the plugins and trigger the [init, system] hook.
Definition: init.js.php:8
$value
Definition: longtext.php:42
elgg_view_menu_item(\ElggMenuItem $item, array $vars=array())
Render a menu item (usually as a link)
Definition: views.php:815
elgg_normalize_url($url)
Definition: output.php:280
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:241
$vars['entity']
if(! $items) $item
Definition: delete.php:17
$key
Definition: summary.php:34
$priority
$m
Definition: metadata.php:11