<?php

session_start();

$pathRoot = dirname(dirname(dirname(__DIR__))) . '/';
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_FNC  . 'fnc_usermanagement.php' );
include_once(PATH_ACT . 'usermanagement_actions.php');

/* Variablen aufbereiten */
$_GET['siteId'] = '3';
$_GET['todoId'] = (isset ($_GET['todoId']) && !empty ($_GET['todoId']))
	? $_GET['todoId']
	: '1';
$_GET['filter'] = (isset ($_GET['filter']) && !empty ($_GET['filter']))
	? $_GET['filter']
	: '';
$_GET['orderBy'] = (isset ($_GET['orderBy']) && !empty ($_GET['orderBy']))
	? $_GET['orderBy']
	: 'userName';
$_GET['orderDir'] = (isset ($_GET['orderDir']) && !empty ($_GET['orderDir']))
	? $_GET['orderDir']
	: 'ASC';

$sqlOrder = ($_GET['orderBy'] === 'userName')
	? 'userLastname ' . $_GET['orderDir'] . ' , userFirstname ' . $_GET['orderDir']
	: $_GET['orderBy'] . ' ' . $_GET['orderDir'];

$dataParameter = array
(
	'1' => array('dataEditing' => 'userId')
);

$arr_todoIds = array
(
	'1' => 'overview',        /* Übersicht */
	'2' => 'create',        /* Benutzer anlegen */
	'3' => 'activate',        /* Benutzer aktivieren */
	'4' => 'deactivate',    /* Benutzer deaktivieren */
	'5' => 'edit',            /* Benutzer bearbeiten */
	'6' => 'delete',        /* Benutzer löschen */
	'7' => 'pwreset'        /* Passwort zurücksetzen */
);

$form_data = null;
$lang['usermanagement']['error'] = $lang['error'];


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

/* Daten für Template aufbereiten */
if ($_GET['todoId'] === '1')
{
	$content = $db->query('SELECT * FROM ' . TBL_USER . ' WHERE userDelete = "N" AND userId != "1" AND ( userFirstname LIKE "%' . $_GET['filter'] . '%" OR userLastname LIKE "%' . $_GET['filter'] . '%" ) ORDER BY ' . $sqlOrder . ';');
	$data['sub_content']['header']['filter'] = $_GET['filter'];
}
else
{
	if (in_array($_GET['todoId'], array('2', '5')))
	{
		$portalNav = fnc_getNavigationArray('portal', true, array('main'), $lang['navi']);
		foreach ($portalNav as $navStart => $navArray)
		{
			foreach ($navArray as $key => $value)
			{
				$websiteKey = array_search($value['portal_navId'], $vC['portal']['website']['navId']);

				if ($websiteKey !== false)
				{
					$portalNav[$navStart][$key]['portal_navName'] = $vC['portal']['website']['names'][$websiteKey];
				}
				else
				{
					$portalNav[$navStart][$key]['portal_navName'] = $lang['navi']['site_' . $value['portal_navId']];
				}

				if ($value['portal_navId'] === '1')
				{
					unset ($portalNav[$navStart][$key]);
				}
			}
		}
		$data['main'][$arr_todoIds[$_GET['todoId']]]['portal_nav'] = ($_SESSION['sess_portalRightsType'] === 'ALL')
			? $portalNav
			: fnc_getPortalNavigationArray('portal', $portalNav, $_SESSION['sess_portalRights']);


		if (!empty ($vC['portal']['website']['navId']))
		{
			foreach ($vC['portal']['website']['navId'] as $key => $navId)
			{
				$prefix = $vC['portal']['website']['prefix'][$key];
				$websiteNav = fnc_getNavigationArray($prefix, false);
				$data['main'][$arr_todoIds[$_GET['todoId']]]['website_nav'][$prefix] = array('data' => $websiteNav, 'key' => $navId);
			}
		}

		if (isset ($_POST) && !empty ($_POST))
		{
			$form_data = $_POST;
		}
	}

	if (!in_array($_GET['todoId'], array('2')))
	{
		$content = $db->query('SELECT * FROM ' . TBL_USER . ' WHERE userId = "' . $_GET['userId'] . '";');
	}
}

if (isset ($_POST) && count($_POST) > 0)
{
	$data['main'][$arr_todoIds[$_GET['todoId']]][] = $_POST;
}
elseif (isset ($content) && $content->num_rows !== 0)
{
	while ($tmp_content = $content->fetch_assoc())
	{
		$tmp_content['userCreateUser'] = fnc_getUsernameById($tmp_content['userCreateUser']);
		$tmp_content['userCreateTS'] = fnc_getTimeByTS($tmp_content['userCreateTS']);
		$tmp_content['userActiveUser'] = fnc_getUsernameById($tmp_content['userActiveUser']);
		$tmp_content['userActiveTS'] = fnc_getTimeByTS($tmp_content['userActiveTS']);
		$tmp_content['user_portalRightsType'] = $tmp_content['userPortalRightsType'];
		$tmp_content['user_portalRights'] = explode(';', $tmp_content['userPortalRights']);

		foreach ($vC['portal']['website']['prefix'] as $website)
		{
			$tmp_right = $db->query('SELECT * FROM ' . TBL_RIGHTS . ' WHERE userId = "' . $tmp_content['userId'] . '" AND rightsPrefix = "' . $website . '" LIMIT 1;');
			if ($tmp_right->num_rows === 0)
			{
				$tmp_content['user_' . $website . 'RightsType'] = 'DEF';
				$tmp_content['user_' . $website . 'Rights'] = '';
			}
			else
			{
				$right = $tmp_right->fetch_assoc();
				$tmp_content['user_' . $website . 'RightsType'] = $right['rightsType'];
				$tmp_content['user_' . $website . 'Rights'] = explode(';', $right['rights']);
			}
		}

		$data['main'][$arr_todoIds[$_GET['todoId']]][] = $tmp_content;
		$form_data = $tmp_content;
	}
}
else
{
	$data['main'][$arr_todoIds[$_GET['todoId']]][0] = array();
}

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

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