<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

//ключик позволяющий загружать файлы на сервер
if (access('edit') || access('add')) LabCMS::i()->setAllowFileUpload();

if ($_SERVER["REQUEST_METHOD"]=="POST")
{
	User::checkToken();
	if (!trim($_POST['news_text']))
	{
		LabCMS::i()->error("Не введен текст новости");
	}

	if (!preg_match('/\d{2}\.\d{2}\.\d{4}/', $_POST['news_date']))
	{
		LabCMS::i()->error("Неправильный формат даты");
	}
	else
		$news_date=preg_replace('/(\d{2})\.(\d{2})\.(\d{4})/', '$3-$2-$1', trim($_POST['news_date']));
		
	if (LabCMS::i()->no_errors)
	{
		if ($mode=="add")
		{
			$news_id=Db::i()->query("INSERT INTO ?_news (news_date, news_title, news_text, lang_id) VALUES(?, ?, ?, ?)", $news_date, $_POST['news_title'], $_POST['news_text'], LabCMS::i()->lang['id']);
		}
		if ($mode=="edit")
		{
			Db::i()->query("UPDATE ?_news SET news_title=?, news_text=?, news_date=? WHERE news_id=?", $_POST['news_title'], $_POST['news_text'], $news_date, $_GET['var1']);
			$news_id=$_GET['var1'];
		}
		LabCMS::i()->reset_cache('html');
		refresh('', LabCMS::i()->url($news_id,'item'));
	}
}

if ($mode=="delete")
{
	User::checkToken();
	LabComments::deleteItem($_GET['var1']);
	Db::i()->query("DELETE FROM ?_news WHERE news_id=?", $_GET['var1']);
	LabCMS::i()->reset_cache('html');
	refresh('', LabCMS::i()->url($_GET['var2']));
}


$mod_template='edit_news.tpl';

$langs=Db::i()->selectCol("SELECT lang_id AS ARRAY_KEY, lang_name FROM ?_langs");
Tpl::i()->assign("langs", $langs);

if ($mode=="edit")
{
	if ($_SERVER['REQUEST_METHOD']=='GET') $item=Db::i()->selectRow("SELECT news_title, news_text, DATE_FORMAT(news_date, '%d.%m.%Y') as news_date FROM ?_news WHERE news_id=?", $_GET['var1']);
	else $item=$_POST;
	Tpl::i()->assign($item);
	
}

if($mode=="add")
{
	if (!$_POST['news_date']) Tpl::i()->assign("news_date", date("d.m.Y"));
}
