<?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 adv_rotator_editor_main extends ModuleTools
{
	public function list_all()
	{
		if ($_SERVER['REQUEST_METHOD']=='POST') {
			if ($_POST['rate']) foreach ($_POST['rate'] as $id=>$val) {
				$value=Factory::create("adv_rotator_editor.value", $id);
				if ($value->id) $value->update(array('rate'=>intval($val)));
			}
			refresh();
		}
		$places_obj=Factory::create('adv_rotator_editor.place');
		if ($places=$places_obj->getAll()) {
			foreach ($places as $place) {
				$values[$place->id]=$place->getChildren();
			}
		}
		
		self::_assign('places', $places);
		self::_assign('values', $values);
		self::_setTpl('places_list');
	}

	public function add_place()
	{
		addBreadcrumb('Добавление места');
		$place=Factory::create("adv_rotator_editor.place");
		$form=$place->buildEditForm();
		if ($form->validate()) {
			$place->add($form->getValue());
			TCache::remove('adv_blocks');
			refresh();
		}
		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}
	
	public function edit_place()
	{
		addBreadcrumb('Изменение места');
		$place=Factory::create("adv_rotator_editor.place", $_GET['var1']);
		if (!$place->id) return self::_error(_m('Рекламное место не найдено'));
		$form=$place->buildEditForm();
		if ($form->validate()) {
			$place->update($form->getValue());
			TCache::remove('adv_blocks');
			refresh();
		}
		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}
	
	public function delete_place()
	{
		User::checkToken();
		$place=Factory::create("adv_rotator_editor.place", $_GET['var1']);
		if (!$place->id) return self::_error(_m('Рекламное место не найдено'));
		$place->delete();
		TCache::remove('adv_blocks');
		refresh();

	}
	
	public function toggle_place()
	{
		User::checkToken();
		$place=Factory::create("adv_rotator_editor.place", $_GET['var1']);
		if (!$place->id) return self::_error(_m('Рекламное место не найдено'));
		$place->toggle();
		TCache::remove('adv_blocks');
		refresh();
	}

	public function add_value()
	{
		addBreadcrumb('Добавление блока');
		$place=Factory::create("adv_rotator_editor.place", $_GET['var1']);
		if (!$place->id) return self::_error(_m('Рекламное место не найдено'));
		$value=Factory::create("adv_rotator_editor.value");
		$form=$value->buildEditForm();
		if ($form->validate()) {
			$value->add($place, $form->getValue());
			TCache::remove('adv_blocks');
			refresh();
		}
		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}
	
	public function edit_value()
	{
		addBreadcrumb('Изменение блока');
		$value=Factory::create("adv_rotator_editor.value", $_GET['var1']);
		if (!$value->id) return self::_error(_m('Значение не найдено'));
		$form=$value->buildEditForm();
		if ($form->validate()) {
			$value->update($form->getValue());
			TCache::remove('adv_blocks');
			refresh();
		}
		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}
	
	public function delete_value()
	{
		User::checkToken();
		$value=Factory::create("adv_rotator_editor.value", $_GET['var1']);
		if (!$value->id) return self::_error(_m('Значение не найдено'));
		$value->delete();
		TCache::remove('adv_blocks');
		refresh();
	}
	
	public function toggle_value()
	{
		User::checkToken();
		$value=Factory::create("adv_rotator_editor.value", $_GET['var1']);
		if (!$value->id) return self::_error(_m('Значение не найдено'));
		$value->toggle();
		TCache::remove('adv_blocks');
		refresh();
	}
}