<?
if($_POST["mag_id"]=="")
{
echo"undefinedata";
exit();
}
//var
$mag_id		=	base64_decode($_POST["mag_id"]);
$mag_name	=	$_POST["mag_name"];
$mag_public =   $_POST["mag_public"];


//call data
 $sql="SELECT * FROM ann_gall WHERE mag_id='".$mag_id."'";
 $result=mysql_query($sql) or die ( $sql);
 $ROW=mysql_fetch_array($result);
$name_time = time();
//ถ้ามีภาพทัมเนล
if($_FILES['mag_thumb']['name'] != ""){

//หาเอกเทนชั่นของภาพ
$doc_filename = stripslashes($_FILES['mag_thumb']['name']);
$doc_extension = getExtension($doc_filename);
//upload doc
$newdocname="../datacenter/upload/frn". $name_time.".".$doc_extension;
copy($_FILES['mag_thumb']['tmp_name'], $newdocname);
$mag_thumb = "frn".$name_time.".".$doc_extension;
if($ROW["mag_thumb"] != "")
	{
unlink("../datacenter/upload/".$ROW["mag_thumb"]);
	}
}
else
{
$mag_thumb = $ROW["mag_thumb"];
}

//เพิ่มจำนวนหน้า
//เรียกหาเลขหน้าสุดท้าย
 $subcorsql="select  * from  ann_gall_page where page_mag_id = '".$mag_id."' order  by page_int desc LIMIT 0,1";
$subcorquery =mysql_query($subcorsql) or die ($subcorsql);
$subcorrow = mysql_fetch_array($subcorquery);
$a = $subcorrow["page_int"];
for($j=1;$j<=(int)($_POST["hdnLine"]);$j++)
		{
			if($_FILES["fileUpload".$j]["name"] != "")
			{   $b = $j+$a;
				$fileextent = getExtension($_FILES["fileUpload".$j]["name"]);
				$pagename = "page".$name_time."_".$j.".".$fileextent;
				$sqlim = "insert into ann_gall_page (page_int, page_mag_id, page_images) values ('$b','$mag_id','$pagename')";
				mysql_query($sqlim);
				
				$images = $_FILES["fileUpload".$j]["tmp_name"];
				$new_images = $pagename;
				copy($images,"../datacenter/upload/".$new_images);
				$width=500; //*** Fix Width & Heigh (Autu caculate) ***//
				$size=GetimageSize($images);
				$height=round($width*$size[1]/$size[0]);

					if($fileextent=="jpg"||$fileextent=="jpeg"){
				$images_orig = ImageCreateFromJPEG($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageJPEG($images_fin,"../datacenter/upload/".$new_images);
				}elseif($fileextent=="png"){
				$images_orig = ImageCreateFromPNG($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImagePNG($images_fin,"../datacenter/upload/".$new_images);
				}elseif($fileextent=="gif"){
				$images_orig = ImageCreateFromGIF($images);
				$photoX = ImagesX($images_orig);
				$photoY = ImagesY($images_orig);
				$images_fin = ImageCreateTrueColor($width, $height);
				ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
				ImageGIF($images_fin,"../datacenter/upload/".$new_images);
				}
				ImageDestroy($images_orig);
				ImageDestroy($images_fin);
			}
		}
//ปรับการเรียกเลขที่ี่

for($j=1;$j<=(int)($_POST["pageedit"]);$j++)
{
$pagerecord  = $_POST["pagesc".$j];

$arrpage	 = explode("-", $pagerecord);

$page_id     = $arrpage[0];
$page_int    = $arrpage[1];
// อัพเดท sql
$sqlpage ="UPDATE ann_gall_page SET page_int = '$page_int' WHERE page_id='$page_id' LIMIT 1";
mysql_query("$sqlpage");
}

//ปรับการบันทักในตารางหลัก

$sqlupdate="UPDATE ann_gall SET mag_name='".$mag_name."' ,mag_public='".$mag_public."' ,mag_thumb='".$mag_thumb."'  WHERE mag_id='".$mag_id."' LIMIT 1";
$sqlup = mysql_query("$sqlupdate") or die("$sqlupdate");

if($sqlup)
{
Jsalert("การแก้ไขแกลลอรี่สำเร็จ");
GoToURL("index.php?option=galllist&action=editgalltail&itemid=".$mag_id);
}
?>