<?
defined('ROOT_PHPLEFT') or exit('phpleft: Not page');
/*
	phpleftDms Copyright (c) 2011 phpleft.COM
	商业授权：QQ45150208
	
*/
$rs = new admin;
$menu = array (
	array('','phpleft.php?file=menu', '频道管理',0),
    array('menu','phpleft.php?file=menu&show=menu', '添加频道',0),
);
$filesp = "menu.php";
if ($post=='menu'){
if ($is=='2'){
$dirTo = PL_ROOT.$alladd['path'];
if (!file_exists($dirTo)){
$alladd[module] = '';
$alladd[fou] = $is;
$alladd[linkurl] = PL_URL.$alladd['path'].'/';
$rs->update_menu($alladd);
$getID=mysql_insert_id();
$dirFrom = PL_ROOT."plugins/".$alladd[plugins]."/show";
copyDir($dirFrom,$dirTo);
$path = $dirTo.'/config.inc.php';
$html = file_get($path);
$html = preg_replace("/[$]moduleid\s*\=\s*[\"'].*?[\"']/is", "\$moduleid = '$getID'", $html);
MakeHtmlFile($path, $html, 1);
$list = sql_show("table=menu&where=id='$getID'");
$filemodule = "moduleid-".$getID.".php";
$pathcachemodule = PL_ROOT.$PL_CFG['cache']."/";
cache_write($filemodule, $list, $pathcachemodule);
}else{
msg('文件夹'.$alladd['path'].'已经存在', 'phpleft.php?file=menu',0,1,1,'n');	
}
}elseif($is=='3'){
$alladd[path] = '';
$alladd[plugins] = '';
$alladd[fou] = $is;
$alladd[linkurl] = $linkurl;
$rs->update_menu($alladd);
}elseif ($is=='1'){
$dirTo = PL_ROOT.$alladd['path'];
if (!file_exists($dirTo)){
$alladd[plugins] = '';
$alladd[fou] = $is;
$alladd[linkurl] = PL_URL.$alladd['path'].'/';
$rs->update_menu($alladd);
$tableadd = cache_read('table.inc.php',PL_ROOT."phpleft/".$alladd[module]."/");
if ($tableadd<>''){
foreach($tableadd as $id=>$m){
	$length = $m['length'] ? "(".$m['length'].")" : '';
	$sqlname.=",
	".$m['thiskeys']." ".$m['keystype'].$length;
}
}else{
	$sqlname=",";
}
$getID=mysql_insert_id();//
$sqltable = $PL_CFG['tb_pre'].$alladd['module']."_".$getID;
$sql = "CREATE TABLE ".$sqltable." 
(
id int NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id)".$sqlname."
) DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci";
$db->query($sql);
$tablem=sql_cache("table=table&where=module='".$alladd[module]."' and  moduleid='".$alladd[module]."'&order=keylist asc");
$files = "table_".$getID.".inc.php";
$pathcache = PL_ROOT.$PL_CFG['cache']."/";

$dirFrom = PL_ROOT."phpleft/".$alladd[module]."/"."show" ;
copyDir($dirFrom,$dirTo);
$path = $dirTo.'/config.inc.php';
$html = file_get($path);
$html = preg_replace("/[$]moduleid\s*\=\s*[\"'].*?[\"']/is", "\$moduleid = '$getID'", $html);
MakeHtmlFile($path, $html, 1);
$list = sql_show("table=menu&where=id='$getID'");
$filemodule = "moduleid-".$getID.".php";
$pathcachemodule = PL_ROOT.$PL_CFG['cache']."/";
cache_write($filemodule, $list, $pathcachemodule);
foreach($tablem as $id=>$m){
	if($m['thiskeys']=="catid"){
		$db->update($PL_CFG['tb_pre']."menu","catid=catid+1","id='".$getID."'");
	}
}

foreach($tableadd as $id=>$m){
$tabledata['keylist'] = $m['keylist'];
$tabledata['module'] = $m['module'];
$tabledata['name'] = $m['name'];
$tabledata['thiskeys'] = $m['thiskeys'];
$tabledata['note'] = $m['note'];
$tabledata['keystype'] = $m['keystype'];
$tabledata['length'] = $m['length'];
$tabledata['type'] = $m['type'];
$tabledata['typevalue'] = $m['typevalue'];
$tabledata['add_js'] = $m['add_js'];
$tabledata['default_value'] = $m['default_value'];
$tabledata['geton'] = $m['geton'];
$tabledata['is_or'] = $m['is_or'];
$tabledata['qis_or'] = $m['qis_or'];
$tabledata['fou'] = $m['fou'];
$tabledata['moduleid'] = $getID;
$rs->update_post($tabledata,'table','moduleid');
}
$tablems=sql_cache("table=table&where=module='".$alladd[module]."' and  moduleid='".$getID."'&order=keylist asc");
cache_write($files, $tablems, $pathcache);
}else{
msg('文件夹'.$alladd['path'].'已经存在', 'phpleft.php?file=menu',0,1,1,'n');		
}
}
$menusp = sql_cache("table=menu&cache=id&order=inlist asc,id asc"); 
$pathcachep = PL_ROOT.$PL_CFG['cache']."/";
cache_write($filesp, $menusp, $pathcachep);
msg('添加成功', 'phpleft.php?file=menu');
}elseif ($post=='key'){
foreach($key as $ids=>$m){
$alladd['inlist'] = $m;
$rs->update_posts($alladd,'menu','inlist',$ids);
}
$menusp = sql_cache("table=menu&cache=id&order=inlist asc,id asc");
$pathcachep = PL_ROOT.$PL_CFG['cache']."/";
cache_write($filesp, $menusp, $pathcachep);
msg('更新成功', 'phpleft.php?file=menu');
}elseif ($post=='bj'){
if ($style=='1'){
$rs->update_posts($alladd,'menu','is_or',$id);	
}else{
$dirTo = PL_ROOT.$alladd[path];

if ($paths<>$alladd[path]){
if (!file_exists($dirTo)){
$dirFrom = PL_ROOT.$paths;    
copyDir($dirFrom,$dirTo);
deldir($dirFrom);
}else{
msg('文件夹'.$alladd['path'].'已经存在', 'phpleft.php?file=menu',0,1,1,'n');		
}
}
$alladd[linkurl] = PL_URL.$alladd['path'].'/';
$rs->update_posts($alladd,'menu','linkurl',$id);

}
$list = sql_show("table=menu&where=id='$id'");
$filemodule = "moduleid-".$id.".php";
$pathcachemodule = PL_ROOT.$PL_CFG['cache']."/";
cache_write($filemodule, $list, $pathcachemodule);
$menusp = sql_cache("table=menu&cache=id&order=inlist asc,id asc");
$pathcachep = PL_ROOT.$PL_CFG['cache']."/";
cache_write($filesp, $menusp, $pathcachep);
msg('更新成功', 'phpleft.php?file=menu');
	
}else{
$plugins_list = sql_cache("table=plugins&cache=id&order=id asc");
foreach($plugins_list as $i=> $c) {
	if($c['plugins_is']=='1'){
$pluginslist[$i][] = $c['plugins'];
$pluginslist[$i][] = $c['plugins_name'];
	}
}
$module_list = sql_cache("table=module&cache=id&order=id asc");
foreach($module_list as $i=> $c) {
$filelist[$i][] = $c['module'];
$filelist[$i][] = $c['modulename'];
$phplefts[$c['module']] = cache_read('config.inc.php',PL_ROOT."phpleft/".$c['module']."/");
$modulename[] = $phplefts[$$c['module']]['modulename'];
}
$menulist = cache_read($filesp,PL_ROOT.$PL_CFG['cache']."/");
if ($show=='bj'){
foreach($menulist as $ids=>$m){
if($m[id]==$id){
	$alladd=$m;	
}
}
}
include FTemplate(ThisFile);	
}

?>