From 0beaedbce34e7a11407f19f0265ac27eaf7010ca Mon Sep 17 00:00:00 2001 From: Sebastian Stumpf Date: Thu, 24 Sep 2015 11:28:59 +0200 Subject: [PATCH] Added methods to activate/deactivate single entries to BaseMenu. --- protected/humhub/widgets/BaseMenu.php | 164 ++++++++++++++++---------- 1 file changed, 102 insertions(+), 62 deletions(-) diff --git a/protected/humhub/widgets/BaseMenu.php b/protected/humhub/widgets/BaseMenu.php index fbaab3c7c5..07f81dec4a 100644 --- a/protected/humhub/widgets/BaseMenu.php +++ b/protected/humhub/widgets/BaseMenu.php @@ -1,24 +1,27 @@ addItemGroup(array('id' => '', 'label' => '')); + $this->addItemGroup(array( + 'id' => '', + 'label' => '' + )); $this->trigger(self::EVENT_INIT); return parent::init(); } @@ -49,105 +55,106 @@ class BaseMenu extends \yii\base\Widget /** * Adds new Item to the menu * - * @param Array $item with item definitions + * @param Array $item + * with item definitions */ public function addItem($item) { - - if (!isset($item['label'])) + if (! isset($item['label'])) $item['label'] = 'Unnamed'; - - if (!isset($item['url'])) + + if (! isset($item['url'])) $item['url'] = '#'; - - if (!isset($item['icon'])) + + if (! isset($item['icon'])) $item['icon'] = ''; - - if (!isset($item['group'])) + + if (! isset($item['group'])) $item['group'] = ''; - - if (!isset($item['htmlOptions'])) + + if (! isset($item['htmlOptions'])) $item['htmlOptions'] = array(); - + /** + * * @deprecated since version 0.11 use directly htmlOptions instead */ if (isset($item['target'])) { $item['htmlOptions']['target'] = $item['target']; } - - if (!isset($item['sortOrder'])) + + if (! isset($item['sortOrder'])) $item['sortOrder'] = 1000; - - if (!isset($item['newItemCount'])) + + if (! isset($item['newItemCount'])) $item['newItemCount'] = 0; - - if (!isset($item['isActive'])) + + if (! isset($item['isActive'])) $item['isActive'] = false; - - if (isset($item['isVisible']) && !$item['isVisible']) + + if (isset($item['isVisible']) && ! $item['isVisible']) return; - - // Build Item CSS Class - if (!isset($item['htmlOptions']['class'])) + + // Build Item CSS Class + if (! isset($item['htmlOptions']['class'])) $item['htmlOptions']['class'] = ""; - + if ($item['isActive']) { $item['htmlOptions']['class'] .= " active"; } - + if (isset($item['id'])) { $item['htmlOptions']['class'] .= " " . $item['id']; } - + $this->items[] = $item; } /** * Adds new Item Group to the menu * - * @param Array $itemGroup with group definition + * @param Array $itemGroup + * with group definition */ public function addItemGroup($itemGroup) { - - if (!isset($itemGroup['id'])) + if (! isset($itemGroup['id'])) $itemGroup['id'] = 'default'; - - if (!isset($itemGroup['label'])) + + if (! isset($itemGroup['label'])) $itemGroup['label'] = 'Unnamed'; - - if (!isset($itemGroup['icon'])) + + if (! isset($itemGroup['icon'])) $itemGroup['icon'] = ''; - - if (!isset($itemGroup['sortOrder'])) + + if (! isset($itemGroup['sortOrder'])) $itemGroup['sortOrder'] = 1000; - - if (isset($itemGroup['isVisible']) && !$itemGroup['isVisible']) + + if (isset($itemGroup['isVisible']) && ! $itemGroup['isVisible']) return; - + $this->itemGroups[] = $itemGroup; } /** * Returns Items of this Navigation * - * @param String $group limits the items to a specified group + * @param String $group + * limits the items to a specified group * @return Array a list of items with definition */ public function getItems($group = "") { - $this->sortItems(); - + $ret = array(); - + foreach ($this->items as $item) { - + if ($group == $item['group']) $ret[] = $item; } - + return $ret; } @@ -156,15 +163,16 @@ class BaseMenu extends \yii\base\Widget */ private function sortItems() { - - usort($this->items, function($a, $b) { + usort($this->items, function ($a, $b) + { if ($a['sortOrder'] == $b['sortOrder']) { return 0; - } else if ($a['sortOrder'] < $b['sortOrder']) { - return -1; - } else { - return 1; - } + } else + if ($a['sortOrder'] < $b['sortOrder']) { + return - 1; + } else { + return 1; + } }); } @@ -173,15 +181,16 @@ class BaseMenu extends \yii\base\Widget */ private function sortItemGroups() { - - usort($this->itemGroups, function($a, $b) { + usort($this->itemGroups, function ($a, $b) + { if ($a['sortOrder'] == $b['sortOrder']) { return 0; - } else if ($a['sortOrder'] < $b['sortOrder']) { - return -1; - } else { - return 1; - } + } else + if ($a['sortOrder'] < $b['sortOrder']) { + return - 1; + } else { + return 1; + } }); } @@ -205,6 +214,37 @@ class BaseMenu extends \yii\base\Widget return $this->render($this->template, array()); } + /** + * Add the active class from a menue item. + * + * @param String $url + * the URL of the item to mark. You can use Url::toRoute(...) to generate it. + */ + public function markAsActive($url) + { + foreach ($this->items as $key => $item) { + if ($item['url'] == $url) { + $this->items[$key]['htmlOptions']['class'] = 'active'; + $this->items[$key]['htmlOptions']['isActive'] = true; + } + } + } + + /** + * Remove the active class from a menue item. + * + * @param String $url + * the URL of the item to mark. You can use Url::toRoute(...) to generate it. + */ + public function markAsInactive($url) + { + foreach ($this->items as $key => $item) { + if ($item['url'] == $url) { + $this->items[$key]['htmlOptions']['class'] = ''; + $this->items[$key]['htmlOptions']['isActive'] = false; + } + } + } } ?>