<?php
/**
 * ****************************************************************
 * Project:      *
 * Copyright:    Copyright (c) 2007
 * Company:      Thomas Niemann
 * ****************************************************************
 * Workfile:     BasicController.php
 * ****************************************************************
 * Description:
 *
 * ***************************************************************/
abstract class BasicController
{
	protected $postVars;
	protected $getVars;
	protected $currentUser;
	protected $smartyInstance;
	protected $configInstance;
	protected $messageInstance;
	protected $contentInstance;
	private   $headlineInstance;	
	protected $navigationInstance;
	protected $errorInstance;
	protected $manager = null;
	protected $error = null;
	protected $message = null;
	protected $info = null;
	protected $success = null;
	protected $navTop = null;
	protected $navRight = null;
	protected $templatePath = null;
	protected $template = null;
	protected $subTemplate = null;
	protected $page = null;
	/****************************************************************
	 * calls setPostGetVars
	 * default Constructor
	 ****************************************************************/
	public function __construct($_postVars, $_getVars)
	{
		//PGC setzen
		$this->setPostGetVars($_postVars, $_getVars);
		//language
		if (isset ($this->getVars['language']) && $this->getVars['language'] != $_SESSION['language'])
		{
			$_SESSION['language'] = $this->getVars['language'];
		}
		elseif (empty ($_SESSION['language']))
		{
			$_SESSION['language'] = 'DE';
		}
		//instanzen
		$this->setSmartyInstance(SmartyHelper :: getInstance());
		$this->setConfigInstance(ConfigLoader :: getInstance());
		$this->setMessageInstance(MessageLoader :: getInstance());
		$this->setErrorLogInstance(ErrorLogLoader :: getInstance());
		$this->setHeadlineInstance(HeadlineLoader :: getInstance());
		$this->setXajaxControllerInstance(XajaxController :: getInstance());
		
		//infotexte
		$this->smartyInstance->assign_by_ref('error', $this->error);
		$this->smartyInstance->assign_by_ref('success', $this->success);
		$this->smartyInstance->assign_by_ref('info', $this->info);
		//base url für rewrite der url
		$this->smartyInstance->assign('BASEURL', $this->configInstance->getConfigPath('baseurl'));		
		$this->smartyInstance->assign('DOMAINROOT', $this->configInstance->getConfigPath('domainRoot'));
		//urlPrefix
		$prefix = $this->configInstance->getConstant('urlPrefix');
		$preUrl = $_SESSION['language'] . '/' . $prefix;
		$this->smartyInstance->assign('URLPREFIX', $preUrl);
		$this->smartyInstance->assign('LANGUAGEURL', $prefix);
		//Smarty Variablen übergeben
		$this->smartyInstance->assign('PAGEIMAGE', $this->configInstance->getConfigPath('image'));
		$this->smartyInstance->assign('ICON', $this->configInstance->getConfigPath('icon'));
		$this->smartyInstance->assign('BUTTON', $this->configInstance->getConfigPath('button'));
		$this->smartyInstance->assign('BBCODEIMAGE', $this->configInstance->getConfigPath('bbcode'));
		$this->smartyInstance->assign('JAVASCRIPT', $this->configInstance->getConfigPath('javascript'));
		$this->smartyInstance->assign('CSS', $this->configInstance->getConfigPath('css'));
		
		//set Signatur
		$this->smartyInstance->assign_by_ref('OWNER', $this->configInstance->getConstant('owner'));
		$this->smartyInstance->assign_by_ref('VERSION', $this->configInstance->getConstant('versionsnr'));
		//set headlines
		$this->setHeadlines();
		//set templatepath
		$this->setTemplates();
		//captcha
		if($this->configInstance->getAppSettings('captchaDisplay') == 'on'){
			$this->smartyInstance->assign('CAPTCHA', true);
		}
	}
	/****************************************************************
	 * saves the post and get vars
	 ****************************************************************/
	private function setPostGetVars($_postVars, $_getVars)
	{
		$this->postVars = $_postVars;
		$this->getVars = $_getVars;
	}
	private function setTemplates()
	{
		if (!empty ($this->getVars['subaction']))
		{
			$this->templatePath = strtolower($this->getVars['nav']) . "/" . $this->getVars['action'] . "/";
			$this->template = strtolower($this->getVars['subaction']);
			if (!empty ($this->getVars['subaction2']) && $this->getVars['action'] != 'regeneration')
			{
				$this->template = $this->getVars['subaction2'];
				$this->templatePath = $this->templatePath . $this->getVars['subaction'] . "/";
			}
		}
		elseif (!empty ($this->getVars['action']))
		{
			$this->templatePath = strtolower($this->getVars['nav']) . "/";
			$this->template = strtolower($this->getVars['action']);
		}
		else
		{
			$this->templatePath = strtolower($this->getVars['nav']) . "/";
			$this->template = strtolower($this->getVars['nav']);
		}
	}
	private function setHeadlines()
	{
		//pageHeadline
		$pageHeadline 			= $this->headlineInstance->getMessage('headline_nav', strtolower($this->getVars['nav']));
		$pageTitle 				= $this->headlineInstance->getMetaInfo('headline_nav', strtolower($this->getVars['nav']),'title');
		$pageKeywords 			= $this->headlineInstance->getMetaInfo('headline_nav', strtolower($this->getVars['nav']),'keywords');
		$pageDescriptiont 		= $this->headlineInstance->getMetaInfo('headline_nav', strtolower($this->getVars['nav']), 'description');
		
		$this->smartyInstance->assign('PAGETITLE', $pageTitle);
		//metatag description
		$this->smartyInstance->assign('DESCRIPTION', $pageDescriptiont);
		$this->smartyInstance->assign('KEYWORDS', $pageKeywords);
		//contentHeadline
		$this->smartyInstance->assign('PAGEHEADLINE', $pageHeadline);
		//BasicHeadlne
		$this->smartyInstance->assign_by_ref('BASICHEADER', $this->configInstance->getConstant('basicHeader'));
	}
	/****************************************************************
	 * saves current user into session
	 ****************************************************************/
	public function setSmartyInstance(SmartyHelper $_value)
	{
		$this->smartyInstance = $_value;
	}
	/*****************************************************************
	 * sets configInstance
	 ****************************************************************/
	public function setConfigInstance(ConfigLoader $_value)
	{
		$this->configInstance = $_value;
	}
	/*****************************************************************
		 * sets smartyInstance
		 ****************************************************************/
	public function setXajaxControllerInstance(XajaxController $_value)
	{
		$this->xajaxControllerInstance = $_value;
	}
	/*****************************************************************
		 * sets configInstance
		 ****************************************************************/
	public function setNavigationInstance(NavigationLoader $_value)
	{
		$this->navigationInstance = $_value;
	}
	/*****************************************************************
	 * sets configInstance
	 ****************************************************************/
	public function setHeadlineInstance(HeadlineLoader $_value)
	{
		$this->headlineInstance = $_value;
	}
	/*****************************************************************
	 * sets messageInstance
	 ****************************************************************/
	public function setMessageInstance(MessageLoader $_value)
	{
		$this->messageInstance = $_value;
	}
	/*****************************************************************
	 * sets contentInstance
	 ****************************************************************/
	public function setContentInstance(ContentLoader $_value)
	{
		$this->contentInstance = $_value;
	}
	/*****************************************************************
	 * sets errorInstance
	 ****************************************************************/
	public function setErrorLogInstance(ErrorLogLoader $_value)
	{
		$this->errorInstance = $_value;
	}
	/****************************************************************
	* fallback for invalid situations
	****************************************************************/
	public function showInvalid()
	{
		if($this->currentUser instanceof UserModell){
			/*Zeitstempel des Abmeldens in die Datenbank schreiben*/
			$this->currentUser->setLastlogout(time());
			$userManager 	= new UserManager();
			$userManager->updateUserLogoutByID($this->currentUser);
			unset($this->currentUser);
		}	
		
		$this->smartyInstance->clear_assign('NAVMEMBER');
		$this->smartyInstance->clear_assign('currentUser');
		$_GET['action'] = null;
		$_GET['nav']	= 'Login';
		
		if(isset($_SESSION['loginerrror'])){		
			$this->smartyInstance->assign('error',$_SESSION['loginerrror']);	
		}
		header('Location: http://www.tablebooking24.com/DE/Bayern/Muenchen/Login/');				
		/*Session beenden*/
		session_unset();		
		session_destroy();		
		die;
	}
	public function cleanPost($_post)
	{
		$postArray = array ();
		foreach ($_post as $key => $value)
		{
			$postArray[$key] = Util :: cleanGPC($value);
		}
		return $postArray;
	}
	/****************************************************************
	* shows the template
	****************************************************************/
	abstract public function show();
	/****************************************************************
	* do some validation stuff to avoid security leaks
	****************************************************************/
	abstract public function isValid();
	/****************************************************************
	* shows the template with print optimization
	* overwrite this method in derived classes to change default show behavior
	****************************************************************/
	abstract public function showPrintable();
}
?>