<?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 sitemap_main extends ModuleTools
{
	public function sitemap()
	{
		if (isset($_GET['data_type']) && $_GET['data_type']=='xml') {
			return $this->_sitemap_xml();
		}
		self::_setTpl('sitemap');

		if (LabCMS::i()->is_cached(self::_getTpl(), 1296000, 0)) return; //кэшируем на две недели
		
		$obj=Factory::create('sitemap.sitemap');
		$sitemap = $obj->getTree(LabCMS::i()->parents[0]);
		
		self::_assign('tree', $sitemap);
		self::_assign('sitemap', true);
	}
	
	public function _sitemap_xml()
	{
		self::_setTpl('xml_sitemap');
		header("Content-type: text/xml");
		LabVars::$forceDisplay=true;
		LabVars::$break=true;
		if (LabCMS::i()->is_cached(self::_getTpl(), 60*60*24, 0)) return;
		
		$obj=Factory::create('sitemap.sitemap');
		$sitemap=$obj->getForSE();
		self::_assign("map", $sitemap);
	}
	
	public function all()
	{
		self::_setTpl('sitemap_all');
		if (LabCMS::i()->is_cached(self::_getTpl(), 1296000, $_GET['var1'])) return; //кэшируем на две недели
		
		if (!config::get('sitemap_at_page')) config::set('sitemap_at_page', 130);
		
		$obj=Factory::create('sitemap.sitemap');
		
		$sitemap=$obj->getForSE();
		
		$page=intval($_GET['var1']);
		
		if ($page) {
			$chunks=array_chunk($sitemap, config::get('sitemap_at_page'), true);
			if (array_key_exists(($page-1), $chunks)) $sitemap=$chunks[($page-1)];
		}
		
		self::_assign('map', $sitemap);
	}
}