<?php

class Controller {

	private $request 	= null;
	private $template 	= '';
	private $view 		= null;
	private $headerView = null;
	private $footerView = null;

	/**
	* Konstruktor, erstellet den Controller.
	*
	* @param Array $request Array aus $_GET & $_POST.
	*/
	public function __construct($request) {
		global $user;
		$this->view = new View();
		$this->headerView = new View();
		$this->footerView = new View();
		$this->request = $request;
		$this->template = !empty($request['action']) ? $request['action'] : 'default';

		if(isset($request['action']) && $request['action'] === "login" && isset($request['username'])) {
			$user->login($request['username'], $request['password']);
		}

		if(isset($request['action']) && $request['action'] === "logout") {
			$user->logout();
		}

		if(!isset($_SESSION['loggedIn']) || (isset($_SESSION['loggedIn']) && !$_SESSION['loggedIn'])) {
			$user->setLoggedIn(false);
		} else {
			$user->loginByUsername($_SESSION['username']);
		}
	}

	/**
	* Methode zum anzeigen des Contents.
	*
	* @return String Content der Applikation.
	*/
	public function display() {
		global $user;

		if($user->getLoggedIn()) {
			$innerView = new View();

			switch($this->template){
				case 'login':
					$innerView->setTemplate('login');
					$entryid = $this->request['id'];
					$entry = Model::getEntry($entryid);
					$innerView->assign('title', $entry['title']);
					$innerView->assign('content', $entry['content']);
					break;
				case 'label':
					$documentView 	= new View();
					$mailView		= new View();
					$callView		= new View();
					$contactView	= new View();
					$documentView->setTemplate('documentlist');
					$mailView->setTemplate('maillist');
					$callView->setTemplate('calllist');
					$contactView->setTemplate('contactlist');
					$callView->assign('calls', Call::getCallsByLabelID($this->request['labelID']));
					$innerView->setTemplate('label');
					$innerView->assign('label', Label::getLabelByID($this->request['labelID']));
					$documentView->assign('labelID', $this->request['labelID']);
					$documentView->assign('documents', Document::getDocumentsByLabelID($this->request['labelID']));
					$mailView->assign('labelID', $this->request['labelID']);
					$mailView->assign('mails', Mail::getMailsByLabelID($this->request['labelID']));
					$innerView->assign('documentlist', $documentView->loadTemplate());
					$innerView->assign('maillist', $mailView->loadTemplate());
					$innerView->assign('calllist', $callView->loadTemplate());
					$innerView->assign('contactlist', $contactView->loadTemplate());
					break;
				case 'manage-labels':
					$innerView->setTemplate('manage-labels');
					$innerView->assign('labels', Label::getAllLabels());
					break;
				case 'manage-label':
					$innerView->setTemplate('manage-label');
					$innerView->assign('label', Label::getLabelByID($this->request['labelID']));
					$innerView->assign('mailboxFolders', MailboxFolder::getMailboxFolderByLabelID($this->request['labelID']));
					$innerView->assign('user', $user);
					break;
				case 'settings':
					$innerView->setTemplate('settings');
					$innerView->assign('user', $user);
					$innerView->assign('mailboxes', Mailbox::getMailboxesByUserID($user->getUserID()));
					break;
				case 'default':
				default:
					$entries = Model::getEntries();
					$innerView->setTemplate('default');
			}

			$this->headerView->setTemplate('header');
			$this->footerView->setTemplate('footer');
			$this->view->setTemplate('matrix');
			$this->headerView->assign('title', 'atOfficeWeb');
			$this->headerView->assign('request', $this->request);
			$this->headerView->assign('labels', Label::getAllLabels());
			$this->headerView->assign('userID', $user->getUserID());
			$this->view->assign('blog_title', $this->headerView->loadTemplate());
			$this->view->assign('blog_footer', $this->footerView->loadTemplate());
			$this->view->assign('blog_content', $innerView->loadTemplate());
			return $this->view->loadTemplate();
		} else {
			$this->view->setTemplate('login');
			//$this->view->assign('blog_content', $innerView->loadTemplate());
			return $this->view->loadTemplate();
		}
	}
}

?>
