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

class admin_redirect_main extends ModuleTools
{
	public function __construct()
	{
		if (!access('admin')) return LabCMS::i()->error403();
	}
	
	public function main()
	{
		self::_setTpl('list');
		$redirect=$this->_getRedirect();
		self::_assign('redirects', $redirect->getList());
	}

	public function add()
	{
		$redirect=$this->_getRedirect();

		$form=$redirect->getEditForm();

		if ($form->validate()) {
			$redirect=$this->_getRedirect();
			$redirect->add($form->getValue());
			refresh();
		}

		self::_setTpl('edit');
		
		addBreadcrumb("Добавление редиректа");
		
		self::_assign('form', $this->_renderForm($form));
	}
	
	public function edit()
	{
		$redirect=$this->_getRedirect($_GET['var1']);

		$form=$redirect->getEditForm();
		
		if ($form->validate()) {
			$redirect->update($form->getValue());
			refresh();
		}
		
		self::_setTpl('edit');
		
		addBreadcrumb("Изменение редиректа");
		
		self::_assign('form', $this->_renderForm($form));
	}
	
	public function delete()
	{
		User::checkToken();
		$redirect=$this->_getRedirect($_GET['var1']);
		$redirect->delete();
		refresh();
	}
	
	public function toggle()
	{
		User::checkToken();
		$redirect=$this->_getRedirect($_GET['var1']);
		$redirect->toggle();
		refresh();
	}
	
	public function up()
	{
		User::checkToken();
		$redirect=$this->_getRedirect($_GET['var1']);
		$redirect->moveUp();
		refresh();
	}
	
	public function down()
	{
		User::checkToken();
		$redirect=$this->_getRedirect($_GET['var1']);
		$redirect->moveDown();
		refresh();
	}
	
	protected function _getRedirect($id=null)
	{
		$redirect=Factory::create('admin_redirect.redirect', $id);
		if ($id && !$redirect->id) return $this->_error(_m('Запись не найдена'));
		return $redirect;
	}
}