<?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
	$ls=$_POST['ls'];
	$lso=$_POST['lso'];
	$id=$_POST['id'];
	$motifnav=$_POST['motifnav'];

	$motifText=htmlspecialchars($_POST['motifText'],ENT_QUOTES);
	$motif_name=$_POST['motif_name'];
	$old_motif_name=$_POST['old_motif_name'];
	
	$motif_href=$_POST['motif_href'];
	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$image=$_FILES['productPic']['name'];
	$size=$_FILES['productPic']['size'];
	$type=$_FILES['productPic']['type'];
	$pro_name=$_POST['motif_name'];
	
	$pathBigPic2="../" .($MotifFolders=="motif"?$MotifFolders."/".$BigFoldersProducts:$MotifFolders) ;
	//主题图片上传文件夹名，默认images，原来是 motif
	$psp=strpos($pro_name,".");
	$plen=($psp===false?strlen($pro_name):$psp);
	$productPic=substr($pro_name,0,$plen).".".get_extension($image); 
	//str_replace($arr ,"",$pro_name).".".get_extension($image);
	$productPic_s=$pathBigPic2."/".substr($pro_name,0,$plen)."_s.".get_extension($image);  //移动端图片
	$pathBigPic=$pathBigPic2."/".$productPic;
	//echo $pathBigPic."==";
	$srcFile=$pathBigPic; //源地址
	$toW=$titlemobPicW;	//手机宽度
	$toH=$titlemobPicH;	//手机高度
	$toFile= $productPic_s; //目标地址 $pathSmallPic."/".$productPic
	
	$ifnohome=( strpos( $motifnav,"首页")===false);
	$iftype=!(strpos($imagetypes,$type)===false);
	$ifhometype= ($type=="image/jpeg");
	
	switch ($ls)
	{
		case "insert":
		
			if ($ifnohome &&  $iftype ||  !$ifnohome && $ifhometype  )
				{
				 if($size < $PicSize) //开始判断图像大小
					{
						//echo $pathBigPic."<br>";"../".$MotifFolders."/".$BigFoldersProducts
						//创建大图文件夹
						//createFolder($pathBigPic);
						$pathSmallPic=$pathBigPic ;
						//echo $pathSmallPic."<br>"; "../".$MotifFolders."/".$SmallFoldersProducts
						//创建小图文件夹
						//createFolder($pathSmallPic);
					
						$arr=array(" ",".");
					//上传文件
					move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
					ImageResize($pathBigPic,$toW,$toH,$toFile); //生成手机图
					 
					
					$insert_sql=sprintf("insert into ls_motif(motifPic,motif_href,motifText,Num,enable) values(%s,%s,%s,%s,%s)",
						GetSQLValueString($productPic, "text"),
						GetSQLValueString($motif_href, "text"),
						GetSQLValueString($motifText, "text"),
						GetSQLValueString($SortingNum, "int"),
						GetSQLValueString("1", "int")
						);
					$insert_query=$opendb->query($insert_sql);
					echo "
						<form name=\"fm\" method=\"post\" action=\"addmotif.php\">
						<input type=\"hidden\" value=\"".$motifText."\" name=\"motifText\">
						<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:15px;\" width=\"8%\" >*缩略图：</td>
										<td style=\"padding-left:5px;\" width=\"92%\" >
											<img src=\" ".$pathBigPic."?i=".rand()." \" width=\"700\" height=\"".$toH."\" border=\"1\" >
											
										</td>
									  </tr>
									  <tr bgcolor=\"#f4f4f4\"> 
										<td style=\"padding-left:15px;\" width=\"8%\">主题简介：</td>
										<td width=\"92%\">
										".$motifText."
										</td>
									  </tr>
									  <tr bgcolor=\"#B9DAFF\"> 
										<td height=\"28\" colspan=\"2\" align=\"center\">
										<input type=\"submit\" value=\"继续添加\">
										<input type=\"button\" value=\"返回列表\" onclick=\"javascript:location='motif.php';\">
										</td>
									  </tr>
									
									</table>
								</td>
							</tr>
							</table>
							</form>
						";
					}						
				 else
					{
						echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
					}
				}
			else
				{
					 echo "	<script>alert('图片类型不对，首页主题图须是jpg，其他页是".$imagetypess." ');history.go(-1);</script>";
				}
			
			break;
		
	
		case "update":
				//	
		if($lso=="default"||$lso=="pagemotif")  // 默认主题图 和 内页主题图
		{
			
			$folderPic=urldecode($_POST['folderPic']);
			if($ChangecatePagePic=="yes")
			{
				//判断图像格式  if (!(strpos($imagetypes,$type)===false) )
				if ( $ifnohome &&  $iftype ||  !$ifnohome && $ifhometype  )
				{
					if($size < $PicSize) //开始判断图像大小
					{
						$pathBigPic=$folderPic;
							
						//上传文件
						move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
						ImageResize($pathBigPic,$toW,$toH,$toFile); //生成手机图
						//echo $pathBigPic."".$toFile; exit;

						
					}
					else
					{
						echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
					}
				}
				else
				{
					 echo "	<script>alert('图片没修改，类型不合适。首页主题图须是jpg，其他页".$imagetypess." ');history.go(-1);</script>";
				}
			} 
			if($lso=="pagemotif")  //保存页面详细
			{
				$menuName=urldecode($_POST['menuName']);	
				$modi_sql="update ls_menu set description='".$motifText."' where menuName='".$menuName."' ";
				//echo $modi_sql;
				$modi_query=$opendb->query($modi_sql);
											
			}
		}  //if($lso=="default"||$lso=="pagemotif")
		else
		{
		  if($ChangecatePagePic=="yes")
			{
				//判断图像格式				if (!(strpos($imagetypes,$type)===false))
				// echo var_dump($ifnohome &&  $iftype ||  !$ifnohome && $ifhometype  ) ;  exit;
				if ( $ifnohome &&  $iftype ||  !$ifnohome && $ifhometype  )
				{
					if($size < $PicSize) //开始判断图像大小
					{
/*						$pathBigPic="../".$MotifFolders."/".$BigFoldersProducts;
						$pathSmallPic="../".$MotifFolders."/".$SmallFoldersProducts;
							
						$arr=array(" ",".");   
						//产生文件名   
						$pic=$_POST['Pic'];
						$pic=trim(str_replace(substr($image,-4),"",$pic));
						//echo $pic;
						$productPic=str_replace($arr ,"",$pic).".".substr($image,-3);
																				
						$pathBigPic=$pathBigPic."/".$productPic;
*/						//echo $pathBigPic;
						//上传文件
						move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
						ImageResize($pathBigPic,$toW,$toH,$toFile); //生成手机图
					
/*						$srcFile=$pathBigPic; //源地址
						$toW=$SmallPicW;			//宽度
						$toH=$SmallPicH;			//高度
						$toFile=$pathSmallPic."/".$productPic; //目标地址
*/						
					}
					else
					{
						echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
					}
				}
				else
				{
					 
					 echo "	<script>alert('图片未修改，图片格式不对。首页主题图须是jpg，其他页是".$imagetypess." ');history.go(-1);</script>"; 
				}
			} 
			
			if($old_motif_name != $motif_name){
				
				if(!file_exists("../".$MotifFolders."/".$BigFoldersProducts."/".$motif_name)){
					$update_sql=sprintf("update ls_motif set motifPic=%s where ID=%s",
						GetSQLValueString($motif_name, "text"),
						GetSQLValueString($id, "int")
					);
					if($update_query=$opendb->query($update_sql)){
						 rename("../".$MotifFolders."/".$BigFoldersProducts."/".$old_motif_name, "../".$MotifFolders."/".$BigFoldersProducts."/".$motif_name);
					echo "<script>alert('数据更新成功!\\n请刷新本页！');location='modimotif.php?id=".$id."&lso=".$lso."&folderPic=".urlencode($folderPic)."&menuName=".urlencode($menuName)."';</script>";
					}
				}else{
					echo "<script>
					alert('文件名重复，请换个名称再试试。".$code."');
					location='modimotif.php?id=".$id."&lso=".$lso."&folderPic=".urlencode($folderPic)."&menuName=".urlencode($menuName)."'
					</script>";
				}
				
			}else{
				
				$update_sql2=sprintf("update ls_motif set motif_href=%s, motifText=%s where ID=%s",
					GetSQLValueString($motif_href, "text"),
					GetSQLValueString($motifText, "text"),
					GetSQLValueString($id, "int")
				);
				if($update_query=$opendb->query($update_sql2)){
					echo "<script>alert('数据更新成功!\\n请刷新本页！');location='modimotif.php?id=".$id."&lso=".$lso."&folderPic=".urlencode($folderPic)."&menuName=".urlencode($menuName)."';</script>";
				}
				//echo $update_sql;
				 //更新数据
			}
		}
		break;	
	}
?>
	</body>
</html>