<?php
require('includes/application_top.php');

$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

if (tep_not_null($action)) {
  //
  $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
  $xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'."\n";
  $xml .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
  $xml .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'."\n";
  $xml .= 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'."\n";
  $xml .= '  <url>'."\n";
  $xml .= '    <loc>'.HTTP_SERVER . DIR_WS_HTTP_CATALOG.'</loc>'."\n";
  $xml .= '    <priority>1.0</priority>'."\n";
  $xml .= '    <changefreq>'.$_POST['index'].'</changefreq>'."\n";
  $xml .= '  </url>'."\n";
  $xml .= '  <url>'."\n";
  $xml .= '    <loc>'.tep_href_link(FILENAME_DEFAULT).'</loc>'."\n";
  $xml .= '    <priority>1.0</priority>'."\n";
  $xml .= '    <changefreq>'.$_POST['index'].'</changefreq>'."\n";
  $xml .= '  </url>'."\n";
  //
	if (isset($_POST['prod'])) {
		$sitemap_sql = tep_db_query("select distinct p.products_id,p.products_ordered from " . TABLE_PRODUCTS . " p where products_status = '1' order by p.products_ordered desc");
		$top = 0;
		while ($wynik = tep_db_fetch_array($sitemap_sql)) {
			if (isset($_POST['automat_prior'])) {
				$top = max($top, $wynik['products_ordered']);
				$ratio = $top > 0 ? $wynik['products_ordered']/$top : 0;
				$priority = $ratio < .1 ? .1 : number_format($ratio, 1, '.', '');
			   } else {
				$priority = $_POST['priot_prod'];
			}
			$xml .= '  <url>'."\n";
			$xml .= '    <loc>'.tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $wynik["products_id"]).'</loc>'."\n";
			$xml .= '    <priority>'.$priority.'</priority>'."\n";
			$xml .= '    <changefreq>'.$_POST['index'].'</changefreq>'."\n";
			$xml .= '  </url>'."\n";
		}
	}
	//
	if (isset($_POST['kate'])) {
		$priority = $_POST['priot_kate'];
		$sitemap_sql = tep_db_query("select distinct categories_id from " . TABLE_CATEGORIES . " p where categories_status = '1' order by parent_id ASC, sort_order ASC, categories_id ASC");
		while ($wynik = tep_db_fetch_array($sitemap_sql)) {
			$xml .= '  <url>'."\n";
			$xml .= '    <loc>'.tep_href_link(FILENAME_DEFAULT, 'cPath=' . $wynik["categories_id"]).'</loc>'."\n";
			$xml .= '    <priority>'.$priority.'</priority>'."\n";
			$xml .= '    <changefreq>'.$_POST['index'].'</changefreq>'."\n";
			$xml .= '  </url>'."\n";
		}
	}
	//
	if (isset($_POST['info'])) {
		$priority = $_POST['priot_info'];
		$sitemap_sql = tep_db_query("select distinct p.pages_id,p.page_type,s.pages_html_text from " . TABLE_PAGES . " p left join " .TABLE_PAGES_DESCRIPTION . " s on p.pages_id = s.pages_id where p.status = '1' and s.language_id = '" . (int)$languages_id . "' order by sort_order ASC");
		while ($wynik = tep_db_fetch_array($sitemap_sql)) {
			if ( ((strpos($wynik['pages_html_text'],'<***>') > 0) && ($wynik['page_type'] == '5' || $wynik['page_type'] == '6')) || ($wynik['page_type'] != '5' && $wynik['page_type'] != '6') ) {
				$xml .= '  <url>'."\n";
				$xml .= '    <loc>'.tep_href_link(FILENAME_PAGES, 'pages_id=' . $wynik["pages_id"]).'</loc>'."\n";
				$xml .= '    <priority>'.$priority.'</priority>'."\n";
				$xml .= '    <changefreq>'.$_POST['index'].'</changefreq>'."\n";
				$xml .= '  </url>'."\n";
			}
		}
	}
	//
	if (isset($_POST['manu'])) {
		$priority = $_POST['priot_manu'];
		$sitemap_sql = tep_db_query("select distinct m.manufacturers_id, m.manufacturers_name from " . TABLE_MANUFACTURERS . " m left join " . TABLE_PRODUCTS . " p on m.manufacturers_id = p.manufacturers_id left join " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c on p.products_id = p2c.products_id left join " . TABLE_CATEGORIES . " c on p2c.categories_id = c.categories_id where c.categories_status = '1' and p.products_status = '1' order by m.manufacturers_name");
		while ($wynik = tep_db_fetch_array($sitemap_sql)) {
			$xml .= '  <url>'."\n";
			$xml .= '    <loc>'.tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $wynik["manufacturers_id"]).'</loc>'."\n";
			$xml .= '    <priority>'.$priority.'</priority>'."\n";
			$xml .= '    <changefreq>'.$_POST['index'].'</changefreq>'."\n";
			$xml .= '  </url>'."\n";
		}
	}
	//
	if (isset($_POST['aktu'])) {
		$priority = $_POST['priot_aktu'];
		$sitemap_sql = tep_db_query("select newsdesk_id from " . TABLE_NEWSDESK . " p where newsdesk_status = '1' order by newsdesk_id ASC");
		while ($wynik = tep_db_fetch_array($sitemap_sql)) {
			$xml .= '  <url>'."\n";
			$xml .= '    <loc>'.tep_href_link(FILENAME_NEWSDESK_INFO, 'newsdesk_id=' . $wynik['newsdesk_id']).'</loc>'."\n";
			$xml .= '    <priority>'.$priority.'</priority>'."\n";
			$xml .= '    <changefreq>'.$_POST['index'].'</changefreq>'."\n";
			$xml .= '  </url>'."\n";
		}
	}
	//
	$xml .= '</urlset>';
	//
	$file = 'sitemap.xml';
	if ($fp = fopen($file, "w+")) {
		flock($fp, 2);
		fwrite($fp, $xml);
		flock($fp, 3);
		fclose($fp);
		tep_redirect(tep_href_link('administracja/seo_oscgold.php?wynik=ok'));
	  } else {
	    tep_redirect(tep_href_link('administracja/seo_oscgold.php?wynik=blad'));
	}
}
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>