<?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 blocks_static_edit_main extends ModuleTools
{
	public function __construct()
	{
		if (!access('admin')) return LabCMS::i()->error403();
	}
	
	public function all()
	{
		self::_setTpl('list');
		$result=Db::i()->select("SELECT * FROM ?_blocks_static");
		$blocks=array();
		foreach ($result as $val) {
			$obj=Factory::create('blocks_static_edit.block');
			$obj->set($val);
			$blocks[$obj->id]=$obj;
		}
		self::_assign('blocks', $blocks);
	}
	
	public function add()
	{
		addBreadcrumb('Добавление блока');
		$block=Factory::create('blocks_static_edit.block');
		$form=$block->buildEditForm();
		
		if ($form->validate()) {
			$block->add($form->getValue());
			refresh();
		}
		
		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}
	
	public function edit()
	{
		addBreadcrumb('Редактирование блока');
		$block=Factory::create('blocks_static_edit.block', $_GET['var1']);
		if (!$block->id) return self::_error(_m('Блок не найден'));
		$form=$block->buildEditForm();
		
		if ($form->validate()) {
			$block->update($form->getValue());
			refresh();
		}
		
		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}
	
	public function delete()
	{
		$block=Factory::create('blocks_static_edit.block', $_GET['var1']);
		if (!$block->id) return self::_error(_m('Блок не найден'));
		$block->delete();
		refresh();
	}
	
	public function toggle()
	{
		$block=Factory::create('blocks_static_edit.block', $_GET['var1']);
		if (!$block->id) return self::_error(_m('Блок не найден'));
		$block->toggle();
		refresh();
	}
}