<?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_tree_main extends ModuleTools
{
	public $ajax_methods=array('sitemap', 'menu', 'show', 'update_tree');
	
	public function __construct()
	{
		if (!access('admin')) {
			if (LabCMS::i()->isAjax()) $this->_json_error(_m('Нет доступа'));
			else return LabCMS::i()->error403();
		}
		$this->module=Factory::create('admin_tree.module');
	}
	
	public function tree()
	{
		self::_assign('tree_items', $this->module->getTree());
		self::_assign('duples', $this->module->getDuples());
		self::_setTpl('tree');
	}
	
	public function add()
	{
		$node=$this->_getNode();
		if ($this->module->validatePost()) {
			$node->set($this->module->getPostData());
			$this->module->add($node);
			refresh();
		}
		
		$this->_prepareEditData($node);
		self::_setTpl('edit');
	}

	public function edit()
	{
		if (!$node=$this->_getNode($_GET['var1'])) return;
		if ($this->module->validatePost()) {
			$data=$this->module->getPostData();
			$node->set($data);
			$this->module->updateNode($node);
			refresh();
		}
		$this->_prepareEditData($node);
		self::_assign('node', $node);
		self::_setTpl('edit');
	}
	
	public function delete()
	{
		User::checkToken();
		if (!$node=$this->_getNode($_GET['var1'])) return;
		$node->delete();
		refresh();
	}
	
	public function ajax()
	{
		if (!in_array($_POST['action'], $this->ajax_methods))
			$this->_json_error(_m('Метод не найден'));
		$method='_json_'.$_POST['action'];
		$result=$this->$method();
		echo real_json_encode($result);
		exit;
	}
	
	protected function _json_sitemap()
	{
		$node=$this->_getNode($_POST['id']);
		$node->sitemap=($node->sitemap)?0:1;
		$this->module->updateNode($node);
		return array('active'=>$node->sitemap);
	}
	
	protected function _json_menu()
	{
		$node=$this->_getNode($_POST['id']);
		$node->menu=($node->menu)?0:1;
		$this->module->updateNode($node);
		return array('active'=>$node->menu);
	}
	
	protected function _json_show()
	{
		$node=$this->_getNode($_POST['id']);
		$node->show=($node->show)?0:1;
		$this->module->updateNode($node);
		return array('active'=>$node->show);
	}
	
	protected function _json_update_tree()
	{
		$this->module->sortMainTree(json_decode($_POST['tree']));
		return array('ок'=>1);
	}
	
	protected function _getNode($id=null)
	{
		if ($id) {
			$node=$this->module->getNodeById($id);
			if (!$node) {
				if (LabCMS::i()->isAjax())_json_error(_m('Узел не найден'));
				else return $this->_error(_m('Узел не найден'));
			}
		} else {
			$node=Factory::create('admin_tree.node_entity');
		}
		return $node;
	}
	
	protected function _prepareEditData($node)
	{
		self::_assign('templates', $this->_getTeplates());
		self::_assign('symlink_select', $this->module->getSelectTree());
		self::_assign('parent_select', $this->module->getSelectTree($node->id));
		self::_assign('modules_list', $this->module->getModules($node));
		self::_assign('langs', $this->_getLangs());
	}
	
	protected function _getLangs()
	{
		return Db::i()->selectCol("SELECT lang_id AS ARRAY_KEY, lang_name FROM ?_langs");
	}
	
	protected function _getTeplates()
	{
		$templates=array();
		foreach (LabCMS::i()->getSkinsTree(config::get('core.skin')) as $skin) {
			foreach (glob($skin.'/*.tpl') as $tpl_name) {
				$templates[basename($tpl_name)]=basename($tpl_name, ".tpl");
			}
		}
		return $templates;
	}
	
	protected function _json_error($msg)
	{
		echo real_json_encode(array('error'=>$msg));
		exit;
	}
}