Hey all.
I’m trying to create a menu using ‘zii.widgets.CMenu’ getting settings and items from db. I need help and suggestion on my code. Later I will create one extension for this.
So I created 2 tables on db to store settings.
The first table contains the CMenu settings, and the other the menu itens, subitens and settings.
First table Menus
[sql]
CREATE TABLE menus
(
alias
varchar(45) NOT NULL DEFAULT ‘’,
title
varchar(100) NOT NULL DEFAULT ‘’,
description
varchar(255) DEFAULT ‘’,
actionPrefix
varchar(45) DEFAULT NULL,
activateItems
tinyint(1) NOT NULL DEFAULT ‘0’,
activateParents
tinyint(1) NOT NULL DEFAULT ‘0’,
activeCssClass
varchar(45) DEFAULT NULL,
encodeLabel
tinyint(1) NOT NULL DEFAULT ‘1’,
hideEmptyItems
tinyint(1) NOT NULL DEFAULT ‘1’,
htmlOptions
varchar(255) DEFAULT NULL,
itemTemplate
varchar(45) DEFAULT ‘{menu}’,
skin
varchar(45) DEFAULT NULL,
submenuHtmlOptions
varchar(255) DEFAULT NULL,
PRIMARY KEY (alias
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
[/sql]
Second table MenuItems
[sql]
CREATE TABLE MenuItems
(
id
int(11) NOT NULL AUTO_INCREMENT,
alias
varchar(45) NOT NULL,
title
varchar(100) NOT NULL,
link
text NOT NULL,
linkType
varchar(15) NOT NULL DEFAULT ‘regular’,
menuType
varchar(45) NOT NULL,
parent
int(10) unsigned DEFAULT NULL,
sublevel
int(10) unsigned DEFAULT NULL,
order
int(10) unsigned DEFAULT NULL,
visible
tinyint(1) NOT NULL DEFAULT ‘1’,
template
varchar(100) DEFAULT ‘{menu}’,
linkOptions
varchar(100) DEFAULT NULL,
itemOptions
varchar(100) DEFAULT NULL,
PRIMARY KEY (id
),
KEY menuType
(menuType
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
[/sql]
Now the function to get the results working…
I’m having troubles to convert the arrays into values that Yii recognize. I already try serialize the input values from user and unserialize when the function call the values from db but I can’t get it working. I’m also having issues on getting the sub items without running multiple dbs selects. I think this can slow down the application.
The code bellows shows my attempt to get it working.
public static function renderMenu($type)
{
$menuSettings=Menus::model()->findByPk($type);
if (count($menuSettings)) {
$menu=array();
$criteria="menuType='".$type."' ORDER BY 'order' ASC";
$model=MenuItems::model()->findAll($criteria);
if (count($model)) {
foreach($model as $menuItem) {
switch ($menuItem->linkType) {
case 'internal':
$url=unserialize($menuItem->link);
break;
case 'external':
$url=$menuItem->link;
break;
case 'none':
$url='#';
break;
}
switch ($menuItem->visible) {
case 1:
$visible=true;
break;
case 2:
$visible=S::app()->user->isGuest;
break;
case 3:
$visible=!(S::app()->user->isGuest);
break;
case 4:
$visible=S::app()->user->isSuperUser;
break;
case 5:
$visible=S::app()->user->isAdmin;
break;
}
$menuItems[]=array(
'label'=>S::t($menuItem->title),
'url'=>$url,
'visible'=>$visible,
'template'=>$menuItem->template,
'linkOptions'=>array($menuItem->linkOptions),
'itemOptions'=>array($menuItem->itemOptions),
'items'=>array(),
);
unset($url);
unset($visible);
}
Yii::app()->getController()->widget('zii.widgets.CMenu',array(
'id'=>$menuSettings->alias,
'actionPrefix'=>$menuSettings->actionPrefix,
'items'=>$menuItems,
)
);
} else {
return;
}
} else {
echo 'Empty menu';
}
}
PLEASE! SOME ONE WITH MORE EXPERIENCE CAN HELP ME?