<?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
*/
	$mod_template="tree.tpl";

	if($_SERVER['REQUEST_METHOD']=="POST")
	{
		User::checkToken();
		
		//изменяем порядок сортировки
		if (count($_POST['pos']))
		foreach ($_POST['pos'] as $key=>$val)
		{
			Db::i()->query("UPDATE ?_tree SET pos=? WHERE tree_id=?", $val, $key);
		}

		if ($_POST['only_pos'])
		{
			LabCMS::i()->reset_cache('all');
			refresh();
		}
		
		//изменяем видимость в меню
		if (count($_POST['menu']))
		{
			Db::i()->query("UPDATE ?_tree SET menu=1 WHERE tree_id IN (?a)", array_keys($_POST['menu']));
			Db::i()->query("UPDATE ?_tree SET menu=0 WHERE tree_id NOT IN (?a)", array_keys($_POST['menu']));
		}
		else
		{
			Db::i()->query("UPDATE ?_tree SET menu=0");
		}

		//изменяем видимость в карте сайта
		if (count($_POST['sitemap']))
		{
			Db::i()->query("UPDATE ?_tree SET sitemap=1 WHERE tree_id IN (?a)", array_keys($_POST['sitemap']));
			Db::i()->query("UPDATE ?_tree SET sitemap=0 WHERE tree_id NOT IN (?a)", array_keys($_POST['sitemap']));
		}
		else
		{
			Db::i()->query("UPDATE ?_tree SET sitemap=0");
		}

		//изменяем видимость на сайте
		if (count($_POST['show']))
		{
			Db::i()->query("UPDATE ?_tree SET `show`=1 WHERE tree_id IN (?a)", array_keys($_POST['show']));
			Db::i()->query("UPDATE ?_tree SET `show`=0 WHERE tree_id NOT IN (?a)", array_keys($_POST['show']));
		}
		else
		{
			Db::i()->query("UPDATE ?_tree SET `show`=0");
		}

		LabCMS::i()->reset_cache('all');
		refresh();
	}

	function recurse_tree($parent_id=0, $level=0, $tree_arr=array())
	{
		$result=Db::i()->select("SELECT t.tree_id,  tree_name,  menu, pos,  `show`, sitemap, symlink, path, external FROM ?_tree t WHERE t.parent_id=? ORDER BY pos", $parent_id);
		if (count($result))
		{
			foreach($result as $tree)
			{
				$menu = ($tree['menu'] ? 1 : 0);
				$show = ($tree['show'] ? 1 : 0);
				$sitemap = ($tree['sitemap'] ? 1 : 0);
				$link=$tree['symlink']?$tree['symlink']:0;
				
				$tree_arr[]=array("name"=>$tree['tree_name'], "level"=>$level, "id"=>$tree['tree_id'], "pos"=>$tree['pos'], 'menu'=>$menu, 'show'=>$show, 'sitemap'=>$sitemap, 'link'=>$link, 'path'=>$tree['path'], 'parent_id'=>$parent_id, 'external'=>$tree['external']);
				$tree_arr+=recurse_tree($tree['tree_id'], $level+1, $tree_arr);
			}
			return $tree_arr;
		}
		else
		{
			return array();
		}
	}
	
	//if (LabCMS::i()->is_cached($mod_template, 60*10, 0)) return;

	$result=Db::i()->select("SELECT t.path, t1.tree_id, t1.tree_name  FROM ?_tree t LEFT JOIN ?_tree t1 ON t.path=t1.path AND t.tree_id!=t1.tree_id AND t1.symlink=0 AND t.symlink=0 WHERE t1.tree_id IS NOT NULL GROUP BY t1.tree_id ORDER BY t.path, t1.tree_id");
	$duples=array();
	if (count($result))
	{
		foreach($result as $tmp)
		$duples[$tmp['path']][]=array($tmp['tree_id'], $tmp['tree_name']);
	}
	Tpl::i()->assign('duples', $duples);

	if (!count($duples))
	{
		$result=Db::i()->select("SELECT path, tree_id, tree_name, symlink  FROM ?_tree ORDER BY symlink");
		$nodes=$links=array();
		if (count($result))
		{
			foreach($result as $tmp)
			{
				if (!$tmp['symlink'])
				{
					$nodes[$tmp['path']]=$tmp;
				}
				else
				{
					$links[$tmp['tree_id']]=$tmp;
				}
			}
		}

		if (count($links))
		{
			foreach($links as $tree_id=>$data)
			{
				if (!array_key_exists($data['path'], $nodes)) {unset($links[$tree_id]); continue;}
				if ($nodes[$data['path']]['tree_id']==$data['symlink']) {unset($links[$tree_id]); continue;}
				$links[$tree_id]['node']=array('tree_id'=>$nodes[$data['path']]['tree_id'], 'tree_name'=>$nodes[$data['path']]['tree_name']);
			}
		}
	}

	Tpl::i()->assign('wrong_links', $links);
	
	$tree_arr=recurse_tree();

	Tpl::i()->assign('tree_items', $tree_arr);
