|
- <?php
-
-
- namespace yii\widgets;
-
- use Yii;
- use yii\base\Widget;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Url;
- use yii\helpers\Html;
-
-
- class Menu extends Widget
- {
-
-
- public $items = [];
-
-
- public $itemOptions = [];
-
-
- public $linkTemplate = '<a href="{url}">{label}</a>';
-
-
- public $labelTemplate = '{label}';
-
-
- public $submenuTemplate = "\n<ul>\n{items}\n</ul>\n";
-
-
- public $encodeLabels = true;
-
-
- public $activeCssClass = 'active';
-
-
- public $activateItems = true;
-
-
- public $activateParents = false;
-
-
- public $hideEmptyItems = true;
-
-
- public $options = [];
-
-
- public $firstItemCssClass;
-
-
- public $lastItemCssClass;
-
-
- public $route;
-
-
- public $params;
-
-
-
-
- public function run()
- {
- if ($this->route === null && Yii::$app->controller !== null) {
- $this->route = Yii::$app->controller->getRoute();
- }
- if ($this->params === null) {
- $this->params = Yii::$app->request->getQueryParams();
- }
- $items = $this->normalizeItems($this->items, $hasActiveChild);
- if (!empty($items)) {
- $options = $this->options;
- $tag = ArrayHelper::remove($options, 'tag', 'ul');
-
- echo Html::tag($tag, $this->renderItems($items), $options);
- }
- }
-
-
-
- protected function renderItems($items)
- {
- $n = count($items);
- $lines = [];
- foreach ($items as $i => $item) {
- $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
- $tag = ArrayHelper::remove($options, 'tag', 'li');
- $class = [];
- if ($item['active']) {
- $class[] = $this->activeCssClass;
- }
- if ($i === 0 && $this->firstItemCssClass !== null) {
- $class[] = $this->firstItemCssClass;
- }
- if ($i === $n - 1 && $this->lastItemCssClass !== null) {
- $class[] = $this->lastItemCssClass;
- }
- if (!empty($class)) {
- if (empty($options['class'])) {
- $options['class'] = implode(' ', $class);
- } else {
- $options['class'] .= ' ' . implode(' ', $class);
- }
- }
-
- $menu = $this->renderItem($item);
- if (!empty($item['items'])) {
- $submenuTemplate = ArrayHelper::getValue($item, 'submenuTemplate', $this->submenuTemplate);
- $menu .= strtr($submenuTemplate, [
- '{items}' => $this->renderItems($item['items']),
- ]);
- }
- $lines[] = Html::tag($tag, $menu, $options);
- }
-
- return implode("\n", $lines);
- }
-
-
-
- protected function renderItem($item)
- {
- if (isset($item['url'])) {
- $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
-
- return strtr($template, [
- '{url}' => Html::encode(Url::to($item['url'])),
- '{label}' => $item['label'],
- ]);
- } else {
- $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
-
- return strtr($template, [
- '{label}' => $item['label'],
- ]);
- }
- }
-
-
-
- protected function normalizeItems($items, &$active)
- {
- foreach ($items as $i => $item) {
- if (isset($item['visible']) && !$item['visible']) {
- unset($items[$i]);
- continue;
- }
- if (!isset($item['label'])) {
- $item['label'] = '';
- }
- $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
- $items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label'];
- $hasActiveChild = false;
- if (isset($item['items'])) {
- $items[$i]['items'] = $this->normalizeItems($item['items'], $hasActiveChild);
- if (empty($items[$i]['items']) && $this->hideEmptyItems) {
- unset($items[$i]['items']);
- if (!isset($item['url'])) {
- unset($items[$i]);
- continue;
- }
- }
- }
- if (!isset($item['active'])) {
- if ($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item)) {
- $active = $items[$i]['active'] = true;
- } else {
- $items[$i]['active'] = false;
- }
- } elseif ($item['active']) {
- $active = true;
- }
- }
-
- return array_values($items);
- }
-
-
-
- protected function isItemActive($item)
- {
- if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
- $route = Yii::getAlias($item['url'][0]);
- if ($route[0] !== '/' && Yii::$app->controller) {
- $route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
- }
- if (ltrim($route, '/') !== $this->route) {
- return false;
- }
- unset($item['url']['#']);
- if (count($item['url']) > 1) {
- $params = $item['url'];
- unset($params[0]);
- foreach ($params as $name => $value) {
- if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
- return false;
- }
- }
- }
-
- return true;
- }
-
- return false;
- }
- }
|