<?php
 //Include the common file
 require_once 'common.php';

header('Content-Type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
 xmlns:xhtml="http://www.w3.org/1999/xhtml">';
function hreflang($value,$language)
{
    include cfg('language', 'languages_path').'lang.'.$language.'.php';

    return $lang[$value];
}

$mainPages = array(
'URL_DJMIXES',
'URL_BLOG',
'URL_CONTACT',
'URL_SITEMAP',
'URL_RSS',
'URL_PARTNERS',
'URL_EVENTS'
);

foreach ($languages as $val) {
    echo '<url>';
    if($row['language_code']==cfg('language', 'site_language'))
      echo '<loc>'.cfg('base', 'site_url').'</loc>';
    else
      echo '<loc>'.cfg('base', 'site_url').$val['language_code'].'/</loc>';

          foreach ($languages as $row) {

            if($row['language_code']==cfg('language', 'site_language'))
               echo '<xhtml:link
					rel="alternate"
					hreflang="'.$row['language_code'].'"
					href="'.cfg('base', 'site_url').'" />';
            else
               echo '<xhtml:link
					rel="alternate"
					hreflang="'.$row['language_code'].'"
					href="'.cfg('base', 'site_url').$row['language_code'].'/" />';
          }
    echo '<changefreq>daily</changefreq>
          <priority>1.0</priority>
	      </url>';
}

for ($i=0; $i<count($mainPages); $i++) {

foreach ($languages as $val) {
    echo '<url>';
    if($row['language_code']==cfg('language', 'site_language'))
      echo '<loc>'.cfg('base', 'site_url').hreflang($mainPages[$i],$val['language_code']).'</loc>';
    else
      echo '<loc>'.cfg('base', 'site_url').$val['language_code'].'/'.hreflang($mainPages[$i],$val['language_code']).'</loc>';

          foreach ($languages as $row) {
                if($row['language_code']==cfg('language', 'site_language'))
                  echo '<xhtml:link
					rel="alternate"
					hreflang="'.$row['language_code'].'"
					href="'.cfg('base', 'site_url').hreflang($mainPages[$i],$row['language_code']).'" />';
                else
                  echo '<xhtml:link
					rel="alternate"
					hreflang="'.$row['language_code'].'"
					href="'.cfg('base', 'site_url').$row['language_code'].'/'.hreflang($mainPages[$i],$row['language_code']).'" />';
          }
    echo '<changefreq>daily</changefreq>
          <priority>1.0</priority>
	      </url>';
}

}

foreach ($languages as $val) {

    $sql = "SELECT c.category_id, t.category_name FROM " . cfg('base', 'table_articles_categories') . " c, " . cfg('base', 'table_articles_category_translations') . " t WHERE t.language_code = '" . $val['language_code'] . "' AND c.category_status = 1 AND c.category_id = t.category_id";
    foreach ($db->query($sql) as $value) {
     echo '<url>';
     if($row['language_code']==cfg('language', 'site_language'))
        echo '<loc>'.cfg('base', 'site_url').hreflang('URL_BLOG',$val['language_code']).'/'.url($value['category_name']).'</loc>';
     else
        echo '<loc>'.cfg('base', 'site_url').$val['language_code'].'/'.hreflang('URL_BLOG',$val['language_code']).'/'.url($value['category_name']).'</loc>';
     foreach ($languages as $row) {
          $cat = $db->fetchRowAssoc("SELECT c.category_id, t.category_name FROM " . cfg('base', 'table_articles_categories') . " c, " . cfg('base', 'table_articles_category_translations') . " t WHERE t.language_code = '" . $row['language_code'] . "' AND c.category_status = 1 AND c.category_id = t.category_id AND c.category_id = ". $value['category_id']);
          if($row['language_code']==cfg('language', 'site_language'))
            echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').hreflang('URL_BLOG',$row['language_code']).'/'.url($cat['category_name']).'" />';
          else
            echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').$row['language_code'].'/'.hreflang('URL_BLOG',$row['language_code']).'/'.url($cat['category_name']).'" />';
     }
     echo '<changefreq>daily</changefreq>
          <priority>1.0</priority>
	      </url>';
    }

}

foreach ($languages as $val) {

    $sql = "SELECT a.article_id, t.article_title FROM " . cfg('base', 'table_articles') . " a, " . cfg('base', 'table_article_translations') . " t WHERE t.language_code = '" . $val['language_code'] . "' AND a.article_status = 1 AND a.article_id = t.article_id";
    foreach ($db->query($sql) as $value) {
     echo '<url>';
     if($row['language_code']==cfg('language', 'site_language'))
        echo '<loc>'.cfg('base', 'site_url').hreflang('URL_BLOG',$val['language_code']).'/'.url(article_category($value['article_id'],$val['language_code'])).'/'.url($value['article_title']).'-'.$value['article_id'].'</loc>';
     else
        echo '<loc>'.cfg('base', 'site_url').$val['language_code'].'/'.hreflang('URL_BLOG',$val['language_code']).'/'.url(article_category($value['article_id'],$val['language_code'])).'/'.url($value['article_title']).'-'.$value['article_id'].'</loc>';
     foreach ($languages as $row) {
          $art = $db->fetchRowAssoc("SELECT a.article_id, t.article_title FROM " . cfg('base', 'table_articles') . " a, " . cfg('base', 'table_article_translations') . " t WHERE t.language_code = '" . $row['language_code'] . "' AND a.article_status = 1 AND a.article_id = t.article_id AND a.article_id = ". $value['article_id']);
          if($row['language_code']==cfg('language', 'site_language'))
            echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').hreflang('URL_BLOG',$row['language_code']).'/'.url(article_category($art['article_id'],$row['language_code'])).'/'.url($art['article_title']).'-'.$art['article_id'].'" />';
          else
            echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').$row['language_code'].'/'.hreflang('URL_BLOG',$row['language_code']).'/'.url(article_category($art['article_id'],$row['language_code'])).'/'.url($art['article_title']).'-'.$art['article_id'].'" />';
     }
    echo '<changefreq>daily</changefreq>
          <priority>1.0</priority>
	      </url>';
    }

}

foreach ($languages as $val) {

    $sql = "SELECT e.event_id, t.event_title FROM " . cfg('base', 'table_events') . " e, " . cfg('base', 'table_event_translations') . " t WHERE t.language_code = '" . $val['language_code'] . "' AND e.event_status = 1 AND e.event_id = t.event_id";
    foreach ($db->query($sql) as $value) {
     echo '<url>';
     if($row['language_code']==cfg('language', 'site_language'))
        echo '<loc>'.cfg('base', 'site_url').hreflang('URL_EVENTS',$val['language_code']).'/'.url($value['event_title']).'-'.$value['event_id'].'</loc>';
     else
        echo '<loc>'.cfg('base', 'site_url').$val['language_code'].'/'.hreflang('URL_EVENTS',$val['language_code']).'/'.url($value['event_title']).'-'.$value['event_id'].'</loc>';
     foreach ($languages as $row) {
          $event = $db->fetchRowAssoc("SELECT e.event_id, t.event_title FROM " . cfg('base', 'table_events') . " e, " . cfg('base', 'table_event_translations') . " t WHERE t.language_code = '" . $row['language_code'] . "' AND e.event_status = 1 AND e.event_id = t.event_id AND e.event_id = ". $value['event_id']);
          if($row['language_code']==cfg('language', 'site_language'))
            echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').hreflang('URL_EVENTS',$row['language_code']).'/'.url($event['event_title']).'-'.$event['event_id'].'" />';
          else
            echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').$row['language_code'].'/'.hreflang('URL_EVENTS',$row['language_code']).'/'.url($event['event_title']).'-'.$event['event_id'].'" />';
     }
     echo '<changefreq>daily</changefreq>
          <priority>1.0</priority>
	      </url>';
    }

}

foreach ($languages as $val) {

    $sql = "SELECT m.mix_id, t.mix_title FROM " . cfg('base', 'table_mixes') . " m, " . cfg('base', 'table_mix_translations') . " t WHERE t.language_code = '" . $val['language_code'] . "' AND m.mix_status = 1 AND m.mix_id = t.mix_id";
    foreach ($db->query($sql) as $value) {
     echo '<url>';
     if($row['language_code']==cfg('language', 'site_language'))
        echo '<loc>'.cfg('base', 'site_url').hreflang('URL_DJMIXES',$val['language_code']).'/'.url($value['mix_title']).'-'.$value['mix_id'].'</loc>';
     else
       echo '<loc>'.cfg('base', 'site_url').$val['language_code'].'/'.hreflang('URL_DJMIXES',$val['language_code']).'/'.url($value['mix_title']).'-'.$value['mix_id'].'</loc>';
     foreach ($languages as $row) {
          $mix = $db->fetchRowAssoc("SELECT m.mix_id, t.mix_title FROM " . cfg('base', 'table_mixes') . " m, " . cfg('base', 'table_mix_translations') . " t WHERE t.language_code = '" . $row['language_code'] . "' AND m.mix_status = 1 AND m.mix_id = t.mix_id AND m.mix_id = ". $value['mix_id']);
          if($row['language_code']==cfg('language', 'site_language'))
             echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').hreflang('URL_DJMIXES',$row['language_code']).'/'.url($mix['mix_title']).'-'.$mix['mix_id'].'" />';
          else
             echo '<xhtml:link
				rel="alternate"
				hreflang="'.$row['language_code'].'"
				href="'.cfg('base', 'site_url').$row['language_code'].'/'.hreflang('URL_DJMIXES',$row['language_code']).'/'.url($mix['mix_title']).'-'.$mix['mix_id'].'" />';
     }
     echo '<changefreq>daily</changefreq>
          <priority>1.0</priority>
	      </url>';
    }

}

echo '</urlset>';
