<?php
require_once('../system/configuration.php');
require(PATH_SYSTEM.'core.php');
try
{
	// Check if user is registered
	$_SESSION['user']->userCheckAccess(LEVEL_REGISTER); // <- Tout le enregistrés peuvent utiliser le support

	// prepare "Support" specific menu
	$support_menu=array();
	$support_menu['support.xhtml'] = 'mnu_tickets_list';
	$support_menu['support-create.xhtml'] = 'mnu_new_ticket';

	$support = new support();
	
	$action_is_valid=false;

	if ( isset($_GET['action']) ) {
		// If the user wants to create a new ticket
		if ( $_GET['action']  == 'create') {
			$action_is_valid=true;
			$template_main = 'support/create';
			// Getting list of hostings of the current user
			$hostings = $_SESSION['hosting']->userListHostings();
			$_SESSION['template']->getWords(array('hostings' => $hostings));
		// If the user wants to see the details of a ticket
		} elseif ( $_GET['action']  == 'show') {
			if ( !textVerification::verifID($_GET['id'])) throw new myException('Trying to see a ticket with an invalid id');
			$action_is_valid=true;
			$details = $support->getTicketDetails($_GET['id'], 0, 1);
			if (!is_null($details)) {
				$ticket = $details[0];
				$lang = new lang();
				$ticket->open_date = date($lang->userGetDateFormat().' '.$lang->userGetTimeFormat(), $ticket->open_date);
				$_SESSION['template']->getWords(array('ticket' => $ticket));
				$pages_count = $support->countTicketTotalPages( $_GET['id'] );
				// add "Reply" menu entry if allowed
				if ( $ticket->status == 'replied' ) {
					$_SESSION['template']->getWords(array('user_actions_allowed' => true));
				}
				$_SESSION['template']->getWords(array('pages_count' => $pages_count));
				$template_main = 'support/show';
			} else throw new myException('Ticket not found or not allowed to this user');
		} else throw new myException('Action not allowed or not recognized !');
	}
	
	// if no valid action was specified (or recognized)
	if (!$action_is_valid) {
		$pages_count=$support->userCountTotalPages();
		$_SESSION['template']->getWords(array('pages_count' => $pages_count));
		//if ( !is_null($tickets_list) ) {
		//    $_SESSION['template']->getWords(array('tickets' => $tickets_list));
		//}
		$template_main = 'support/list';
	}
	
	// Load templates
	$_SESSION['template']->loadTemplate('_contentbegin');
	$_SESSION['template']->loadTemplate($template_main);
	$_SESSION['template']->loadTemplate('_contentend');
	$_SESSION['template']->getWords(array('sql' => $_SESSION['database']->executed_req));
	// Add "Support" specific menu
	$_SESSION['template']->addMenu('support',$support_menu);
	// Parse templates
	$_SESSION['template']->parseTemplate();

} catch (myException $error) {
	$error->displayErrorMessage();
}
?>
