<?php
    session_start(); 
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	$opendb=new mydb();
	require_once('inc/char_set.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

	$ls=$_POST['ls'];
	$id=$_POST['id'];

	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$dalei=$_POST['cateName'];
	$cateTitle=$_POST['cateTitle'];
	$cateKeywords=$_POST['cateKeywords'];
	$cateDescription=$_POST['cateDescription'];
	//$catePagePic = $_POST['catePagePic'];
	$catePagePicWidth=$_POST['width'];
	$catePagePicHeight=$_POST['height'];
	$cateBrief=$_POST['cateBrief'];
	
	$image=$_FILES['catePagePic']['name'];   
  $size=$_FILES['catePagePic']['size'];   
  $type=$_FILES['catePagePic']['type'];
	
	//echo "图像名称：".$image."<br>";
	//echo "图像大小：".$size."<br>";
	//echo "图像类型：".$type."<br>";
	
	switch ($ls)
	{
		case "insert":  ///添加大类 Start
		   
									//开始判断图像类型
									if ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg" )
									{
										if($size < $PicSize) //开始判断图像大小
										{
												// 开始判断是否有重复的
												$select_sql="select * from ls_products where dalei='".$dalei."' ";
												$select_query=$opendb->query($select_sql);
												$select_rows=mysql_num_rows($select_query);
												if ($select_rows == 0)
												   {
																						
															//调用字符串截取函数 substr_cut
															$cateFolders=substr_cut($dalei,$FolderLength);
																			
															$path = "../".$cateFolders;
															//调用建立文件夹函数 createFolder
															createFolder($path);
															
															$arr=array(" ",".");   
													  	//产生文件名   
													  	$catePagePic=str_replace($arr ,"",$cateFolders."_banner").".".substr($image,-3);
															
															$BannerPath =$path ."/".$catePagePic;
//上传文件
															move_uploaded_file($_FILES['catePagePic']['tmp_name'], $BannerPath);															move_uploaded_file($_FILES['catePagePic']['tmp_name'], $BannerPath);
															
															$insert_sql=sprintf("insert into ls_products 
															(dalei,cateFolders,cateTitle,cateKeywords,cateDescription,catePagePic,catePagePicWidth,catePagePicHeight,cateBrief,Num,enable) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
																GetSQLValueString($dalei, "text"),
																GetSQLValueString($cateFolders, "text"),
																GetSQLValueString($cateTitle, "text"),
																GetSQLValueString($cateKeywords, "text"),
																GetSQLValueString($cateDescription, "text"),
																GetSQLValueString($catePagePic, "text"),
																GetSQLValueString($catePagePicWidth, "text"),
																GetSQLValueString($catePagePicHeight, "text"),
																GetSQLValueString($cateBrief, "text"),
																GetSQLValueString($SortingNum, "int"),
																GetSQLValueString("1", "int")
															 );
															//echo $insert_sql;
															$insert_query=$opendb->query($insert_sql);
															echo "<script>alert('数据添加成功！');location='cate.php';</script>";
										
											}
											else
											{
												echo "<script>alert('已存在此大类名！\\n请填写别的大类名！');history.go(-1);</script>";	
											}
										
										}
										else
										{
											echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
										}
									}
									else
									{
										 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
									}
		
		 break;	 ///添加大类 End
	   
	  
	  case "modi":   ///修改大类 Start
	   	
	  						$select_sql="select * from ls_products where ID=".$id." ";
								$select_query=$opendb->query($select_sql);
								$select_rows=mysql_fetch_assoc($select_query);
	  						
	  						//调用字符串截取函数 substr_cut
	  						$cateFolders=substr_cut($dalei,$FolderLength);
								//获取文??鋗???岑???????????????讜????????????????????洉癯彥灵潬摡摥晟汩??呔彐佐呓??卅?慣整慐敧楐?孝琧灭湟浡???慂湮牥慐桴????????????????椤獮牥彴煳?灳楲瑮?椢獮牥?湩潴氠彳牰摯捵獴????????搨污楥挬瑡?汯敤獲挬瑡呥瑩敬挬瑡?祥潷摲?慣整敄捳楲瑰潩?慣整慐敧楐?慣整慐敧楐坣摩桴挬瑡健条健捩效杩瑨挬瑡?楲晥丬浵攬慮汢?瘠污敵?┨?猥┬?猥┬?猥┬?猥┬?猥┬?∠?????????瑥兓噌污敵瑓楲杮?慤敬?∠整瑸??????????瑥兓噌污敵瑓楲杮?慣整潆摬牥?∠整瑸??????????瑥兓噌污敵瑓楲杮?慣整楔汴?∠整瑸??????????瑥兓噌污敵瑓楲杮?慣整敋睹牯獤?琢硥??????????敇却?慖畬卥牴湩?挤瑡?獥牣灩楴湯?琢硥??????????敇却?慖畬卥牴湩?挤瑡健条健捩?琢硥??????????敇却?慖畬卥牴湩?挤瑡健条健捩楗瑤?∠整瑸??????????瑥兓噌污敵瑓楲杮?慣整慐敧楐?楥桧?∠整瑸??????????瑥兓噌污敵瑓楲杮?慣整牂敩?∠整瑸??????????瑥兓噌污敵瑓楲杮?潓瑲湩乧浵?椢瑮??????????瑥兓噌污敵瑓楲杮∨??椢瑮??????????????????支档?椤獮牥彴煳?????????湩敳瑲煟敵祲?灯湥扤?畱牥?椤獮牥彴煳?????????捥潨∠猼牣灩?污牥??????????潬慣楴湯?慣整瀮灨??捳楲瑰??????????????????汥敳?????笉??????捥潨∠猼牣灩?污牥?????????岡湜?????????栻獩潴祲朮????捳楲瑰???????紉???????????????攉獬?????笉?????攉档??猼牣灩?污牥????????楐卣穩????楨瑳牯?潧???猯牣灩?????????????????汥敳????笉?????攠档??猼牣灩?污牥???????????????????????屜??????朮晩?湰?樮数??楨瑳牯?潧???猯牣灩??????紉??戠敲歡※????????湅?????挠獡?洢摯?????????瑓牡????????敳敬瑣獟汱∽敳敬瑣?映潲?獬灟潲畤瑣?桷牥??∽?摩∮∠?????敳敬瑣煟敵祲?灯湥扤?畱牥?猤汥捥彴煳??????敳敬瑣牟睯?祭煳彬敦捴彨獡潳?猤汥捥彴畱牥?????????????????????畳獢牴损瑵?????慣整潆摬牥?畳獢牴损瑵?慤敬??汯敤?湥瑧?????瀉??∠浩条?楧≦簠?琤灹8?浩????簠?琤灹??∠浩条?橰数≧?????笉?????晩?楳敺??楐卣穩???????????????笉????????????????????????敳敬瑣獟汱∽敳敬瑣?映潲?獬灟潲畤瑣?桷牥?慤敬?∧?慤敬??∠???????敳敬瑣煟敵祲?灯湥扤?畱牥?猤汥捥彴煳????????敳敬瑣牟睯?祭煳彬畮彭潲獷?敳敬瑣煟敵祲???????晩?猤汥捥彴潲獷??????????笠箖件路径
								$pathNew = "../".$cateFolders;
								$pathOld = "../".$select_rows['cateFolders'];
	  						//echo $pathNew."<br>".$pathOld."<br>";
	  				 	//文件夹换名
	  				 	if($pathNew!=$pathOld)
								{
	  				 	rename($pathOld,$pathNew);
	  					}
	  				 	if($ChangecatePagePic=="yes")
	  				 	{
	  				 		//判断图像格式
	  				 		if ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg" )
										{
											if($size < $PicSize) //开始判断图像大小
											{
					  				 		$arr=array(" ",".");   
											  	//产生文件名   
											  	$catePagePic=str_replace($arr ,"",$select_rows['cateFolders']."_banner").".".substr($image,-3);
													
													$BannerPath =$pathNew ."/".$catePagePic;
													//echo $BannerPath."<br>";
													//上传文件
													move_uploaded_file($_FILES['catePagePic']['tmp_name'], $BannerPath);
											}
											else
											{
												echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
											}
										}
										else
										{
											 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
										}
	  				 	}
	  				 	
	  				 	//开始更新数据
	  				 	$update_sql=sprintf("update ls_products set dalei=%s, cateFolders=%s,cateTitle=%s, cateKeywords=%s, cateDescription=%s, catePagePicWidth=%s, catePagePicHeight=%s, cateBrief=%s where ID=%s",
						   							  GetSQLValueString($dalei, "text"),
															GetSQLValueString($cateFolders, "text"),
															GetSQLValueString($cateTitle, "text"),
															GetSQLValueString($cateKeywords, "text"),
															GetSQLValueString($cateDescription, "text"),
															GetSQLValueString($catePagePicWidth, "text"),
															GetSQLValueString($catePagePicHeight, "text"),
															GetSQLValueString($cateBrief, "text"),
															GetSQLValueString($id, "int")
															);
								//echo $update_sql;
								$update_query=$opendb->query($update_sql);
								echo "<script>alert('数据更新成功！');location='cate.php';</script>";
	   
	   break;  ///修改大类 End
	  
	  }
	 
	  ///删除大类  Start
	 if ($_GET['ls']=="del")
	 {
	 			$id=$_GET['id'];
	   		
	   		$select_sql="select * from ls_products where ID=".$id." ";
				$select_query=$opendb->query($select_sql);
				$select_rows=mysql_fetch_assoc($select_query);
	   		
	   		//echo $select_rows['cateFolders']."<br>".$select_rows['catePagePic']."<br>";
	   		//获得图片路径
	   		$pathFoldersPic="../".$select_rows['cateFolders']."/".$select_rows['catePagePic'];
	   		//获得文件路径
	   		$pathFolders="../".$select_rows['cateFolders'];
	   		//echo $pathFolders.$pathFoldersPic."<br>";
	   		
	   		//删除图片
	   		unlink($pathFoldersPic);
	   		//删除文件夹
	   		rmdir($pathFolders);
	   		
	   		$del_sql="delete from ls_products where ID=".$id."";
	   		//echo $del_sql;
	   		$del_query=$opendb->query($del_sql);
	   		echo "<script>alert('数据删除成功！');location='cate.php';</script>";
	 }
	 ///删除大类  End
?>

</body>
</html>