<?php
/**
 * class _controller
 *
 * Description for class _controller
 *
 * @author:
*/
namespace area\presentation\controller;

use core\controller;
use core\database;
use core\url;
use core\user;

class _controller extends controller {
	protected $userId = null;
	protected $userName = null;

	public function __construct() {
		global $__routing, $__template;
		
		$userId = "";
		$userName = "";
		$presentationId = isset($__routing[1]["id"]) ? $__routing[1]["id"] : "";

		if(!isset($_SESSION["authPresentation"][$presentationId]) || !$this->validKey($_SESSION["authPresentation"][$presentationId]["userId"])) {
			url::redirectArea("");
		}

		$this->userId = $_SESSION["authPresentation"][$presentationId]["userId"];
		$this->userName = $_SESSION["authPresentation"][$presentationId]["userName"];

		$__template->setTemplateDir(__DIR__."/../views/");

		parent::__construct();
	}

	private function validKey($key) {
		global $__database;

		try {
			$query = $__database->prepare("SELECT * FROM `results_users` WHERE `user_id`=:user_id");
			$query->bindParam(":user_id", $key);
			$query->execute();
			if($record = $query->fetch()) {
				return true;
			}
		} catch(PDOException $error) {
			throw new databaseException($error->getMessage(), $error->getCode());
		}

		return false;
	}
}
?>