<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

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

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

global $item_category;
global $footer_pages;
global $header_pages;
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']
);

$db->query("SET NAMES 'utf8'");

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

$item_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.html';

$item_category = 3;
$footer_pages = 7;
$header_pages = 5;
$helper_pages = 61;

function char_replace($text) {
	return str_replace($search, $replace, $text);
}

function get_structure_by_root($id, $parent_short_uri = '/', $is_item_category = false, &$return_array) {
	global $db;
	global $url;
	
	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 ($is_item_category)	{
				$tmp['is_item_category'] = $is_item_category;
				get_structure_by_root($tmp['id'], $tmp['full_short_uri'], true, &$return_array);
			} else {
				get_structure_by_root($tmp['id'], $tmp['full_short_uri'], false, &$return_array);
			}
			
			$tmp['url'] = $url . $tmp['full_short_uri'];

			if (!($tmp['is_root'])) {
				$return_array[$tmp['id']] = $tmp;
			}
		}
	}
}

function get_manufacturer_to_item_categories($structure_array) {
	global $db;
	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 print_items_by_category($category_array) {
	global $db;
	global $item_array;
	global $date;
	global $fh;
	
	foreach ($category_array as $category) {
		$sql = "SELECT
					i.*,
					m.name AS manufacturer_name
				FROM
					items i
				JOIN
					manufacturers m
				ON
					m.id = i.manufacturer_id
				WHERE
					i.structure_id=" . $db->real_escape_string($category['id']);
		
		$result = $db->query($sql);
		
		if ($result->num_rows > 0) {
			$name = substr($category['full_short_uri'], 1, -1);
			$name = str_replace('/', ' &gt; ', $name);
			fwrite($fh, '<h3 class="sitemap_sub_header"><a href="' . $category['url'] . '">' . $name . '</a></h3><ul>');
		
			while ($tmp = $result->fetch_assoc()) {
				fwrite($fh, '<li><a href="' . $category['url'] . $tmp['short_uri'] . '.html">' . $tmp['name'] . '</a></li>');
			}
			
			fwrite($fh, "</ul>");
		}	
	}
}

function print_items_by_manufacturer($manufacturer_id, $structure_id) {
	global $db;
	global $item_array;
	global $manufacturer_array;
	global $manufacturer_to_item_categories_array;
	global $date;
	global $fh;

	$sql = "SELECT *
			FROM items
			WHERE manufacturer_id=" . $db->real_escape_string($manufacturer_id);
			" AND structure_id=" . $db->real_escape_string($structure_id);

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

	if ($result->num_rows > 0) {
		$name = $manufacturer_array[$manufacturer_id]['name'];
		$url = $manufacturer_array[$manufacturer_id]['url'] . substr($manufacturer_to_item_categories_array[$manufacturer_id][$structure_id]['partial_short_url'], 1);
		fwrite($fh, '<h3 class="sitemap_sub_header"><a href="' . $url . '">' . $name . '</a></h3><ul>');
		while ($tmp = $result->fetch_assoc()) {
			if (isset($manufacturer_array[$tmp['manufacturer_id']])) {
				if (isset($manufacturer_to_item_categories_array[$tmp['manufacturer_id']][$tmp['structure_id']])) {
					fwrite($fh, '<li><a href="' . $manufacturer_array[$tmp['manufacturer_id']]['url'] . substr($manufacturer_to_item_categories_array[$manufacturer_id][$structure_id]['partial_short_url'], 1) . $tmp['short_uri'] . '.html' . $tmp['name'] . '</a></li>');
				}
			}
		}
	}
}

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

//fwrite($fh, '<div id="sitemap">');

$header_array = array();
get_structure_by_root($header_pages, '/', false, $header_array);
if ($header_array != array()) {
	fwrite($fh, '<h2 class="sitemap_sub_header">Header Men&uuml;</h2><ul class="site_list">');
	
	foreach ($header_array as $header) {
		fwrite($fh, '<li><a href="' . $header['url'] . '">' . $header['name'] . '</a></li>');
	}
	
	fwrite($fh, "</ul>");
}

$footer_array = array();
get_structure_by_root($footer_pages, '/', false, $footer_array);
if ($footer_array != array()) {
	fwrite($fh, '<h2 class="sitemap_sub_header">Footer Men&uuml;</h2><ul class="site_list">');
	
	foreach ($footer_array as $footer) {
		fwrite($fh, '<li><a href="' . $footer['url'] . '">' . $footer['name'] . '</a></li>');
	}
	
	fwrite($fh, "</ul>");
}

$category_array = array();
get_structure_by_root($item_category, '/', true, $category_array);
if ($category_array != array()) {
	fwrite($fh, '<h2 class="sitemap_sub_header">Warengruppenmen&uuml;</h2><ul>');
	
	print_items_by_category($category_array);
	
	fwrite($fh, "</ul>");
}

$category_array = array();
get_structure_by_root($item_category, '/', true, $category_array);

get_manufacturer_to_item_categories($category_array);
get_manufacturers();

//$manufactuerer_category_name = "Fishe";
//fwrite($fh, '<h2 class="sitemap_sub_header">'. $manufactuerer_category_name . '</h2><ul>');
//foreach ($manufacturer_array as $manufacturer) {
//	if (isset($manufacturer_to_item_categories_array[$manufacturer['id']])) {
//		foreach ($manufacturer_to_item_categories_array[$manufacturer['id']] as $data) {
//			print_items_by_manufacturer($manufacturer['id'], $data['structure_id']);
//		}
//	}
//}
//fwrite($fh, '</ul>');

//fwrite($fh, "</div>");

fclose($fh);
