You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
14KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\widgets;
  8. use Yii;
  9. use yii\base\Widget;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Url;
  12. use yii\helpers\Html;
  13. /**
  14. * Menu displays a multi-level menu using nested HTML lists.
  15. *
  16. * The main property of Menu is [[items]], which specifies the possible items in the menu.
  17. * A menu item can contain sub-items which specify the sub-menu under that menu item.
  18. *
  19. * Menu checks the current route and request parameters to toggle certain menu items
  20. * with active state.
  21. *
  22. * Note that Menu only renders the HTML tags about the menu. It does do any styling.
  23. * You are responsible to provide CSS styles to make it look like a real menu.
  24. *
  25. * The following example shows how to use Menu:
  26. *
  27. * ~~~
  28. * echo Menu::widget([
  29. * 'items' => [
  30. * // Important: you need to specify url as 'controller/action',
  31. * // not just as 'controller' even if default action is used.
  32. * ['label' => 'Home', 'url' => ['site/index']],
  33. * // 'Products' menu item will be selected as long as the route is 'product/index'
  34. * ['label' => 'Products', 'url' => ['product/index'], 'items' => [
  35. * ['label' => 'New Arrivals', 'url' => ['product/index', 'tag' => 'new']],
  36. * ['label' => 'Most Popular', 'url' => ['product/index', 'tag' => 'popular']],
  37. * ]],
  38. * ['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],
  39. * ],
  40. * ]);
  41. * ~~~
  42. *
  43. * @author Qiang Xue <qiang.xue@gmail.com>
  44. * @since 2.0
  45. */
  46. class Menu extends Widget
  47. {
  48. /**
  49. * @var array list of menu items. Each menu item should be an array of the following structure:
  50. *
  51. * - label: string, optional, specifies the menu item label. When [[encodeLabels]] is true, the label
  52. * will be HTML-encoded. If the label is not specified, an empty string will be used.
  53. * - encode: boolean, optional, whether this item`s label should be HTML-encoded. This param will override
  54. * global [[encodeLabels]] param.
  55. * - url: string or array, optional, specifies the URL of the menu item. It will be processed by [[Url::to]].
  56. * When this is set, the actual menu item content will be generated using [[linkTemplate]];
  57. * otherwise, [[labelTemplate]] will be used.
  58. * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
  59. * - items: array, optional, specifies the sub-menu items. Its format is the same as the parent items.
  60. * - active: boolean, optional, whether this menu item is in active state (currently selected).
  61. * If a menu item is active, its CSS class will be appended with [[activeCssClass]].
  62. * If this option is not set, the menu item will be set active automatically when the current request
  63. * is triggered by `url`. For more details, please refer to [[isItemActive()]].
  64. * - template: string, optional, the template used to render the content of this menu item.
  65. * The token `{url}` will be replaced by the URL associated with this menu item,
  66. * and the token `{label}` will be replaced by the label of the menu item.
  67. * If this option is not set, [[linkTemplate]] or [[labelTemplate]] will be used instead.
  68. * - submenuTemplate: string, optional, the template used to render the list of sub-menus.
  69. * The token `{items}` will be replaced with the rendered sub-menu items.
  70. * If this option is not set, [[submenuTemplate]] will be used instead.
  71. * - options: array, optional, the HTML attributes for the menu container tag.
  72. */
  73. public $items = [];
  74. /**
  75. * @var array list of HTML attributes shared by all menu [[items]]. If any individual menu item
  76. * specifies its `options`, it will be merged with this property before being used to generate the HTML
  77. * attributes for the menu item tag. The following special options are recognized:
  78. *
  79. * - tag: string, defaults to "li", the tag name of the item container tags.
  80. *
  81. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  82. */
  83. public $itemOptions = [];
  84. /**
  85. * @var string the template used to render the body of a menu which is a link.
  86. * In this template, the token `{url}` will be replaced with the corresponding link URL;
  87. * while `{label}` will be replaced with the link text.
  88. * This property will be overridden by the `template` option set in individual menu items via [[items]].
  89. */
  90. public $linkTemplate = '<a href="{url}">{label}</a>';
  91. /**
  92. * @var string the template used to render the body of a menu which is NOT a link.
  93. * In this template, the token `{label}` will be replaced with the label of the menu item.
  94. * This property will be overridden by the `template` option set in individual menu items via [[items]].
  95. */
  96. public $labelTemplate = '{label}';
  97. /**
  98. * @var string the template used to render a list of sub-menus.
  99. * In this template, the token `{items}` will be replaced with the rendered sub-menu items.
  100. */
  101. public $submenuTemplate = "\n<ul>\n{items}\n</ul>\n";
  102. /**
  103. * @var boolean whether the labels for menu items should be HTML-encoded.
  104. */
  105. public $encodeLabels = true;
  106. /**
  107. * @var string the CSS class to be appended to the active menu item.
  108. */
  109. public $activeCssClass = 'active';
  110. /**
  111. * @var boolean whether to automatically activate items according to whether their route setting
  112. * matches the currently requested route.
  113. * @see isItemActive()
  114. */
  115. public $activateItems = true;
  116. /**
  117. * @var boolean whether to activate parent menu items when one of the corresponding child menu items is active.
  118. * The activated parent menu items will also have its CSS classes appended with [[activeCssClass]].
  119. */
  120. public $activateParents = false;
  121. /**
  122. * @var boolean whether to hide empty menu items. An empty menu item is one whose `url` option is not
  123. * set and which has no visible child menu items.
  124. */
  125. public $hideEmptyItems = true;
  126. /**
  127. * @var array the HTML attributes for the menu's container tag. The following special options are recognized:
  128. *
  129. * - tag: string, defaults to "ul", the tag name of the item container tags. Set to false to disable container tag.
  130. *
  131. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  132. */
  133. public $options = [];
  134. /**
  135. * @var string the CSS class that will be assigned to the first item in the main menu or each submenu.
  136. * Defaults to null, meaning no such CSS class will be assigned.
  137. */
  138. public $firstItemCssClass;
  139. /**
  140. * @var string the CSS class that will be assigned to the last item in the main menu or each submenu.
  141. * Defaults to null, meaning no such CSS class will be assigned.
  142. */
  143. public $lastItemCssClass;
  144. /**
  145. * @var string the route used to determine if a menu item is active or not.
  146. * If not set, it will use the route of the current request.
  147. * @see params
  148. * @see isItemActive()
  149. */
  150. public $route;
  151. /**
  152. * @var array the parameters used to determine if a menu item is active or not.
  153. * If not set, it will use `$_GET`.
  154. * @see route
  155. * @see isItemActive()
  156. */
  157. public $params;
  158. /**
  159. * Renders the menu.
  160. */
  161. public function run()
  162. {
  163. if ($this->route === null && Yii::$app->controller !== null) {
  164. $this->route = Yii::$app->controller->getRoute();
  165. }
  166. if ($this->params === null) {
  167. $this->params = Yii::$app->request->getQueryParams();
  168. }
  169. $items = $this->normalizeItems($this->items, $hasActiveChild);
  170. if (!empty($items)) {
  171. $options = $this->options;
  172. $tag = ArrayHelper::remove($options, 'tag', 'ul');
  173. echo Html::tag($tag, $this->renderItems($items), $options);
  174. }
  175. }
  176. /**
  177. * Recursively renders the menu items (without the container tag).
  178. * @param array $items the menu items to be rendered recursively
  179. * @return string the rendering result
  180. */
  181. protected function renderItems($items)
  182. {
  183. $n = count($items);
  184. $lines = [];
  185. foreach ($items as $i => $item) {
  186. $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
  187. $tag = ArrayHelper::remove($options, 'tag', 'li');
  188. $class = [];
  189. if ($item['active']) {
  190. $class[] = $this->activeCssClass;
  191. }
  192. if ($i === 0 && $this->firstItemCssClass !== null) {
  193. $class[] = $this->firstItemCssClass;
  194. }
  195. if ($i === $n - 1 && $this->lastItemCssClass !== null) {
  196. $class[] = $this->lastItemCssClass;
  197. }
  198. if (!empty($class)) {
  199. if (empty($options['class'])) {
  200. $options['class'] = implode(' ', $class);
  201. } else {
  202. $options['class'] .= ' ' . implode(' ', $class);
  203. }
  204. }
  205. $menu = $this->renderItem($item);
  206. if (!empty($item['items'])) {
  207. $submenuTemplate = ArrayHelper::getValue($item, 'submenuTemplate', $this->submenuTemplate);
  208. $menu .= strtr($submenuTemplate, [
  209. '{items}' => $this->renderItems($item['items']),
  210. ]);
  211. }
  212. if ($tag === false) {
  213. $lines[] = $menu;
  214. } else {
  215. $lines[] = Html::tag($tag, $menu, $options);
  216. }
  217. }
  218. return implode("\n", $lines);
  219. }
  220. /**
  221. * Renders the content of a menu item.
  222. * Note that the container and the sub-menus are not rendered here.
  223. * @param array $item the menu item to be rendered. Please refer to [[items]] to see what data might be in the item.
  224. * @return string the rendering result
  225. */
  226. protected function renderItem($item)
  227. {
  228. if (isset($item['url'])) {
  229. $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
  230. return strtr($template, [
  231. '{url}' => Html::encode(Url::to($item['url'])),
  232. '{label}' => $item['label'],
  233. ]);
  234. } else {
  235. $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
  236. return strtr($template, [
  237. '{label}' => $item['label'],
  238. ]);
  239. }
  240. }
  241. /**
  242. * Normalizes the [[items]] property to remove invisible items and activate certain items.
  243. * @param array $items the items to be normalized.
  244. * @param boolean $active whether there is an active child menu item.
  245. * @return array the normalized menu items
  246. */
  247. protected function normalizeItems($items, &$active)
  248. {
  249. foreach ($items as $i => $item) {
  250. if (isset($item['visible']) && !$item['visible']) {
  251. unset($items[$i]);
  252. continue;
  253. }
  254. if (!isset($item['label'])) {
  255. $item['label'] = '';
  256. }
  257. $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
  258. $items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label'];
  259. $hasActiveChild = false;
  260. if (isset($item['items'])) {
  261. $items[$i]['items'] = $this->normalizeItems($item['items'], $hasActiveChild);
  262. if (empty($items[$i]['items']) && $this->hideEmptyItems) {
  263. unset($items[$i]['items']);
  264. if (!isset($item['url'])) {
  265. unset($items[$i]);
  266. continue;
  267. }
  268. }
  269. }
  270. if (!isset($item['active'])) {
  271. if ($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item)) {
  272. $active = $items[$i]['active'] = true;
  273. } else {
  274. $items[$i]['active'] = false;
  275. }
  276. } elseif ($item['active']) {
  277. $active = true;
  278. }
  279. }
  280. return array_values($items);
  281. }
  282. /**
  283. * Checks whether a menu item is active.
  284. * This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
  285. * When the `url` option of a menu item is specified in terms of an array, its first element is treated
  286. * as the route for the item and the rest of the elements are the associated parameters.
  287. * Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
  288. * be considered active.
  289. * @param array $item the menu item to be checked
  290. * @return boolean whether the menu item is active
  291. */
  292. protected function isItemActive($item)
  293. {
  294. if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
  295. $route = $item['url'][0];
  296. if ($route[0] !== '/' && Yii::$app->controller) {
  297. $route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
  298. }
  299. if (ltrim($route, '/') !== $this->route) {
  300. return false;
  301. }
  302. unset($item['url']['#']);
  303. if (count($item['url']) > 1) {
  304. foreach (array_splice($item['url'], 1) as $name => $value) {
  305. if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
  306. return false;
  307. }
  308. }
  309. }
  310. return true;
  311. }
  312. return false;
  313. }
  314. }