<?
define("ADMIN_AUTHORIZATION",1);
include("../../../includes/common.php");

$table = "news";

$id = trim($_GET['id']=="")?trim($_POST['id']):trim($_GET['id']);

$CheckObj=new TCheckForm;

//$res=pg_query($DB_LINK, );


/*getting row*/
if($id>0){
	$row = mysql_fetch_object(mysql_query("select * from $table where id=$id"));
	$date = $row->date;
	$title = $row->title;
	$title_eng = $row->title_eng;
	$text = $row->text;
	$text_eng = $row->text_eng;
	
	$date = date("d.m.Y", $date);
}
else{
	$date = date("d.m.Y", time());
	$id=-1;
}

/* generating fields */
$fields=Array();
if ($id>0) {
    $fields[id]=Array($id,'ID:');
}

$fields = Array_Merge($fields,Array(
	"title" => Array('<input type="text" size=40 class="text_input" >','Заголовок:',"Введите заголовок пожалуйста"),
	
	"text" => Array('<textarea cols="75" rows="15">','Текст:'),
	
	"date" => Array('<input type="text" size=15 class="text_input" > напр. '.date("d.m.Y", time()),'Дата:',"Укажите дату, пожалуйста"),
	
));

if(isset($_POST['subm'])){
	reset ($_POST);
	while (list($key, $value) = each ($_POST)) {
		$$key = $value;
	}
	
	$date_ar = explode(".", $date);
	if ($date_ar[0] <= 0 || $date_ar[0] > 31 || $date_ar[1] <= 0 || $date_ar[1]>12 || $date_ar[2] < 1900)
	{
		$error = "Пожалуйста, укажите правильную дату<br>\n";
	}
			
	if($error==""){
		$date = mktime(0, 0, 0, $date_ar[1], $date_ar[0], $date_ar[2]);
		
		$arr = Array(
			"title" => addslashes($title),
	
			"text" => addslashes($text),
			
			"date" => intval($date),
	
		);

		
		if(($id<0?AddRecord($table,$arr,1):UpdateRecord($table,$arr,$id)) ){
			$_SESSION['STATUS_MSG'] = "Информация успешно сохранена!";
			header("Location: ./index.php");
			exit;
		}
		else{
			$_SESSION['ERROR_MSG'] = "Ошибка при работе с базой данных.";
			header("Location: ./index.php");
			exit;
		}
	}
}	
$pass="";
$c_pass="";
foreach($fields as $key=>$value){
	$$key=htmlspecialchars(stripslashes($$key));
	$smarty->assign($key,$$key);
}

/* smarty */
$smarty->assign('id',$id);

$page_title = $id>0?"Изменение новости":"Добавление новости";
$smarty->assign('index_page','./index.php?l_pager');
$smarty->assign('page_title',$page_title);
$smarty->assign('top_menu_tpl',"admin_news_menu.tpl");

$smarty->assign('error',$error);
$smarty->assign('status',$status);

/* table template */
$smarty->assign('fields',$fields);
$smarty->assign('table_width',"95%");
$smarty->assign('main_tpl',"admin_add_form.tpl");
/* display content */
$smarty->display('admin_index.tpl');

?>