PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/zii/widgets/CMenu.php

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