Merge branch 'master' of github.com:humhub/humhub

This commit is contained in:
Lucas Bartholemy 2015-09-24 15:38:22 +02:00
commit 22e8867782

View File

@ -1,24 +1,27 @@
<?php <?php
namespace humhub\widgets; namespace humhub\widgets;
class BaseMenu extends \yii\base\Widget class BaseMenu extends \yii\base\Widget
{ {
const EVENT_INIT = 'init'; const EVENT_INIT = 'init';
const EVENT_RUN = 'run'; const EVENT_RUN = 'run';
/** /**
*
* @var Array of items * @var Array of items
*/ */
public $items = array(); public $items = array();
/** /**
*
* @var Array of item groups * @var Array of item groups
*/ */
public $itemGroups = array(); public $itemGroups = array();
/** /**
*
* @var String type of the navigation, optional for identifing. * @var String type of the navigation, optional for identifing.
*/ */
public $type = ""; public $type = "";
@ -27,8 +30,8 @@ class BaseMenu extends \yii\base\Widget
* Template of the navigation * Template of the navigation
* *
* Available default template views: * Available default template views:
* - leftNavigation * - leftNavigation
* - tabMenu * - tabMenu
* *
* @var String template file * @var String template file
*/ */
@ -41,7 +44,10 @@ class BaseMenu extends \yii\base\Widget
*/ */
public function init() public function init()
{ {
$this->addItemGroup(array('id' => '', 'label' => '')); $this->addItemGroup(array(
'id' => '',
'label' => ''
));
$this->trigger(self::EVENT_INIT); $this->trigger(self::EVENT_INIT);
return parent::init(); return parent::init();
} }
@ -49,105 +55,106 @@ class BaseMenu extends \yii\base\Widget
/** /**
* Adds new Item to the menu * Adds new Item to the menu
* *
* @param Array $item with item definitions * @param Array $item
* with item definitions
*/ */
public function addItem($item) public function addItem($item)
{ {
if (! isset($item['label']))
if (!isset($item['label']))
$item['label'] = 'Unnamed'; $item['label'] = 'Unnamed';
if (!isset($item['url'])) if (! isset($item['url']))
$item['url'] = '#'; $item['url'] = '#';
if (!isset($item['icon'])) if (! isset($item['icon']))
$item['icon'] = ''; $item['icon'] = '';
if (!isset($item['group'])) if (! isset($item['group']))
$item['group'] = ''; $item['group'] = '';
if (!isset($item['htmlOptions'])) if (! isset($item['htmlOptions']))
$item['htmlOptions'] = array(); $item['htmlOptions'] = array();
/** /**
*
* @deprecated since version 0.11 use directly htmlOptions instead * @deprecated since version 0.11 use directly htmlOptions instead
*/ */
if (isset($item['target'])) { if (isset($item['target'])) {
$item['htmlOptions']['target'] = $item['target']; $item['htmlOptions']['target'] = $item['target'];
} }
if (!isset($item['sortOrder'])) if (! isset($item['sortOrder']))
$item['sortOrder'] = 1000; $item['sortOrder'] = 1000;
if (!isset($item['newItemCount'])) if (! isset($item['newItemCount']))
$item['newItemCount'] = 0; $item['newItemCount'] = 0;
if (!isset($item['isActive'])) if (! isset($item['isActive']))
$item['isActive'] = false; $item['isActive'] = false;
if (isset($item['isVisible']) && !$item['isVisible']) if (isset($item['isVisible']) && ! $item['isVisible'])
return; return;
// Build Item CSS Class // Build Item CSS Class
if (!isset($item['htmlOptions']['class'])) if (! isset($item['htmlOptions']['class']))
$item['htmlOptions']['class'] = ""; $item['htmlOptions']['class'] = "";
if ($item['isActive']) { if ($item['isActive']) {
$item['htmlOptions']['class'] .= " active"; $item['htmlOptions']['class'] .= " active";
} }
if (isset($item['id'])) { if (isset($item['id'])) {
$item['htmlOptions']['class'] .= " " . $item['id']; $item['htmlOptions']['class'] .= " " . $item['id'];
} }
$this->items[] = $item; $this->items[] = $item;
} }
/** /**
* Adds new Item Group to the menu * Adds new Item Group to the menu
* *
* @param Array $itemGroup with group definition * @param Array $itemGroup
* with group definition
*/ */
public function addItemGroup($itemGroup) public function addItemGroup($itemGroup)
{ {
if (! isset($itemGroup['id']))
if (!isset($itemGroup['id']))
$itemGroup['id'] = 'default'; $itemGroup['id'] = 'default';
if (!isset($itemGroup['label'])) if (! isset($itemGroup['label']))
$itemGroup['label'] = 'Unnamed'; $itemGroup['label'] = 'Unnamed';
if (!isset($itemGroup['icon'])) if (! isset($itemGroup['icon']))
$itemGroup['icon'] = ''; $itemGroup['icon'] = '';
if (!isset($itemGroup['sortOrder'])) if (! isset($itemGroup['sortOrder']))
$itemGroup['sortOrder'] = 1000; $itemGroup['sortOrder'] = 1000;
if (isset($itemGroup['isVisible']) && !$itemGroup['isVisible']) if (isset($itemGroup['isVisible']) && ! $itemGroup['isVisible'])
return; return;
$this->itemGroups[] = $itemGroup; $this->itemGroups[] = $itemGroup;
} }
/** /**
* Returns Items of this Navigation * 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 * @return Array a list of items with definition
*/ */
public function getItems($group = "") public function getItems($group = "")
{ {
$this->sortItems(); $this->sortItems();
$ret = array(); $ret = array();
foreach ($this->items as $item) { foreach ($this->items as $item) {
if ($group == $item['group']) if ($group == $item['group'])
$ret[] = $item; $ret[] = $item;
} }
return $ret; return $ret;
} }
@ -156,15 +163,16 @@ class BaseMenu extends \yii\base\Widget
*/ */
private function sortItems() private function sortItems()
{ {
usort($this->items, function ($a, $b)
usort($this->items, function($a, $b) { {
if ($a['sortOrder'] == $b['sortOrder']) { if ($a['sortOrder'] == $b['sortOrder']) {
return 0; return 0;
} else if ($a['sortOrder'] < $b['sortOrder']) { } else
return -1; if ($a['sortOrder'] < $b['sortOrder']) {
} else { return - 1;
return 1; } else {
} return 1;
}
}); });
} }
@ -173,15 +181,16 @@ class BaseMenu extends \yii\base\Widget
*/ */
private function sortItemGroups() private function sortItemGroups()
{ {
usort($this->itemGroups, function ($a, $b)
usort($this->itemGroups, function($a, $b) { {
if ($a['sortOrder'] == $b['sortOrder']) { if ($a['sortOrder'] == $b['sortOrder']) {
return 0; return 0;
} else if ($a['sortOrder'] < $b['sortOrder']) { } else
return -1; if ($a['sortOrder'] < $b['sortOrder']) {
} else { return - 1;
return 1; } else {
} return 1;
}
}); });
} }
@ -205,6 +214,37 @@ class BaseMenu extends \yii\base\Widget
return $this->render($this->template, array()); 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;
}
}
}
} }
?> ?>