<?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
*/

	$mod_template="edit.tpl";

	if ($_SERVER['REQUEST_METHOD']=='POST')
	{
		User::checkToken();
		if (!trim($_POST['pattern'])) LabCMS::i()->error("Паттерн не может быть пустым");
		if (!trim($_POST['link'])) LabCMS::i()->error("ссылка не может быть пустой");
		
		if(LabCMS::i()->no_errors)
		{
			if ($mode=="add")
			{
				$pos=Db::i()->selectCell("SELECT MAX(pos) FROM ?_redirects");
				Db::i()->query("INSERT INTO ?_redirects (pattern, link, pos) VALUES(?, ? , ?)", $_POST['pattern'], $_POST['link'], (intval($pos)+1));
			}
			
			if($mode=="edit")
			{
				Db::i()->query("UPDATE ?_redirects SET pattern=?, link=? WHERE id=?", $_POST['pattern'], $_POST['link'], $_GET['var1']);
			}
			refresh();
		}
	}

	if ($mode=="delete")
	{
		User::checkToken();
		Db::i()->query("DELETE FROM ?_redirects WHERE id=?", $_GET['var1']);
		refresh();
	}
	
	if($mode=="edit")
	{
		Tpl::i()->assign(Db::i()->selectRow("SELECT pattern, link, id FROM ?_redirects WHERE id=?", $_GET['var1']));
	}
	
	if($mode=="up")
	{
		User::checkToken();
		$pos=Db::i()->selectCol("SELECT pos FROM ?_redirects WHERE pos<=(SELECT pos FROM ?_redirects WHERE id=?) ORDER BY pos DESC LIMIT 2", $_GET['var1']);
		if (count($pos)==2) Db::i()->query("UPDATE ?_redirects SET pos=ABS(pos-?d-?d) WHERE pos IN (?a)", $pos[0], $pos[1], $pos);
		refresh();
	}
	
	if($mode=="down")
	{
		User::checkToken();
		$pos=Db::i()->selectCol("SELECT pos FROM ?_redirects WHERE pos>=(SELECT pos FROM ?_redirects WHERE id=?) ORDER BY pos LIMIT 2", $_GET['var1']);
		if (count($pos)==2) Db::i()->query("UPDATE ?_redirects SET pos=ABS(pos-?d-?d) WHERE pos IN (?a)", $pos[0], $pos[1], $pos);
		refresh();
	}

	if($mode=="toggle")
	{
		User::checkToken();
		Db::i()->query("UPDATE ?_redirects SET active=1-active WHERE id=?", $_GET['var1']);
		refresh();
	}
	
	
