Elgg  Version 1.11
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 
55  protected $text;
56 
60  protected $href = null;
61 
65  protected $title = false;
66 
70  protected $confirm = '';
71 
72 
80  public function __construct($name, $text, $href) {
81  $this->text = $text;
82  if ($href) {
83  $this->href = elgg_normalize_url($href);
84  } else {
85  $this->href = $href;
86  }
87 
88  $this->data['name'] = $name;
89  }
90 
117  public static function factory($options) {
118  if (!isset($options['name']) || !isset($options['text'])) {
119  return null;
120  }
121  if (!isset($options['href'])) {
122  $options['href'] = '';
123  }
124 
125  $item = new \ElggMenuItem($options['name'], $options['text'], $options['href']);
126  unset($options['name']);
127  unset($options['text']);
128  unset($options['href']);
129 
130  // special catch in case someone uses context rather than contexts
131  if (isset($options['context'])) {
132  $options['contexts'] = $options['context'];
133  unset($options['context']);
134  }
135 
136  // make sure contexts is set correctly
137  if (isset($options['contexts'])) {
138  $item->setContext($options['contexts']);
139  unset($options['contexts']);
140  }
141 
142  if (isset($options['link_class'])) {
143  $item->setLinkClass($options['link_class']);
144  unset($options['link_class']);
145  } elseif (isset($options['class'])) {
146  elgg_deprecated_notice("\ElggMenuItem::factory() does not accept 'class' key anymore, use 'link_class' instead", 1.9);
147  $item->setLinkClass($options['class']);
148  unset($options['class']);
149  }
150 
151  if (isset($options['item_class'])) {
152  $item->setItemClass($options['item_class']);
153  unset($options['item_class']);
154  }
155 
156  if (isset($options['data']) && is_array($options['data'])) {
157  $item->setData($options['data']);
158  unset($options['data']);
159  }
160 
161  foreach ($options as $key => $value) {
162  if (isset($item->data[$key])) {
163  $item->data[$key] = $value;
164  } else {
165  $item->$key = $value;
166  }
167  }
168 
169  return $item;
170  }
171 
182  public function setData($key, $value = null) {
183  if (is_array($key)) {
184  $this->data = array_merge($this->data, $key);
185  } else {
186  $this->data[$key] = $value;
187  }
188  }
189 
196  public function getData($key) {
197  if (isset($this->data[$key])) {
198  return $this->data[$key];
199  } else {
200  return null;
201  }
202  }
203 
210  public function setName($name) {
211  $this->data['name'] = $name;
212  }
213 
219  public function getName() {
220  return $this->data['name'];
221  }
222 
229  public function setText($text) {
230  $this->text = $text;
231  }
232 
238  public function getText() {
239  return $this->text;
240  }
241 
249  public function setHref($href) {
250  $this->href = $href;
251  }
252 
258  public function getHref() {
259  return $this->href;
260  }
261 
268  public function setContext($contexts) {
269  if (is_string($contexts)) {
270  $contexts = array($contexts);
271  }
272  $this->data['contexts'] = $contexts;
273  }
274 
280  public function getContext() {
281  return $this->data['contexts'];
282  }
283 
291  public function inContext($context = '') {
292  if (in_array('all', $this->data['contexts'])) {
293  return true;
294  }
295 
296  if ($context) {
297  return in_array($context, $this->data['contexts']);
298  }
299 
300  foreach ($this->data['contexts'] as $context) {
301  if (elgg_in_context($context)) {
302  return true;
303  }
304  }
305  return false;
306  }
307 
314  public function setSelected($state = true) {
315  $this->data['selected'] = $state;
316  }
317 
323  public function getSelected() {
324  return $this->data['selected'];
325  }
326 
333  public function setTooltip($text) {
334  $this->title = $text;
335  }
336 
342  public function getTooltip() {
343  return $this->title;
344  }
345 
352  public function setConfirmText($text) {
353  $this->confirm = $text;
354  }
355 
361  public function getConfirmText() {
362  return $this->confirm;
363  }
364 
371  public function setLinkClass($class) {
372  if (!is_array($class)) {
373  $this->data['linkClass'] = array($class);
374  } else {
375  $this->data['linkClass'] = $class;
376  }
377  }
378 
384  public function getLinkClass() {
385  return implode(' ', $this->data['linkClass']);
386  }
387 
394  public function addLinkClass($class) {
395  $this->addClass($this->data['linkClass'], $class);
396  }
397 
404  public function setItemClass($class) {
405  if (!is_array($class)) {
406  $this->data['itemClass'] = array($class);
407  } else {
408  $this->data['itemClass'] = $class;
409  }
410  }
411 
417  public function getItemClass() {
418  // allow people to specify name with underscores and colons
419  $name = strtolower($this->getName());
420  $name = str_replace('_', '-', $name);
421  $name = str_replace(':', '-', $name);
422  $name = str_replace(' ', '-', $name);
423 
424  $class = implode(' ', $this->data['itemClass']);
425  if ($class) {
426  return "elgg-menu-item-$name $class";
427  } else {
428  return "elgg-menu-item-$name";
429  }
430  }
431 
439  public function addItemClass($class) {
440  $this->addClass($this->data['itemClass'], $class);
441  }
442 
443  // @codingStandardsIgnoreStart
451  protected function addClass(array &$current, $additional) {
452  if (!is_array($additional)) {
453  $current[] = $additional;
454  } else {
455  $current = array_merge($current, $additional);
456  }
457  }
458  // @codingStandardsIgnoreEnd
459 
460 
468  public function setWeight($priority) {
469  elgg_deprecated_notice("\ElggMenuItem::setWeight() deprecated by \ElggMenuItem::setPriority()", 1.9);
470  $this->data['priority'] = $priority;
471  }
472 
479  public function getWeight() {
480  elgg_deprecated_notice("\ElggMenuItem::getWeight() deprecated by \ElggMenuItem::getPriority()", 1.9);
481  return $this->data['priority'];
482  }
483 
490  public function setPriority($priority) {
491  $this->data['priority'] = $priority;
492  }
493 
499  public function getPriority() {
500  return $this->data['priority'];
501  }
502 
509  public function setSection($section) {
510  $this->data['section'] = $section;
511  }
512 
518  public function getSection() {
519  return $this->data['section'];
520  }
521 
528  public function setParentName($name) {
529  $this->data['parent_name'] = $name;
530  }
531 
537  public function getParentName() {
538  return $this->data['parent_name'];
539  }
540 
550  public function setParent($parent) {
551  $this->data['parent'] = $parent;
552  }
553 
562  public function getParent() {
563  return $this->data['parent'];
564  }
565 
575  public function addChild($item) {
576  $this->data['children'][] = $item;
577  }
578 
588  public function setChildren($children) {
589  $this->data['children'] = $children;
590  }
591 
600  public function getChildren() {
601  return $this->data['children'];
602  }
603 
613  public function sortChildren($sortFunction) {
614  foreach ($this->data['children'] as $key => $node) {
615  $this->data['children'][$key]->data['original_order'] = $key;
616  }
617  usort($this->data['children'], $sortFunction);
618  }
619 
626  public function getValues() {
627  $values = get_object_vars($this);
628  unset($values['data']);
629 
630  return $values;
631  }
632 
640  public function getContent(array $vars = array()) {
641  elgg_deprecated_notice("\ElggMenuItem::getContent() deprecated by elgg_view_menu_item()", 1.9);
642  return elgg_view_menu_item($this, $vars);
643  }
644 }
$context
Definition: add.php:11
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
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.
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.
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
$item
Definition: item.php:12
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
$value
Definition: longtext.php:26
elgg_normalize_url($url)
Definition: output.php:311
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:250
$options
Definition: index.php:14
$key
Definition: summary.php:34
$priority
if(file_exists($welcome)) $vars
Definition: upgrade.php:93
ui datepicker title
Definition: admin.php:616
elgg input text
Definition: admin.php:490
elgg_view_menu_item(\ElggMenuItem $item, array $vars=array())
Render a menu item (usually as a link)
Definition: views.php:733