<?php
!function_exists('html') && exit('ERR');


if($job=='list'&&$Apower[menu_list])
{
	$query = $db->query("SELECT * FROM {$pre}menu WHERE moduleid=0 AND type=0 AND fid=0 ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$rs[hide]=$rs[hide]?'<a style="color:blue;">隱藏</a>':'顯示';
		$listdb[]=$rs;
		$query2 = $db->query("SELECT * FROM {$pre}menu WHERE moduleid=0 AND type=0 AND fid='$rs[id]' ORDER BY list DESC");
		while($rs2 = $db->fetch_array($query2)){
			$rs2[hide]=$rs2[hide]?'<a style="color:blue;">隱藏</a>':'顯示';
			$rs2[icon]='&nbsp;&nbsp;&nbsp;&nbsp;|--------';
			$listdb[]=$rs2;
		}
	}

	$ShowMenu[intval($webdb[ShowMenu])]=" checked ";

	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/guidemenu/menu.htm");
	require(dirname(__FILE__)."/"."template/guidemenu/list.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($job=='edit'&&$Apower[menu_list])
{
	$atc="edit";
	$rsdb=$db->get_one("SELECT * FROM {$pre}menu WHERE id='$id'");
	$target[$rsdb[target]]=' checked ';
	$hide[$rsdb[hide]]=' checked ';

	$selected=select_fupmenu('fid',$rsdb[fid]);
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/guidemenu/menu.htm");
	require(dirname(__FILE__)."/"."template/guidemenu/edit.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($action=='edit'&&$Apower[menu_list])
{
	if(!$postdb[name])
	{
		showerr("名稱不能為空");
	}
	if(!$postdb['linkurl'])
	{
		showerr("鏈接地址不能為空");
	}
	$postdb[name]=filtrate($postdb[name]);
	$postdb[linkurl]=filtrate($postdb[linkurl]);

	$db->query("UPDATE {$pre}menu SET fid='$fid',name='$postdb[name]',linkurl='$postdb[linkurl]',color='$postdb[color]',target='$postdb[target]',hide='$postdb[hide]',list='$postdb[list]' WHERE id='$id'");
	menu_cache();
	jump("修改succeed","?lfj=guidemenu&job=list",1);
}
elseif($job=='add'&&$Apower[menu_list])
{
	$target[0]=' checked ';
	$hide[0]=' checked ';
	$atc="add";
	$selected=select_fupmenu('fid',$rsdb[fid]);
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/guidemenu/menu.htm");
	require(dirname(__FILE__)."/"."template/guidemenu/edit.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($action=='add'&&$Apower[menu_list])
{
	if(!$postdb[name]){
		showerr("名稱不能為空");
	}
	if(!$postdb['linkurl']){
		showerr("鏈接地址不能為空");
	}
	$postdb[name]=filtrate($postdb[name]);
	$postdb[linkurl]=filtrate($postdb[linkurl]);

	$db->query("INSERT INTO `{$pre}menu` (`fid`, `name`, `linkurl`, `color`, `target`, `moduleid`, `hide`, `list`) VALUES ('$fid', '$postdb[name]', '$postdb[linkurl]', '$postdb[color]', '$postdb[target]', 0, '$postdb[hide]', '$postdb[list]')");
	menu_cache();
	jump("添加succeed","?lfj=guidemenu&job=list",1);
}
elseif($action=='delete'&&$Apower[menu_list])
{
	$rs = $db->get_one("SELECT * FROM {$pre}menu WHERE moduleid=0 AND type=0 AND fid='$id'");
	if($rs){
		showerr("請先刪除子菜單或者把子菜單移走.才能刪除此菜單");
	}

	$db->query("DELETE FROM `{$pre}menu` WHERE id='$id'");
	menu_cache();
	jump("刪除succeed","?lfj=guidemenu&job=list",1);
}
elseif($action=="editlist"&&$Apower[menu_list])
{
	foreach( $order AS $key=>$value)
	{
		$db->query("UPDATE {$pre}menu SET list='$value' WHERE id='$key'");
	}
	menu_cache();
	jump("修改succeed","?lfj=guidemenu&job=list",1);
}
elseif($action=="setShowMenu"&&$Apower[menu_list])
{
	write_config_cache($webdbs);
	jump("修改succeed","?lfj=guidemenu&job=list",1);
}


function menu_cache(){
	global $db,$pre;
	$query = $db->query("SELECT * FROM {$pre}menu WHERE moduleid=0 AND type=0 AND hide=0 AND fid=0 ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$array[]="$rs[name]|$rs[linkurl]|$rs[target]|$rs[color]|$rs[id]";
		
		//$show.='
		//var Menu_'.$rs[id].' = "';

		$query2 = $db->query("SELECT * FROM {$pre}menu WHERE moduleid=0 AND type=0 AND hide=0 AND fid='$rs[id]' ORDER BY list DESC");
		while($rs2 = $db->fetch_array($query2)){
			/*
			if(ereg("^\/",$rs2[linkurl])){
				$url="\$webdb[www_url]$rs2[linkurl]";
			}elseif(!ereg("://",$rs2[linkurl])){
				$url="\$webdb[www_url]/$rs2[linkurl]";
			}
			$show.="<a href='$url'>$rs2[name]</a><br>";
			*/
			$show.="\r\n\$MenuArray[$rs[id]][]='".addslashes("$rs2[name]|$rs2[linkurl]|$rs2[target]|$rs2[color]|$rs2[id]")."';";
		}

		//$show.='";
		//';
	}
	$webdbs[guide_word]=implode("\r\n",$array);
	write_config_cache($webdbs);

	write_file(PHP168_PATH."php168/menu.js.php",'<?php'.$show."\r\n?>");
}


function select_fupmenu($name='fid',$id=0){
	global $db,$pre;
	$select="<select name='$name'><option value='0'>請選擇</option>";
	$query = $db->query("SELECT * FROM {$pre}menu WHERE moduleid=0 AND type=0 AND fid=0 ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$ckk=$id==$rs[id]?' selected ':'';
		$select.="<option value='$rs[id]' $ckk style='color:blue;'>$rs[name]</option>";
	}
	$select.="</select>";
	return $select;
}