dhtmlx attachEvent("onClick",openlink) function , add new Attribute to a dom
<?php
$userinfo = json_decode(VSession::get("user_info"), true);
Yii::app()->params['username'] = $userinfo['name']; // set name to yii global variables
?>
<style type='text/css'>
.group_label{
clear: both;
display: block;
font-weight: normal;
line-height: 20px;
padding: 3px 10px;
white-space: nowrap;
font-weight:bold;
}
</style>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<img src="<?php echo $baseUrl; ?>/img/global_advantest_logo.gif" class="brand pull-left" alt="Advantest" >
<div id="nav_menu_id" class="nav-collapse" style="float: left; margin-left: 150px;">
<?php
/**
* author cherry
* desc get all menus from database.
* date : 2013/13/06
*/
function getMenus() {
$menu=array();
$sql = "SELECT auto_id as id,label,link,parent,roles FROM etlt_Menus";
$conn = Yii::app()->db;
$rows = $conn->createCommand($sql)->queryAll();
//Yii::app()->params['cmenus'] = $rows;
}
function getMenuParent($id) {
$cmenus = Yii::app()->params['cmenus'];
$parent_menus = array();
foreach ($cmenus as $key => $v) {
if ($v["parent"] == $id) {
$parent_menus[] = $v;
}
}
return $parent_menus;
}
/**
*
* dynamic build menus
* @param <type> $menuId
* @param <type> $order
* @return string
*/
function buildMenu($menuId, $order) {
$menus = getMenuParent($menuId);
$order++;
$html = "";
if ($menus) {
if ($order == 1) {
$html .="<ul id='yw0' class='pull-right nav' style='float: left;'>";
} else {
$html .="<ul class='dropdown-menu'>";
}
$role = json_decode(VSession::get("user_info"),TRUE);
foreach ($menus as $menu) {
if ($menu["roles"]=="*" || stripos($menu["roles"],$role["type"])!==false) {
if ($menu["link"] == "#") {
$html.="<li class='dropdown item-test' tabindex='-1' >";
$html.="<a href='#' data-toggle='dropdown' class='dropdown-toggle'>" . $menu["label"] . "<span class='caret'></span></a>";
} else {
$html.="<li class='item-test'>";
$m_label = preg_replace("/(\(user\))/i", "(" . Yii::app()->params['username'] . ")", $menu["label"]);
if ($menu["link"] != "") {
$m_link = Yii::app()->createUrl($menu["link"]);
$html.="<a href='" . $m_link . "'>" . $m_label . "</a>";
} else {
$html.= "<label class='group_label'>".$m_label."</label>";
}
}
$html.= buildMenu($menu['id'], $order);
$html.="</li>";
}
}
$html.="</ul>";
}
return $html;
}
//getMenus();
//echo buildMenu(0, 0);
function getparentmenu(){
/*
author : mort
*/
$sql = "SELECT auto_id as id,label,link,parent,roles FROM etlt_Menus WHERE parent=0";
$conn = Yii::app()->db;
$rows = $conn->createCommand($sql)->queryAll();
return $rows;
}
function getsubmenubyid($id){
$sql = "SELECT auto_id as id,label,link,parent,roles FROM etlt_Menus WHERE parent="."'".$id."'";
$conn = Yii::app()->db;
$rows = $conn->createCommand($sql)->queryAll();
return $rows;
}
?>
</div>
</div><!-- container -->
</div><!-- navbar-inner -->
</div><!-- subnav -->
<div id="menuData" style="display:none">
<?php $parentmenu=getparentmenu();
foreach($parentmenu as $k => $v){
$html.='<div id='.'"'.$v['id'].'"'.' text='.'"'.$v['label'].'"'.'link='.'"'.$v['link'].'"'.'>';
$submenus_by_id=getsubmenubyid($v["id"]);
foreach($submenus_by_id as $s_k=>$s_v){
$html.='<div id='.'"'.$s_v["id"].'"'.' text='.'"'.$s_v["label"].'"'.'link='.'"'.$s_v['link'].'"'.'></div>';
}
$html.="</div>";
}
echo $html;
?>
</div>
<div style="display:none">
<?php echo $html;?>
</div>
<script>
document.getElementByTagName("div").setAttribute("link");
var nav_Menu;
function initMenu(){
nav_Menu=new dhtmlXMenuObject("nav_menu_id");
nav_Menu.setIconsPath("../../img/icons");
nav_Menu.loadFromHTML("menuData",true);
nav_Menu.attachEvent("onClick",openlink);
}
function openlink(id){
var menu_clicked_href=document.getElementById(id).attributes["link"].value;
window.location.href="<?php echo Yii::app()->request->baseUrl;?>"+"/"+menu_clicked_href;
}
</script>
<script>
initMenu();
</script>