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

if (!access('admin')) return LabCMS::i()->error403();

$json_response=array();

switch(true)
{
	case(LabCMS::i()->isAjax() && $_POST['action']=='show'):
		toggle_item($_POST['id'], 'show');
	break;
	case(LabCMS::i()->isAjax() && $_POST['action']=='sitemap'):
		toggle_item($_POST['id'], 'sitemap');
	break;
	case(LabCMS::i()->isAjax() && $_POST['action']=='menu'):
		toggle_item($_POST['id'], 'menu');
	break;
	case($_GET['action']=='delete' && access('admin')):
		include dirname(__FILE__)."/delete.php";
	break;
	
	case(isset($_GET['var1'])):
		include dirname(__FILE__)."/edit.php";
	break;
	
	default:
		include dirname(__FILE__)."/tree.php";
	break;
}


function json_error($msg)
{
	echo real_json_encode(array('error'=>$msg));
	exit;
}

function toggle_item($id, $item)
{
	User::checkToken();
	if (!$id || !$item) return false;
	Db::i()->query("UPDATE ?_tree SET ?#=1-?# WHERE tree_id=?", $item, $item, $id);
	$active=Db::i()->selectCell("SELECT ?# FROM ?_tree WHERE tree_id=?", $item, $id);
	if($active===null) json_error('Элемент не найден');
	echo real_json_encode(array('active'=>intval($active)));
	exit;
}