name = $name; $this->text = $text; if ($href) { $this->href = elgg_normalize_url($href); } } /** * ElggMenuItem factory method * * This static method creates an ElggMenuItem from an associative array. * Required keys are name, text, and href. * * @param array $options Option array of key value pairs * * @return ElggMenuItem or NULL on error */ public static function factory($options) { if (!isset($options['name']) || !isset($options['text'])) { return NULL; } $item = new ElggMenuItem($options['name'], $options['text'], $options['href']); unset($options['name']); unset($options['text']); unset($options['href']); // special catch in case someone uses context rather than contexts if (isset($options['context'])) { $options['contexts'] = $options['context']; unset($options['context']); } foreach ($options as $key => $value) { $item->$key = $value; } // make sure contexts is set correctly if (isset($options['contexts'])) { $item->setContext($options['contexts']); } return $item; } /** * Get the identifier of the menu item * * @return string */ public function getName() { return $this->name; } /** * Get the display text of the menu * * @return string */ public function getText() { return $this->text; } /** * Get the URL of the menu item * * @return string */ public function getHref() { return $this->href; } /** * Set the contexts that this menu item is available for * * @param array $contexts An array of context strings * * @return void */ public function setContext($contexts) { if (is_string($contexts)) { $contexts = array($contexts); } $this->contexts = $contexts; } /** * Get an array of context strings * * @return array */ public function getContext() { return $this->contexts; } /** * Should this menu item be used given the current context * * @param string $context A context string (default is empty string for * current context stack. * * @return bool */ public function inContext($context = '') { if ($context) { return in_array($context, $this->contexts); } if (in_array('all', $this->contexts)) { return true; } foreach ($this->contexts as $context) { if (elgg_in_context($context)) { return true; } } return false; } /** * Set the selected flag * * @param bool $state Selected state (default is true) * * @return void */ public function setSelected($state = true) { $this->selected = $state; } /** * Get selected state * * @return bool */ public function getSelected() { return $this->selected; } /** * Set the tool tip text * * @param string $text The text of the tool tip * * @return void */ public function setTooltip($text) { $this->title = $text; } /** * Get the tool tip text * * @return string */ public function getTooltip() { return $this->title; } /** * Set the anchor class * * @param mixed $class An array of class names, or a single string class name. * * @return void */ public function setLinkClass($class) { if (!is_array($class)) { $this->linkClass[] = $class; } else { $this->linkClass = $class; } } /** * Get the anchor classes as text * * @return string */ public function getLinkClass() { return implode(' ', $this->linkClass); } /** * Set the li classes * * @param mixed $class An array of class names, or a single string class name. * * @return void */ public function setItemClass($class) { if (!is_array($class)) { $this->itemClass[] = $class; } else { $this->itemClass = $class; } } /** * Get the li classes as text * * @return string */ public function getItemClass() { return implode(' ', $this->itemClass); } /** * Set the weight of the menu item * * @param int $weight The lower weight items float to the top of the menu * * @return void */ public function setWeight($weight) { $this->weight = $weight; } /** * Get the weight of the menu item * * @return int */ public function getWeight() { return $this->weight; } /** * Set the section identifier * * @param string $section The identifier of the section * * @return void */ public function setSection($section) { $this->section = $section; } /** * Get the section identifier * * @return string */ public function getSection() { return $this->section; } /** * Set the parent identifier * * @param string $parent_name The identifier of the parent ElggMenuItem * * @return void */ public function setParentName($parent_name) { $this->parent_name = $parent_name; } /** * Get the parent identifier * * @return string */ public function getParentName() { return $this->parent_name; } /** * Set the parent menu item * * @param ElggMenuItem $parent * * @return void */ public function setParent($parent) { $this->parent = $parent; } /** * Get the parent menu item * * @return ElggMenuItem or null */ public function getParent() { return $this->parent; } /** * Add a child menu item * * @param ElggMenuItem $item * * @return void */ public function addChild($item) { $this->children[] = $item; } /** * Get the children menu items * * @return array */ public function getChildren() { return $this->children; } /** * Sort the children * * @param string $sort_function * * @return void */ public function sortChildren($sort_function) { usort($this->children, $sort_function); } /** * Get the menu link * * @params array $vars Options to pass to output/url * * @return string */ public function getLink(array $vars = array()) { $vars['text'] = $this->text; if ($this->href) { $vars['href'] = $this->href; } if ($this->linkClass) { $vars['class'] = $this->linkClass; } if ($this->title) { $vars['title'] = $this->title; } return elgg_view('output/url', $vars); } }