<?php
session_start();

include('includes/ajax_check_login.php');

require_once('includes/common.php');

if( !isset($_POST['news_id']) 
|| !isset($_POST['video_no']) 
|| !isset($_POST['subject']) 
|| ( $static_uint_news_id = (int)$_POST['news_id'] ) <= 0 
|| ( $static_str_video_no = process_quotes(strip_tags($_POST['video_no'])) ) == '' )
	exit(json_encode(array('ErrCode'=>-1, 'ErrMessage'=>'輸入資料不足', 'RedirectURL'=>'')));
else
	$static_str_subject = trim(strip_tags($_POST['subject']));

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

if( !$obj_class_admin->get_news_by_news_id($static_uint_news_id) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'指定的新聞不存在！', 'RedirectURL'=>'')));
else if( $obj_class_admin->get_news_video_by_news_id_n_video_no($static_uint_news_id, $static_str_video_no) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'指定的影片已存在！', 'RedirectURL'=>'')));
else if( !$obj_class_admin->add_news_video($static_uint_news_id, $static_str_subject, $static_str_video_no) )
	exit(json_encode(array('ErrCode'=>0, 'ErrMessage'=>'新增新聞影片失敗！', 'RedirectURL'=>'')));

if( !$obj_class_admin->check_news_has_pictures($static_uint_news_id) && ( $arr_news_videos = $obj_class_admin->get_news_videos_by_news_id($static_uint_news_id) ) )
{
	$str_img_file_name = '../images/news/' . $static_uint_news_id . '.jpg';

	if( !file_exists($str_img_file_name) || count($arr_news_videos) == 1 )
	{
		$str_image_url = 'http://i.ytimg.com/vi/' . $arr_news_videos[0]['VideoNo'] . '/0.jpg';
		$str_temp_file_name = '../images/news/' . time() . '.jpg';

		if( file_put_contents(dirname(__FILE__) . '/' . $str_temp_file_name, @file_get_contents($str_image_url)) )
		{
			if( !( $arr_image_info = getimagesize($str_temp_file_name) ) || $arr_image_info[2] != IMAGETYPE_JPEG || !( $img_file = imagecreatefromjpeg($str_temp_file_name) ) )
				unlink($str_temp_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_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_img_file_name, 83);
			imagedestroy($img_temp_cover);
		}
	}
}

exit(json_encode(array('ErrCode'=>1, 'ErrMessage'=>'新增新聞影片成功', 'RedirectURL'=>'')));
?>