PageRenderTime 119ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/application/themes/freshy2/Freshy2Menu.php

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