123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- <?php
- class ElggMenuItem {
-
- protected $data = array(
-
- 'name' => '',
-
- 'contexts' => array('all'),
-
- 'section' => 'default',
-
- 'priority' => 100,
-
- 'selected' => false,
-
- 'parent_name' => '',
-
- 'parent' => null,
-
- 'children' => array(),
-
- 'itemClass' => array(),
-
- 'linkClass' => array(),
- );
-
- protected $text;
-
- protected $href = null;
-
- protected $title = false;
-
- protected $confirm = '';
-
- public function __construct($name, $text, $href) {
- $this->text = $text;
- if ($href) {
- $this->href = elgg_normalize_url($href);
- } else {
- $this->href = $href;
- }
- $this->data['name'] = $name;
- }
-
- public static function factory($options) {
- if (!isset($options['name']) || !isset($options['text'])) {
- return null;
- }
- if (!isset($options['href'])) {
- $options['href'] = '';
- }
- $item = new \ElggMenuItem($options['name'], $options['text'], $options['href']);
- unset($options['name']);
- unset($options['text']);
- unset($options['href']);
-
- if (isset($options['context'])) {
- $options['contexts'] = $options['context'];
- unset($options['context']);
- }
-
-
- if (isset($options['contexts'])) {
- $item->setContext($options['contexts']);
- unset($options['contexts']);
- }
- if (isset($options['link_class'])) {
- $item->setLinkClass($options['link_class']);
- unset($options['link_class']);
- } elseif (isset($options['class'])) {
- elgg_deprecated_notice("\ElggMenuItem::factory() does not accept 'class' key anymore, use 'link_class' instead", 1.9);
- $item->setLinkClass($options['class']);
- unset($options['class']);
- }
- if (isset($options['item_class'])) {
- $item->setItemClass($options['item_class']);
- unset($options['item_class']);
- }
- if (isset($options['data']) && is_array($options['data'])) {
- $item->setData($options['data']);
- unset($options['data']);
- }
-
- foreach ($options as $key => $value) {
- if (isset($item->data[$key])) {
- $item->data[$key] = $value;
- } else {
- $item->$key = $value;
- }
- }
- return $item;
- }
-
- public function setData($key, $value = null) {
- if (is_array($key)) {
- $this->data = array_merge($this->data, $key);
- } else {
- $this->data[$key] = $value;
- }
- }
-
- public function getData($key) {
- if (isset($this->data[$key])) {
- return $this->data[$key];
- } else {
- return null;
- }
- }
-
- public function setName($name) {
- $this->data['name'] = $name;
- }
-
- public function getName() {
- return $this->data['name'];
- }
-
- public function setText($text) {
- $this->text = $text;
- }
-
- public function getText() {
- return $this->text;
- }
-
- public function setHref($href) {
- $this->href = $href;
- }
-
- public function getHref() {
- return $this->href;
- }
-
- public function setContext($contexts) {
- if (is_string($contexts)) {
- $contexts = array($contexts);
- }
- $this->data['contexts'] = $contexts;
- }
-
- public function getContext() {
- return $this->data['contexts'];
- }
-
- public function inContext($context = '') {
- if (in_array('all', $this->data['contexts'])) {
- return true;
- }
- if ($context) {
- return in_array($context, $this->data['contexts']);
- }
- foreach ($this->data['contexts'] as $context) {
- if (elgg_in_context($context)) {
- return true;
- }
- }
- return false;
- }
-
- public function setSelected($state = true) {
- $this->data['selected'] = $state;
- }
-
- public function getSelected() {
- return $this->data['selected'];
- }
-
- public function setTooltip($text) {
- $this->title = $text;
- }
-
- public function getTooltip() {
- return $this->title;
- }
-
- public function setConfirmText($text) {
- $this->confirm = $text;
- }
-
- public function getConfirmText() {
- return $this->confirm;
- }
-
- public function setLinkClass($class) {
- if (!is_array($class)) {
- $this->data['linkClass'] = array($class);
- } else {
- $this->data['linkClass'] = $class;
- }
- }
-
- public function getLinkClass() {
- return implode(' ', $this->data['linkClass']);
- }
-
- public function addLinkClass($class) {
- $this->addClass($this->data['linkClass'], $class);
- }
-
- public function setItemClass($class) {
- if (!is_array($class)) {
- $this->data['itemClass'] = array($class);
- } else {
- $this->data['itemClass'] = $class;
- }
- }
-
- public function getItemClass() {
-
- $name = strtolower($this->getName());
- $name = str_replace('_', '-', $name);
- $name = str_replace(':', '-', $name);
- $name = str_replace(' ', '-', $name);
- $class = implode(' ', $this->data['itemClass']);
- if ($class) {
- return "elgg-menu-item-$name $class";
- } else {
- return "elgg-menu-item-$name";
- }
- }
-
- public function addItemClass($class) {
- $this->addClass($this->data['itemClass'], $class);
- }
-
-
- protected function addClass(array &$current, $additional) {
- if (!is_array($additional)) {
- $current[] = $additional;
- } else {
- $current = array_merge($current, $additional);
- }
- }
-
-
- public function setWeight($priority) {
- elgg_deprecated_notice("\ElggMenuItem::setWeight() deprecated by \ElggMenuItem::setPriority()", 1.9);
- $this->data['priority'] = $priority;
- }
-
- public function getWeight() {
- elgg_deprecated_notice("\ElggMenuItem::getWeight() deprecated by \ElggMenuItem::getPriority()", 1.9);
- return $this->data['priority'];
- }
-
- public function setPriority($priority) {
- $this->data['priority'] = $priority;
- }
-
- public function getPriority() {
- return $this->data['priority'];
- }
-
- public function setSection($section) {
- $this->data['section'] = $section;
- }
-
- public function getSection() {
- return $this->data['section'];
- }
-
- public function setParentName($name) {
- $this->data['parent_name'] = $name;
- }
-
- public function getParentName() {
- return $this->data['parent_name'];
- }
-
- public function setParent($parent) {
- $this->data['parent'] = $parent;
- }
-
- public function getParent() {
- return $this->data['parent'];
- }
-
- public function addChild($item) {
- $this->data['children'][] = $item;
- }
-
- public function setChildren($children) {
- $this->data['children'] = $children;
- }
-
- public function getChildren() {
- return $this->data['children'];
- }
-
- public function sortChildren($sortFunction) {
- foreach ($this->data['children'] as $key => $node) {
- $this->data['children'][$key]->data['original_order'] = $key;
- }
- usort($this->data['children'], $sortFunction);
- }
-
- public function getValues() {
- $values = get_object_vars($this);
- unset($values['data']);
- return $values;
- }
-
- public function getContent(array $vars = array()) {
- elgg_deprecated_notice("\ElggMenuItem::getContent() deprecated by elgg_view_menu_item()", 1.9);
- return elgg_view_menu_item($this, $vars);
- }
- }
|