<?php

/**
 * class controller
 *
 * Description for class controller
 *
 * @author:
*/
namespace core;

class controller {
	public $user_id = null;
	public $controller = null;
	public $action = null;

	public function __construct() {
		$this->user_id = user::identifier();
	}

	public function defaults($response_get) {
		$this->setVariable("response_get", $response_get);
	}

	protected function setVariable($name, $value) {
		global $__template;

		$__template->setVariable($name, $value);
	}

	protected function view($model = null, $view = null, $layout = "layout") {
		global $__template;

		if(empty($view)) {
			$view = $this->action;
		}

		$__template->setVariable("_MODEL", $model);
		$__template->setVariable("_FLASH_MESSAGES", flashMessages::show());
		$__template->show("_shared/{$layout}.tpl", "{$this->controller}/{$view}.tpl");
		exit;
	}

	protected function viewDialog($model = null, $view = null, $layout = "layout") {
		global $__template;

		if(empty($view)) {
			$view = $this->action;
		}

		$__template->setVariable("_MODEL", $model);
		$__template->setVariable("_FLASH_MESSAGES", flashMessages::show());
		
		$body = $__template->render("_shared/{$layout}.tpl", "{$this->controller}/{$view}.tpl");
		$captures = $__template->getCaptures();

		$footer = isset($captures["footer"]) ? $captures["footer"] : "";

		response::json(array("body" => $body, "footer" => $footer));
	}

	protected function viewError($view, $data = null, $layout = "layoutError") {
		global $__template;

		switch($view) {
			case "404":
				header("HTTP/1.0 404 Not Found");
				break;
		}

		$__template->setVariable("_DATA", $data);
		$__template->show("_shared/{$layout}.tpl", "_shared/error/{$view}.tpl");
		exit;
	}
}
?>