<?php
require_once '../../library/config.php';

if (isset($_POST['del_path'])){
	$deleteImage = '../'.$_POST['del_path'];
	unlink($deleteImage);
}

if (isset($_POST['del_slide'])){
	$deleteImage = '../'.$_POST['del_slide'];
	$deleteImage_m = str_replace('/slide_images/', '/slide_images/m_', $deleteImage);
	$deleteImage_s = str_replace('/slide_images/', '/slide_images/s_', $deleteImage);
	unlink($deleteImage);
	unlink($deleteImage_m);
	unlink($deleteImage_s);
}

/************ news **********/
if (isset($_POST['new_news_image'])){
	$img_name  = $_POST['new_news_image'];
	$ep_web  = $_POST['news_type'];
	
	$r = mysql_query("SHOW TABLE STATUS WHERE `Name` = 'news'");
	$row = mysql_fetch_array($r);
	$Auto_increment = $row['Auto_increment'];
	mysql_free_result($r);
	
	
	mysql_query("INSERT INTO `news` (ep_web, ep_subject, cp_subject, ep_pic,  ep_date, last_update) VALUES ('$ep_web', 'Untitled News', '未命名新聞', '$img_name', NOW(), NOW())", $connePaper);
	?>
    <script type="text/javascript">
		window.location = "?action=edit&id=<?php echo $Auto_increment;?>";
	</script>
    
<?php     
}

if(isset($_POST['updates_news_image'])){
	$img_name  = $_POST['updates_news_image'];
	$id  = $_POST['news_id'];
	$ep_web  = $_POST['news_type'];
	
	mysql_query("UPDATE `news` SET `ep_pic` = '$img_name', `ep_web` = '$ep_web' WHERE `news_id` = '$id'", $connePaper);
}

/************ album **********/
if (isset($_POST['new_album_image'])){
	$img_name  = $_POST['new_album_image'];
	$ep_web  = $_POST['album_type'];
	
	$r = mysql_query("SHOW TABLE STATUS WHERE `Name` = 'album'");
	$row = mysql_fetch_array($r);
	$Auto_increment = $row['Auto_increment'];
	mysql_free_result($r);
	
	$sql_rank = mysql_query('SELECT `rank` FROM `album` ORDER BY `rank` DESC LIMIT 0, 1');
	$row_rank = mysql_fetch_assoc($sql_rank);
	$insert_rank = $row_rank['rank']+1;
	
	
	mysql_query("INSERT INTO `album` (ep_web, ep_subject, cp_subject, ep_pic,  ep_date, last_update, rank) VALUES ('$ep_web', 'Untitled Album', '未命名相簿', '$img_name', NOW(), NOW(), '$insert_rank')", $connePaper);
	?>
    <script type="text/javascript">
		window.location = "?action=edit&id=<?php echo $Auto_increment;?>";
	</script>
    
<?php     
}

if(isset($_POST['updates_album_image'])){
	$img_name  = $_POST['updates_album_image'];
	$id  = $_POST['album_id'];
	$ep_web  = $_POST['album_type'];
	
	mysql_query("UPDATE `album` SET `ep_pic` = '$img_name', `ep_web` = '$ep_web' WHERE `album_id` = '$id'", $connePaper);
}

/************ banner **********/

if (isset($_POST['new_slide_image'])){
	$img_name  = $_POST['new_slide_image'];
	$target  = $_POST['target'];
	
	$r = mysql_query("SHOW TABLE STATUS WHERE `Name` = 'banner'");
	$row = mysql_fetch_array($r);
	$Auto_increment = $row['Auto_increment'];
	mysql_free_result($r);
	
	$sql_rank = mysql_query('SELECT `rank` FROM `banner` ORDER BY `rank` DESC LIMIT 0, 1');
	$row_rank = mysql_fetch_assoc($sql_rank);
	$insert_rank = $row_rank['rank']+1;
	
	mysql_query("INSERT INTO `banner` (target, ep_subject, cp_subject, ep_pic,  last_update, rank) VALUES ('$target', 'Untitled Silde', '未命名Slide', '$img_name', NOW(), '$insert_rank')", $connePaper);
	?>
    <script type="text/javascript">
		window.location = "?action=edit&id=<?php echo $Auto_increment;?>";
	</script>
    
<?php     
}

if(isset($_POST['updates_slide_image'])){
	$img_name  = $_POST['updates_slide_image'];
	$id  = $_POST['banner_id'];
	$target  = $_POST['target'];
	
	mysql_query("UPDATE `banner` SET `ep_pic` = '$img_name', `target` = '$target' WHERE `banner_id` = '$id'", $connePaper);
}



/************ banner **********/

if(isset($_POST['updates_product_image'])){
	$img_name  = $_POST['updates_product_image'];
	$id  = $_POST['p_id'];
	
	mysql_query("UPDATE `product_list` SET `p_image` = '$img_name' WHERE `p_id` = '$id'", $connePaper);
}

mysql_close($connePaper);

?>
