<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<link href="../../../ru/update/1.70 - 2.26/css/admin.css" rel="stylesheet" type="text/css">
		<title>无标题文档</title>
	</head>

	<body>
<?PHP
	require_once('../../../ru/update/1.70 - 2.26/inc/conn.php');
	require_once('../../../ru/update/1.70 - 2.26/inc/constant.php');
	require_once('../../../ru/update/1.70 - 2.26/inc/ls_syscode.php');

	$opendb=new mydb();
	require_once('../../../ru/update/1.70 - 2.26/inc/char_set.php');
	
	$ls=$_POST['ls'];
	//$subcateName=replaceCatesName($_POST['subcateName']);
	$subcateName=trim($_POST['subcateName']);
	//获得中类名称，用来判断此类下是否有同名的小类
	$midcateName = trim($_POST['midcateName']);
	
	$cateTitle=$_POST['cateTitle'];
	$cateKeywords=$_POST['cateKeywords'];
	$cateDescription=$_POST['cateDescription'];
	
	$subcateBrief=htmlspecialchars(str_replace("\n","<br />",$_POST['subcateBrief']), ENT_QUOTES);
	$subcateDetail=htmlspecialchars($_POST['subcateDetail'], ENT_QUOTES);
	$subcateFolders=$_POST['subcateFolders'];
	if($subcateFolders=="")				//如果没有设目录则截取
	{$subcateFolders=$subcateName;}
	$subcateFolders=trim(clearFolder(strtolower(substr_cut($subcateFolders,$FolderLength))));
	
	$subcateID=$_POST['subcateID'];
	$midcateName=trim($_POST['midcateName']);
	if($subcateID!=""){
		$midcate_sql="select * from ls_products where midcateName='".$midcateName."' ";
		$midcate_query=$opendb->query($midcate_sql);
		$midcate_rows=mysql_fetch_assoc($midcate_query);
		$midcateFolders=$midcate_rows['midcateFolders'];
		$cateID=$midcate_rows['midcateID'];
	
		$cate_sql="select * from ls_products where ID=".$cateID." ";
		$cate_query=$opendb->query($cate_sql);
		$cate_rows=mysql_fetch_assoc($cate_query);
		$cateFolders=$cate_rows['cateFolders'];
	}
	switch ($ls){
		case "insert":
			
			// 开始判断是否有重复的
			$select_sql="select * from ls_products where subcateName='".$subcateName."' and midcateName='".$midcateName."'";
			$select_query=$opendb->query($select_sql);
			$select_rows=mysql_num_rows($select_query);
			if($select_rows==0)
				{
					$path ="../".$cateFolders."/".$midcateFolders."/".$subcateFolders;
					//调用建立文件夹函数 createFolder
					createFolder($path);
					//echo 	$path;				
					$select_sql2="select * from ls_products where subcateID=".$subcateID." ";
					$select_query2=$opendb->query($select_sql2);
					$select_rows2=mysql_num_rows($select_query2); //总类树
												
					$insert_sql=sprintf("insert into ls_products 
											(cateTitle,cateKeywords,cateDescription,subcateName,subcateFolders,subcateBrief,subcateDetail,subcateID,Num,enable) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
												GetSQLValueString($cateTitle, "text"),
												GetSQLValueString($cateKeywords, "text"),
												GetSQLValueString($cateDescription, "text"),
												GetSQLValueString($subcateName, "text"),
												GetSQLValueString($subcateFolders, "text"),
												GetSQLValueString($subcateBrief, "text"),
												GetSQLValueString($subcateDetail, "text"),
												GetSQLValueString($subcateID, "text"),
												GetSQLValueString($select_rows2+1, "int"),
												GetSQLValueString("1", "int")
											 );
					//echo $insert_sql;
					$insert_query=$opendb->query($insert_sql);
					writelog($_SERVER['PHP_SELF'],"add sub-category: ".$subcateName,"ID: ".$id);
					echo "<script>alert('数据添加成功！');location='cate.php';</script>";
				}
				else
				{
					echo "<script>alert('已存在此小类名！\\n请填写别的小类名！');history.go(-1);</script>";	
				}
			break;
		case "update":
				$select_sql="select * from ls_products where ID='".$subcateID."' ";
				$select_query=$opendb->query($select_sql);
				$select_rows=mysql_fetch_assoc($select_query);
				//获取文件夹路径
				$pathNew = "../".$cateFolders."/".$midcateFolders."/".$subcateFolders;
				$pathOld = "../".$cateFolders."/".$midcateFolders."/".$select_rows['subcateFolders'];
				if($pathNew!=$pathOld)
				{
				//文件夹换名
	  		rename($pathOld,$pathNew);
				}
				//开始更新数据
				$update_sql=sprintf("update ls_products set cateTitle=%s, cateKeywords=%s, cateDescription=%s, subcateName=%s, subcateFolders=%s,subcateBrief=%s,subcateDetail=%s where ID=%s ",
												GetSQLValueString($cateTitle, "text"),
												GetSQLValueString($cateKeywords, "text"),
												GetSQLValueString($cateDescription, "text"),
												GetSQLValueString($subcateName, "text"),
												GetSQLValueString($subcateFolders, "text"),
												GetSQLValueString($subcateBrief, "text"),
												GetSQLValueString($subcateDetail, "text"),
												GetSQLValueString($subcateID, "text")
												);
					//echo $update_sql;
					$update_query=$opendb->query($update_sql);
					writelog($_SERVER['PHP_SELF'],"modify sub-category: ".$subcateName,"ID: ".$subcateID);
					echo "<script>alert('数据更新成功！');location='cate.php';</script>";
			break;
	}
	
	if($_GET['ls']=="del")
	{
				$id=$_GET['id'];
				//echo $id;	
				$select_sql="select * from ls_products where ID=".$id."";
				$select_subcate_query=$opendb->query($select_sql);
				$select_subcate_rows=mysql_fetch_assoc($select_subcate_query);
				
				$select_mid_sql="select * from ls_products where ID='".$select_subcate_rows['subcateID']."'";
				$select_mid_cate_query=$opendb->query($select_mid_sql);
				$select_mid_cate_rows=mysql_fetch_assoc($select_mid_cate_query);
				
				$select_cate_sql="select * from ls_products where ID='".$select_mid_cate_rows['midcateID']."'";
				$select_cate_query=$opendb->query($select_cate_sql);
				$select_cate_rows=mysql_fetch_assoc($select_cate_query);
				
				//获取文件夹路径
				$path = "../".$select_cate_rows['cateFolders']."/".$select_mid_cate_rows['midcateFolders']."/".$select_subcate_rows['subcateFolders'];
				//删除文件夹
	   		   //echo $path."<br>";indexdetails.html
			   @unlink($path."/indexdetails.html");
		   	@unlink($path."/index.html");
	   		rmdir($path);
	   		$del_sql="delete from ls_products where ID=".$id."";
	   		//echo $del_sql;
	   		$del_query=$opendb->query($del_sql);
			$num_sql2="update ls_products set Num=Num-1 where Num>".$select_subcate_rows['Num']." ";//all order number-1
			$down_query=$opendb->query($num_sql2);
			writelog($_SERVER['PHP_SELF'],"delete sub-category: ".$subcateName,"ID: ".$id);
	   		echo "<script>alert('数据删除成功！');location='cate.php';</script>";
	}
?>
</body>
</html>