<?php 
require_once("core/common.php");
function recursiveSitemapItems($id, $count, $rating) {
    global $conf;
    $count = $count+1;
    
	//get Parent ids homepage
	$sParentIds = "";
	$psParentIds = new PreparedStatement("SELECT `id` FROM `content` WHERE `parentid` = 0");
	$psParentIds->execute();
	while ($fParentIds = $psParentIds->fetch()) {
		$sParentIds .= " `content`.`parentid` = ". $fParentIds['id']." OR";
	}
	$sParentIds = substr($sParentIds, 0, -3);
	
	//fetch content of page
    $ps = new PreparedStatement("SELECT `content`.`id` as `contentid`, 
MAX(`log`.`log_date`) as `logdate`, 
`url_alias`, `typeid`, 
`var`.`value` as `pagecat` 
FROM `content` 
LEFT JOIN `log` ON `content`.`id`=`recordid` 
LEFT JOIN `var` ON `typeid` = `var`.`id` 
WHERE `content`.`parentid` = [1] AND (`var`.`value` = 'Voorpagina' OR `var`.`value` = 'Pagina' OR `var`.`value` = 'Link') AND !((`content`.`menu_title` = '404' OR `content`.`menu_title` = 'sitemap') AND ({$sParentIds}))
GROUP BY `contentid`;");
    $ps->setValue(1, $id);
    $ps->execute();
    $i = '';
    
    $amountItems = $ps->getAffectedRows();
    
    while ($row = $ps->fetch()) {
        //fill links
        $i .= "    <url>
        <loc>". $conf[domain] .$row['url_alias']."</loc>
        <lastmod>". substr($row['logdate'],0,10) ."</lastmod>
        <changefreq>weekly</changefreq>
        <priority>".($rating == 1 ? "1.0" : "0.".$rating*10)."</priority>
    </url>\n" . recursiveSitemapItems($row['contentid'], $count, $rating > 0.5 ? $rating-0.1 : $rating);
    }
    return $i;
}

$links = recursiveSitemapItems(0,0,1);

echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<?php echo $links; ?>
</urlset>
