<?php

// ochrana proti vstuput z venčí
defined('_EXEC') or die();

/**
 * Templating class
 */
class Template
{
	private $data;

	private $template 	= 'main';
	private $run 		= false;

	private static $instance;


	private function __construct()
	{
		$this->data = new SmartObject();
	}


	public static function getInstance()
	{
		if (self::$instance === NULL)
		{
			self::$instance = new self;
		}
		return self::$instance;
	}


	public static function setTemplateType($type)
	{
		$path = DIR_ROOT . DS . 'templates' . DS . 'template.' . $type . '.php';
		if (!file_exists($path))
		{
			throw new Exception('class Template:: Template not found (' . $path . ')');
		}
		$class = self::getInstance();
		$class->template = $type;
	}


	public static function start()
	{
		$class = self::getInstance();
		$class->run = true;
		ob_start();
		$class->registerTemplater();
	}


	public static function stop()
	{
		$class = self::getInstance();
		$class->run = false;
		$class->registerTemplater();
	}

	public static function isRunning()
	{
		$class = self::getInstance();
		return $class->run;
	}


	private function registerTemplater()
	{
		register_shutdown_function(function(){
			if (Template::isRunning())
			{
				Template::template();
			}
		});
	}

	private function unregisterTemplater()
	{
		register_shutdown_function(function(){});
	}


	public static function template()
	{
		$class 	= self::getInstance();
		$path 	= DIR_ROOT . DS . 'templates' . DS . 'template.' . $class->template . '.php';

		// load template
		ob_start();
		require($path);
		$tmp = ob_get_clean();

		$content = ob_get_clean();
		$class->set('content', $content);

		$data = $class->data->getData();
		if (!empty($data))
		{
			foreach ($data as $property => $value)
			{
				$property = '{' . $property . '}';
				$tmp = str_replace($property, $value, $tmp);
			}
		}

		$tmp = preg_replace('~{(.*?)}~', '', $tmp);
		echo $tmp;
	}


	/**
	 * nastaví vlastnosti objektu
	 *
	 * @param mixed $properties vlastnosti
	 */
	public function setProperties($properties)
	{
		$class = self::getInstance();
		return $class->data->setProperties($properties);
	}


	/**
	 * nastaví vlastnost parametru sobjektu a navrátí minulou hodnotu
	 *
	 * @param string 	$property 	vlastnost
	 * @param mixed 	$value    	hodnota vlastnosti
	 */
	public static function set($property, $value = null)
	{
		$class = self::getInstance();
		return $class->data->set($property, $value);
	}


	/**
	 * nastaví defaultní hodnotu vlastnosti, pokud není nastavena
	 *
	 * @param  string $property vlastnost
	 * @param  mixed $default  	defaultní hodnota vlastnosti
	 *
	 * @return mixed           	navrátí minulou hodnotu vlastnosti
	 */
	public static function def($property, $default = null)
	{
		$class = self::getInstance();
		return $class->data->def($property, $default);
	}


	/**
	 * získá hodnotu vlastnosti
	 *
	 * @param  string $property vlastnost
	 * @param  mixed $default  	standardní hodnota, pokud vlastnost není definována
	 *
	 * @return mixed           	buď hodnota vlastnosti nebo standardní hodnota
	 */
	public static function get($property, $default = null)
	{
		$class = self::getInstance();
		return $class->data->get($property, $default = null);
	}

	public static function addMessage($message, $type = 'info')
	{
		$class = self::getInstance();
		$msg = $class->get('messages');
		$msg .= '<div class="alert alert-' . $type . '">' . $message . '</div>';
		$class->set('messages', $msg);
	}
}