<?php
session_start();

include('includes/ajax_check_login.php');

if( !isset($_POST['picture_id']) || ( $static_uint_picture_id = (int)$_POST['picture_id'] ) <= 0 )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'輸入資料不足', 'RedirectURL'=>'')));

require_once('includes/common.php');

require_once('includes/admin.php');
$obj_class_admin = new class_admin();

if( !( $arr_news_picture = $obj_class_admin->get_news_picture_by_picture_id($static_uint_picture_id) ) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'所選擇的圖片不存在', 'RedirectURL'=>'')));
else
{
	mysqli_query($obj_class_admin->sql_connect, 'BEGIN;');

	if( !$obj_class_admin->delete_news_picture($static_uint_picture_id) )
	{
		mysqli_query($obj_class_admin->sql_connect, 'ROLLBACK;');
		mysqli_query($obj_class_admin->sql_connect, 'COMMIT;');
		
		exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'圖片刪除失敗！', 'RedirectURL'=>'')));
	}
	else if( @file_exists($str_file_name = '../images/news/' . $arr_news_picture[0]['NewsId'] . '/' . $static_uint_picture_id . '.jpg') && !@unlink($str_file_name) )
	{
		mysqli_query($obj_class_admin->sql_connect, 'ROLLBACK;');
		mysqli_query($obj_class_admin->sql_connect, 'COMMIT;');
		
		exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'照片刪除失敗！', 'RedirectURL'=>'')));
	}

	mysqli_query($obj_class_admin->sql_connect, 'COMMIT;');

	if( $arr_news_picture[0]['IsCover'] )
	{
		$str_target_file_name = '../images/news/' . $arr_news_picture[0]['NewsId'] . '.jpg';
		unlink($str_target_file_name);
		
		if( ( $arr_news_pictures = $obj_class_admin->get_news_pictures_by_news_id($arr_news_picture[0]['NewsId']) ) && $obj_class_admin->set_news_picture_is_cover($arr_news_pictures[0]['PictureId']) )
		{
			$str_source_file_name = '../images/news/' . $arr_news_picture[0]['NewsId'] . '/' . $arr_news_pictures[0]['PictureId'] . '.jpg';
				
			if( $arr_image_info = getimagesize($str_source_file_name) )
			{
				$float_img_file_ratio = (float)$arr_image_info[1] / (float)$arr_image_info[0];
				$float_target_ratio = (float)NEWS_COVER_HEIGHT / (float)NEWS_COVER_WIDTH;

				if( $float_img_file_ratio == $float_target_ratio )
				{
					$uint_source_width = $arr_image_info[0];
					$uint_source_height = $arr_image_info[1];

					$uint_source_x = 0;
					$uint_source_y = 0;
				}
				else if( $float_img_file_ratio < $float_target_ratio )
				{
					$uint_source_width = round($arr_image_info[1] / $float_target_ratio);
					$uint_source_height = $arr_image_info[1];

					$uint_source_x = floor(( $arr_image_info[0] - $uint_source_width ) / 2);
					$uint_source_y = 0;
				}
				else
				{
					$uint_source_width = $arr_image_info[0];
					$uint_source_height = round($arr_image_info[0] * $float_target_ratio);

					$uint_source_x = 0;
					$uint_source_y = floor(( $arr_image_info[1] - $uint_source_height ) / 2);
				}

				$img_file = imagecreatefromjpeg($str_source_file_name);

				$img_temp_cover = imagecreatetruecolor(NEWS_COVER_WIDTH, NEWS_COVER_HEIGHT);
				$color_white = imagecolorallocate($img_temp_cover, 255, 255, 255);
				imagefill($img_temp_cover, 0, 0, $color_white);

				imagecopyresampled($img_temp_cover, $img_file, 0, 0, $uint_source_x, $uint_source_y, NEWS_COVER_WIDTH, NEWS_COVER_HEIGHT, $uint_source_width, $uint_source_height);
				imagedestroy($img_file);

				imagejpeg($img_temp_cover, $str_target_file_name, 83);
				imagedestroy($img_temp_cover);
			}
		}
	}

	exit(json_encode(array('ErrCode'=>1, 'ErrMessage'=>'圖片刪除成功', 'RedirectURL'=>'')));
}
?>