<?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 menu_tree_main extends ModuleTools
{
	protected $deep_level;
	public function __construct()
	{
		self::_setTpl('menu');
		if (LabCMS::i()->is_cached(self::_getTpl(), -1, LabCMS::i()->id)) return;
		
		$first_point_id=config::get('start_id'); //id корня всего дерева
		$this->deep_level=config::get('deep_level'); //глубина построения
		
		self::_assign('tree', $this->tree($first_point_id));
	}
	
	public function tree($parent_id, $level=0)
	{
		if (!$this->deep_level || $this->deep_level>$level) {
			$result=Db::i()->select(
				"SELECT t.tree_id, parent_id, name, path
				FROM ?_tree t
				WHERE menu=1 AND parent_id=?
				ORDER BY pos",
				$parent_id
			);
			if (count($result)) {
				foreach ($result as $key=>$tmp) {
					$tree[$key]=array(
					'tree_id'=>$tmp['tree_id'],
					'name'=>$tmp['name'],
					'path'=>$tmp['path'],
					'subtree'=>$this->tree($tmp['tree_id'], $level+1));
					if (in_array($tmp['tree_id'], LabCMS::i()->parents)) $tree[$key]['active']=1;
					if ($tmp['tree_id']==LabCMS::i()->id) $tree[$key]['current']=1;
					if (!is_array($tree[$key]['subtree'])) unset($tree[$key]['subtree']);
				}
				return $tree;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}