<?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'];
	$id=$_POST['id'];
	
	$title=$_POST['newsTitle'];
	$keywords=$_POST['newsKeywords'];
	$description=$_POST['newsDescription'];
	$videoName=$_POST['videoName'];
	$videoText=$_POST['videoText'];
	$videoText=editorprocess($videoText,$_SERVER['PHP_SELF'],$title,$title); 
	
	$videoW=$_POST['videoW'];
	$videoH=$_POST['videoH'];
	$videoCode=$_POST['videoCode'];
	$ChangevideoPic=$_POST['ChangevideoPic'];
	$Changevideo=$_POST['Changevideo'];
	$date=$_POST['date'];
	$endtime=$_POST['endtime'];
	$cateName=trim($_POST['cateName']);
	$videoPic=$_POST['videoPic'];
	$OldPic=$_POST['OldPic'];
	$OldVideo=$_POST['OldVideo'];
	
	$templ=$_POST['templ'][0];

	$video_name=$_FILES['videoFile']['name'];   
    $video_size=$_FILES['videoFile']['size'];   
    $video_type=$_FILES['videoFile']['type'];
	
	$image=$_FILES['videoPic']['name'];   
  	$size=$_FILES['videoPic']['size'];   
  	$type=$_FILES['videoPic']['type'];
	$videopath="../".$videoFolders;
	$arr=array(" ",".");  
	//echo $video_type."--==".$type;exit;
			 
	switch ($ls)
	{
		case "insert":
			if($type!= NULL){ //图片
				if (!(strpos($imagetypes,$type)===false))
				{
					if($size < $PicSize) //开始判断图像大小
					{
						//产生文件名   
						$productPic=str_replace($arr ,"",time()).".".get_extension($image);//substr($image,-3);
						//上传文件
						$pathPic=$videopath."/".$productPic;
						$pathBigPic=$videopath."/".str_replace(".","_b.",$productPic);
						//echo $pathPic."--".$pathBigPic."--".$_FILES['videoPic']['tmp_name']; exit;
						move_uploaded_file($_FILES['videoPic']['tmp_name'], $pathBigPic);
						
						$srcFile=$pathBigPic; //源地址
						$toW=$SmallPicW;			//宽度
						$toH=$SmallPicH;			//高度
						$toFile=$pathPic; //目标地址
						//生成缩略图
						ImageResize($srcFile,$toW,$toH,$toFile);

					}else{
						echo "	<script>alert('图片最大为".$PicSize."！');history.go(-1);</script>";
					}
				}else{
					 echo "	<script>alert('请选择图片上传，或者你选择的文件不符\n只允许格式 ".$imagetypess." ');history.go(-1);</script>";
				}
			}
				
			if($video_type != NULL){
				if (!(strpos($videotypes,$video_type)===false))
				{
					//echo $video_size."--".$videoSizeMax ; exit;
					
					if($video_size < $videoSizeMax*1000000) //开始判断图像大小
					{
						$videoN = str_replace($arr ,"",time()).".".get_extension($video_name);//substr($video_name,-3);
						$videopath = $videopath."/".$videoN;
						move_uploaded_file($_FILES['videoFile']['tmp_name'], $videopath);
						
						$insert_sql=sprintf("insert into ls_video(videoPic,newsTitle, newsKeywords, newsDescription,videoFile,videoName,videoText,date,endtime,Num,code,cateName, enable) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
									GetSQLValueString($productPic, "text"),
									GetSQLValueString($title, "text"),
									GetSQLValueString($keywords, "text"),
									GetSQLValueString($description, "text"),
									GetSQLValueString($videoN, "text"),
									GetSQLValueString($videoName, "text"),
									GetSQLValueString($videoText, "text"),
									GetSQLValueString($date,"date"),
									GetSQLValueString($endtime,"text"),
									GetSQLValueString($SortingNum, "int"),
									GetSQLValueString($videoCode, "text"),
									GetSQLValueString($cateName, "text"),
									GetSQLValueString("1", "int")
									);
						 
						$insert_query=$opendb->query($insert_sql);
						if($insert_query) $ls_msg="视频添加成功！"; else $ls_msg="视频添加失败！";
			
						echo "<form name=\"fm\" method=\"post\" action=\"addvideo.php\">
								<input type=\"hidden\" value=\"".$videoText."\" name=\"videoText\">
								<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>
										".$videoName."
										</td>
									  </tr>
									  <tr bgcolor=\"#f4f4f4\"> 
										<td style=\"padding-left:50px;\" width=\"30%\">简介：</td>
										<td>
										".$videoText."
										</td>
									  </tr>
									  <tr bgcolor=\"#B9DAFF\"> 
										<td height=\"28\" colspan=\"2\" align=\"center\">
										<input type=\"submit\" value=\"继续添加\">
										<input type=\"button\" value=\"返回列表\" onclick=\"javascript:location='video.php';\">
										</td>
									  </tr>
									
									</table>
								</td>
								</tr>
								</table>
								</form>
							";

					}						
						 else
						{
							echo "	<script>alert('视频最大为".$videoSizeMax."M ！');history.go(-1);</script>";
						}
					}
					else
					{
						 echo "	<script>alert('请选择视频上传，或者你选择的文件不符\n只允许格式 ".$videotypess." ');history.go(-1);</script>";
					}
				}else{
						if($videoCode != NULL){
							$insert_sql=sprintf("insert into ls_video(videoName,newsTitle, newsKeywords, newsDescription,videoText,date,endtime,Num,code,cateName,videoPic,enable) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
							GetSQLValueString($videoName, "text"),
							GetSQLValueString($title, "text"),
							GetSQLValueString($keywords, "text"),
							GetSQLValueString($description, "text"),
							GetSQLValueString($videoText, "text"), 
							GetSQLValueString($date,"date"),
							GetSQLValueString($endtime,"text"),
							GetSQLValueString($SortingNum, "int"),
							GetSQLValueString($videoCode, "text"),
							GetSQLValueString($cateName, "text"),
							GetSQLValueString($productPic, "text"),
							GetSQLValueString("1", "int")
							);
							$insert_query=$opendb->query($insert_sql);
							if($insert_query){
								$ls_msg="视频添加成功！";
								
							}
						}else{
								$ls_msg="请选择视频文件或链接至少添加一个!";
								 
						}
					}
					if($templ=="yes" && $cateName!="video"){ //创建模板
						$tpl=$cateName.".html";
						$tplshow=str_replace(".","show.",$tpl);
	
						if(!file_exists("../Templates1/".$tpl)  )  
							if(!copy("../Templates1/video.html","../Templates1/".$tpl)) $ls_msg.=$tpl."复制模板错误!";//复制模板
							else $ls_msg.=$tpl."模板已复制。";
						else $ls_msg.= $tpl."已模板存在。".($tplarcate?$tpl."使用大类模板。":"");
					
						if(!file_exists("../Templates1/".$tplshow)) 
							if(!copy("../Templates1/articleshow.html","../Templates1/".$tplshow)) $ls_msg.=$tplshow."复制模板错误!";//复制模板
							else $ls_msg.=$tplshow."模板已复制。";
						else $ls_msg.= $tplshow."已模板存在。".($tplarcate?$tplshow."或使用大类模板。":"");
					}
			echo "	<script>alert('".($ls_msg==""?"??":$ls_msg)."');location='video.php';</script>";
			break;
		
	
		case "update":
 			if($type!= NULL && $ChangevideoPic=="yes" ){ //图片
				if (!(strpos($imagetypes,$type)===false))
				{
					if($size < $PicSize) //开始判断图像大小
					{
						//产生文件名   
						$productPic=str_replace($arr ,"",time()).".".get_extension($image);//.substr($image,-3);
						//上传文件
						$pathPic=$videopath."/".$productPic;
						$pathBigPic=$videopath."/".str_replace(".","_b.",$productPic);
						move_uploaded_file($_FILES['videoPic']['tmp_name'], $pathBigPic);
						//var_dump($_FILES['videoPic']['tmp_name'], $pathBigPic);exit;
						$srcFile=$pathBigPic; //源地址
						$toW=$SmallPicW;			//宽度
						$toH=$SmallPicH;			//高度
						$toFile=$pathPic; //目标地址
						//生成缩略图
						ImageResize($srcFile,$toW,$toH,$toFile);

					}else{
						echo "	<script>alert('图片最大为".round(($PicSize/1000))."K ！');history.go(-1);</script>";
					}
				}else{
					 echo "	<script>alert('请选择图片上传，或者你选择的文件不符\n只允许格式 ".$imagetypess." ');history.go(-1);</script>";
				}
			}else { 	
 				$productPic= $OldPic ;  
			} //no change pic
				

		  if($Changevideo=="yes")
		  {
				//判断格式
				if (!(strpos($videotypes,$video_type)===false))
				{
					if($video_size < $videoSizeMax*1000000 ) //开始判断大小
					{
						$pathBigPic="../".$videoFolders."/".$BigFoldersProducts;
						$pathSmallPic="../".$videoFolders."/".$SmallFoldersProducts;
						
						$pic=$_POST['Pic'];
						unlink($videopath."/".$pic);
						$videoN = str_replace($arr ,"",time()).".".get_extension($video_name);//.substr($video_name,-3);
						$videopath = $videopath."/".$videoN;
						move_uploaded_file($_FILES['videoFile']['tmp_name'], $videopath);
						
						$update_sql2=sprintf("update ls_video set videoFile=%s where ID=%s",
							GetSQLValueString($videoN, "text"),
							GetSQLValueString($id, "int")
						);
						$update_query=$opendb->query($update_sql2);
					}
					else
					{
						echo "	<script>alert('最大为".round($PicSize/1000)."K ！');history.go(-1);</script>";
					}
				}
				else
				{
					 echo "	<script>alert('请选择视频上传，或者你选择的文件不符合要求!\格式 ".$videotypess."');history.go(-1);</script>";
				}
			} else { 		  
 				$videoN= $OldVideo ;  
			}//no change video
			
			$update_sql=sprintf("update ls_video set videoName=%s, code=%s, videoText=%s, newsTitle=%s, newsKeywords=%s, newsDescription=%s, videoW=%s, videoH=%s, date=%s, endtime=%s, cateName=%s, videoPic=%s where ID=%s", 
						GetSQLValueString($videoName, "text"),
						GetSQLValueString($videoCode, "text"),
						GetSQLValueString($videoText, "text"),
						GetSQLValueString($title, "text"),
						GetSQLValueString($keywords, "text"),
						GetSQLValueString($description, "text"),
						GetSQLValueString($videoW, "int"),
						GetSQLValueString($videoH, "int"),
						GetSQLValueString($date,"date"),
						GetSQLValueString($endtime,"text"),
						GetSQLValueString($cateName, "text"),
						GetSQLValueString($productPic,"text"),
						GetSQLValueString($id, "int")
					);
		   
		  //更新数据
		  //echo $update_sql;exit;
		  $update_query=$opendb->query($update_sql);
		  echo "<script>alert('数据更新成功!\\n');location='video.php?id=".$id."';</script>";
				
		break;	
	}
?>
	</body>
</html>