<?php

session_start();

$pathRoot = __DIR__ . '/';

require_once($pathRoot . '.config/config_global.php');
require_once(PATH_CLS . 'rendering/viewClass.php');

/* Funktionen und Aktionen einbinden */
include_once(PATH_FNC . 'fnc_portal.php');

/* Variablen aufbereiten */
$editable = false;
$_GET['siteId'] = '20';
$_GET['navId'] = (isset ($_GET['navId']) && !empty ($_GET['navId']))
	? $_GET['navId']
	: '1';
$web_key = array_search($_GET['siteId'], $vC['portal']['website']['navId']);
$prefix = ($web_key !== false)
	? $vC['portal']['website']['prefix'][$web_key]
	: '';
$pathWay = ($prefix !== '')
	? fnc_createPathHome($prefix, $_GET['navId'])
	: array();

/* Template - Definitionen einbinden */
include_once(PATH_WEBSITE_TPL . $prefix . '/tpl_definitions.php');

/* Daten für Template aufbereiten */
$navComplete = fnc_getNavigationArray($prefix);
$arr_navs = fnc_getSingleNavigationArrays($prefix, $navComplete, $def, $pathWay);

$specialSite = fnc_getNavType($prefix, $_GET['navId']);

$navpointData = $db->query('SELECT * FROM ' . $prefix . '_' . TBL_NAVI . ' WHERE ' . $prefix . '_navId = ' . $_GET['navId'] . ';');
$navpoint = $navpointData->fetch_assoc();

if (!is_null($navpoint[$prefix . '_keyvisual']))
{
	/* Keyvisual */
	$keyvisualData = $db->query('SELECT * FROM ' . $prefix . '_' . TBL_IMAGE . ' WHERE id = ' . $navpoint[$prefix . '_keyvisual'] . ';');
	$keyvisual = $keyvisualData->fetch_assoc();
	$data['keyvisual'][] = array('id' => $keyvisual['id'], 'path' => PATH_MED . $prefix . '/' . $_GET['navId'] . '/', 'src' => $keyvisual['imgName'] . '.' . $keyvisual['imgExtension'], 'title' => $keyvisual['imgTitle']);
	$data['galery'][] = array('id' => $keyvisual['id'], 'path' => PATH_MED . $prefix . '/' . $_GET['navId'] . '/', 'src' => $keyvisual['imgName'] . '_orig.' . $keyvisual['imgExtension'], 'title' => $keyvisual['imgTitle']);

	/* Slider - Daten */
	if ($keyvisual['sliderContent'] !== '')
	{
		$sliders = explode(';', $keyvisual['sliderContent']);

		foreach ($sliders as $imageId)
		{
			$imageData = $db->query('SELECT * FROM ' . $prefix . '_' . TBL_IMAGE . ' WHERE id = ' . $imageId . ';');
			$image = $imageData->fetch_assoc();
			$data['keyvisual'][] = array('id' => $image['id'], 'path' => PATH_MED . $prefix . '/' . $_GET['navId'] . '/', 'src' => $image['imgName'] . '.' . $image['imgExtension'], 'title' => $image['imgTitle']);
			$data['galery'][] = array('id' => $image['id'], 'path' => PATH_MED . $prefix . '/' . $_GET['navId'] . '/', 'src' => $image['imgName'] . '_orig.' . $image['imgExtension'], 'title' => $image['imgTitle']);
		}

	}
}
else
{
	$data['keyvisual'][] = array('id' => null, 'path' => PATH_MED . $prefix . '/', 'navId' => '', 'src' => 'keyvisual_0.jpg', 'title' => null);
}

if ($specialSite !== false)
{
	if ($specialSite === 'uebersicht')
	{
		$contents = $db->query('SELECT * FROM ' . $prefix . '_' . TBL_NAVI . ' WHERE ' . $prefix . '_navStart = ' . $_GET['navId'] . ' ORDER BY ' . $prefix . '_navSort ASC;');

		while ($content = $contents->fetch_assoc())
		{
			$tmp_content['headline'] = $content[$prefix . '_navName'];
			$tmp_content['link'] = $content[$prefix . '_navTarget'];
			$tmp_special = fnc_getNavType($prefix, $content[$prefix . '_navId']);

			if ($tmp_special !== false)
			{
				$sub_contents = $db->query('SELECT * FROM ' . $prefix . '_' . $tmp_special . ' WHERE siteId = ' . $content[$prefix . '_navId'] . ';');
				if ($sub_contents === false)
				{
					continue;
				}
				$sub_content = $sub_contents->fetch_assoc();
				$sub_content['type'] = $tmp_special;
			}
			else
			{
				$sub_contents = $db->query('SELECT * FROM ' . $prefix . '_' . TBL_CONTENT . ' WHERE siteId = ' . $content[$prefix . '_navId'] . ';');
				if ($sub_contents === false)
				{
					continue;
				}
				$tmp_content = $sub_contents->fetch_assoc();
				if ($tmp_content['siteContents'] !== '')
				{
					$tmp_data['content']['pageContents'] = fnc_getSiteContents($tmp_content['siteContents'], $prefix);
					$sub_content = $tmp_data['content']['pageContents'];
					$sub_content['type'] = 'standard';
				}
			}
			$data['content']['uebersicht'][] = $sub_content;
		}
	}
	elseif ($specialSite === 'spielbericht')
	{
		$contents = $db->query('SELECT * FROM ' . $prefix . '_' . $specialSite . ' WHERE siteId = ' . $_GET['navId'] . ';');
		$content = $contents->fetch_assoc();

		$dateTime = DateTime::createFromFormat('Y-m-d', $content['datum']);
		$data['content']['spielbericht'] = $content;
	}
	elseif ($specialSite === 'turnier')
	{
		$contents = $db->query('SELECT * FROM ' . $prefix . '_' . $specialSite . ' WHERE siteId = ' . $_GET['navId'] . ';');
		$content = $contents->fetch_assoc();

		$dateTime = DateTime::createFromFormat('Y-m-d', $content['datum']);
		$data['content']['turnier'] = $content;
	}
	elseif ($specialSite === 'widget')
	{
		$contents = $db->query('SELECT * FROM ' . $prefix . '_' . $specialSite . ' WHERE siteId = ' . $_GET['navId'] . ';');
		if ($contents->num_rows !== 0)
		{
			$content = $contents->fetch_assoc();
			$data['content']['widget'] = array('id' => $content['id'], 'tpl' => $content['widget'], 'schluessel' => $vC['portal']['website'][$content['widget']][$web_key], 'wettbewerb' => $content['wettbewerb']);
		}
	}
}
else
{
	$contents = $db->query('SELECT * FROM ' . $prefix . '_content WHERE siteId = ' . $_GET['navId'] . ';');
	if ($contents->num_rows !== 0)
	{
		$content = $contents->fetch_assoc();
		if ($content['siteContents'] !== '')
		{
			$data['content']['pageContents'] = fnc_getSiteContents($content['siteContents'], $prefix);
			foreach ($data['content']['pageContents'] as $tmp_content)
			{
				if (isset($tmp_content['type']) && $tmp_content['type'] === 'textimage')
				{
					$data['galery'][] = array('id' => $tmp_content['content_image_id'], 'path' => PATH_MED . $prefix . '/' . $_GET['navId'] . '/', 'src' => $tmp_content['imgName'] . '_orig.' . $tmp_content['imgExtension'], 'title' => $tmp_content['imgTitle']);
				}
			}
		}
	}
}

foreach ($arr_navs as $key => $value)
{
	if ($value !== null)
	{
		$data[$def['navPostion'][$key]] = $value;
	}
}

/* Template rendern */
$render = new viewClass ();
$render->setConfig($vC);
$render->setPrefix($prefix);
$render->setPathTpl(PATH_WEBSITE_TPL);
$render->setLang($lang['website']);
$render->setTemplate('main');
if ($specialSite !== false)
{
	$render->setSpecialSite(true);
}
$render->setSiteId($_GET['navId']);
$render->setEditable($editable);
$render->setWebkey($_GET['siteId']);
$render->setData($data);
$render->setNavpath($pathWay);

$html = $render->output();
$html = str_replace(array('PHP_ROOT_PATH/', 'PHP_SITE_PATH/'), array(PATH_ROOT, PATH_SITE), $html);
// $html	= str_replace ( array ( "\r" , "\n" , "\t" ) , array ( '' , '' , '' ) , $html );
echo $html;