<script language="JavaScript" type="text/javascript">
<?php
$my_menu = "";
$ssqlx = "select * from
			(
			select *, '00' as sort1,
			case when ftmenu_type = 'master' then '01'
			when ftmenu_type = 'laporan' then '03'
			else concat('02_',ftmenu_type)
			end as sort2
			from tlmenu
			where ifnull(ftmenu_name2,'')='' and fnstatus = 1 
			) d order by sort1,sort2,fnurut,ftmenu_code";
$result = mysql_query("SELECT DISTINCT ftmenu_category,fticon FROM ( $ssqlx) m ");
$rownum = mysql_num_rows($result);
$my_menu_arr = array();

if ($rownum > 0)
{
	while ($row = mysql_fetch_array($result))
	{
		$ssql = "SELECT a.ftsecurity_code
			FROM (
			select 0 as fx,ftsecurity_code,ftmodule_code,ftparent_code,ftsecurity_name
			from tlsecurity
			where ftmodule_code='" . $row["ftmenu_category"] . "'
			union all
			select 1 as fx,concat('laporan_',ftreport_group,'_',ftreport_code) as ftsecurity_code,ftreport_group as ftmodule_code, concat('Laporan ',ftreport_group) as ftparent_code,ftreport_name
			from tlreport
			where ftreport_group='" . $row["ftmenu_category"] . "'
			) A
			WHERE EXISTS(SELECT b.fnId FROM tluser_access b WHERE b.fbaccess='1' and b.ftusername='" . $_SESSION['username'] . "' AND a.ftsecurity_code=b.ftsecurity_code)
			ORDER BY a.ftsecurity_code  asc";
		if (!RetRstField($ssql))
		{
			continue;
		}

		$my_menu2 = "";
		$result2 = mysql_query("SELECT DISTINCT ftmenu_type FROM ( $ssqlx) m  WHERE ftmenu_category = '" . $row["ftmenu_category"] . "'");
		$rownum2 = mysql_num_rows($result2);
		if ($rownum2 > 0)
		{
			$m = 0;
			while ($row2 = mysql_fetch_array($result2))
			{
				if ($row2["ftmenu_type"] == "master")
				{
					$img_src = "ThemeOffice/home.png";
				}
				else
				if ($row2["ftmenu_type"] == "transaksi")
				{
					$img_src = "ThemeOffice/edit.png";
				}
				else
				if ($row2["ftmenu_type"] == "laporan")
				{
					$img_src = "ThemeOffice/document.png";
				}

				// Coding Urutan Menu

				$result3 = mysql_query("SELECT * FROM ( $ssqlx) m  WHERE ftmenu_category = '" . $row["ftmenu_category"] . "' AND ftmenu_type = '" . $row2["ftmenu_type"] . "' order by sort1,sort2,fnurut,ftmenu_code ");

				// echo ("SELECT * FROM ( $ssqlx) m  WHERE ftmenu_category = '" . $row["ftmenu_category"] . "' AND ftmenu_type = '" . $row2["ftmenu_type"] ."'");

				$rownum3 = mysql_num_rows($result3);
				if ($rownum3 > 0)
				{
					while ($row3 = mysql_fetch_array($result3))
					{
						$exp = explode('=', $row3['a_href']);
						$exp1 = explode("&", $exp[1]);
						if ($this->GetSecurity($exp1[0]))
						{
							$ssql = "SELECT *
									FROM tlmenu
									WHERE ftmenu_name='{$row3['ftmenu_name']}' AND
									ftmenu_category='{$row3['ftmenu_category']}' AND
									ftmenu_type='{$row3['ftmenu_type']}' AND
									ifnull(ftmenu_name2,'')<>'' and ifnull(ftmenu_name3,'')='' and fnstatus = 1
									ORDER BY fnurut asc";
							$rs_submenu = mysql_query($ssql) or die($ssql);
							$m1 = 0;
							$menux = "";
							while ($r_submenu = mysql_fetch_assoc($rs_submenu))
							{

								// =================================

								$ssql = "SELECT *
											FROM tlmenu
											WHERE ftmenu_name='{$r_submenu['ftmenu_name']}' AND
											ftmenu_category='{$r_submenu['ftmenu_category']}' AND
											ftmenu_type='{$r_submenu['ftmenu_type']}' AND
											ftmenu_name2='{$r_submenu['ftmenu_name2']}' and
											ifnull(ftmenu_name3,'')<>'' and ifnull(ftmenu_name4,'')='' and fnstatus = 1
											ORDER BY fnurut,ftmenu_code asc";
								$rs_submenu_2 = mysql_query($ssql) or die($ssql);
								$m1_2 = 0;
								$menux_2 = "";
								while ($r_submenu_2 = mysql_fetch_assoc($rs_submenu_2))
								{

									// =================================

									$ssql = "SELECT *
											FROM tlmenu
											WHERE ftmenu_name='{$r_submenu_2['ftmenu_name']}' AND
											ftmenu_category='{$r_submenu_2['ftmenu_category']}' AND
											ftmenu_type='{$r_submenu_2['ftmenu_type']}' AND
											ftmenu_name2='{$r_submenu_2['ftmenu_name2']}' and
											ftmenu_name3='{$r_submenu_2['ftmenu_name3']}' and
											ifnull(ftmenu_name4,'')<>'' and fnstatus = 1
											ORDER BY fnurut, ftmenu_code";
									$rs_submenu_3 = mysql_query($ssql) or die($ssql);
									$m1_3 = 0;
									$menux_3 = "";
									while ($r_submenu_3 = mysql_fetch_assoc($rs_submenu_3))
									{
										$m1_3++;
										$exp_3 = explode('=', $r_submenu_3['a_href']);
										$exp1_3 = explode("&", $exp_3[1]);
										if ($this->GetSecurity($exp1_3[0]))
										{
											$menux_3.= "['<img src=\"{$img_src}\" />','{$r_submenu_3['ftmenu_name4']}','{$r_submenu_3['a_href']}','iframe1','{$r_submenu_3['ftmenu_name4']}'],";
										}
									}

									// =================================

									$m1_2++;
									$exp_2 = explode('=', $r_submenu_2['a_href']);
									$exp1_2 = explode("&", $exp_2[1]);
									if ($this->GetSecurity($exp1_2[0]))
									{
										$menux_2.= "['<img src=\"{$img_src}\" />','{$r_submenu_2['ftmenu_name3']}','{$r_submenu_2['a_href']}','iframe1','{$r_submenu_2['ftmenu_name3']}', $menux_3],";
									}
								}

								// =================================

								$m1++;
								$exp = explode('=', $r_submenu['a_href']);
								$exp1 = explode("&", $exp[1]);
								if ($this->GetSecurity($exp1[0]))
								{
									$menux.= "['<img src=\"{$img_src}\" />','{$r_submenu['ftmenu_name2']}','{$r_submenu['a_href']}','iframe1','{$r_submenu['ftmenu_name2']}',{$menux_2}], ";
								}
							}

							//                                echo "{$row3['ftmenu_name']}: $m1";

							if (($m1 == 0) || ($m1 != 0) && ($menux != ''))
							{
								if ($row3["ftmenu_name"] == 'ganti password')
								{
									$ssql = "SELECT fnid from tluser WHERE ftusername='{$_SESSION['username']}'";
									$rs = mysql_query($ssql);
									$r = mysql_fetch_array($rs);
									$row3["a_href"].= "&mode=edit&id=" . $r[0] . "&search=new";
									$my_menu2.= "['<img src=" . $img_src . " />','" . ucwords(str_replace("_", "&nbsp;", $row3["ftmenu_name"])) . "','" . $row3["a_href"] . "','iframe1','" . $row3["ftmenu_name"] . "',{$menux}],\n";
								}
								else
								{
                                                                        $row3["a_href"].= "&search=new";
									$my_menu2.= "['<img src=" . $img_src . " />','" . ucwords(str_replace("_", "&nbsp;", $row3["ftmenu_name"])) . "','" . $row3["a_href"] . "','iframe1','" . $row3["ftmenu_name"] . "',{$menux}],\n";
								}
							}
						}
					}
				}

				mysql_free_result($result3);
				$m++;
				if ($m < $rownum2)
				{
					$my_menu2.= "_cmSplit,";
				}
			}
		}

		mysql_free_result($result2);

		// Menu Caption

		if (strtolower($row["ftmenu_category"]) == "inventory")
		{
			$ftmenu_category = "Inventory";
			$cmSplit = "_cmSplit,";
		}
		else
		{
			$ftmenu_category = $row["ftmenu_category"];
			$cmSplit = "_cmSplit,";
		}
		$icon=$row["fticon"];
		$m = "['".$icon."','" . strtoupper(ucwords(str_replace("_", " ", $ftmenu_category))) . "','" . strtolower(str_replace(" ", "", $ftmenu_category)) . ".php?p=1','iframe1','" . $ftmenu_category . "',{$my_menu2}],{$cmSplit}\n";
		$my_menu.= $m;
		$my_menu_arr[strtolower($row["ftmenu_category"]) ] = $m;
	}
}

mysql_free_result($result);

// Menu Order


if (($my_menu_arr["security"] != ""))
{
	$my_menu_system.= "['','SYSTEM','','iframe1','System'," . $my_menu_arr["system"] . $my_menu_arr["security"] . "],_cmSplit, \n";
}


// vino perubahan menu kategori > dinamis
$result_category = mysql_query("SELECT ftmenu_category as category FROM tlmenu GROUP BY ftmenu_category order by ftmenu_category ASC");

while ($dresult = mysql_fetch_array($result_category))
{
	$category = $dresult['category'];
	$my_menux.= $my_menu_arr[$category];
}
 
 
 
/*     kondisi menu old  if(($my_menu_arr["system"]!="")||($my_menu_arr["setting"]!="")||($my_menu_arr["security"]!="")||($my_menu_arr["saldo_awal"]!="")) {
            $my_menu_system.="['','SYSTEM','','iframe1','System',".$my_menu_arr["system"].$my_menu_arr["setting"].$my_menu_arr["security"].$my_menu_arr["saldo_awal"]."],_cmSplit, \n";
        }
        $my_menux.=$my_menu_system;
        $my_menux.=$my_menu_arr["pembelian"];

        if(($my_menu_arr["inventory_bahan_baku"]!="")||($my_menu_arr["inventory_setengah_jadi"]!="")||($my_menu_arr["inventory_sparepart"]!="")||($my_menu_arr["inventory"]!="")) {
            $my_menu_inventory.="['','INVENTORY','','iframe1','Inventory',".$my_menu_arr["inventory_bahan_baku"].$my_menu_arr["inventory_setengah_jadi"].$my_menu_arr["inventory_sparepart"].$my_menu_arr["inventory"]."],_cmSplit, \n";
        }

        $my_menux.=$my_menu_inventory;
        $my_menux.=$my_menu_arr["penjualan"];
        $my_menux.=$my_menu_arr["keuangan"];
        $my_menux.=$my_menu_arr["akunting"];
        
         $my_menux.=$my_menu_arr["preproduksi"];
         $my_menux.=$my_menu_arr["produksi"];
         $my_menux.=$my_menu_arr["ppic"];
         $my_menux.=$my_menu_arr["tracking"]; */
		 
?>
//lihat settingan beri kondisi di sini

		var myMenu =[<?php echo $my_menux; ?>];
<?php
$result_a= mysql_query("SELECT fbaccess FROM tluser_access_setting WHERE ftusername='" . $_SESSION['username'] . "' AND ftsecurity_code ='left_panel'");
$faccessa = mysql_fetch_array($result_a);

$result_b= mysql_query("SELECT fbaccess FROM tluser_access_setting WHERE ftusername='" . $_SESSION['username'] . "' AND ftsecurity_code ='top_panel'");
$faccessb = mysql_fetch_array($result_b);


if($faccessa['fbaccess']==1 and $faccessb['fbaccess']==1){	
?>
		//aktifkan ini untuk menu leftpanel
		cmDraws ('myMenuID', myMenu, 'hbr', cmThemeOffice, 'ThemeOffice');
		cmDraw ('myMenuIDtop', myMenu, 'hbr', cmThemeOffice, 'ThemeOffice');
<?php 
}
else if($faccessa['fbaccess']==1){
?>
		cmDraws ('myMenuID', myMenu, 'hbr', cmThemeOffice, 'ThemeOffice');		
<?php
}
else if($faccessb['fbaccess']==1){
?>

		//aktifkan ini untuk menu toppanel
 		document.getElementById("toggle-button").style.visibility = 'hidden';
		document.getElementById("search").style.visibility = 'hidden';
		document.getElementById("btn-query").style.visibility = 'hidden';
		document.getElementsByClassName('kirihead')[0].style.visibility = 'hidden';
		document.getElementsByClassName('kiri')[0].style.visibility = 'hidden';
		document.getElementsByClassName('kanan')[0].style.margin=0; 
		cmDraw ('myMenuIDtop', myMenu, 'hbr', cmThemeOffice, 'ThemeOffice');
 		
<?php }?>
		
</script>