<?php
define('IN_SYS',TRUE);
define('IN_AdminMenu',TRUE);
require './admin_config.php';
require '../config.php';
require '../setup.php';
require("../lib/admin_chk.php");
?>
<?php
$pageTitle="栏目/分类管理";

$cateTable="category";
$infoTable="content";

$getid = intval(trim($_GET["cataid"]));
if($getid=="" || $getid=="0" || !is_numeric($getid))
{
	$mtitle="根栏目";
	$mpx = 0;
	$mtime = now();
	$mtp = "无";
	$getid ="0";
	$mInfoType="News";//==信息类别==
}
else
{	
	$conn = new dbo();
	$sql="Select * from ".DB_TablePre.$cateTable." where id=".$getid;
	$conn->query($sql);
	$rsp = mysql_fetch_array($conn->res);
	if($rsp)
	{
		$mtitle = $rsp["title"];
		$mpx = $rsp["SortId"];
		$mSortId=$rsp["SortId"];
		$mtp = $rsp["tp"];
		$mtime = $rsp["adate"];	
		$mxia = $rsp["xia"];	
		$thisKeywords=$rsp["thisKeywords"];
		$thisDescription=$rsp["thisDescription"];
		//$Readme=$rsp["readme"];
		$mInfoType=$rsp["InfoType"];//==信息类别==
		$mImgW=$rsp["ImgW"];//==略缩图片宽==
		$mImgH=$rsp["ImgH"];//==略缩图片高==
		$pageUrl = $rsp['pageUrl'];
		$English = $rsp['English'];
	}
}

$action = trim($_GET["action"]);

if(!$action=="")
{
	$doid = trim($_GET["doid"]);
	
	$cat_add_name = trim($_POST["cat_add_name"]);
	$ainipic = trim($_POST["aimageUrl"]);
	$px = trim($_POST["mSortId"]);
	$AInfoType = trim($_POST["AInfoType"]);
	$AImgW = trim($_POST["AImgW"]);
	$AImgH = trim($_POST["AImgH"]);
	$AddThisKeywords = trim($_POST["AddThisKeywords"]);
	$AddThisDescription = trim($_POST["AddThisDescription"]);
	//$AddReadme = trim($_POST["AddReadme"]);
	$xiaji = trim($_POST["xiaji"]);
	//获取添加时表单提交的信息
	
	//获取修改的表单项目
	$cat_mod_name = trim($_POST["cat_mod_name"]);
	$Mminipic = trim($_POST["mimageUrl"]);
	$mpx = trim($_POST["mSortId"]);
	$mInfoType = trim($_POST["mInfoType"]);
	$mImgW = trim($_POST["mImgW"]);
	$mImgH = trim($_POST["mImgH"]);	
	$thisKeywords = trim($_POST["thisKeywords"]);
	$thisDescription = trim($_POST["thisDescription"]);
	//$readme = trim($_POST["readme"]);
	$xia = trim($_POST["xia"]);
	$formpageUrl = trim($_POST["formpageUrl"]);
	$English = trim($_POST["English"]);
 	switch($action)
	{
		case "add";//添加处理
			if($cat_add_name=="")
			{
				OutErrorBack("请填写栏目名称!");
			}
			if($AInfoType=="")
			{
				OutErrorBack("请选择信息类型!");
			}
			
			$conn = new dbo();
			$conn->query("Select * from ".DB_TablePre.$cateTable." where id=".$doid);			
			$rsp = mysql_fetch_array($conn->res);
			if($rsp)
			{
				$arootid = $rsp["rootid"];
				$alevel = $rsp["Mlevel"];
			}
			else
			{
				$arootid = "";
				$alevel = 0;
			}
			$alevel++;
			
			if($arootid =="")
			{
				$rsarootid = "*";
				$rsaMyrootid = ",*,";
			}
			else
			{
				$rsarootid = $arootid.",*";
				$rsaMyrootid = ",".$arootid.",*,";
			}
			
			$ins_sql = "INSERT INTO ".DB_TablePre.$cateTable." (`title`, `CatalogParentId`, `Mlevel`, `SortId`, `rootid`, `MyRootid`, `tp`, `thisKeywords`, `thisDescription`, `adate`, `xia`, `InfoType`, `ImgW`, `ImgH`, `pageUrl`,`English`) VALUES ('$cat_add_name', '$doid', '$alevel', '1', '$rsarootid', '$rsaMyrootid', '$ainipic', '$AddThisKeywords', '$AddThisDescription', NOW(), $xiaji, '$AInfoType', '$AImgW', '$AImgH', '$formpageUrl','$English')";
			
			//echo $ins_sql;
			
			$conn->query($ins_sql);
			
			$menuadd = $conn->res;
			
			if($menuadd)
			{
				//echo "插入成功!";
			}
			else
			{
				echo "插入失败!";
				exit;
			}	
					
			$gid = mysql_insert_id(); //获取插入的ID号
		    $conn->query("select * from ".DB_TablePre.$cateTable." order by id desc LIMIT 1");
			$rsU = mysql_fetch_array($conn->res);
			if($rsU)
			{
				$gidu = $rsU["id"];
				$groot = $rsU["rootid"];
				$c_groot = str_replace("*",$gidu,$groot);
				$rsUMyrootid = str_replace("*",$gidu,$rsU["MyRootid"]);
			}
			
			$conn->query("update ".DB_TablePre.$cateTable." set `rootid`='$c_groot',`MyRootid`='$rsUMyrootid',`SortId`='$gid' where `id`='$gid'");		
			$menuupdate = $conn->res;
			if($menuupdate)
			{
				//echo "栏目修改成功!<br>";
			}
			else
			{
				echo "栏目修改失败!<br>";
				exit;
			}
			Jqlib::CategoryCreate(0,$cateTable);
			OutErrorGo("栏目添加成功!","?cataid=".$doid."");
			
		break;
		
		case "modify";//修改处理
			if($cat_mod_name=="")
			{
				OutErrorBack("请填写栏目名称!");
			}
			if($mInfoType=="")
			{
				OutErrorBack("请选择信息类型!");
			}
			$sqlupdate = "update ".DB_TablePre.$cateTable." set pageUrl='$formpageUrl',title='$cat_mod_name',tp='$Mminipic',xia=$xia,SortId='$mpx',ImgW='$mImgW',ImgH='$mImgH',InfoType='$mInfoType',thisKeywords='$thisKeywords',thisDescription='$thisDescription',English='$English' where id= '$doid'";
			$conn = new dbo();
			$conn->query($sqlupdate);
			$menuupdate = $conn->res;
			if($menuupdate)
			{
				//echo "栏目修改成功!<br>";
			}
			else
			{
				echo "栏目修改失败!<br>";
				exit;
			}
			Jqlib::CategoryCreate(0,$cateTable);
			OutErrorGo("栏目修改成功!","?cataid=".$doid."");
		
		break;
		
		case "del";//删除处理	
			$TypeId=GetSqlSuper($doid,$cateTable);//运用函数获取当前下属所有类别

			$conn->query("select * from ".$infoTable." where type in ($TypeId)");
			while($rs = @mysql_fetch_array($conn-res))
			{
				if(isset($rs["pic"]))
				{
					ImgDelpic($rs["pic"],".."); //删除图片
				}
				if(isset($rs["content"]))
				{
					ImgDel(RequestImg($rs["content"]),"","..");
				}
			}
			
			$conn->query("delete from ".DB_TablePre.$infoTable." where type in ($TypeId)");			
			$conn->query("delete from ".DB_TablePre.$cateTable." where id in ($TypeId)");
			Jqlib::CategoryCreate(0,$cateTable);
			OutErrorGo("栏目删除成功!","?");
		
			break;
		
		case "up"://往前排序
			UpDowngo($cateTable,"up");
		break;

		case "down":
			UpDowngo($cateTable,"down");
		break;
	
	}	
}

//排序函数
function UpDowngo($cateTable,$action)
{
	$conn = new dbo();
	$CatalogId=trim($_REQUEST["cataid"]);
	$CatalogParentId=trim($_REQUEST["CatalogParentId"]);
	$Mlevel=trim($_REQUEST["Mlevel"]);
	if(isset($CatalogId))
	{
		$myquery = $conn->query("select SortId from ".DB_TablePre.$cateTable." where id='".$CatalogId."'");
		$rs = mysql_fetch_row($conn->res);
		$SortId = $rs[0];
		//mysql_free_result($myquery);
		if($action=="up")
		{
			$sql="select id,SortId from ".DB_TablePre.$cateTable." where CatalogParentId=".$CatalogParentId." and Mlevel=".$Mlevel." and SortId < ".$SortId." order by SortId desc limit 1";
		}
		elseif($action=="down")
		{
			$sql="select id,SortId from ".DB_TablePre.$cateTable." where CatalogParentId=".$CatalogParentId." and Mlevel=".$Mlevel." and SortId > ".$SortId." order by SortId asc limit 1";
		}
		
		$upquery = $conn->query($sql);		
		$rs_go = mysql_fetch_row($conn->res);
		if($rs_go)
		{
			$conn->query("update ".DB_TablePre.$cateTable." set SortId='$rs_go[1]' where id='$CatalogId'");
			$conn->query("update ".DB_TablePre.$cateTable." set SortId='$SortId' where id='$rs_go[0]'");
		}
		Jqlib::CategoryCreate(0,$cateTable);
		header("location: ?");
	}
}
?><!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,nofollow" />
<title><?php echo $pageTitle?><?php echo $sysTitle?></title>
<link href="templates/admin.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../lib/jquery/jquery.js"></script>
<script type="text/javascript" src="js/controlPanelMenu.js"></script>
<script type="text/javascript" src="js/commmonAdmin.js"></script>
</head>
<body>
<?php
include("./admin_header.php")
?>
<div id="containerAdmin" class="wrapper_m">
<div id="sidebarAdmin">
<?php
include("./admin_controlPanelDir.php")
?>
</div>
<div id="contentbarAdmin">
<div class="tips_2"><?php echo adminPageLocation($pageTitle)?></div>
<table class="catalogTableV0 mt_5">
<tr>
<td class="c_l">
<div class="catalogList">
<ul class="addCat">
<li><span class="cTitle"><a href="?cataid=0">根目录</a></span></li>
<?php
ShowMenuAll(0,$cateTable);
?>
</ul>
</div>
</td>
<td class="c_r">
<table align="center" cellspacing="1" class="catalogTableV1">
<tr>
<td colspan="2" class="head1">信息统计</td>
</tr>
<tr>
<td class="cw_1">类别名称:</td>
<td class="cw_2"><?php echo $mtitle?></td>
</tr>
<tr>
<td class="cw_1">添加时间:</td>
<td class="cw_2"><?php echo $mtime?></td>
</tr>
</table>
<table align="center" cellspacing="1" class="catalogTableV1">
<form action="?action=modify&doid=<?php echo $getid?>" method="post" name="modify" id="modify" target="_parent">
<tr>
<td colspan="2" class="head1">修改当前类别</td>
</tr>
<tr>
<td class="cw_1">名称:</td>
<td class="cw_2"><input name="cat_mod_name" type="text" class="moduleBorderA moduleInputA iW_150" value="<?php echo $mtitle?>"></td>
</tr>
<tr>
<td class="cw_1">类别主图:</td>
<td class="cw_2"><input id="mimageUrl" name="mimageUrl" class="moduleBorderA moduleInputA iW_150" value="<?php echo $mtp ?>"><br /><iframe src="../apps/UploadThumbnail.php?bd=mimageUrl&form=modify" name="key" width="220" height="22" marginheight="0" scrolling="No" frameborder="0" id="key">IE</iframe></td>
</tr>

<tr>
<td class="cw_1">WAP类别主图:</td>
<td class="cw_2"><input id="formpageUrl" name="formpageUrl" class="moduleBorderA moduleInputA iW_150" value="<?php echo $pageUrl ?>"><br /><iframe src="../apps/UploadThumbnail.php?bd=formpageUrl&form=modify" name="key" width="220" height="22" marginheight="0" scrolling="No" frameborder="0" id="key">IE</iframe></td>
</tr>
<tr>
<td class="cw_1">英文名称:</td>
<td class="cw_2"><input name="English" id="English" type="text" class="moduleBorderA moduleInputA iW_150" value="<?php echo $English?>"></td>
</tr>
<tr>
<td class="cw_1">排序:</td>
<td class="cw_2"><input name="mSortId" id="mSortId" type="text" class="moduleBorderA moduleInputA iW_50" value="<?php echo $mSortId?>"></td>
</tr>
<tr>
<td class="cw_1">类型:</td>
<td class="cw_2"><select name="mInfoType" id="mInfoType" class="moduleBorderSelectA">
<option value="ClsNewsList"<?php echo Check_option("ClsNewsList",$mInfoType)?>>新闻列表</option>
<option value="ClsImagesList"<?php echo Check_option("ClsImagesList",$mInfoType)?>>缩略图列表</option>
<option value="ClsSinglePage"<?php echo Check_option("ClsSinglePage",$mInfoType)?>>单页</option>
<option value="ClsVideosList"<?php echo Check_option("ClsVideosList",$mInfoType)?>>视频列表</option>
<option value="ClsDownloadsList"<?php echo Check_option("ClsDownloadsList",$mInfoType)?>>下载列表</option>
</select></td>
</tr>
<tr>
<td class="cw_1">略缩图尺寸:</td>
<td class="cw_2">宽:<input name="mImgW" type="text" class="moduleBorderA moduleInputA iW_50" value="<?php echo $mImgW?>" maxlength="4">px&nbsp;&nbsp;高:<input name="mImgH" type="text" class="moduleBorderA moduleInputA iW_50" value="<?php echo $mImgH?>" maxlength="4">px</td>
</tr>
<tr>
<td class="cw_1">关键字：<br>(Keywords):</td>
<td class="cw_2"><textarea name="thisKeywords" cols="30" rows="4" class="moduleBorderA iW_200" id="thisKeywords"><?php echo $thisKeywords;?></textarea></td>
</tr>
<tr>
<td class="cw_1">描述：<br>(Description):</td>
<td class="cw_2"><textarea name="thisDescription" cols="30" rows="4" class="moduleBorderA iW_200" id="thisDescription"><?php echo $thisDescription;?></textarea></td>
</tr>
<tr>
<td class="cw_1">是否有子分类:</td>
<td class="cw_2">
<?php
echo "<label><input type='radio' name='xia' value='1' ".Check_s($rsp["xia"],1)." style='border:none;'><span class=font_green>是√</span></label>&nbsp;&nbsp;&nbsp;&nbsp;<label><input type='radio' name='xia' value='0' ".Check_s($rsp["xia"],0)." style='border:none;'><span class=font_red>否×</span></label>";
?></td>
</tr>
<tr>
<td colspan="2" class="cw_3"><input type="submit" value=" 确认修改 " name="mod_submit" class="btn_3"></td>
</tr>
</form>
</table>
<table align="center" cellspacing="1" class="catalogTableV1">
<form action="?action=add&doid=<?php echo $getid?>" method="post" name="addcata" id="addcata" target="_parent">
<tr>
<td colspan="2" class="head1">新增类别</td>
</tr>
<tr>
<td class="cw_1">名称:</td>
<td class="cw_2"><input type="text" name="cat_add_name" class="moduleBorderA moduleInputA iW_150" value=""></td>
</tr>
<tr>
<td class="cw_1">类别主图:</td>
<td class="cw_2"><input id="aimageUrl" name="aimageUrl" class="moduleBorderA moduleInputA iW_150" value=""><br><iframe src="../apps/UploadThumbnail.php?bd=aimageUrl&form=addcata" name="key" width="220" height="22" marginheight="0" scrolling="No" frameborder="0" id="key">IE</iframe>
</label>
</td>
</tr>

<tr>
<td class="cw_1">WAP类别主图:</td>
<td class="cw_2"><input id="formpageUrl" name="formpageUrl" class="moduleBorderA moduleInputA iW_150" value=""><br /><iframe src="../apps/UploadThumbnail.php?bd=formpageUrl&form=modify" name="key" width="220" height="22" marginheight="0" scrolling="No" frameborder="0" id="key">IE</iframe></td>
</tr>
<tr>
<td class="cw_1">英文名称:</td>
<td class="cw_2"><input name="English" id="English" type="text" class="moduleBorderA moduleInputA iW_150" value=""></td>
</tr>
<tr>
<td class="cw_1">排序:</td>
<td class="cw_2"><input name="text" type="text" class="moduleBorderA moduleInputA iW_50" id="text" value="0"></td>
</tr>
<tr>
<td class="cw_1">类型:</td>
<td class="cw_2"><select name="AInfoType" id="AInfoType" class="moduleBorderSelectA">
<option value="ClsNewsList"<?php echo Check_option("ClsNewsList",$mInfoType)?>>新闻列表</option>
<option value="ClsImagesList"<?php echo Check_option("ClsImagesList",$mInfoType)?>>缩略图列表</option>
<option value="ClsSinglePage"<?php echo Check_option("ClsSinglePage",$mInfoType)?>>单页</option>
<option value="ClsVideosList"<?php echo Check_option("ClsVideosList",$mInfoType)?>>视频列表</option>
<option value="ClsDownloadsList"<?php echo Check_option("ClsDownloadsList",$mInfoType)?>>下载列表</option>
</select></td>
</tr>
<tr>
<td class="cw_1">略缩图尺寸:</td>
<td class="cw_2">宽:<input name="AImgW" type="text" class="moduleBorderA moduleInputA iW_50" id="AImgW" value="<?php echo $mImgW?>" maxlength="3">&nbsp;&nbsp;高:<input name="AImgH" type="text" class="moduleBorderA moduleInputA iW_50" id="AImgH" value="<?php echo $mImgH?>" maxlength="3"></td>
</tr>
<tr>
<td class="cw_1">关键字：<br>(Keywords):</td>
<td class="cw_2"><textarea name="AddThisKeywords" cols="30" rows="4" class="moduleBorderA iW_200" id="AddThisKeywords"></textarea></td>
</tr>
<tr>
<td class="cw_1">描述：<br>(Description):</td>
<td class="cw_2"><textarea name="AddThisDescription" cols="30" rows="4" class="moduleBorderA iW_200" id="AddThisDescription"></textarea></td>
</tr>
<tr>
<td class="cw_1">是否有子分类:</td>
<td class="cw_2"><label><input type="radio" name="xiaji" id="xiaji" value="true"><span class="font_green">是√</span></label>&nbsp;&nbsp;&nbsp;&nbsp;<label><input name="xiaji" id="xiaji" type="radio" value="false" checked><span class="font_red">否×</span></label></td>
</tr>
<tr>
<td colspan="2" class="cw_3"><input name="add_submit" type="submit" class="btn_3" id="add_submit" value=" 确认添加 "></td>
</tr>
</form>
</table>
<table align="center" cellspacing="1" class="catalogTableV1">
<form name="del" method="post"  action="?action=del&doid=<?php echo $getid?>">
<tr>
<td colspan="2" class="cw_3"><input type="button" name="del_submit" value="删除当前类" class="btn_2" onClick="dodel()">
<div class="tips_4" style="text-align:center; font:bold 12px/17px ''; padding:5px 0px; margin-top:10px;">(提示：删除一个栏目，其下的所有子栏目和内容将被清空，请慎重操作!)</div></td>
</tr>
</form>
</table>
<script type="text/javascript">
if (abc=<?php echo $getid?> < 1)
{document.modify.mod_submit.disabled = true;document.del.del_submit.disabled = true;}
else
{document.modify.mod_submit.disabled = false;document.del.del_submit.disabled = false;}
</script>
</td>
</tr>
</table>
</div>
<div class="clearfix"></div>
</div>
<?php
include("./admin_footer.php")
?>
</body>
</html>