<?php 
	header( 'Content-type: application/xml; charset="UTF8"', true );
	header( 'Pragma: no-cache' );
	
	/* Cargamos la Configuración e Inicio de la Aplicación */ 
	require ('../application/config/config.php');
	require ('../application/config/autoload.php');

	/* Cargamos las sesiones */
	require('../application/inc/session-public.php');

	/* Cargamos los mensajes de acuerdo al idioma general (admin) */
	$langadm = '../application/lang/'.$_SESSION["_l"].'.php';
	if(file_exists($langadm) and is_file($langadm))
	require($langadm);
	/* Cargamos los mensajes de acuerdo al idioma general (public)*/
	$lang = '../application/lang/public/'.$_SESSION["_l"].'.php';
	if(file_exists($lang) and is_file($lang))
	require($lang);

	require ('../application/libs_extra/seo/google_sitemap.class.php');
	//require ('../application/libs_extra/seo/limpiarcadena.php');
	$data = array();
	$arraymenu = array();
	$array = array_merge($_TRLS["Menu"]["Superior"],$_TRLS["Menu"]["servicios"],$_TRLS["Menu"]["nomenu"]);
	$array = array_unique($array);
	
	
	foreach($array as $link=>$value){
		$arraymenu[] = "es/".str_replace(".php",".html",$link);
	}
	$total = count($arraymenu);
	for($i=0; $i<$total; $i++){
		$indice = key($arraymenu);
		$data[] = array(
						"loc" => URL."/".$arraymenu[$indice],
						"changefreq" => "monthly",
						"priority"=>"1"
						);
		next($arraymenu);
	}

	/* Links de Seguros */
	$rows = array();
	$total = 0;
	try {
		$pdo = libs_bd_pdo::getInstance();
		$query = 'SELECT  a.Titulo, a.Descripcion,a.Slug FROM seguros as a';
		$stm = $pdo->prepare($query);
		$stm->execute();
		$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
		$total = $stm->rowCount();
	}catch (Exception $ex) {
		libs_exception_log::crearLog($pdo, $ex, ERROR_LOG, ERROR_NO, SALT, DEV_ENV);
	}	

	$link = "";
	for($i=0; $i<$total; $i++){
		$data[] = array(
					"loc" => URL."/nuestros-seguros/{$rows[$i]["Slug"]}",
					"changefreq" => "monthly",
					"priority"=>"1"
					);
	}
	/* Links de Notas */
	$rows = array();
	$total = 0;
	try {
		$pdo = libs_bd_pdo::getInstance();
		$query = 'SELECT a.Slug as SlugNota, b.Slug as SlugCategoria FROM notas a JOIN notas_tipo b ON(a.IDNotaTipo = b.IDNotaTipo)';
		$stm = $pdo->prepare($query);
		$stm->execute();
		$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
		$total = $stm->rowCount();
	}catch (Exception $ex) {
		libs_exception_log::crearLog($pdo, $ex, ERROR_LOG, ERROR_NO, SALT, DEV_ENV);
	}	

	$link = "";
	for($i=0; $i<$total; $i++){
		$data[] = array(
					"loc" => URL."/notas/{$rows[$i]["SlugCategoria"]}/{$rows[$i]["SlugNota"]}",
					"changefreq" => "weekly",
					"priority"=>"1"
					);
	}
	/* Links de segutips */
	$rows = array();
	$total = 0;
	try {
		$pdo = libs_bd_pdo::getInstance();
		$query = 'SELECT a.Slug as SlugTip, b.Slug as SlugCategoria FROM tips a JOIN tips_categorias b ON(a.IDTipsCategorias = b.IDTipsCategorias)';
		$stm = $pdo->prepare($query);
		$stm->execute();
		$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
		$total = $stm->rowCount();
	}catch (Exception $ex) {
		libs_exception_log::crearLog($pdo, $ex, ERROR_LOG, ERROR_NO, SALT, DEV_ENV);
	}	

	$link = "";
	for($i=0; $i<$total; $i++){
		$data[] = array(
					"loc" => URL."/centro-de-ayuda/{$rows[$i]["SlugCategoria"]}/{$rows[$i]["SlugTip"]}",
					"changefreq" => "weekly",
					"priority"=>"1"
					);
	}

	/* Links de preguntas frecuentes */
	$rows = array();
	$total = 0;
	try {
		$pdo = libs_bd_pdo::getInstance();
		$query = 'SELECT a.Slug FROM preguntas_frecuentes_categorias as a';
		$stm = $pdo->prepare($query);
		$stm->execute();
		$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
		$total = $stm->rowCount();
	}catch (Exception $ex) {
		libs_exception_log::crearLog($pdo, $ex, ERROR_LOG, ERROR_NO, SALT, DEV_ENV);
	}	

	$link = "";
	for($i=0; $i<$total; $i++){
		$data[] = array(
					"loc" => URL."/preguntas-frecuentes/{$rows[$i]["Slug"]}",
					"changefreq" => "weekly",
					"priority"=>"1"
					);
	}

	/* Links de glosario de términos */
	$rows = array();
	$total = 0;
	try {
		$pdo = libs_bd_pdo::getInstance();
		$query = 'SELECT distinct SUBSTRING(Titulo,1,1) as Slug FROM terminos order by 1 ASC';
		$stm = $pdo->prepare($query);
		$stm->execute();
		$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
		$total = $stm->rowCount();
	}catch (Exception $ex) {
		libs_exception_log::crearLog($pdo, $ex, ERROR_LOG, ERROR_NO, SALT, DEV_ENV);
	}	

	$link = "";
	for($i=0; $i<$total; $i++){
		$data[] = array(
					"loc" => URL."/glosario-de-terminos/".strtolower($rows[$i]["Slug"]),
					"changefreq" => "weekly",
					"priority"=>"1"
					);
	}

	/* Links de glosario de nuestro personal */
	/*
	$rows = array();
	$total = 0;
	try {
		$pdo = libs_bd_pdo::getInstance();
		$query = 'SELECT IDAgente as Slug, Nombre from agentes';
		$stm = $pdo->prepare($query);
		$stm->execute();
		$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
		$total = $stm->rowCount();
	}catch (Exception $ex) {
		libs_exception_log::crearLog($pdo, $ex, ERROR_LOG, ERROR_NO, SALT, DEV_ENV);
	}	

	$link = "";
	for($i=0; $i<$total; $i++){
		$nombre = libs_utilerias_cadena::quitarAcentosUrl($rows[$i]["Nombre"]);
		$data[] = array(
					"loc" => URL."/nuestra-gente/".strtolower($rows[$i]["Slug"])."/".$nombre,
					"changefreq" => "weekly",
					"priority"=>"1"
					);
	}
	*/
	$pdo = null;
	// Generación del mapa del sitio
	$site_map_container = new google_sitemap($data);
	$site_map_container->printxml();	
?>