<?php
	require_once('_approot.php');	
?>
<!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="css/admin.css" rel="stylesheet" type="text/css">
		<title>无标题文档</title>
	</head>

	<body>
<?php
	$_SESSION['savepmidcate']=$_POST;
	$picNameOld=$_POST['PicOld'];
	$ls=$_POST['ls'];
	//$midcateName=replaceCatesName($_POST['midcateName']);
	$midcateName=trim($_POST['midcateName']);
	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$catePagePicWidth=$_POST['width'];
	$catePagePicHeight=$_POST['height'];
	
	$image=$_FILES['productPic']['name'];   
	$size=$_FILES['productPic']['size'];   
	$type=$_FILES['productPic']['type'];
	
	//获得大类名称，用来判读此类下是否有同名的中类
	$cateName = str_replace("'","`",trim($_POST['cateName']));
	$cateTitle=str_replace("'","`",$_POST['cateTitle']);
	$cateKeywords=str_replace("'","`",$_POST['cateKeywords']);
	$cateDescription=str_replace("'","`",$_POST['cateDescription']);


	$midcateBrief= str_replace("'","''",htmlspecialchars($_POST['midcateBrief'], ENT_QUOTES)) ;
	$midcateBrief=editorprocess($midcateBrief,$_SERVER['PHP_SELF'],$midcateName,$midcateName); //综合处理
 	$midcateDetail=str_replace("'","''",htmlspecialchars($_POST['midcateDetail'], ENT_QUOTES));
	$midcateDetail=editorprocess($midcateDetail,$_SERVER['PHP_SELF'],$midcateName,$midcateName); //综合处理
	

	$midcateFolders_str = $_POST['midcateFolders'];
	if(preg_match("/[\x7f-\xff]/", $midcateFolders_str)){
		$midcateFolders=Pinyin($midcateFolders_str,1);
	}else{
		$midcateFolders=$midcateFolders_str;
	}
	if($midcateFolders=="")				//如果没有设目录则截取
	{$midcateFolders=$midcateName;}
	$midcateFolders=trim(clearFolder(strtolower(substr_cut($midcateFolders,$FolderLength))));

	
	$midcateID=$_POST['midcateID'];
	$cate_id=$_POST['cate_id'];
	$cateName=trim($_POST['cateName']);
	
	if($midcateID!="")
	{
		$cate_sql="select * from ls_products where ID='".$cate_id."' ";
		$cate_query=$opendb->query($cate_sql);
		$cate_rows=mysqli_fetch_assoc($cate_query);
		$cateFolders=$cate_rows['cateFolders'];
	}
	
	$path = "../".$cateFolders."/".$midcateFolders;
	
	switch ($ls)
	{
		case "insert":
						
			$midcateName = str_replace("'","''",$midcateName);
			// 开始判断是否有重复的
			$select_sql="select * from ls_products where midcateName='".$midcateName."' and cateName='".$cateName."'";
			$select_query=$opendb->query($select_sql);
			$select_rows=mysqli_num_rows($select_query);
			if($select_rows==0)
			{

				//调用建立文件夹函数 createFolder
				createFolder($path);
				//echo 	$path;
				if($ChangecatePagePic=="yes")
	  			{
					//判断图像格式
					if (!(strpos($imagetypes,$type)===false))
						{
							if($size < $PicSize) //开始判断图像大小
							{
		
								$randomnum=NULL;
								for($j=0;$j<=5;$j++)//随机数字的长度，本例随机数长度为6
								{
								srand((double)microtime()*1000000);
								$randomnumber=rand(!$j ? 1: 0,9);//产生随机数，不以0为第一个数，有些特殊的地方0开头被系统省略
								$randomnum.=$randomnumber;
								}
								
								$dest=date("Ymd").time().$randomnum;//设置文件名为时间戳加上随机数避免重复
		
								$pic = $_FILES['productPic']['name'];
								$pic=substr($pic,(strlen($pic)-4),strlen($pic));
								$picName = $dest.$pic;
								$pathBigPic = $path."/".$picName;
								//上传文件
								move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
							}
							else
							{
								echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
							}
						}
						else
						{
							echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式".$imagetypess."');history.go(-1);</script>";
						}
		
					//echo $update_sql;
					//更新数据
				}
					
					$select_sql2="select * from ls_products where midcateID=".$midcateID." ";
					$select_query2=$opendb->query($select_sql2);
					$select_rows2=mysqli_num_rows($select_query2); //总类树
		
					$insert_sql=sprintf("insert into ls_products (cateTitle,cateKeywords,cateDescription,midcateName,midcateFolders,midcateBrief,catePic,midcateDetail,midcateID,Num,enable,catePagePicWidth,catePagePicHeight,date) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
												GetSQLValueString($cateTitle, "text"),
												GetSQLValueString($cateKeywords, "text"),
												GetSQLValueString($cateDescription, "text"),
												GetSQLValueString($midcateName, "text"),
												GetSQLValueString($midcateFolders, "text"),
												GetSQLValueString($midcateBrief, "text"),
												GetSQLValueString($picName, "text"),
												GetSQLValueString($midcateDetail, "text"),	
												GetSQLValueString($midcateID, "text"),
												GetSQLValueString($select_rows2+1, "int"),
												GetSQLValueString("1", "int"),
												GetSQLValueString($catePagePicWidth, "text"),
												GetSQLValueString($catePagePicHeight, "text"),
												GetSQLValueString(date('Y-m-d H:i:s'), "text")
											 );
					//echo $insert_sql;
					$insert_query=$opendb->query($insert_sql);
					writelog($_SERVER['PHP_SELF'],"add mid-category: ".$midcateName,"ID: ".$id);
					unset($_SESSION['savepmidcate']);
					
					echo "<script>alert('数据添加成功！');location='cate.php';</script>";
				}
				else
				{
					echo "<script>alert('已存在此中类名！\\n请填写别的中类名！');history.go(-1);</script>";	
				}
	
				break;
		
		case "update":
			
			if($ChangecatePagePic=="yes")
	  		{
				//判断图像格式
				if (!(strpos($imagetypes,$type)===false) )
					{
						if($size < $PicSize) //开始判断图像大小
						{
							
							$oldbigpic=$path."/".$picNameOld;
							if($picNameOld != null)
							unlink($oldbigpic);		//delete old big pic
							
							$randomnum=NULL;
							for($j=0;$j<=5;$j++)//随机数字的长度，本例随机数长度为6
							{
							srand((double)microtime()*1000000);
							$randomnumber=rand(!$j ? 1: 0,9);//产生随机数，不以0为第一个数，有些特殊的地方0开头被系统省略
							$randomnum.=$randomnumber;
							}
							
							$dest=date("Ymd").time().$randomnum;//设置文件名为时间戳加上随机数避免重复
	
							$pic = $_FILES['productPic']['name'];
							$pic=substr($pic,(strlen($pic)-4),strlen($pic));
							$picName = $dest.$pic;
							$pathBigPic =$path."/".$picName;
							//上传文件
							move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
						}
						else
						{
							echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
						}
					}
					else
					{
						echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 ".$imagetypess."');history.go(-1);</script>";
					}
					
					$update_pic_sql=sprintf("update ls_products set catePic=%s where ID=%s",
					GetSQLValueString($picName, "text"),
					GetSQLValueString($midcateID, "int")
					);
				//更新产品图片
					$update_query=$opendb->query($update_pic_sql);
				//echo $update_sql;
				//更新数据
			}
				
				$select_sql="select * from ls_products where ID='".$midcateID."' ";
				$select_query=$opendb->query($select_sql);
				$select_rows=mysqli_fetch_assoc($select_query);
				//获取文件夹路径
				$pathNew = "../".$cateFolders."/".$midcateFolders;
				$pathOld = "../".$cateFolders."/".$select_rows['midcateFolders'];
				if($pathNew!=$pathOld)
				{
				//文件夹换名
		  		rename($pathOld,$pathNew);
				}
				//开始更新数据
				$update_sql=sprintf("update ls_products set cateTitle=%s, cateKeywords=%s, cateDescription=%s, midcateName=%s, midcateFolders=%s,midcateBrief=%s,midcateDetail=%s,catePagePicWidth=%s, catePagePicHeight=%s where ID=%s ",
												GetSQLValueString($cateTitle, "text"),
												GetSQLValueString($cateKeywords, "text"),
												GetSQLValueString($cateDescription, "text"),
												GetSQLValueString($midcateName, "text"),
												GetSQLValueString($midcateFolders, "text"),
												GetSQLValueString($midcateBrief, "text"),
												GetSQLValueString($midcateDetail, "text"),					
												GetSQLValueString($catePagePicWidth, "text"),
												GetSQLValueString($catePagePicHeight, "text"),
												GetSQLValueString($midcateID, "text")
												);
					//echo $update_sql;
					$update_query=$opendb->query($update_sql);
					writelog($_SERVER['PHP_SELF'],"modify mid-category: ".$midcateName,"ID: ".$midcateID);
					unset($_SESSION['savepmidcate']);
					echo "<script>alert('数据更新成功！');location='cate.php';</script>";
			break;
	}
	
	if($_GET['ls']=="del")
	{
				
			$id=$_GET['id'];
			//echo $id;	
			$select_midcate_sql="select * from ls_products where ID=".$id."";
			$select_midcate_query=$opendb->query($select_midcate_sql);
			$select_midcate_rows=mysqli_fetch_assoc($select_midcate_query);
			
			$select_cate_sql="select * from ls_products where ID='".$select_midcate_rows['midcateID']."'";
			$select_cate_query=$opendb->query($select_cate_sql);
			$select_cate_rows=mysqli_fetch_assoc($select_cate_query);
			
			//获取文件夹路径
			$path = "../".$select_cate_rows['cateFolders']."/".$select_midcate_rows['midcateFolders'];
			//删除文件夹
	   		//die($path."<br>");
			@unlink($path."/indexdetails.html");
			@unlink($path."/index.html");
	   		deldir($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_midcate_rows['Num']." ";//all order number-1
			$down_query=$opendb->query($num_sql2);
			writelog($_SERVER['PHP_SELF'],"delete mid-category: ".$midcateName,"ID: ".$id);
	   		echo "<script>alert('数据删除成功！');location='cate.php';</script>";
	   		
	}
?>

</body>
</html>