<?php
include_once '../../config.inc.php';

global $db;
global $date;
global $structure_array;
global $manufacturer_array;
global $manufacturer_to_item_categories_array;
global $url;
global $file;
global $fh;

global $item_category;
global $helper_pages;

$db = new mysqli(
	$config_shop_system['db_host'],
	$config_shop_system['db_user'],
	$config_shop_system['db_password'],
	$config_shop_system['db_name']
);

$date = date('Y-m-d');

$structure_array = array();
$manufacturer_array = array();
$manufacturer_to_item_categories_array = array();

$url = $config_shop_system['MAIN_URL'];
$file = $config_shop_system['ROOT_DIR'] . 'web/' . $config_shop_system['SHOP_SYSTEM'] . '/documents/files/sitemap.xml';

$item_category = 3;
$helper_pages = 61;

function get_structure($id, $parent_short_uri = '/', $is_item_category = false) {
	global $db;
	global $structure_array;
	global $url;
	global $date;
	global $fh;
	
	global $item_category;
	global $helper_pages;

	$sql = "SELECT * FROM structure WHERE parent_id=" . $db->real_escape_string($id);

	$result = $db->query($sql);
	
	if ($result->num_rows > 0) {
		while ($tmp = $result->fetch_assoc()) {
			if ($id != 0) {
				$tmp['full_short_uri'] = $parent_short_uri . $tmp['short_uri'] . '/';
				$tmp['is_root'] = 0;
			} else {
				$tmp['full_short_uri'] = $parent_short_uri;
				$tmp['is_root'] = 1;
			}
				
			if ($tmp['parent_id'] == $item_category || $is_item_category)	{
				$tmp['is_item_category'] = $is_item_category;
				$tmp['sub_categories'] = get_structure($tmp['id'], $tmp['full_short_uri'], true);
			} else {
				$tmp['sub_categories'] = get_structure($tmp['id'], $tmp['full_short_uri']);
			}
			
			$tmp['url'] = $url . $tmp['full_short_uri'];

			if (($tmp['id'] != $helper_pages) || ($tmp['parent_id'] != $helper_pages)) {
				if (!($tmp['is_root'])) {
					$structure_array[$tmp['id']] = $tmp;
					
					fwrite($fh, "<url>\n");
					fwrite($fh, "<loc>" . $tmp['url'] . "</loc>\n");
					fwrite($fh, "<lastmod>" . $date . "</lastmod>\n");
					fwrite($fh, "<changefreq>daily</changefreq>\n");
					fwrite($fh, "<priority>1.0</priority>\n");
					fwrite($fh, "</url>\n");
				}
			}
		}
	}
}

function get_manufacturer_to_item_categories() {
	global $db;
	global $structure_array;
	global $manufacturer_to_item_categories_array;

	$sql = "SELECT
				i.manufacturer_id,
				i.structure_id,
				s.short_uri AS structure_short_uri
			FROM
				items i
			LEFT JOIN
				structure s
			ON
				i.structure_id = s.id
			GROUP BY
				i.manufacturer_id, i.structure_id";

	$result = $db->query($sql);

	if ($result->num_rows > 0) {
		while ($tmp = $result->fetch_assoc()) {
			if (isset($structure_array[$tmp['structure_id']])) {
				$tmp['partial_short_url'] = $structure_array[$tmp['structure_id']]['full_short_uri'];
			} else {
				$tmp['partial_short_url'] = '';
			}
			
			$manufacturer_to_item_categories_array[$tmp['manufacturer_id']][$tmp['structure_id']] = $tmp;
		}
	}
}

function get_manufacturers() {
	global $db;
	global $url;
	global $manufacturer_array;
	
	$sql = "SELECT * FROM manufacturers";
	
	$result = $db->query($sql);
	
	if ($result->num_rows > 0) {
		while ($tmp = $result->fetch_assoc()) {
			$tmp['url'] = $url . '/Kaufen/' . $tmp['short_uri'] . '/';
			
			$manufacturer_array[] = $tmp;
		}
	}
}

function get_items() {
	global $db;
	global $structure_array;
	global $manufacturer_array;
	global $manufacturer_to_item_categories_array;
	
	global $date;
	global $fh;
	
	$sql = "SELECT
				i.*,
				m.name AS manufacturer_name
			FROM
				items i
			JOIN
				manufacturers m
			ON
				m.id = i.manufacturer_id";
	
	$result = $db->query($sql);
	
	if ($result->num_rows > 0) {
		while ($tmp = $result->fetch_assoc()) {
			if (isset($structure_array[$tmp['structure_id']])) {
				fwrite($fh, "<url>\n");
				fwrite($fh, "<loc>" . $structure_array[$tmp['structure_id']]['url'] . $tmp['short_uri'] . '.html' . "</loc>\n");
				fwrite($fh, "<lastmod>" . $date . "</lastmod>\n");
				fwrite($fh, "<changefreq>daily</changefreq>\n");
				fwrite($fh, "<priority>1.0</priority>\n");
				fwrite($fh, "</url>\n");
			}
	
			if (isset($manufacturer_array[$tmp['manufacturer_id']])) {
				if (isset($manufacturer_to_item_categories_array[$tmp['manufacturer_id']][$tmp['structure_id']])) {
					fwrite($fh, "<url>\n");
					fwrite($fh, "<loc>" . $manufacturer_array[$tmp['manufacturer_id']]['url'] . substr($manufacturer_to_item_categories_array[$tmp['manufacturer_id']][$tmp['structure_id']]['partial_short_url'], 1) . $tmp['short_uri'] . '.html' . "</loc>\n");
					fwrite($fh, "<lastmod>" . $date . "</lastmod>\n");
					fwrite($fh, "<changefreq>daily</changefreq>\n");
					fwrite($fh, "<priority>1.0</priority>\n");
					fwrite($fh, "</url>\n");
				}
			}
		}
	}
}

$fh = fopen($file, 'w');

fwrite($fh, '<?xml version="1.0" encoding="UTF-8"?>' . "\n");
fwrite($fh, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n");

get_structure(0);
get_manufacturer_to_item_categories();
get_manufacturers();
get_items();

fwrite($fh, "</urlset>\n");

fclose($fh);
