<?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
*/
switch (true)
{
	case($_GET['data_type']=='xml'):
		include dirname(__FILE__)."/sitemap.xml.php";
	break;

	case($_GET['action']=='all'):
		include dirname(__FILE__)."/sitemap_all.php";
	break;
	
	default:
		include dirname(__FILE__)."/sitemap.php";
}

function getFlatSitemap()
{
	$sitemap=googleSiteMap();
	
	if (is_array(LabConfig::$mcfg['sitemap_modules']) && count(LabConfig::$mcfg['sitemap_modules']))
	{
		$sitemap_modules=Db::i()->select("SELECT module, name, module_id FROM ?_modules WHERE module IN (?a) AND disable=0", LabConfig::$mcfg['sitemap_modules']);
	
	
		foreach($sitemap_modules as $sitemap_module)
		{
			include LAB_PATH_MODULES.'/'.$sitemap_module['module']."/mod_sitemap.inc";
			$function_name=$sitemap_module['module']."_sitemap";
			$sitemap=array_merge($sitemap, $function_name($sitemap_module));
		}
	}
	return $sitemap;
}

function getTreeSitemap($id=0)
{
	$result=Db::i()->select("SELECT t.tree_id, parent_id, tree_name, path FROM ?_tree t WHERE parent_id=? AND sitemap=1 ORDER BY pos", $id);
	if (count($result))
	{
		foreach($result as $key=>$element)
		{
				$tree[$key] = array(
				'path' => $element['path'],
				'name' => $element['tree_name'],
				'subtree' => getTreeSitemap($element['tree_id'])
				);
				if (!is_array($tree[$key]['subtree'])) unset($tree[$key]['subtree']);
		}
		return $tree;
	}
	else
	{
		return false;
	}
}

function googleSiteMap($id=0, $sitemap=array())
{
	global $sitemap;
	$result=Db::i()->select("SELECT t.tree_id, parent_id, tree_name, path FROM ?_tree t WHERE parent_id=? AND sitemap=1 ORDER BY pos", $id);
	if (count($result)) $tree.="\r\n<ul".((!$id)?" id='adoptme'":"").">\r\n";
	foreach($result as $var)
	{
		$sitemap[LabCMS::i()->url(false, false, false, $var['path'])]=array();
		$sitemap=googleSiteMap($var['tree_id'], $sitemap);
	}
	return $sitemap;
}