Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 
121  public static function factory($options) {
122  if (!isset($options['name']) || !isset($options['text'])) {
123  return null;
124  }
125  if (!isset($options['href'])) {
126  $options['href'] = '';
127  }
128 
129  $item = new \ElggMenuItem($options['name'], $options['text'], $options['href']);
130  unset($options['name']);
131  unset($options['text']);
132  unset($options['href']);
133 
134  // special catch in case someone uses context rather than contexts
135  if (isset($options['context'])) {
136  $options['contexts'] = $options['context'];
137  unset($options['context']);
138  }
139 
140  // make sure contexts is set correctly
141  if (isset($options['contexts'])) {
142  $item->setContext($options['contexts']);
143  unset($options['contexts']);
144  }
145 
146  if (isset($options['link_class'])) {
147  $item->setLinkClass($options['link_class']);
148  unset($options['link_class']);
149  } elseif (isset($options['class'])) {
150  elgg_deprecated_notice("\ElggMenuItem::factory() does not accept 'class' key anymore, use 'link_class' instead", 1.9);
151  $item->setLinkClass($options['class']);
152  unset($options['class']);
153  }
154 
155  if (isset($options['item_class'])) {
156  $item->setItemClass($options['item_class']);
157  unset($options['item_class']);
158  }
159 
160  if (isset($options['data']) && is_array($options['data'])) {
161  $item->setData($options['data']);
162  unset($options['data']);
163  }
164 
165  foreach ($options as $key => $value) {
166  if (isset($item->data[$key])) {
167  $item->data[$key] = $value;
168  } else {
169  $item->$key = $value;
170  }
171  }
172 
173  return $item;
174  }
175 
186  public function setData($key, $value = null) {
187  if (is_array($key)) {
188  $this->data = array_merge($this->data, $key);
189  } else {
190  $this->data[$key] = $value;
191  }
192  }
193 
200  public function getData($key) {
201  if (isset($this->data[$key])) {
202  return $this->data[$key];
203  } else {
204  return null;
205  }
206  }
207 
214  public function setName($name) {
215  $this->data['name'] = $name;
216  }
217 
223  public function getName() {
224  return $this->data['name'];
225  }
226 
233  public function setText($text) {
234  $this->text = $text;
235  }
236 
242  public function getText() {
243  return $this->text;
244  }
245 
253  public function setHref($href) {
254  $this->href = $href;
255  }
256 
262  public function getHref() {
263  return $this->href;
264  }
265 
272  public function setContext($contexts) {
273  if (is_string($contexts)) {
274  $contexts = array($contexts);
275  }
276  $this->data['contexts'] = $contexts;
277  }
278 
284  public function getContext() {
285  return $this->data['contexts'];
286  }
287 
295  public function inContext($context = '') {
296  if (in_array('all', $this->data['contexts'])) {
297  return true;
298  }
299 
300  if ($context) {
301  return in_array($context, $this->data['contexts']);
302  }
303 
304  foreach ($this->data['contexts'] as $context) {
305  if (elgg_in_context($context)) {
306  return true;
307  }
308  }
309  return false;
310  }
311 
318  public function setSelected($state = true) {
319  $this->data['selected'] = $state;
320  }
321 
327  public function getSelected() {
328  return $this->data['selected'];
329  }
330 
337  public function setTooltip($text) {
338  $this->title = $text;
339  }
340 
346  public function getTooltip() {
347  return $this->title;
348  }
349 
356  public function setConfirmText($text) {
357  $this->confirm = $text;
358  }
359 
365  public function getConfirmText() {
366  return $this->confirm;
367  }
368 
375  public function setLinkClass($class) {
376  if (!is_array($class)) {
377  $this->data['linkClass'] = array($class);
378  } else {
379  $this->data['linkClass'] = $class;
380  }
381  }
382 
388  public function getLinkClass() {
389  return implode(' ', $this->data['linkClass']);
390  }
391 
398  public function addLinkClass($class) {
399  $this->addClass($this->data['linkClass'], $class);
400  }
401 
408  public function setDeps($modules) {
409  $this->data['deps'] = (array) $modules;
410  }
411 
417  public function getDeps() {
418  $modules = (array) $this->data['deps'];
419  return array_filter($modules, function($m) {
420  return is_string($m) && !empty($m);
421  });
422  }
423 
430  public function addDeps($modules) {
431  $current = $this->getDeps();
432  $this->setDeps($current + (array) $modules);
433  }
434 
441  public function setItemClass($class) {
442  if (!is_array($class)) {
443  $this->data['itemClass'] = array($class);
444  } else {
445  $this->data['itemClass'] = $class;
446  }
447  }
448 
454  public function getItemClass() {
455  // allow people to specify name with underscores and colons
456  $name = strtolower($this->getName());
457  $name = str_replace('_', '-', $name);
458  $name = str_replace(':', '-', $name);
459  $name = str_replace(' ', '-', $name);
460 
461  $class = implode(' ', $this->data['itemClass']);
462  if ($class) {
463  return "elgg-menu-item-$name $class";
464  } else {
465  return "elgg-menu-item-$name";
466  }
467  }
468 
476  public function addItemClass($class) {
477  $this->addClass($this->data['itemClass'], $class);
478  }
479 
480  // @codingStandardsIgnoreStart
488  protected function addClass(array &$current, $additional) {
489  if (!is_array($additional)) {
490  $current[] = $additional;
491  } else {
492  $current = array_merge($current, $additional);
493  }
494  }
495  // @codingStandardsIgnoreEnd
496 
497 
505  public function setWeight($priority) {
506  elgg_deprecated_notice("\ElggMenuItem::setWeight() deprecated by \ElggMenuItem::setPriority()", 1.9);
507  $this->data['priority'] = $priority;
508  }
509 
516  public function getWeight() {
517  elgg_deprecated_notice("\ElggMenuItem::getWeight() deprecated by \ElggMenuItem::getPriority()", 1.9);
518  return $this->data['priority'];
519  }
520 
527  public function setPriority($priority) {
528  $this->data['priority'] = $priority;
529  }
530 
536  public function getPriority() {
537  return $this->data['priority'];
538  }
539 
546  public function setSection($section) {
547  $this->data['section'] = $section;
548  }
549 
555  public function getSection() {
556  return $this->data['section'];
557  }
558 
565  public function setParentName($name) {
566  $this->data['parent_name'] = $name;
567  }
568 
574  public function getParentName() {
575  return $this->data['parent_name'];
576  }
577 
587  public function setParent($parent) {
588  $this->data['parent'] = $parent;
589  }
590 
599  public function getParent() {
600  return $this->data['parent'];
601  }
602 
612  public function addChild($item) {
613  $this->data['children'][] = $item;
614  }
615 
625  public function setChildren($children) {
626  $this->data['children'] = $children;
627  }
628 
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  $this->data['children'][$key]->data['original_order'] = $key;
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 
677  public function getContent(array $vars = array()) {
678  elgg_deprecated_notice("\ElggMenuItem::getContent() deprecated by elgg_view_menu_item()", 1.9);
679  return elgg_view_menu_item($this, $vars);
680  }
681 }
$modules
Boot all the plugins and trigger the [init, system] hook.
Definition: init.js.php:8
$contexts
Definition: contents.php:13
$context
Definition: add.php:11
$m
Definition: metadata.php:11
elgg_normalize_url($url)
Definition: output.php:290
setParentName($name)
Set the parent identifier.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
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.
$value
Definition: longtext.php:26
getContext()
Get an array of context strings.
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
$vars['entity']
$state
Definition: contents.php:4
$options
Elgg admin footer.
Definition: footer.php:6
getContent(array $vars=array())
Get the menu item content (usually a link)
setChildren($children)
Set the menu item'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.
$key
Definition: summary.php:34
getTooltip()
Get the tool tip text.
setContext($contexts)
Set the contexts that this menu item is available for.
$item
Definition: item.php:12
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:241
getSection()
Get the section identifier.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
getPriority()
Get the priority of the menu item.
elgg_view_menu_item(\ElggMenuItem $item, array $vars=array())
Render a menu item (usually as a link)
Definition: views.php:774
setName($name)
Set the identifier of the menu item.
setParent($parent)
Set the parent menu item.
setWeight($priority)
Set the priority of the menu item.
setHref($href)
Set the URL of the menu item.
setSelected($state=true)
Set the selected flag.
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.
if($item->getSelected()) $children
Definition: item.php:21
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.
getParent()
Get the parent menu item.
setSection($section)
Set the section identifier.
setDeps($modules)
Set required AMD modules.
getWeight()
Get the priority of the menu item.
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.
setPriority($priority)
Set the priority of the menu item.
addLinkClass($class)
Add a link class.