<?php
require_once('api/Simpla.php');
$simpla = new Simpla();

header("Content-type: text/xml; charset=UTF-8");
print '<?xml version="1.0" encoding="UTF-8"?>'."\n";
print '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";

// Главная страница
$url = $simpla->config->root_url;
$lastmod = date("Y-m-d");
/*
print "\t<url>"."\n";
print "\t\t<loc>$url</loc>"."\n";
print "\t\t<lastmod>$lastmod</lastmod>"."\n";
print "\t</url>"."\n";
*/

_addUrl('/tuning-centr/');
_addUrl('/catalog/');
_addUrl('/brands/');
_addUrl('/actions/');
_addUrl('/sitemap/');

//$x = $simpla->articles->get_articles_categories(); echo '<pre>'; print_r($x);die;

$services_root = '/' . $simpla->services->get_root_url();

// Страницы
foreach($simpla->pages->get_pages() as $p)
{
	if(!$p->visible || $p->id == 5 || $p->id == 7) continue;
	//if($p->visible && ($p->menu_id == 1 || $p->menu_id == 3)){
		if($url == $simpla->config->root_url) $url = $simpla->config->root_url . '/';
		else $url = $simpla->config->root_url . esc($p->parentUrl . $p->url). '/';
		
		//if($p->url == 'tuning-centr') continue;
		//if(strpos($url, '/tuning-centr/')) $url = $simpla->config->root_url.'/tuning-centr/'.esc($p->url). '/';
		
		if($p->menu_id == 3) {
            $url = $simpla->config->root_url . $services_root . esc($p->url). '/';
        }
		
		
		print "\t<url>"."\n";
		print "\t\t<loc>$url</loc>"."\n";
		print "\t\t<priority>0.9</priority>"."\n";
		print "\t</url>"."\n";
		
	//}
}

// Блог
foreach($simpla->blog->get_posts(array('visible'=>1)) as $p)
{
	$url = $simpla->config->root_url.'/tehinfo/'.esc($p->url). '/';
	print "\t<url>"."\n";
	print "\t\t<loc>$url</loc>"."\n";
	print "\t\t<priority>0.9</priority>"."\n";
	print "\t</url>"."\n";
}

//actions
$query = $simpla->db->placehold("SELECT * FROM __actions");
$res = $simpla->db->query($query);
$ids = $simpla->db->results();

foreach($ids as $p){
	 if(!$p->visible) continue;
	print "\t<url>"."\n";
	print "\t\t<loc>".$simpla->config->root_url .'/actions/'. $p->url."/</loc>"."\n";
	print "\t\t<priority>0.9</priority>"."\n";
	print "\t</url>"."\n";
}

//Статьи Категории
$query = $simpla->db->placehold("SELECT * FROM __article_categories WHERE visible=1");
$res = $simpla->db->query($query);
$ids = $simpla->db->results();

foreach($ids as $p){
	 if(!$p->visible || !$p->parent_id) continue;
	print "\t<url>"."\n";
	print "\t\t<loc>".$simpla->config->root_url .'/nashi-raboty/'. $p->url."/</loc>"."\n";
	print "\t\t<priority>0.9</priority>"."\n";
	print "\t</url>"."\n";
}

//Статьи
foreach($simpla->articles->get_articles(array('visible'=>1)) as $p)
{
	$url = $simpla->config->root_url.'/nashi-raboty/'.esc($p->url). '/';
	print "\t<url>"."\n";
	print "\t\t<loc>$url</loc>"."\n";
	print "\t\t<priority>0.9</priority>"."\n";
	print "\t</url>"."\n";
}

// Категории
foreach($simpla->categories->get_categories() as $c)
{
	if($c->visible)
	{
		$url = $simpla->config->root_url.'/catalog/'.esc($c->url). '/';
		print "\t<url>"."\n";
		print "\t\t<loc>$url</loc>"."\n";
		print "\t\t<priority>0.9</priority>"."\n";
		print "\t</url>"."\n";
	}
}

// Бренды
foreach($simpla->brands->get_brands() as $b)
{
	if(!$b->visible) continue;
	$url = $simpla->config->root_url.'/brands/'.esc($b->url). '/';
	print "\t<url>"."\n";
	print "\t\t<loc>$url</loc>"."\n";
	print "\t\t<priority>0.9</priority>"."\n";
	print "\t</url>"."\n";
}

// Товары
$simpla->db->query("SELECT url FROM __products WHERE visible=1");
foreach($simpla->db->results() as $p)
{
	$url = $simpla->config->root_url.'/products/'.esc($p->url) . '/';
	print "\t<url>"."\n";
	print "\t\t<loc>$url</loc>"."\n";
	print "\t\t<priority>0.9</priority>"."\n";
	print "\t</url>"."\n";
}

print '</urlset>'."\n";

function esc($s)
{
	return(htmlspecialchars($s, ENT_QUOTES, 'UTF-8'));
}

function _addUrl($url){
	global $simpla;
	$url = $simpla->config->root_url . $url;
	print "\t<url>"."\n";
	print "\t\t<loc>$url</loc>"."\n";
	print "\t\t<priority>0.9</priority>"."\n";
	print "\t</url>"."\n";
}