Everyone have seen a multilayer menu in several CMS
How to do that using a table ? Lets start!
your model should have id, parent_id and title, where parent_id refers to another record of the same table
Also you should create an extra relation
public function relations() {
return array(
....
'Childs' => array(self::HAS_MANY, 'yourModel', 'parent_id'),
);
}
Now, in your controller (or component) create this methods
private static $menuTree = array();
public static function getMenuTree() {
if (empty(self::$menuTree)) {
$rows = YourModel::model()->findAll('parent_id IS NULL');
foreach ($rows as $item) {
self::$menuTree[] = self::getMenuItems($item);
}
}
return self::$menuTree;
}
private static function getMenuItems($modelRow) {
if (!$modelRow)
return;
if (isset($modelRow->Childs)) {
$chump = self::getMenuItems($modelRow->Childs);
if ($chump != null)
$res = array('label' => $modelRow->title, 'items' => $chump, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
else
$res = array('label' => $modelRow->title, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
return $res;
} else {
if (is_array($modelRow)) {
$arr = array();
foreach ($modelRow as $leaves) {
$arr[] = self::getMenuItems($leaves);
}
return $arr;
} else {
return array('label' => ($modelRow->title), 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
}
}
}
And in your theme call the first method
$this->widget('zii.widgets.CMenu', array(
'items' =>$this->getMenuTree()
));
Now you have make a multi-layer menu
DataBase
Hi, Thanks for this wiki
Can you tell us structure table in database and how can I check user have permission to item Menu with ACL??
for example:
at below code I can check only admin user can access see item Users
array('label'=>'Users','items'=>array( array('label'=>'Create','url'=>array('/authItem/create'), 'visible'=>Yii::app()->user->name=="admin" ), array('label'=>'Admin','url'=>array('/authItem/admin'), **'visible'=>Yii::app()->user->name=="admin"** ) )),
sorry for bad english :D
About permissions
Hi mahdi1986
Thank you for your comment
You could use Yii::app()->user->checkAccess('whattask') foreach
('label' => ($modelRow->title), 'visible'=> Yii::app()->user->checkAccess('the_task'.$modelRow->id) 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)))
but you have to know about the roles permissions, check that
http://www.yiiframework.com/doc/guide/1.1/en/topics.auth
and
http://www.yiiframework.com/wiki/328/simple-rbac/
best regards
About structure table
CREATE TABLE `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `parent_id` int(11) DEFAULT NULL, `title` varchar(45) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`); );
Left Menu Bar
Hi,
I need to show left menu bar with categories and sub categories on home page,
Is it possible?
RE: Left Menu Bar
Hi
as the left menu is the same with main menu (there are different css) yes it is possible!
just add class zii.widgets.CMenu and manipulate the alignments by css
$this->widget('zii.widgets.CMenu', array( 'items' =>$this->getMenuTree() 'htmlOptions'=>array('class'=>'vertical-menu'), ));
What kind of CSS do I use for the CMenu?
Hi,
Are there good CSS solution for CMenu to view the multi level menu items? :)
RE: CSS for multi lever menu
Hi blaces
If you want to set css for each level you could write css like that
ul li { background: #f00; } ul li ul li { background: #0f0; } ul li ul li ul li { background: #00f; }
Customize look
How can this be customized to use Bootstrap classes, hard-coded into the controller or at the view?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.