<?php

$siteId = 20;
$saisonStart = array(2013, 2012, 2011, 2010, 2009);
$bereichArray = array('Frauen', 'Herren');
$chronikIds = array(array(36, 42), array(48, 54));
$teams = array(array(1, 2), array(1, 2));
$teamNames = array(array('Verbandsliga', 'Bezirksliga'), array('Kreisliga A Alb', 'Reserve'));
$navPoints = array('Spielberichte', 'Tabelle', 'Mannschaft');
$navSpecial = array('Y', 'Y', 'N');
$navType = array('uebersicht', 'widget', null);


define('PATH_ROOT', '../');
include_once(PATH_ROOT . '.config/config_global.php');
include_once(PATH_FNC . 'fnc_portal.php');

$db = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);
$db->set_charset('utf8');

$yearOrder = 1;

foreach ($saisonStart as $startYear)
{
	$startDate = $startYear . '-08-01';
	$endDate = ($startYear + 1) . '-07-31';

	foreach ($bereichArray as $key => $bereich)
	{
		foreach ($teams[$key] as $teamKey => $nummer)
		{
			$berichte = $db->query('SELECT * FROM berichte WHERE Datum >= "' . $startDate . '" AND Datum <= "' . $endDate . '" AND Bereich = "' . $bereich . '" AND Nummer = "' . $nummer . '" ORDER BY Datum DESC;');
			$count = $berichte->num_rows;
			$i = 0;
			$teamName = $teamNames[$key][$teamKey];
			$chronikId = $chronikIds[$key][$teamKey];

			if ($count > 0)
			{
				$insertTeam = $db->query('INSERT INTO svjf_navi VALUES ( NULL , "' . $chronikId . '" , "' . $db->real_escape_string($teamName . ' ' . $startYear . '/' . ($startYear + 1)) . '" , "" , "' . mb_strtolower($teamName, 'UTF-8') . '_' . $startYear . '" , "Y" , "' . $yearOrder . '" , "N" , NULL );');
				$teamId = $db->insert_id;
				$updateTeam = $db->query('UPDATE svjf_navi SET svjf_navTarget = "index.php?siteId=' . $siteId . '&navId=' . $teamId . '" WHERE svjf_navId = "' . $teamId . '";');

				foreach ($navPoints as $navKey => $navPoint)
				{
					$insertAreas = $db->query('INSERT INTO svjf_navi VALUES ( NULL , "' . $teamId . '" , "' . $navPoint . '" , "" , "' . mb_strtolower($navPoint, 'UTF-8') . '" , "Y" , "' . ($navKey + 1) . '" , "' . $navSpecial[$navKey] . '" , "' . $navType[$navKey] . '" );');
					$newNavStart = $db->insert_id;
					$updateAreas = $db->query('UPDATE svjf_navi SET svjf_navTarget = "index.php?siteId=' . $siteId . '&navId=' . $newNavStart . '" WHERE svjf_navId = "' . $newNavStart . '";');

					if ($navPoint = 'Spielberichte')
					{
						while ($bericht = $berichte->fetch_assoc())
						{
							foreach ($bericht as $tmp_key => $value)
							{
								$bericht[$tmp_key] = $db->real_escape_string($value);
							}
							$insert = $db->query('INSERT INTO svjf_navi VALUES ( NULL , "' . $newNavStart . '" , "' . ($count - $i) . '. Spieltag" , "" , "spieltag_' . ($count - $i) . '" , "Y" , "' . ($i + 1) . '" , "Y" , "spielbericht" );');
							$navId = $db->insert_id;
							$update = $db->query('UPDATE svjf_navi SET svjf_navTarget = "index.php?siteId=' . $siteId . '&navId=' . $navId . '" WHERE svjf_navId = "' . $navId . '";');
							$insert = $db->query('INSERT INTO svjf_spielbericht VALUES ( "' . $navId . '" , "' . $bericht['Head'] . '" , "' . $bericht['Heim'] . '" , "' . $bericht['ErgH'] . '" , "' . $bericht['ErgG'] . '" , "' . $bericht['HalbH'] . '" , "' . $bericht['HalbG'] . '" , "' . $bericht['Gegner'] . '" , "' . $bericht['Text'] . '" , "' . $bericht['Bild'] . '" , "' . $bericht['Spieler'] . '" , "' . $bericht['Tore'] . '" , "' . $bericht['Besonderes'] . '" , "' . $bericht['Vorschau'] . '" , "' . $bericht['Datum'] . '" );');

							$i++;
						}
					}
					else
					{
						/* TODO: svjf_content und andere Parts schreiben */
					}
				}

			}
		}
	}

	$yearOrder++;
}