<?php

session_start();

$pathRoot = dirname(__DIR__) . '/';
define('TBL_PREFIX', 'portal');

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

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

/* Variablen aufbereiten */
$_GET['siteId'] = (isset ($_GET['siteId']) && !empty ($_GET['siteId']))
	? $_GET['siteId']
	: 'login';
$textsFromConfig = true;

/* Portal Zugriffsrechte prüfen */
include_once(PATH_INC . 'portal_rights.php');

/* ToDo: Daten für Content Tabellen auslagern */

/* Daten für Template aufbereiten */
if ((!is_numeric($_GET['siteId']) && $_GET['siteId'] !== 'login') || (isset ($_SESSION['sess_loginStatus']) && $_SESSION['sess_loginStatus'] === 'changePW'))
{
	$content = $db->query('SELECT * FROM ' . TBL_PREFIX . '_' . TBL_CONTENT . ' WHERE siteName = "' . $_GET['siteId'] . '";');
	if ($content->num_rows !== 0)
	{
		$content_data = $content->fetch_assoc();
		$table = explode('_', $content_data['siteContents'])[0];
		$id = explode('_', $content_data['siteContents'])[1];

		$form = $db->query('SELECT * FROM ' . TBL_PREFIX . '_' . TBL_CONTENT . '_' . $table . ' WHERE id = "' . $id . '";');
		$form_data = $form->fetch_assoc();
		$fields = explode(';', $form_data['content']);
		unset ($form_data['id']);
		unset ($form_data['content']);

		$data['content'] = $form_data;
		$data['content']['type'] = $table;
		$data['content']['elems'] = array();

		foreach ($fields as $field)
		{
			$tmp_field = explode(':', $field);
			$elem = explode('_', $tmp_field[0]);
			$elem_data = explode('_', $tmp_field[1]);
			$tmp_data = (isset($elem[1]))
				? array('tag_name' => $elem[0], 'type' => $elem[1])
				: array('tag_name' => $elem[0]);
			$tmp_data['name'] = $elem_data[0];
			$tmp_data['value'] = (isset ($elem_data[1]))
				? $elem_data[1]
				: '';
			$tmp_data['text'] = (isset ($lang[$table][$elem_data[0]]))
				? $lang[$table][$elem_data[0]]
				: '';
			$data['content']['elems'][] = $tmp_data;
		}

	}
}

elseif (isset ($_SESSION['sess_loginStatus']) && $_SESSION['sess_loginStatus'] === 'loggedIn')
{
	/* Angemeldet */
	$data['navi_special'] = fnc_getNavigationArray(TBL_PREFIX, true, $navPositions = array('special', 'login'), $lang['navi']);
	$navi_main = fnc_getNavigationArray(TBL_PREFIX, true, $navPositions = array('main'), $lang['navi']);
	$data['navi_main'] = ($_SESSION['sess_portalRightsType'] === 'ALL')
		? $navi_main
		: fnc_getPortalNavigationArray(TBL_PREFIX, $navi_main, $_SESSION['sess_portalRights']);
}
else
{
	$data['navi_special'] = fnc_getNavigationArray(TBL_PREFIX, true, $navPositions = array('special', 'logout'), $lang['navi']);
	$login = $db->query('SELECT * FROM ' . TBL_PREFIX . '_' . TBL_CONTENT . ' WHERE siteName = "login";');
	$login_data = $login->fetch_assoc();
	$table = explode('_', $login_data['siteContents'])[0];
	$id = explode('_', $login_data['siteContents'])[1];

	$form = $db->query('SELECT * FROM ' . TBL_PREFIX . '_' . TBL_CONTENT . '_' . $table . ' WHERE id = "' . $id . '";');
	$form_data = $form->fetch_assoc();
	$fields = explode(';', $form_data['content']);
	unset ($form_data['id']);
	unset ($form_data['content']);

	$data['login'] = $form_data;
	$data['login']['elems'] = array();
	foreach ($fields as $field)
	{
		$tmp_field = explode(':', $field);
		$elem = explode('_', $tmp_field[0]);
		$elem_data = explode('_', $tmp_field[1]);
		$tmp_data = (isset($elem[1]))
			? array('tag_name' => $elem[0], 'type' => $elem[1])
			: array('tag_name' => $elem[0]);
		$tmp_data['name'] = $elem_data[0];
		$tmp_data['value'] = (isset ($elem_data[1]))
			? $elem_data[1]
			: '';
		$tmp_data['text'] = (isset ($lang[$table][$elem_data[0]]))
			? $lang[$table][$elem_data[0]]
			: '';
		$data['login']['elems'][] = $tmp_data;
	}
}

$data['headline_main'] = $lang['headline_main'];
$data['headline_site'] = $lang['headline'][$_GET['siteId']];
$data['keyvisual'] = (file_exists(PATH_IMG . $vC['portal']['layout'] . '/keyvisual_' . $_GET['siteId'] . '.jpg'))
	? PATH_IMG . $vC['portal']['layout'] . '/keyvisual_' . $_GET['siteId'] . '.jpg'
	: PATH_IMG . $vC['portal']['layout'] . '/keyvisual_0.jpg';

/* Template rendern */
$render = new viewClass ();
$render->setPrefix($vC['portal']['layout']);
$render->setPathTpl(PATH_PORTAL_TPL);
$render->setConfig($vC);
$render->setTemplate('portal');
$render->setSiteId($_GET['siteId']);
$render->setEditable(false);
$render->setData($data);
$render->setLang($lang);
if (isset ($arrError) && !empty ($arrError))
{
	/* ToDo: Fehlerbehandlung */
	$render->setErrors($arrError);
}
if (isset ($_POST) && !empty ($_POST))
{
	/* ToDo: POST Daten */
	$render->setFormdata($_POST);
}

$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;