<?php
session_start();

if( isset($_SESSION['init_info']) )
{
	if( ( $str_temp = trim($_SESSION['init_info']) ) != '' )
		$str_init_info = $str_temp;

	unset($_SESSION['init_info']);
}

include('includes/check_login.php');

if( !isset($_REQUEST['news_id']) || ( $static_uint_news_id = (int)$_REQUEST['news_id'] ) <= 0 )
{
	$_SESSION['init_info'] = '輸入資料不足！';

	exit('<script language="javascript">history.back();</script>');
}

require_once('includes/common.php');

require_once('includes/author.php');
$obj_class_author = new class_author();

if( !( $arr_news = $obj_class_author->get_news_by_news_id($static_uint_news_id) ) )
{
	$_SESSION['init_info'] = '指定的新聞不存在！';

	exit('<script language="javascript">history.back();</script>');
}

$str_content_file_name = '../documents/news/' . $arr_news[0]['NewsId'] . '.html';

if( file_exists($str_content_file_name) && ( $uint_file_size = filesize($str_content_file_name) ) && ( $file_handle = @fopen($str_content_file_name, 'r') ) )
{
	$str_content = fread($file_handle, $uint_file_size);
	fclose($file_handle);
}
else
	$str_content = '';

if( $str_content )
	$str_content = preg_replace('/src="images\/uploads\//i', 'src="../images/uploads/', $str_content);

$arr_news_cates_is_set = $obj_class_author->get_news_cates_by_news_id($static_uint_news_id);

$arr_news_cates = $obj_class_author->get_news_cates();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="content-language" content="zh-tw" />
	<meta name="robots" content="none" />
	<link rel="stylesheet" type="text/css" href="author.css" />
	<link rel="stylesheet" type="text/css" href="redactor/redactor.css" />
	<style type="text/css">
.redactor_box {
	width: <?=NEWS_WIDTH?>px;
}
	</style>
	<title>編輯新聞</title>
	<script type="text/javascript" src="scripts/jquery.js"></script>
	<script type="text/javascript" src="redactor/redactor.ewmib.js"></script>
	<script type="text/javascript" src="redactor/zh_tw.js"></script>
	<script type="text/javascript" src="scripts/check_date_format.js"></script>
	<script type="text/javascript">
<!--
var global_bool_processing = false;

$(function()
{
	$('#redactor').redactor({
		lang: 'zh_tw', 
		focus: true, 
		imageUpload: 'ajax_do_upload_image.php', 
		minHeight: <?=NEWS_HEIGHT?>
	});
});

function do_edit_news()
{
	if( global_bool_processing )
		return false;

	if( document.edit_news.date.value == '' )
	{
		alert('請輸入日期');
		return false;
	}

	if( document.edit_news.subject.value == '' )
	{
		alert('請輸入新聞標題');
		return false;
	}

	if( document.edit_news.author.value == '' )
	{
		alert('請輸入新聞作者');
		return false;
	}

	if( document.edit_news.abstract.value == '' )
	{
		alert('請輸入新聞摘要');
		return false;
	}

	if( document.edit_news.news_content.value == '' )
	{
		alert('請輸入新聞內容');
		return false;
	}

	if( !check_date_format(document.edit_news.date.value) )
	{
		alert('請輸入正確的日期格式(yyyy-mm-dd)');
		return false;
	}

	var obj_form_data = new FormData(document.edit_news);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_edit_news.php',
		type: 'POST',
		data: obj_form_data,
		cache: false,
		processData: false,
		contentType: false,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['RedirectURL'] != '' )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrCode'] == '1' )
				history.back();

			global_bool_processing = false;
		}
	});

	return false;
}
//-->
	</script>
</head>

<body<?=( isset($str_init_info) ? ' onLoad="javascript:alert(\'' . $str_init_info . '\');"' : '' )?>>
<? include('_title.php'); ?>
<? include('_menu.php'); ?>
<div id="content">
<h2>編輯新聞</h2>
<form name="edit_news">
<fieldset class="data">
<legend>新聞內容</legend>
<input type="hidden" name="news_id" value="<?=$arr_news[0]['NewsId']?>" />
<div class="field"><span class="subject">所屬分類</span><?
if( $arr_news_cates )
{
	echo('<ul>');

	foreach( $arr_news_cates as $arr_row )
		echo('<li><input type="checkbox" name="news_cate_id[]" value="' . $arr_row['NewsCateId'] . '" ' . ( $arr_news_cates_is_set && false !== in_2d_array($arr_row['NewsCateId'], $arr_news_cates_is_set, 'NewsCateId') ? 'checked ' : '' ) . '/>' . $arr_row['NewsCateName'] . '</li>');

	echo('</ul>');
}
?></div>
<div class="field"><span class="subject">日期</span><input class="content" type="text" name="date" value="<?=$arr_news[0]['Date']?>" maxlength="10" /></div>
<div class="field"><span class="subject">新聞標題</span><input class="content" type="text" name="subject" value="<?=$arr_news[0]['Subject']?>" maxlength="255" /></div>
<div class="field"><span class="subject">新聞作者</span><input class="content" type="text" name="author" value="<?=$arr_news[0]['Author']?>" maxlength="255" /></div>
<div class="field"><span class="subject">新聞摘要</span><textarea class="content" name="abstract"><?=$arr_news[0]['Abstract']?></textarea></div>
<div class="field"><span class="subject">新聞內容</span><textarea id="redactor" class="content" name="news_content"><?=$str_content?></textarea></div>
</fieldset>
<div class="buttons"><input class="previous" type="button" value="回上一頁" onclick="history.back();" /><input class="submit" id="button_submit" type="button" value="確定編輯" onclick="do_edit_news();" /></div>
</form>
</div>
<? include('_footer.php'); ?>
</body>

</html>