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

$table = "faqs";

$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"));
	$title = $row->title;
	$answer = $row->answer;
}
else{
	$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" >','Вопрос:',"Введите вопрос, пожалуйста."),
	"answer" => Array('<textarea cols="75" rows="15">','Ответ:'),
));

if(isset($_POST['subm'])){
	reset ($_POST);
	while (list($key, $value) = each ($_POST)) {
		$$key = $value;
	}
		
	if($error==""){
		
		$arr = Array(
			"title" => addslashes($title),
			"answer" => addslashes($answer),
		);
		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_faq_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');

?>