<?php

/**
 * sitemap.xml
 *
 * Generates a sitemap of the web according to the information
 * stored on iw4_menu and iw4_identificadores tables.
 *
 * It's necessary to activate SEO config on JSON so the CMS
 * could put this data
 *
 * @package Core
 */

// Init app
ini_set ("display_errors", 1);
error_reporting (E_ALL ^ E_NOTICE);
define("APP_RUNNING", true);
define("APP_FOLDER", dirname(__FILE__) );


// Require all classes and files needed by application and parse configuration.
require APP_FOLDER . "/core/scripts/startup.php";


$_SERVER["REQUEST_URI"] = "/";
$site_id = Context::getRequest()->getSiteID();


// Connect the default db connection
$conn = Context :: getConnection () ;
$conn->connect();


// Retreive data
$ids = $conn->prepare("
    select 
        * 
    
    from 
        iw4_identificadores 
        left join iw4_menu ON iw4_menu.iw4id = iw4_identificadores.iditem AND iw4_identificadores.tipomodulo = 0
        
    where 
        sitemap like '|1|%' and (iw4_identificadores.idsite = {$site_id} OR iw4_identificadores.idsite = 0) AND
        (iw4_menu.iw4menu = 0 OR iw4_menu.iw4menu = NULL)
        "
)->execute();


// Parse
$sitemap = array();
$langs = $conn->prepare("select valor from iw4_config where clave='show_langs' and idsite = {$site_id}")->execute();
$langs = Helper::stringToList($langs[0]["valor"]);


foreach ($langs as $lang) {

    Context::setLang ($lang);
                
    foreach ($ids as $id) {
                
        // Check current language
        if ($id['idioma'] != $lang) {
            continue ;
        }
    
        
        if ($id["tipomodulo"] == 0) {
            $item = new Section($id["iditem"]);
        }
        else {
            $item = new Item($id["iditem"], $id["tipomodulo"]);

            if ( ! $item -> getOneLink ()) {
                continue ;
            }
                                
            $item -> setLink ($item -> getOneLink ()) ;
                                
        }
        
        $link = substr_count($item->getFullLink(), 'http');
        
        $info = explode("||", trim($id["sitemap"], "|"));
        
        if($link <= 1) {
            $sitemap[] = array(
                "loc"       => $item->getFullLink(),
                "changefreq"    => $info[2],
                "priority"  => $info[1]
            );
        }
    }

}

$conn->disconnect();

header("Content-Type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>
    <urlset
    xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9
    http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'>
\n";

    echo "<url>
        <loc>http://".$_SERVER['HTTP_HOST']."</loc>
        <changefreq>weekly</changefreq>
        <priority>10</priority>
    </url>\n";
    
    
foreach ($sitemap as $url) {

    echo "<url>
    <loc>".$url['loc']."</loc>
    <changefreq>".$url['changefreq']."</changefreq>
    <priority>".$url['priority']."</priority>
    </url>\n";
}
echo '</urlset>';

?>
