Menu.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace backend\widgets;
  3. use Yii;
  4. use yii\helpers\ArrayHelper;
  5. use yii\helpers\Html;
  6. use yii\helpers\Url;
  7. /**
  8. * Class Menu
  9. * Theme menu widget.
  10. */
  11. class Menu extends \yii\widgets\Menu
  12. {
  13. /**
  14. * @inheritdoc
  15. */
  16. public $linkTemplate = '<a href="{url}" title="{label}">{icon} <span>{label}</span></a>';
  17. public $submenuTemplate = "\n<ul class='treeview-menu' {show}>\n{items}\n</ul>\n";
  18. public $activateParents = true;
  19. /**
  20. * @inheritdoc
  21. */
  22. protected function renderItem($item)
  23. {
  24. if(isset($item['items'])) {
  25. $linkTemplate = '<a href="{url}" title="{label}">{icon} <span>{label}</span> <i class="fa fa-angle-left pull-right"></i></a>';
  26. } else {
  27. $linkTemplate = $this->linkTemplate;
  28. }
  29. if (isset($item['url'])) {
  30. $template = ArrayHelper::getValue($item, 'template', $linkTemplate);
  31. $replace = !empty($item['icon']) ? [
  32. '{url}' => Url::to($item['url']),
  33. '{label}' => $item['label'],
  34. '{icon}' => Html::icon($item['icon'])
  35. ] : [
  36. '{url}' => Url::to($item['url']),
  37. '{label}' => $item['label'],
  38. '{icon}' => Html::icon('circle-o'),
  39. ];
  40. return strtr($template, $replace);
  41. } else {
  42. $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
  43. $replace = !empty($item['icon']) ? [
  44. '{label}' => $item['label'],
  45. '{icon}' => Html::icon($item['icon'])
  46. ] : [
  47. '{label}' => $item['label'],
  48. '{icon}' => Html::icon('circle-o'),
  49. ];
  50. return strtr($template, $replace);
  51. }
  52. }
  53. /**
  54. * Recursively renders the menu items (without the container tag).
  55. * @param array $items the menu items to be rendered recursively
  56. * @return string the rendering result
  57. */
  58. protected function renderItems($items)
  59. {
  60. $n = count($items);
  61. $lines = [];
  62. foreach ($items as $i => $item) {
  63. $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
  64. $tag = ArrayHelper::remove($options, 'tag', 'li');
  65. $class = [];
  66. if ($item['active']) {
  67. $class[] = $this->activeCssClass;
  68. }
  69. if ($i === 0 && $this->firstItemCssClass !== null) {
  70. $class[] = $this->firstItemCssClass;
  71. }
  72. if ($i === $n - 1 && $this->lastItemCssClass !== null) {
  73. $class[] = $this->lastItemCssClass;
  74. }
  75. if (!empty($class)) {
  76. if (empty($options['class'])) {
  77. $options['class'] = implode(' ', $class);
  78. } else {
  79. $options['class'] .= ' ' . implode(' ', $class);
  80. }
  81. }
  82. $menu = $this->renderItem($item);
  83. if (!empty($item['items'])) {
  84. $menu .= strtr($this->submenuTemplate, [
  85. '{show}' => $item['active'] ? "style='display: block'" : '',
  86. '{items}' => $this->renderItems($item['items']),
  87. ]);
  88. }
  89. $lines[] = Html::tag($tag, $menu, $options);
  90. }
  91. return implode("\n", $lines);
  92. }
  93. /**
  94. * @inheritdoc
  95. */
  96. protected function normalizeItems($items, &$active)
  97. {
  98. foreach ($items as $i => $item) {
  99. if (isset($item['visible']) && !$item['visible']) {
  100. unset($items[$i]);
  101. continue;
  102. }
  103. if (!isset($item['label'])) {
  104. $item['label'] = '';
  105. }
  106. $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
  107. $items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label'];
  108. $items[$i]['icon'] = isset($item['icon']) ? $item['icon'] : '';
  109. $hasActiveChild = false;
  110. if (isset($item['items'])) {
  111. $items[$i]['items'] = $this->normalizeItems($item['items'], $hasActiveChild);
  112. if (empty($items[$i]['items']) && $this->hideEmptyItems) {
  113. unset($items[$i]['items']);
  114. if (!isset($item['url'])) {
  115. unset($items[$i]);
  116. continue;
  117. }
  118. }
  119. }
  120. if (!isset($item['active'])) {
  121. if ($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item)) {
  122. $active = $items[$i]['active'] = true;
  123. } else {
  124. $items[$i]['active'] = false;
  125. }
  126. } elseif ($item['active']) {
  127. $active = true;
  128. }
  129. }
  130. return array_values($items);
  131. }
  132. /**
  133. * Checks whether a menu item is active.
  134. * This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
  135. * When the `url` option of a menu item is specified in terms of an array, its first element is treated
  136. * as the route for the item and the rest of the elements are the associated parameters.
  137. * Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
  138. * be considered active.
  139. * @param array $item the menu item to be checked
  140. * @return boolean whether the menu item is active
  141. */
  142. protected function isItemActive($item)
  143. {
  144. if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
  145. $route = $item['url'][0];
  146. if ($route[0] !== '/' && Yii::$app->controller) {
  147. $route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
  148. }
  149. $arrayRoute = explode('/', ltrim($route, '/'));
  150. $arrayThisRoute = explode('/', $this->route);
  151. if ($arrayRoute[0] !== $arrayThisRoute[0]) {
  152. return false;
  153. }
  154. if (isset($arrayRoute[1]) && $arrayRoute[1] !== $arrayThisRoute[1]) {
  155. return false;
  156. }
  157. if (isset($arrayRoute[2]) && $arrayRoute[2] !== $arrayThisRoute[2]) {
  158. return false;
  159. }
  160. unset($item['url']['#']);
  161. if (count($item['url']) > 1) {
  162. foreach (array_splice($item['url'], 1) as $name => $value) {
  163. if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
  164. return false;
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. return false;
  171. }
  172. }