<?
/*VAR*/

if(isset($_POST["subsection"]))
{
$subsection		= $_POST["subsection"];
}
else
{
$subsection		= $_GET["subsection"];
}
if(isset($_POST["itemid"]))
{
$itemid		    = base64_decode($_POST["itemid"]);
}
else
{
$itemid		    = base64_decode($_GET["itemid"]);
}
$bannerdisplay      = $_REQUEST["bannerdisplay"];
$subaction			= $_REQUEST["subaction"];
$clearstatus	    = $_POST["clearstatus"];
/**/
$banner_name		= $_POST["banner_name"];
$banner_name_eng	= $_POST["banner_name_eng"];
$banner_link		= $_POST["banner_link"];
$name_time          = time() ;
$sql="SELECT * FROM ann_banner WHERE idann_banner='".$itemid."'";
$result=mysql_query($sql) or die ($sql);
$ROW=mysql_fetch_array($result);
if($subsection=="status")
{
if($ROW["banner_status"]=="Y"){$alterrow="N";}elseif($ROW["banner_status"]=="N"){$alterrow="Y";}
$update = "UPDATE ann_banner SET banner_status='".$alterrow."' WHERE idann_banner='".$itemid."'";
mysql_query($update);
jsalert("เปลี่ยนสถานะ Banner สำเร็จ");
GoToURL("index.php?option=ann-banner&bannerdisplay=$bannerdisplay");
}
if($subsection=="chsort")
{
if($subaction =="up")
	{
$current = getItem("select banner_sort from ann_banner  WHERE idann_banner ='".$itemid."' limit 0,1");
$newsort = $current-1;
$SQL="UPDATE ann_banner SET banner_sort='".$newsort."' WHERE idann_banner='".$itemid."'";
$RESULT=mysql_query($SQL)or die($SQL);
//จัดของเก่าให้อยู่ตำแหน่งเรา
$BSQL="UPDATE ann_banner SET banner_sort='".$current."' WHERE  banner_sort='".$newsort."' AND idann_banner!='".$itemid."' AND banner_display='".$bannerdisplay."'";
$BRESULT=mysql_query($BSQL)or die($BSQL);
}
if($subaction =="down")
	{
$current = getItem("select banner_sort from ann_banner  WHERE idann_banner ='".$itemid."' limit 0,1");
$newsort = $current+1;
$SQL="UPDATE ann_banner SET banner_sort='".$newsort."' WHERE idann_banner='".$itemid."'";
$RESULT=mysql_query($SQL)or die($SQL);
//จัดของเก่าให้อยู่ตำแหน่งเรา
$BSQL="UPDATE ann_banner SET banner_sort='".$current."' WHERE  banner_sort='".$newsort."' AND idann_banner!='".$itemid."' AND banner_display='".$bannerdisplay."'";
$BRESULT=mysql_query($BSQL)or die($BSQL);
}
jsalert("เลื่อนลำดับสำเร็จ");
Gotourl("index.php?option=ann-banner&bannerdisplay=".$bannerdisplay);
}
if($subsection=="editbanner")
{
if($banner_name==""){jsalert("Error!");  jsback(); exit();}
if($_FILES['banner_file']['name'] != ""){
//หาเอกเทนชั่นของภาพ
$bdoc_filename = stripslashes($_FILES['banner_file']['name']);
$bdoc_extension = getExtension($bdoc_filename);
//upload doc
$bnewdocname="../datacenter/banner/banner". $name_time.".".$bdoc_extension;
copy($_FILES['banner_file']['tmp_name'], $bnewdocname);
$banner_file = "banner".$name_time.".".$bdoc_extension;
if($ROW["banner_file"] != "")
	{
unlink("../datacenter/banner/".$ROW["banner_file"]);
	}
}
else
{
$banner_file = $ROW["banner_file"];
}



if($_FILES['banner_file_eng']['name'] != ""){
//หาเอกเทนชั่นของภาพ
$bdoc_filename = stripslashes($_FILES['banner_file_eng']['name']);
$bdoc_extension = getExtension($bdoc_filename);
//upload doc
$bnewdocname="../datacenter/banner/banner_eng". $name_time.".".$bdoc_extension;
copy($_FILES['banner_file_eng']['tmp_name'], $bnewdocname);
$banner_file_eng = "banner_eng".$name_time.".".$bdoc_extension;
if($ROW["banner_file_eng"] != "")
	{
unlink("../datacenter/banner/".$ROW["banner_file_eng"]);
	}
}
else
{
$banner_file_eng = $ROW["banner_file_eng"];
}


$sqlupd = "update ann_banner set banner_name = '".$banner_name."',	banner_name_eng = '".$banner_name_eng."',	banner_file='".$banner_file."', banner_file_eng='".$banner_file_eng."', banner_link='".$banner_link."' WHERE idann_banner ='".$itemid."'";
mysql_query($sqlupd) or die ($sqlupd);
jsalert("การอัพเดท Banner สำเร็จ");
GoToURL("index.php?option=ann-banner&bannerdisplay=$bannerdisplay");
}



// del banner 

elseif($subsection=="del")
{
	$fileban = getItem("select banner_file from ann_banner where idann_banner='".$itemid."'");
	$fileban2 = getItem("select banner_file_eng from ann_banner where idann_banner='".$itemid."'");
	$currentsort = getItem("select 	banner_sort from ann_banner where idann_banner='".$itemid."'");
	$rim ="../upload/banner/".$fileban;
	$rim2 ="../upload/banner/".$fileban2;
	if(file_exists($rim)){unlink($rim);}
    if(file_exists($rim2)){unlink($rim2);}
    $updatesort = "UPDATE ann_banner SET banner_sort = banner_sort-1 WHERE banner_sort>'".$currentsort."' AND 
	banner_display='".$bannerdisplay."'";
	$sortdisplay = mysql_query($updatesort);
	if($sortdisplay){
	$SQL="DELETE FROM ann_banner WHERE idann_banner=".$itemid;
	$RESULT=mysql_query($SQL)or die($SQL);
jsalert("การลบ Banner สำเร็จ");
GoToURL("index.php?option=ann-banner&bannerdisplay=$bannerdisplay");
	}
}

// add banner 

if($subsection=="addbanner")
{
if($banner_name==""){jsalert("Error!");  jsback(); exit();}
if($_FILES['banner_file']['name'] != ""){
//หาเอกเทนชั่นของภาพ
$bdoc_filename = stripslashes($_FILES['banner_file']['name']);
$bdoc_extension = getExtension($bdoc_filename);
//upload doc
$bnewdocname="../datacenter/banner/banner". $name_time.".".$bdoc_extension;
copy($_FILES['banner_file']['tmp_name'], $bnewdocname);
$banner_file = "banner".$name_time.".".$bdoc_extension;
}


if($_FILES['banner_file_eng']['name'] != ""){
//หาเอกเทนชั่นของภาพ
$bdoc_filename = stripslashes($_FILES['banner_file_eng']['name']);
$bdoc_extension = getExtension($bdoc_filename);
//upload doc
$bnewdocname="../datacenter/banner/banner_eng". $name_time.".".$bdoc_extension;
copy($_FILES['banner_file_eng']['tmp_name'], $bnewdocname);
$banner_file_eng = "banner_eng".$name_time.".".$bdoc_extension;
}
$lastnum = getItem("select banner_sort from ann_banner  where banner_display='".$bannerdisplay."' order by banner_sort desc limit 0,1");
$specnum = $lastnum+1;
$sqladd = "INSERT INTO ann_banner(
banner_name, banner_name_eng, banner_file, banner_file_eng, banner_sort,banner_display, banner_link)
VALUES ( '".$banner_name."','".$banner_name_eng."', '".$banner_file."', '".$banner_file_eng."', '".$specnum."', '".$bannerdisplay."', '".$banner_link."')";
mysql_query($sqladd) or die ($sqladd);
jsalert("บันทึก Banner สำเร็จ");
GoToURL("index.php?option=ann-banner&bannerdisplay=".$bannerdisplay);
}
?>