<?php
//error_reporting(0);
//Trim the MOTHEFUCKING OPIS
function strTrimTotal($input) {
    $input = trim($input);
    for($i=0;$i<strlen($input);$i++) {
        if(substr($input, $i, 1) != " ") {
            $output .= trim(substr($input, $i, 1));
        } else {
            $output .= " ";
        }
    }
    return $output;
}

function size_readable($size, $unit = null, $retstring = null, $si = true)
{
    // Units
    if ($si === true) {
        $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
        $mod   = 1000;
    } else {
        $sizes = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
        $mod   = 1024;
    }
    $ii = count($sizes) - 1;

    // Max unit
    $unit = array_search((string) $unit, $sizes);
    if ($unit === null || $unit === false) {
        $unit = $ii;
    }

    // Return string
    if ($retstring === null) {
        $retstring = '%01.2f %s';
    }

    // Loop
    $i = 0;
    while ($unit != $i && $size >= 1024 && $i < $ii) {
        $size /= $mod;
        $i++;
    }

    return sprintf($retstring, $size, $sizes[$i]);
}

/*require('lib_security.php');

require('lib_mysql.php');

require('lib_format.php');

$db = new Mysql("localhost", "root", "ovcapass", "demsar");*/

//funkcija generira xml za prikaz
function GenerateStili($db)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = false;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/teksti.xml");

	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;

	$main_element = $vsebina->createElement("meni");
	
		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis","Izberite svoj stil"));
			$kategorije->appendChild($vsebina->createElement("link",""));
			$kategorije->appendChild($vsebina->createElement("bold","1"));

		$main_element->appendChild($kategorije);

	$result_k = $db->query("SELECT * FROM modul_sifranti_stili ORDER BY teza DESC;");
	for($x = 1;$r_k = $result_k->fetch(MYSQL_ASSOC);$x++)
	{

		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis",$r_k['name']));
			$kategorije->appendChild($vsebina->createElement("link","/stili/" . convertToLink($r_k['name'])));
			$kategorije->appendChild($vsebina->createElement("bold","0"));

		$main_element->appendChild($kategorije);
	}

	$vsebina->appendChild($main_element);
	
		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis","novosti"));
			$kategorije->appendChild($vsebina->createElement("link","/novosti"));
			$kategorije->appendChild($vsebina->createElement("bold","1"));

		$main_element->appendChild($kategorije);
		
		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis","outlet"));
			$kategorije->appendChild($vsebina->createElement("link","/novosti"));
			$kategorije->appendChild($vsebina->createElement("bold","1"));

		$main_element->appendChild($kategorije);

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	$nodelist = $xpath->query("/meni");
	$oldnode = $nodelist->item(0);

	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/XML/teksti.xml");


}

//funkcija generira xml za prikaz
function GenerateStiliEng($db)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = false;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/teksti.xml");

	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;

	$main_element = $vsebina->createElement("meni");

		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis","Chose your own style"));
			$kategorije->appendChild($vsebina->createElement("link",""));
			$kategorije->appendChild($vsebina->createElement("bold","1"));

		$main_element->appendChild($kategorije);

	$result_k = $db->query("SELECT * FROM modul_sifranti_stili ORDER BY teza DESC;");
	for($x = 1;$r_k = $result_k->fetch(MYSQL_ASSOC);$x++)
	{

		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis",$r_k['name_eng']));
			$kategorije->appendChild($vsebina->createElement("link","/styles/" . convertToLink($r_k['name_eng'])));
			$kategorije->appendChild($vsebina->createElement("bold","0"));

		$main_element->appendChild($kategorije);
	}

	$vsebina->appendChild($main_element);
	
		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis","novosti"));
			$kategorije->appendChild($vsebina->createElement("link","/eng/new-stuff"));
			$kategorije->appendChild($vsebina->createElement("bold","1"));

		$main_element->appendChild($kategorije);
		
		$kategorije = $vsebina->createElement("item");

			$kategorije->appendChild($vsebina->createElement("napis","outlet"));
			$kategorije->appendChild($vsebina->createElement("link","/eng/outlet"));
			$kategorije->appendChild($vsebina->createElement("bold","1"));

		$main_element->appendChild($kategorije);

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	$nodelist = $xpath->query("/meni");
	$oldnode = $nodelist->item(0);

	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/XML/teksti_eng.xml");


}


function GenerateNaslovka($db)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = false;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/naslovka.xml");

	$zebre = array("Casual","Chick","Chillout","Clasic","Eco","Sport");
	$bad = array(".",",");
	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;

	$main_element = $vsebina->createElement("izdelki");

	$result_k = $db->query("SELECT * FROM modul_naslovnica m, modul_naslovnica_slo s WHERE s.item_id=m.id ORDER BY vrstni_red ASC;");
	for($x = 1;$r_k = $result_k->fetch(MYSQL_ASSOC);$x++)
	{

		$kategorije = $vsebina->createElement("izdelek");
		
	
		if($r_k['fslika_slo']){
			$kategorije->appendChild($vsebina->createElement("fs","1"));
			$kategorije->appendChild($vsebina->createElement("slika","/files/images/naslovnica/fslika_slo/" . $r_k['fslika_slo']));
			$kategorije->appendChild($vsebina->createElement("opis","FULLSCREN"));
			
			$kategorije->appendChild($vsebina->createElement("cena","1"));
			$kategorije->appendChild($vsebina->createElement("link",$r_k['link']));
			
			$kategorije->appendChild($vsebina->createElement("prip","1"));
			$kategorije->appendChild($vsebina->createElement("zebra","/img/zebre/casual.png"));
		}else{
			
			if(stripos($r_k['cena'],".")){
				$r_k['cena']=str_replace(".","",$r_k['cena']);
				$c_arr=str_split($r_k['cena']);
				
				$gen_cena=$r_k['cena'];
				
				
				if(count($c_arr)<5){
					for($x=5;$x>count($c_arr);$x--)
						$gen_cena="0".$gen_cena;
				}
				
				//vejica
				$kategorije->appendChild($vsebina->createElement("cena",$gen_cena));
				$kategorije->appendChild($vsebina->createElement("vejica","1"));
			}else{
			
				$r_k['cena']=str_replace(".","",$r_k['cena']);
				$c_arr=str_split($r_k['cena']);
				
				$gen_cena=$r_k['cena'];
				
				
				if(count($c_arr)<5){
					for($x=5;$x>count($c_arr);$x--)
						$gen_cena="0".$gen_cena;
				}
			
				//ni vejice
				$kategorije->appendChild($vsebina->createElement("cena",$gen_cena));
				$kategorije->appendChild($vsebina->createElement("vejica","0"));
			}
			$kategorije->appendChild($vsebina->createElement("fs","0"));
			$kategorije->appendChild($vsebina->createElement("opis",$r_k['naslov']));
			
			$kategorije->appendChild($vsebina->createElement("link",$r_k['link']));
			
			$kategorije->appendChild($vsebina->createElement("prip",$r_k['oblak']));
			$zebra = strtolower($zebre[$r_k['zebra']]) . ".png";
			$kategorije->appendChild($vsebina->createElement("zebra","/img/zebre/" . $zebra));
			$kategorije->appendChild($vsebina->createElement("slika","/files/images/naslovnica/slika/" . $r_k['slika']));
		}
		$main_element->appendChild($kategorije);
	}
	$vsebina->appendChild($main_element);

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	$nodelist = $xpath->query("/izdelki");
	$oldnode = $nodelist->item(0);

	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/XML/naslovka.xml");


}

function GeneratePodXML_eng($db, $cat)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = false;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/cat.xml");

	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;

	$main_element = $vsebina->createElement("slides");

	$result_k = $db->query("SELECT * FROM modul_flash WHERE kategorija='".$cat."' AND naslov_eng<>'' AND naslov2_eng<>'' ORDER BY id DESC;");
	for($x = 1;$r_k = $result_k->fetch(MYSQL_ASSOC);$x++)
	{

		$kategorije = $vsebina->createElement("slide");
			$kategorije->setAttribute("id", $x);
			$kategorije->appendChild($vsebina->createElement("company",$r_k['naslov_eng']));
			$kategorije->appendChild($vsebina->createElement("product",$r_k['naslov2_eng']));
			$kategorije->appendChild($vsebina->createElement("price",Format_Cena($r_k['cena']) . " €"));
			$kategorije->appendChild($vsebina->createElement("tag",$r_k['pasica']));

			$kategorije->appendChild($vsebina->createElement("bonus",$r_k['bonus']));
			$kategorije->appendChild($vsebina->createElement("popust",$r_k['popust']));
			$kategorije->appendChild($vsebina->createElement("link",$r_k['link']));

			$kategorije->appendChild($vsebina->createElement("pic","/files/images/flash/v/" . $r_k['slika']));

		$main_element->appendChild($kategorije);
	}

	$vsebina->appendChild($main_element);

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	$nodelist = $xpath->query("/slides");
	$oldnode = $nodelist->item(0);

	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/XML/eng/cat_" . $cat . ".xml");


}

function GenerateNaslovnicaXML($db)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = true;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/naslovka_slo.xml");

	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;

	$main_element = $vsebina->createElement("slides");

	$sql = $db->query("SELECT * FROM modul_naslovnica WHERE osnutek='0' ORDER BY vrstni_red ASC;");
	for($x = 1;$row = $sql->fetch();$x++)
	{

		$kategorije = $vsebina->createElement("slide");
			$kategorije->setAttribute("id", $x);
			$kategorije->appendChild($vsebina->createElement("pic","/files/images/naslovnica/slika/".$row['slika']));
			$kategorije->appendChild($vsebina->createElement("bigpic","/files/images/naslovnica/slika2/".$row['slika2']));
			$kategorije->appendChild($vsebina->createElement("link",$row['link']));

			$naslovi = $vsebina->createElement("title");
				$naslovi->appendChild($vsebina->createElement("line1",$row['line1']));
				$naslovi->appendChild($vsebina->createElement("line2",$row['line2']));
				$naslovi->appendChild($vsebina->createElement("line3",$row['line3']));
				$naslovi->appendChild($vsebina->createElement("line4",$row['line4']));
			$kategorije->appendChild($naslovi);

			$kategorije->appendChild($vsebina->createElement("tag",$row['tag']));
			$kategorije->appendChild($vsebina->createElement("splat",$row['splat']));
			$kategorije->appendChild($vsebina->createElement("desc",$row['opis']));
			$kategorije->appendChild($vsebina->createElement("button",$row['button']));
			$kategorije->appendChild($vsebina->createElement("bonus",$row['bonus']));


		$main_element->appendChild($kategorije);
	}

	$vsebina->appendChild($main_element);

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	$nodelist = $xpath->query("/slides");
	$oldnode = $nodelist->item(0);

	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/XML/naslovka_slo.xml");


}

function GenerateNaslovnicaXML_eng($db)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = true;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/naslovka_eng.xml");

	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;

	$main_element = $vsebina->createElement("slides");

	$sql = $db->query("
		SELECT 
			n.slika, n.slika2, n.link, n.bonus, 
			e.line1 as line1_eng, e.line2 as line2_eng, e.line3 as line3_eng, e.line4 as line4_eng, e.tag as tag_eng, e.splat as splat_eng, e.opis as opis_eng, e.button as button_eng 
		FROM modul_naslovnica n LEFT JOIN modul_naslovnica_eng e ON e.item_id=n.id
		WHERE n.osnutek='0' 
		ORDER BY n.vrstni_red ASC;
	");
	for($x = 1;$row = $sql->fetch();$x++)
	{

		$kategorije = $vsebina->createElement("slide");
			$kategorije->setAttribute("id", $x);
			$kategorije->appendChild($vsebina->createElement("pic","/files/images/naslovnica/slika/".$row['slika']));
			$kategorije->appendChild($vsebina->createElement("bigpic","/files/images/naslovnica/slika2/".$row['slika2']));
			$kategorije->appendChild($vsebina->createElement("link",$row['link']));

			$naslovi = $vsebina->createElement("title");
				$naslovi->appendChild($vsebina->createElement("line1",$row['line1_eng']));
				$naslovi->appendChild($vsebina->createElement("line2",$row['line2_eng']));
				$naslovi->appendChild($vsebina->createElement("line3",$row['line3_eng']));
				$naslovi->appendChild($vsebina->createElement("line4",$row['line4_eng']));
			$kategorije->appendChild($naslovi);

			$kategorije->appendChild($vsebina->createElement("tag",$row['tag_eng']));
			$kategorije->appendChild($vsebina->createElement("splat",$row['splat_eng']));
			$kategorije->appendChild($vsebina->createElement("desc",$row['opis_eng']));
			$kategorije->appendChild($vsebina->createElement("button",$row['button_eng']));
			$kategorije->appendChild($vsebina->createElement("bonus",$row['bonus']));


		$main_element->appendChild($kategorije);
	}

	$vsebina->appendChild($main_element);

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	$nodelist = $xpath->query("/slides");
	$oldnode = $nodelist->item(0);

	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/XML/naslovka_eng.xml");


}





function GenerateSiteMapXML($db)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = true;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/sitemap.xml");

	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;
	$main_element = $vsebina->createElement("urlset");
  	$main_element->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
 	$main_element->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
 	$main_element->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');

   //bolj pomembni
    $static_sites1=array("http://www.outoflimitz.com/",
					   "http://www.outoflimitz.com/o-nas",
					   "http://www.outoflimitz.com/vroci_paketki",
					   "http://www.outoflimitz.com/vse_znamke",
					   "http://www.outoflimitz.com/novice",
					   "http://www.outoflimitz.com/darilni_boni");
    //staticni sajti
	for($x = 0;$x<count($static_sites1);$x++)
	{
		$kategorije = $vsebina->createElement("url");
			$kategorije->appendChild($vsebina->createElement("loc",$static_sites1[$x]));
			$kategorije->appendChild($vsebina->createElement("changefreq","monthly"));
			$kategorije->appendChild($vsebina->createElement("priority","0.85"));

		$main_element->appendChild($kategorije);
	}

	//glavne kategorije
   $izdelki=$db->query("SELECT * FROM modul_izdelki_kategorije WHERE parent='0'");

	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/'.convertToLink($i['ime']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.85"));
		   $main_element->appendChild($kategorije);
	}

	//podkategorije
    $izdelki=$db->query("SELECT * FROM modul_izdelki_kategorije WHERE parent!='0'");
	while ($i=$izdelki->fetch()) {
		   $par=$db->query("SELECT * FROM modul_izdelki_kategorije WHERE parent='0' AND id='".$i['parent']."'");
		   $p=$par->fetch();
    	   $link_do_izdelka='http://www.outoflimitz.com/'.convertToLink($p['ime']).'-'.convertToLink($i['ime']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.85"));
		   $main_element->appendChild($kategorije);
	}

	//izdelki
   $izdelki=$db->query("SELECT id,ime FROM modul_izdelki WHERE osnutek=0 ORDER BY id");
	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/izdelek/'.convertToLink($i['ime']).'/'.$i['id'];
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","monthly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.75"));
		   $main_element->appendChild($kategorije);
	}


	//o nas
    $izdelki=$db->query("SELECT naslov FROM modul_onas WHERE osnutek='0' AND naslov != 'O nas'");
	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/o-nas/'.convertToLink($i['naslov']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","monthly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.65"));
		   $main_element->appendChild($kategorije);
	}


	//novice
    $izdelki=$db->query("SELECT naslov FROM modul_news WHERE osnutek='0'");
	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/novice/'.convertToLink($i['naslov']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","monthly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.65"));
		   $main_element->appendChild($kategorije);
	}


	$izdelki = $db->query("SELECT p.id,p.ime FROM modul_izdelki_proizvajalci p, modul_izdelki i WHERE i.proizvajalec=p.id AND i.osnutek='0' AND i.deleted='0'  GROUP BY p.id ORDER BY ime ASC");

	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/znamke/'.convertToLink($i['ime']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.75"));
		   $main_element->appendChild($kategorije);
	}

	$vsebina->appendChild($main_element);

		//manj pomembni
    $static_sites2=array("http://www.outoflimitz.com/prijava",
					   "http://www.outoflimitz.com/registracija");
    //staticni sajti
	for($x = 0;$x<count($static_sites2);$x++)
	{
		$kategorije = $vsebina->createElement("url");
			$kategorije->appendChild($vsebina->createElement("loc",$static_sites2[$x]));
			$kategorije->appendChild($vsebina->createElement("changefreq","monthly"));
			$kategorije->appendChild($vsebina->createElement("priority","0.65"));

		$main_element->appendChild($kategorije);
	}

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	//carobna koda
	$xpath->registerNamespace('sitemap', "http://www.sitemaps.org/schemas/sitemap/0.9");
    $nodelist = $xpath->query("//sitemap:urlset");
	#$nodelist = $xpath->query("/urlset");
	$oldnode = $nodelist->item(0);


	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/sitemap.xml");


}

function GeneratePSHB($db)
{

	$dom = new DomDocument;
	$dom->preserveWhiteSpace = true;
	$dom->load($_SERVER['DOCUMENT_ROOT'] . "/XML/pshb.xml");

	//nastavimo začasna DOMdocumenta
	$vsebina = new DomDocument;
	$main_element = $vsebina->createElement("urlset");
  $main_element->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
 $main_element->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
 $main_element->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');

   //bolj pomembni
    $static_sites1=array("http://www.outoflimitz.com/",
					   "http://www.outoflimitz.com/o-nas",
					   "http://www.outoflimitz.com/vroci_paketki",
					   "http://www.outoflimitz.com/vse_znamke",
					   "http://www.outoflimitz.com/novice");
    //staticni sajti
	for($x = 0;$x<count($static_sites1);$x++)
	{
		$kategorije = $vsebina->createElement("url");
			$kategorije->appendChild($vsebina->createElement("loc",$static_sites1[$x]));
			$kategorije->appendChild($vsebina->createElement("changefreq","monthly"));
			$kategorije->appendChild($vsebina->createElement("priority","0.85"));

		$main_element->appendChild($kategorije);
	}

	//glavne kategorije
   $izdelki=$db->query("SELECT * FROM modul_izdelki_kategorije WHERE parent='0'");

	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/'.convertToLink($i['ime']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.85"));
		   $main_element->appendChild($kategorije);
	}

	//podkategorije
    $izdelki=$db->query("SELECT * FROM modul_izdelki_kategorije WHERE parent!='0'");
	while ($i=$izdelki->fetch()) {
		   $par=$db->query("SELECT * FROM modul_izdelki_kategorije WHERE parent='0' AND id='".$i['parent']."'");
		   $p=$par->fetch();
    	   $link_do_izdelka='http://www.outoflimitz.com/'.convertToLink($p['ime']).'-'.convertToLink($i['ime']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.85"));
		   $main_element->appendChild($kategorije);
	}

	//izdelki
   $izdelki=$db->query("SELECT id,ime FROM modul_izdelki WHERE osnutek=0 ORDER BY id");
	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/izdelek/'.convertToLink($i['ime']).'/'.$i['id'];
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.75"));
		   $main_element->appendChild($kategorije);
	}


	//o nas
    $izdelki=$db->query("SELECT naslov FROM modul_onas WHERE osnutek='0' AND naslov != 'O nas'");
	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/o-nas/'.convertToLink($i['naslov']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.65"));
		   $main_element->appendChild($kategorije);
	}


	//novice
    $izdelki=$db->query("SELECT naslov FROM modul_news WHERE osnutek='0'");
	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/novice/'.convertToLink($i['naslov']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.65"));
		   $main_element->appendChild($kategorije);
	}


	$izdelki = $db->query("SELECT p.id,p.ime FROM modul_izdelki_proizvajalci p, modul_izdelki i WHERE i.proizvajalec=p.id AND i.osnutek='0' AND i.deleted='0'  GROUP BY p.id ORDER BY ime ASC");

	while ($i=$izdelki->fetch()) {
    	   $link_do_izdelka='http://www.outoflimitz.com/znamke/'.convertToLink($i['ime']);
    	   $kategorije = $vsebina->createElement("url");
		   	 $kategorije->appendChild($vsebina->createElement("loc",$link_do_izdelka));
		   	 $kategorije->appendChild($vsebina->createElement("changefreq","weekly"));
		   	 $kategorije->appendChild($vsebina->createElement("priority","0.75"));
		   $main_element->appendChild($kategorije);
	}

	$vsebina->appendChild($main_element);

		//manj pomembni
    $static_sites2=array("http://www.outoflimitz.com/prijava",
					   "http://www.outoflimitz.com/registracija");
    //staticni sajti
	for($x = 0;$x<count($static_sites2);$x++)
	{
		$kategorije = $vsebina->createElement("url");
			$kategorije->appendChild($vsebina->createElement("loc",$static_sites2[$x]));
			$kategorije->appendChild($vsebina->createElement("changefreq","monthly"));
			$kategorije->appendChild($vsebina->createElement("priority","0.65"));

		$main_element->appendChild($kategorije);
	}

	//UPDATE starega XML dokumenta
	$xpath = new DOMXpath($dom);
	//carobna koda
	$xpath->registerNamespace('sitemap', "http://www.sitemaps.org/schemas/sitemap/0.9");
    $nodelist = $xpath->query("//sitemap:urlset");
	#$nodelist = $xpath->query("/urlset");
	$oldnode = $nodelist->item(0);


	//kategorije
	$newnode = $dom->importNode($vsebina->documentElement, true);
	$oldnode->parentNode->replaceChild($newnode, $oldnode);

	$dom->formatOutput = true;
	$dom->save($_SERVER['DOCUMENT_ROOT'] . "/XML/pshb.xml");


}

?>
