Advertisement
Guest User

EMenu.php

a guest
Jan 17th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. class EMenu extends CMenu
  2. {
  3.     /**
  4.      * Checks whether a menu item is active.
  5.      * This is done by checking if the currently requested URL is generated by the 'url' option
  6.      * of the menu item. Note that the GET parameters not specified in the 'url' option will be ignored.
  7.      * @param array $item the menu item to be checked
  8.      * @param string $route the route of the current request
  9.      * @return boolean whether the menu item is active
  10.      */
  11.     protected function isItemActive($item,$route)
  12.     {
  13.         $uri     = Yii::app()->request->requestUri;
  14.         $subitem = substr($item['url'], 0, -1);
  15.  
  16.         if(!strcasecmp($item['url'], $uri) || ($subitem && (strpos($uri, $subitem) !== false)))
  17.             return true;
  18.         else
  19.             return parent::isItemActive($item,$route);
  20.     }
  21.  
  22.     /**
  23.      * Normalizes the {@link items} property so that the 'active' state is properly identified for every menu item.
  24.      * @param array $items the items to be normalized.
  25.      * @param string $route the route of the current request.
  26.      * @param boolean $active whether there is an active child menu item.
  27.      * @return array the normalized menu items
  28.      */    
  29.     protected function normalizeItems($items, $route, &$active)
  30.     {
  31.         foreach($items as $i=>$item)
  32.         {
  33.             if(isset($item['visible']) && !$item['visible'])
  34.             {
  35.                 unset($items[$i]);
  36.                 continue;
  37.             }
  38.  
  39.             if(!isset($item['label']))
  40.             {
  41.                 $item['label']='';
  42.             }
  43.  
  44.             if($this->encodeLabel)
  45.             {
  46.                 $items[$i]['label']=CHtml::encode($item['label']);
  47.             }
  48.  
  49.             $hasActiveChild=false;
  50.  
  51.             if(isset($item['items']))
  52.             {
  53.                 $items[$i]['items']=$this->normalizeItems($item['items'],$route,$hasActiveChild);
  54.                 if(empty($items[$i]['items']) && $this->hideEmptyItems)
  55.                 {
  56.                     unset($items[$i]['items']);
  57.                     if(!isset($item['url']))
  58.                     {
  59.                         unset($items[$i]);
  60.                         continue;
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             if(!isset($item['active']))
  66.             {
  67.                 if($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item,$route))
  68.                 {
  69.                     $active = $items[$i]['active'] = true;
  70.                     if (!$hasActiveChild)
  71.                     {
  72.                         $items[$i]['linkOptions']['rel'] = 'nofollow';
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     $items[$i]['active'] = false;
  78.                 }
  79.             }
  80.             elseif($item['active'])
  81.             {
  82.                 $active = true;
  83.             }
  84.         }
  85.  
  86.         return array_values($items);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement