<?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'];
	$BigClassName=$_POST['BigClassName'];
	$MiddleClassName=$_POST['MiddleClassName'];
	$SmallClassName=$_POST['SmallClassName'];
	$additionalAttributes=0;
	if(isset($_POST['showonhome'])){
		$additionalAttributes+=1<<intval($_POST['showonhome']);
	}
	if(isset($_POST['hotproduct'])){
		$additionalAttributes+=1<<intval($_POST['hotproduct']);
	}
	if(isset($_POST['newproduct'])){
		$additionalAttributes+=1<<intval($_POST['newproduct']);
	}
	//echo $_POST['hotproduct']."--".$_POST['newproduct']."--".$additionalAttributes;
	$pic=$_POST['Pic'];
	$No=$_POST['No'];
	$canshu = htmlspecialchars(trim($_POST['canshu']), ENT_QUOTES);
	
	$_POST['automationGif']?$automationGif=$_POST['automationGif']:$automationGif=0;

	
	if($SmallClassName=="")
	{
		if($MiddleClassName=="")
		{		
			$productID=$BigClassName;
			$select_BigClassName_sql="select * from ls_products where ID=".$productID." ";
			$select_BigClassName_query=$opendb->query($select_BigClassName_sql);
			$select_BigClassName_rows=mysql_fetch_assoc($select_BigClassName_query);
			$pathFoldersProducts="../".$select_BigClassName_rows['cateFolders'];
			$productPID=$select_BigClassName_rows['ID'];// 为产生顺序号Num采集变量
		}
		else
		{
			$productID=$BigClassName;
			$select_BigClassName_sql="select * from ls_products where ID=".$productID." ";
			$select_BigClassName_query=$opendb->query($select_BigClassName_sql);
			$select_BigClassName_rows=mysql_fetch_assoc($select_BigClassName_query);
						
			$productID=$MiddleClassName;
			$select_MiddleClassName_sql="select * from ls_products where ID=".$productID." ";
			$select_MiddleClassName_query=$opendb->query($select_MiddleClassName_sql);
			$select_MiddleClassName_rows=mysql_fetch_assoc($select_MiddleClassName_query);
			$pathFoldersProducts="../".$select_BigClassName_rows['cateFolders']."/".$select_MiddleClassName_rows['midcateFolders'];
			$productPID=$select_MiddleClassName_rows['ID'];

		}
    }
	else
	{

		
		$productID=$BigClassName;
		$select_BigClassName_sql="select * from ls_products where ID=".$productID." ";
		$select_BigClassName_query=$opendb->query($select_BigClassName_sql);
		$select_BigClassName_rows=mysql_fetch_assoc($select_BigClassName_query);
						
		$productID=$MiddleClassName;
		$select_MiddleClassName_sql="select * from ls_products where ID=".$productID." ";
		$select_MiddleClassName_query=$opendb->query($select_MiddleClassName_sql);
		$select_MiddleClassName_rows=mysql_fetch_assoc($select_MiddleClassName_query);
		
		$productID=$SmallClassName;	
		$select_SmallClassName_sql="select * from ls_products where ID=".$productID." ";
		$select_SmallClassName_query=$opendb->query($select_SmallClassName_sql);
		$select_SmallClassName_rows=mysql_fetch_assoc($select_SmallClassName_query);
		
		$pathFoldersProducts="../".$select_BigClassName_rows['cateFolders']."/".$select_MiddleClassName_rows['midcateFolders']."/".$select_SmallClassName_rows['subcateFolders'];
		$productPID=$select_SmallClassName_rows['ID'];

	}
	 
	$productItemNo=$_POST['productItemNo'];
	$productName=$_POST['productName'];
	$productSpecification=htmlspecialchars($_POST['productSpecification'], ENT_QUOTES);
	$productTitle=$_POST['productTitle'];
	$productDescription=$_POST['productDescription'];
	$productKeywords=$_POST['productKeywords'];
	
	$image=$_FILES['productPic']['name'];   
  	$size=$_FILES['productPic']['size'];   
  	$type=$_FILES['productPic']['type'];
	switch ($ls)
	{
		case "insert":
		
			if ($type == "image/gif" || $type == "image/png" || $type == "image/x-png" || $type == "image/pjpeg" || $type == "image/jpeg" ){
				 if($size < $PicSize){
					 	//开始判断图像大小
						$pathBigPic=$pathFoldersProducts."/".$BigFoldersProducts;
						//echo $pathBigPic."<br>";
						//创建大图文件夹
						createFolder($pathBigPic);
						$pathSmallPic=$pathFoldersProducts."/".$SmallFoldersProducts;
						//echo $pathSmallPic."<br>";
						//创建小图文件夹
						createFolder($pathSmallPic);
						$pathPagePic=$pathFoldersProducts."/".$showproductsFolders;
						//echo $pathSmallPic."<br>";
						//创建大图浏览（.htm）文件夹
						createFolder($pathPagePic);
															
						$arr=array(" ",".");   
						//产生文件名
						$productPic=str_replace($arr ,"",time()).".".substr($image,-3);
													  	
						$pathBigPic=$pathBigPic."/".$productPic;
						//echo  $pathBigPic;
						move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
						chmod($pathBigPic, 0644);
						$toW=$BigPicW;			//宽度
						$toH=$BigPicH;			//高度
						$toFile=$pathBigPic;	//目标地址
						$srcFile=$pathBigPic;	//目标地址
						
						if(!$_POST['automationGif'])  //是动画则不缩小
						{
							ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
						}
						chmod($toFile,0644);

						$srcFile=$pathBigPic; //源地址
						$toW=$SmallPicW;			//宽度
						$toH=$SmallPicH;			//高度
						$toFile=$pathSmallPic."/".$productPic; //目标地址
						if(!$_POST['automationGif']) //是动画则不缩小
						{
							ImageResize($srcFile,$toW,$toH,$toFile);
						}
						else
						{
							copy( $pathBigPic,$toFile);
						}
						chmod($toFile,0644);
						$select_sql2="select * from ls_products where productID=".$productPID." ";
						$select_query2=$opendb->query($select_sql2);
						$select_rows2=mysql_num_rows($select_query2); //总产品数
						$insert_sql=sprintf("insert into ls_products 															(productPic,productItemNo,productName,productSpecification,productID,Num,No,AdditionalAttributes,canshu,enable,automationGif,productTitle,productKeywords,productDescription) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
						GetSQLValueString($productPic, "text"),
						GetSQLValueString($productItemNo, "text"),
						GetSQLValueString($productName, "text"),
						GetSQLValueString($productSpecification, "text"),
						GetSQLValueString($productID, "int"),
						GetSQLValueString($select_rows2+1, "int"),
						GetSQLValueString($No, "int"),
						GetSQLValueString($additionalAttributes, "int"),
						GetSQLValueString($canshu, "text"),
						GetSQLValueString("1", "int"),
						GetSQLValueString($automationGif, "int"),
						GetSQLValueString($productTitle, "text"),
						GetSQLValueString($productKeywords, "text"),
						GetSQLValueString($productDescription, "text")

						);
						//echo $insert_sql;
						//添加数据
						
						$insert_query=$opendb->query($insert_sql);
														
						echo "
						<form name=\"fm\" method=\"post\" action=\"addproducts.php\">
						<input type=\"hidden\" value=\"".$BigClassName."\" name=\"BigClassName\">
						<input type=\"hidden\" value=\"".$SmallClassName."\" name=\"SmallClassName\">
						<input type=\"hidden\" value=\"".$MiddleClassName."\" name=\"MiddleClassName\">
						<input type=\"hidden\" value=\"".$productItemNo."\" name=\"productItemNo\">
						<input type=\"hidden\" value=\"".$productName."\" name=\"productName\">
						<input type=\"hidden\" value=\"".$canshu."\" name=\"canshu\">
						<input type=\"hidden\" value=\"".$productSpecification."\" name=\"productSpecification\">
						<table width='96%' border='0' align='center' cellpadding='1' cellspacing='1' bgcolor='#6396d6'>
						<tr>
						<td height='18' background='images/admin_bg.gif'><div align='center'><font color='#ffffff'><strong>产品信息</strong></font></div>
						</td>
						</tr>
						<tr>
						<td height='213' valign='top' bgcolor='#dee7ff'><br>
						<table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\">
					 	<tr bgcolor=\"#B9DAFF\"> 
						<td height=\"28\" colspan=\"2\"></td>
						 </tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">所属类别：</td>
						<td>
						";
						if($select_BigClassName_rows['cateName']!="")
						{
						echo " 大类：".$select_BigClassName_rows['cateName'];
						}
						if($select_MiddleClassName_rows['midcateName']!="")
						{
						echo "  中类：".$select_MiddleClassName_rows['midcateName'];
						}
						if($select_SmallClassName_rows['subcateName']!="")
						{
						echo "  小类：".$select_SmallClassName_rows['subcateName'];
						}
						echo "</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">产品编号：</td>
						<td>
						".$productItemNo."
						</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">产品名称：</td>
						<td>
						".$productName."
						</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">产品其他参数：</td>
						<td>
						".str_replace("\n",'<br />',htmlspecialchars_decode($canshu,ENT_QUOTES))."
						</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" >*缩略图：</td>
						<td style=\"padding-left:5px;\" >
						<img src=\" ".$toFile." \" width=\"".$toW."\" height=\"".$toH."\" border=\"1\" >
						</td>
						</tr>
						<tr bgcolor=\"#f4f4f4\"> 
						<td style=\"padding-left:50px;\" width=\"30%\">产品规格：</td>
						<td>
						".htmlspecialchars_decode($productSpecification,ENT_QUOTES)."
						</td>
						 </tr>
						<tr bgcolor=\"#B9DAFF\"> 
						<td height=\"28\" colspan=\"2\" align=\"center\">
						<input type=\"submit\" value=\"继续添加\">
						
						<input type=\"button\" value=\"返回列表\" onclick=\"javascript:location='products.php?cateid=".$BigClassName."&catename=".$catename."&midcateid=".$MiddleClassName."&midcatename=".$midcatename."&subcateid=".$SmallClassName."&subcatename=".$subcatename."';\">
						</td>
						 </tr>
						</table>
						</td>
						</tr>
						</table>
						</form>
						";
										
					}						
						else
						{
							echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
						}
				
			 }
			else
				{
					 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
				}
		break;
		
	case "update":
		if($ChangecatePagePic=="yes")
	  	{
	  	//判断图像格式
	  		if ($type == "image/gif" || $type == "image/png" || $type == "image/x-png" || $type == "image/pjpeg" || $type == "image/jpeg" )
				{
					if($size < $PicSize) //开始判断图像大小
						{
							$pathBigPic=$pathFoldersProducts."/".$BigFoldersProducts;
							//echo $pathBigPic."<br>";
							$pathSmallPic=$pathFoldersProducts."/".$SmallFoldersProducts;
							
							$oldbigpic=$pathBigPic."/".$pic;
							unlink($oldbigpic);		//delete old big pic
							
							$oldsmallpic=$pathSmallPic."/".$pic;
							unlink($oldsmallpic);	//delete old small pic

															
							$arr=array(" ",".");   
							//产生文件名   
							$pic=substr($pic,0,(strlen($pic)-4));
							//echo $pic;
							$productPic=$pic.".".substr($image,-3);
												  														  	
							$pathBigPic=$pathBigPic."/".$productPic;
							//echo $pathBigPic;
							//上传文件
							move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
							$toW=$BigPicW;			//宽度
							$toH=$BigPicH;			//高度
							$toFile=$pathBigPic;		//目标地址
							$srcFile=$pathBigPic;	//目标地址
							if(!$_POST['automationGif'])  //是动画则不缩小
							{
								ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
							}

							chmod($toFile,0644);
							//判断是否有图片的备份文件，如果有同时也更新图片的备份文件
							if(file_exists($pathBigPic)){
								copy($pathBigPic,$pathBigPic.".bak");
							}
													  	
							$srcFile=$pathBigPic; //源地址
							$toW=$SmallPicW;			//宽度
							$toH=$SmallPicH;			//高度
							$toFile=$pathSmallPic."/".$productPic; //目标地址
													  	//生成缩略图
							if(!$_POST['automationGif']) //是动画则不缩小
							{
								ImageResize($srcFile,$toW,$toH,$toFile);
							}
							else
							{
								copy( $pathBigPic,$toFile);
							}
														

							chmod($toFile,0644);
				        }
						else
						{
							echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
						}
					}
					else
					{
						echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
					}
					
					$update_pic_sql=sprintf("update ls_products set productPic=%s where ID=%s",
					GetSQLValueString($productPic, "text"),
					GetSQLValueString($id, "int")
					);
				//更新产品图片
					$update_query=$opendb->query($update_pic_sql);
				} 
	  			     	
				$update_sql=sprintf("update ls_products set productItemNo=%s, productName=%s, productSpecification=%s, AdditionalAttributes=%s, No=%s,canshu=%s,waterMark=%s ,automationGif=%s ,productTitle=%s ,productKeywords=%s ,productDescription=%s where ID=%s",
				GetSQLValueString($productItemNo, "text"),
				GetSQLValueString($productName, "text"),
				GetSQLValueString($productSpecification, "text"),
				GetSQLValueString($additionalAttributes, "int"),
				GetSQLValueString($No, "int"),
				GetSQLValueString($canshu, "text"),
				GetSQLValueString(0, "int"),
				GetSQLValueString($automationGif, "int"),
				GetSQLValueString($productTitle, "text"),
				GetSQLValueString($productKeywords, "text"),
				GetSQLValueString($productDescription, "text"),
				GetSQLValueString($id, "int")
				);
				//echo $update_sql;
				//更新数据
				$update_query=$opendb->query($update_sql);
				
				
				//更新类别
				$update_sql="";
				$products_sql="select * from ls_products where ID=".$id." ";
				$products_query=$opendb->query($products_sql);
				$products_rows=mysql_fetch_assoc($products_query);
				$oldfolder=getproductfolder($products_rows['productID']);
				if($BigClassName!=""&&$MiddleClassName==""&&$SmallClassName=="")
				{
					if($BigClassName!=$products_rows['productID']){ $update_sql="update ls_products set productID=".$BigClassName." where ID=".$id." ";
					$newfolder=getproductfolder($BigClassName);
					}
				}
				if($BigClassName!=""&&$MiddleClassName!=""&&$SmallClassName=="")
				{
					if($MiddleClassName!=$products_rows['productID']){ $update_sql="update ls_products set productID=".$MiddleClassName." where ID=".$id." ";
					$newfolder=getproductfolder($MiddleClassName);
					}
				}
				if($BigClassName!=""&&$MiddleClassName!=""&&$SmallClassName!="")
				{
					if($SmallClassName!=$products_rows['productID']){ $update_sql="update ls_products set productID=".$SmallClassName." where ID=".$id." ";
					$newfolder=getproductfolder($SmallClassName);
					}
				}
				
				if($update_sql!="")
				{
					$products_query=$opendb->query($update_sql);
					//echo $update_sql."update";
					if($products_query)//移动产品目录
					{
						if(!file_exists ("../".$newfolder."/".$SmallFoldersProducts)){mkdir ("../".$newfolder."/".$SmallFoldersProducts);}
						if(!file_exists ("../".$newfolder."/".$BigFoldersProducts)){mkdir ("../".$newfolder."/".$BigFoldersProducts);}
						if(!file_exists ("../".$newfolder."/".$showproductsFolders)){mkdir ("../".$newfolder."/".$showproductsFolders);}

						copy("../".$oldfolder."/".$SmallFoldersProducts."/".$products_rows['productPic'], "../".$newfolder."/".$SmallFoldersProducts."/".$products_rows['productPic']);
						copy("../".$oldfolder."/".$BigFoldersProducts."/".$products_rows['productPic'], "../".$newfolder."/".$BigFoldersProducts."/".$products_rows['productPic']);
						unlink ("../".$oldfolder."/".$SmallFoldersProducts."/".$products_rows['productPic']);
						unlink ("../".$oldfolder."/".$BigFoldersProducts."/".$products_rows['productPic']);

					}
				}
				writelog($_SERVER['PHP_SELF'],"change products: ".$productName,"ID: ".$id);

				$_SESSION["refreshModi"]=true;
				echo "<script>alert('数据更新成功!\\n');location='modiproducts.php?id=".$id."&cateid=".$BigClassName."&catename=".$catename."&midcateid=".$MiddleClassName."&midcatename=".$midcatename."&subcateid=".$SmallClassName."&subcatename=".$subcatename."';</script>";
		break;
	}
?>
</body>
</html>
