PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/yii/framework/zii/widgets/CMenu.php

https://github.com/ashie1287/headfirst
PHP | 278 lines | 127 code | 8 blank | 143 comment | 33 complexity | a6c0517f6e52166c2c48aaf27a605e45 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * CMenu class file.
  4. *
  5. * @author Jonah Turnquist <poppitypop@gmail.com>
  6. * @author Qiang Xue <qiang.xue@gmail.com>
  7. * @link http://www.yiiframework.com/
  8. * @copyright Copyright &copy; 2008-2010 Yii Software LLC
  9. * @license http://www.yiiframework.com/license/
  10. */
  11. /**
  12. * CMenu displays a multi-level menu using nested HTML lists.
  13. *
  14. * The main property of CMenu is {@link items}, which specifies the possible items in the menu.
  15. * A menu item has three main properties: visible, active and items. The "visible" property
  16. * specifies whether the menu item is currently visible. The "active" property specifies whether
  17. * the menu item is currently selected. And the "items" property specifies the child menu items.
  18. *
  19. * The following example shows how to use CMenu:
  20. * <pre>
  21. * $this->widget('zii.widgets.CMenu', array(
  22. * 'items'=>array(
  23. * array('label'=>'Home', 'url'=>array('site/index')),
  24. * array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array(
  25. * array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')),
  26. * array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')),
  27. * )),
  28. * array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest),
  29. * ),
  30. * ));
  31. * </pre>
  32. *
  33. * @author Jonah Turnquist <poppitypop@gmail.com>
  34. * @author Qiang Xue <qiang.xue@gmail.com>
  35. * @version $Id: CMenu.php 2326 2010-08-20 17:02:07Z qiang.xue $
  36. * @package zii.widgets
  37. * @since 1.1
  38. */
  39. class CMenu extends CWidget
  40. {
  41. /**
  42. * @var array list of menu items. Each menu item is specified as an array of name-value pairs.
  43. * Possible option names include the following:
  44. * <ul>
  45. * <li>label: string, required, specifies the menu item label. When {@link encodeLabel} is true, the label
  46. * will be HTML-encoded.</li>
  47. * <li>url: string or array, optional, specifies the URL of the menu item. It is passed to {@link CHtml::normalizeUrl}
  48. * to generate a valid URL. If this is not set, the menu item will be rendered as a span text.</li>
  49. * <li>visible: boolean, optional, whether this menu item is visible. Defaults to true.
  50. * This can be used to control the visibility of menu items based on user permissions.</li>
  51. * <li>items: array, optional, specifies the sub-menu items. Its format is the same as the parent items.</li>
  52. * <li>active: boolean, optional, whether this menu item is in active state (currently selected).
  53. * If a menu item is active and {@link activeClass} is not empty, its CSS class will be appended with {@link activeClass}.
  54. * If this option is not set, the menu item will be set active automatically when the current request
  55. * is triggered by {@link url}. Note that the GET parameters not specified in the 'url' option will be ignored.</li>
  56. * <li>template: string, optional, the template used to render this menu item.
  57. * In this template, the token "{menu}" will be replaced with the corresponding menu link or text.
  58. * Please see {@link itemTemplate} for more details. This option has been available since version 1.1.1.</li>
  59. * <li>linkOptions: array, optional, additional HTML attributes to be rendered for the link or span tag of the menu item.</li>
  60. * <li>itemOptions: array, optional, additional HTML attributes to be rendered for the container tag of the menu item.</li>
  61. * </ul>
  62. */
  63. public $items=array();
  64. /**
  65. * @var string the template used to render an individual menu item. In this template,
  66. * the token "{menu}" will be replaced with the corresponding menu link or text.
  67. * If this property is not set, each menu will be rendered without any decoration.
  68. * This property will be overridden by the 'template' option set in individual menu items via {@items}.
  69. * @since 1.1.1
  70. */
  71. public $itemTemplate;
  72. /**
  73. * @var boolean whether the labels for menu items should be HTML-encoded. Defaults to true.
  74. */
  75. public $encodeLabel=true;
  76. /**
  77. * @var string the CSS class to be appended to the active menu item. Defaults to 'active'.
  78. * If empty, the CSS class of menu items will not be changed.
  79. */
  80. public $activeCssClass='active';
  81. /**
  82. * @var boolean whether to automatically activate items according to whether their route setting
  83. * matches the currently requested route. Defaults to true.
  84. * @since 1.1.3
  85. */
  86. public $activateItems=true;
  87. /**
  88. * @var boolean whether to activate parent menu items when one of the corresponding child menu items is active.
  89. * The activated parent menu items will also have its CSS classes appended with {@link activeCssClass}.
  90. * Defaults to false.
  91. */
  92. public $activateParents=false;
  93. /**
  94. * @var boolean whether to hide empty menu items. An empty menu item is one whose 'url' option is not
  95. * set and which doesn't contain visible child menu items. Defaults to true.
  96. */
  97. public $hideEmptyItems=true;
  98. /**
  99. * @var array HTML attributes for the menu's root container tag
  100. */
  101. public $htmlOptions=array();
  102. /**
  103. * @var array HTML attributes for the submenu's container tag.
  104. */
  105. public $submenuHtmlOptions=array();
  106. /**
  107. * @var string the HTML element name that will be used to wrap the label of all menu links.
  108. * For example, if this property is set as 'span', a menu item may be rendered as
  109. * &lt;li&gt;&lt;a href="url"&gt;&lt;span&gt;label&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
  110. * This is useful when implementing menu items using the sliding window technique.
  111. * Defaults to null, meaning no wrapper tag will be generated.
  112. * @since 1.1.4
  113. */
  114. public $linkLabelWrapper;
  115. /**
  116. * @var string the CSS class that will be assigned to the first item in the main menu or each submenu.
  117. * Defaults to null, meaning no such CSS class will be assigned.
  118. * @since 1.1.4
  119. */
  120. public $firstItemCssClass;
  121. /**
  122. * @var string the CSS class that will be assigned to the last item in the main menu or each submenu.
  123. * Defaults to null, meaning no such CSS class will be assigned.
  124. * @since 1.1.4
  125. */
  126. public $lastItemCssClass;
  127. /**
  128. * Initializes the menu widget.
  129. * This method mainly normalizes the {@link items} property.
  130. * If this method is overridden, make sure the parent implementation is invoked.
  131. */
  132. public function init()
  133. {
  134. $this->htmlOptions['id']=$this->getId();
  135. $route=$this->getController()->getRoute();
  136. $this->items=$this->normalizeItems($this->items,$route,$hasActiveChild);
  137. }
  138. /**
  139. * Calls {@link renderMenu} to render the menu.
  140. */
  141. public function run()
  142. {
  143. $this->renderMenu($this->items);
  144. }
  145. /**
  146. * Renders the menu items.
  147. * @param array menu items. Each menu item will be an array with at least two elements: 'label' and 'active'.
  148. * It may have three other optional elements: 'items', 'linkOptions' and 'itemOptions'.
  149. */
  150. protected function renderMenu($items)
  151. {
  152. if(count($items))
  153. {
  154. echo CHtml::openTag('ul',$this->htmlOptions)."\n";
  155. $this->renderMenuRecursive($items);
  156. echo CHtml::closeTag('ul');
  157. }
  158. }
  159. /**
  160. * Recursively renders the menu items.
  161. * @param array the menu items to be rendered recursively
  162. */
  163. protected function renderMenuRecursive($items)
  164. {
  165. $count=0;
  166. $n=count($items);
  167. foreach($items as $item)
  168. {
  169. $count++;
  170. $options=isset($item['itemOptions']) ? $item['itemOptions'] : array();
  171. $class=array();
  172. if($item['active'] && $this->activeCssClass!='')
  173. $class[]=$this->activeCssClass;
  174. if($count===1 && $this->firstItemCssClass!='')
  175. $class[]=$this->firstItemCssClass;
  176. if($count===$n && $this->lastItemCssClass!='')
  177. $class[]=$this->lastItemCssClass;
  178. if($class!==array())
  179. {
  180. if(empty($options['class']))
  181. $options['class']=implode(' ',$class);
  182. else
  183. $options['class'].=' '.implode(' ',$class);
  184. }
  185. echo CHtml::openTag('li', $options);
  186. if(isset($item['url']))
  187. {
  188. $label=$this->linkLabelWrapper===null ? $item['label'] : '<'.$this->linkLabelWrapper.'>'.$item['label'].'</'.$this->linkLabelWrapper.'>';
  189. $menu=CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
  190. }
  191. else
  192. $menu=CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
  193. if(isset($this->itemTemplate) || isset($item['template']))
  194. {
  195. $template=isset($item['template']) ? $item['template'] : $this->itemTemplate;
  196. echo strtr($template,array('{menu}'=>$menu));
  197. }
  198. else
  199. echo $menu;
  200. if(isset($item['items']) && count($item['items']))
  201. {
  202. echo "\n".CHtml::openTag('ul',$this->submenuHtmlOptions)."\n";
  203. $this->renderMenuRecursive($item['items']);
  204. echo CHtml::closeTag('ul')."\n";
  205. }
  206. echo CHtml::closeTag('li')."\n";
  207. }
  208. }
  209. /**
  210. * Normalizes the {@link items} property so that the 'active' state is properly identified for every menu item.
  211. * @param array the items to be normalized.
  212. * @param string the route of the current request.
  213. * @param boolean whether there is an active child menu item.
  214. * @return array the normalized menu items
  215. */
  216. protected function normalizeItems($items,$route,&$active)
  217. {
  218. foreach($items as $i=>$item)
  219. {
  220. if(isset($item['visible']) && !$item['visible'])
  221. {
  222. unset($items[$i]);
  223. continue;
  224. }
  225. if($this->encodeLabel)
  226. $items[$i]['label']=CHtml::encode($item['label']);
  227. $hasActiveChild=false;
  228. if(isset($item['items']))
  229. {
  230. $items[$i]['items']=$this->normalizeItems($item['items'],$route,$hasActiveChild);
  231. if(empty($items[$i]['items']) && $this->hideEmptyItems)
  232. unset($items[$i]['items']);
  233. }
  234. if(!isset($item['active']))
  235. {
  236. if($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item,$route))
  237. $active=$items[$i]['active']=true;
  238. else
  239. $items[$i]['active']=false;
  240. }
  241. else if($item['active'])
  242. $active=true;
  243. }
  244. return array_values($items);
  245. }
  246. /**
  247. * Checks whether a menu item is active.
  248. * This is done by checking if the currently requested URL is generated by the 'url' option
  249. * of the menu item. Note that the GET parameters not specified in the 'url' option will be ignored.
  250. * @param array the menu item to be checked
  251. * @param string the route of the current request
  252. * @return boolean whether the menu item is active
  253. */
  254. protected function isItemActive($item,$route)
  255. {
  256. if(isset($item['url']) && is_array($item['url']) && !strcasecmp(trim($item['url'][0],'/'),$route))
  257. {
  258. if(count($item['url'])>1)
  259. {
  260. foreach(array_splice($item['url'],1) as $name=>$value)
  261. {
  262. if(!isset($_GET[$name]) || $_GET[$name]!=$value)
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. return false;
  269. }
  270. }