<?php

class template{

  protected $_oSmarty;
  private $list_module = array();
  private $list_css = array();
  private $list_js = array();
  private $list_menu = array();
  private $list_menu_title = array();

	public function __construct()
	{
		$lang = new lang();
		$this->_oSmarty = new Smarty();
		$this->_oSmarty->clear_compiled_tpl();
		$this->_oSmarty->template_dir = PATH_TPL.$this->userGetTemplate().'/template/';
		$this->_oSmarty->compile_dir = PATH_CACHE.'smarty/compiled/';
		$this->_oSmarty->cache_dir = PATH_CACHE.'smarty/cache/';
		$this->_oSmarty->config_dir = PATH_LANG.$lang->userGetLang().'/';
		$this->_oSmarty->load_filter('output','trimwhitespace');
		$this->_oSmarty->caching = false;
		$this->_oSmarty->assign('HOST', TPL_HOST);
		$this->_oSmarty->assign('IMAGE', 'image/');
		$this->_oSmarty->assign('TPL_IMAGE', 'theme/'.$this->userGetTemplate().'/image/');
		$this->_oSmarty->assign('JAVASCRIPT', 'javascript/');
		$this->_oSmarty->assign('TPL_JAVASCRIPT', 'themes/'.$this->userGetTemplate().'/javascript/');
		$this->_oSmarty->assign('TPL_STYLE', 'themes/'.$this->userGetTemplate().'/style/');
		$this->_oSmarty->assign('STYLE', 'style/');
		$this->_oSmarty->assign('CHARSET', WEBSITE_CHARSET);
		$this->_oSmarty->assign('USER_INFOS', $_SESSION['user']->information_user);
		$this->loadModifiers();
	} // End of __construct

	public function addMenu( $title, $list_menu )
	{
		$lang = new lang();
		if ( !is_array($list_menu) ) throw new myException('list_menu is not an array');
		if ( empty($title) ) throw new myException('The title of the new menu can\'t be empty');
		if ( count($this->list_menu) != count($this->list_menu_title) ) throw new myException('Arrays white information is not equal');
		foreach ( $list_menu as $key => $value ) 
		{
			$list_menu[$key] = $lang->getWordFromLangFile($value);
		}
		$this->list_menu[] = $list_menu;
		$this->list_menu_title[] = $lang->getWordFromLangFile($title);
		return TRUE;

	}
	
	
	public function getWords($words)
	{
		$this->_oSmarty->assign($words);
		return TRUE;
	} // End of getWords
	
	public function loadCSS($css_files)
	{
		if ( is_array($css_files) ) {
			foreach($css_files as $key => $value)
			{
				if ( in_array($value, $this->list_css ) ) throw new myException("CSS file '$value' is already loaded");
				$this->list_css[] = $value;
			}
			return TRUE;
		}
		if ( in_array($css_files, $this->list_css ) ) throw new myException("CSS file '$css_files' is already loaded");
		$this->list_css[] = $css_files;
		return TRUE;
	}
	
	public function loadJS($js_files)
	{
		if ( is_array($js_files) ) {
			foreach($js_files as $key => $value)
			{
				if ( in_array($value, $this->list_js ) ) throw new myException("JS file '$value' is already loaded");
				$this->list_js[] = $value;
			}
			return TRUE;
		}
		if ( in_array($js_files, $this->list_js ) ) throw new myException("JS file '$css_files' is already loaded");
		$this->list_js[] = $js_files;
		return TRUE;
	}
	
	public function loadTemplate($module)
	{
		if ( in_array($module, $this->list_module) ) {
			throw new myException("Module '$module' is already loaded");
		}
		$this->list_module[] = $module;	
		return TRUE;		
	} // End of loadTemplate
	
	
	public function parseTemplate()
	{
		if ( is_null($this->list_module) ) {
			return FALSE;
		}
		$this->_oSmarty->assign('css_files', $this->list_css);
		$this->_oSmarty->assign('js_files', $this->list_js);
		foreach( $this->list_module as $key => $value ) 
		{
			if ( !$this->_oSmarty->template_exists($value.'.tpl') ) {
				throw new myException("Template file $value is not found");
			}
		}
		$this->_oSmarty->assign('list_menu_title', $this->list_menu_title);
		$this->_oSmarty->assign('list_menu', $this->list_menu);
		foreach( $this->list_module as $key => $value ) 
		{
			$this->_oSmarty->display($value.'.tpl');
		}
	} // End of parseTemplate
	
	
	public function userGetTemplate()
	{
		if ( !TEMPLATE_USE_OTHERS ) {
			return TEMPLATE_DEFAULT;
		}
		if( !$_SESSION['user']->user_not_logued ) {
			if( !$template = $_SESSION['user']->userGetTemplate() ) {
				throw new myException('The user is connected but getUserTemplate has returned false');
			}
			$template_allowed = unserialize(TEMPLATE_ALLOWED);
			if ( !in_array($template, $template_allowed) ) {
				throw new myException('Template of the user is not allowed in configuration file');
			}
		} else {
			$template = TEMPLATE_DEFAULT;
		}
		return $template;
	} // End of getTemplate
	
	/**
	 * @brief assign var when the right is autorised
	 * @return NULL
	*/
	public function userSetRightsDisplay()
	{
		
		if ( !is_null($_SESSION['user']->information_user) ) {
			$right = $_SESSION['user']->information_user->lvl;
			if ( $right >= LEVEL_REGISTER ) $_SESSION['template']->getWords(array('is_connected' => 1));
			if ( $right >= LEVEL_CUSTOMER ) $_SESSION['template']->getWords(array('is_client' => 1));
			if ( $right >= LEVEL_MODERATOR ) $_SESSION['template']->getWords(array('is_moderator' => 1));
			if ( $right >= LEVEL_SUPPORT ) $_SESSION['template']->getWords(array('is_support' => 1));
			if ( $right >= LEVEL_ADMIN ) $_SESSION['template']->getWords(array('is_admin' => 1));
			if ( $right >= LEVEL_CUSTOMER and isset($_SESSION['hosting']->information_hosting->id) ) {
				$_SESSION['template']->getWords(array('hosting_specified' => $_SESSION['hosting']->information_hosting->id));
				$_SESSION['template']->getWords(array('HOSTING_INFOS' => $_SESSION['hosting']->information_hosting));
			}
			if ( $right > LEVEL_ADMIN or $right < LEVEL_REGISTER ) throw new myException("Right is not correct '$right'");
		}
	} // End of displayMenus
	
	/**
	 * @brief Check template existance and syntax
	 * @param template -> Name of template
	 * @return True/False
	*/
	public function checkTemplateExistence($template)
	{
		$list_template = unserialize(TEMPLATE_ALLOWED);
		if ( in_array($template, $list_template) ) return true;
		else return false;
	}
	
	/**
	 * @brief Load modifier at the startup of the script
	 * @brief If necessary, create a modifier for a custom modification of strings
	 * @brief For the exemple : {#test#|ucfirst} or {$test.test|ucfirst}
	 * @return null
	 */
	 public function loadModifiers()
	 {
	 	//$this->_oSmarty->register_modifier('ucfirst', 'ucfirst');
	 }


} // End of class

?>
