Виджет главного меню с подсветкой активных пунктов не только конкретных действий, но и отдельных страниц:
Код: Выделить всё
<?php
class MainMenu extends CWidget {
public $items=array();
public function run() {
$items=array();
$controller=$this->controller;
foreach($this->items as $item) {
if(isset($item['visible']) && !$item['visible'])
continue;
if(isset($item['access']) && $item['access']=='admin' && Yii::app()->user->isGuest)
continue;
$item2=array();
$item2['label']=$item['label'];
if(is_array($item['url']))
$item2['url']=$controller->createUrl($item['url'][0]);
else
$item2['url']=$item['url'];
$pattern=isset($item['pattern'])?$item['pattern']:$item['url'];
$item2['active']=$this->isActive($pattern,$controller);
$items[]=$item2;
}
$this->render('mainMenu',array('items'=>$items));
}
protected function isActive($pattern,$controller) {
if(!is_array($pattern) || !isset($pattern[0]))
return false;
$controllerID=$controller->uniqueID;
$actionID=$controller->action->id;
$currentID=0;
if (method_exists($controller, 'load'))
{
$model=$controller->load(null, true);
if(isset($model)) $currentID=$model->id;
}
$pattern[0]=trim($pattern[0],'/');
$matched=false;
$fullurl=$controllerID.'/'.$actionID.'/id/'.$currentID;
if(strpos($pattern[0],'/')!==false) {
$matched=$pattern[0]===$controllerID.'/'.$actionID;
if(!$matched) $matched=$pattern[0]===$fullurl;
}
else {
$matched=$pattern[0]===$controllerID;
if(!$matched) $matched=$pattern[0]===substr($controller->createUrl($fullurl), strlen(Yii::app()->getRequest()->getBaseUrl())+1);
}
if($matched && count($pattern)>1) {
foreach(array_splice($pattern,1) as $name=>$value) {
if(!isset($_GET[$name]) || $_GET[$name]!=$value)
return false;
}
return true;
}
else
return $matched;
}
}
Код: Выделить всё
public function load($id=null, $docheck=false) {
if($this->_model===null) {
if($id!==null || isset($_GET['id']))
$this->_model=page::model()->findbyPk($id!==null ? $id : $_GET['id']);
if($this->_model===null && !$docheck)
throw new CHttpException(404,'The requested page does not exist.');
}
return $this->_model;
}
Код: Выделить всё
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>FALSE,
),