<?php

class FrontController extends FrontControllerCore
{
	public $confirmations = array();

	public function display()
	{
		Tools::safePostVars();

		// assign css_files and js_files at the very last time
		if ((Configuration::get('PS_CSS_THEME_CACHE') || Configuration::get('PS_JS_THEME_CACHE')) && is_writable(_PS_THEME_DIR_.'cache'))
		{
			// CSS compressor management
			if (Configuration::get('PS_CSS_THEME_CACHE'))
				$this->css_files = Media::cccCss($this->css_files);
			//JS compressor management
			if (Configuration::get('PS_JS_THEME_CACHE') && !$this->useMobileTheme())
				$this->js_files = Media::cccJs($this->js_files);
		}

		/* Override destroy all css */
		$this->css_files = array();

		/* Override Add real css */
		$this->css_files[_THEME_CSS_DIR_.'strapivarious.css?v='.filemtime($this->getThemeDir().'css/strapivarious.css')] = 'all';
		$this->css_files[_THEME_CSS_DIR_.'plugins.css?v='.filemtime($this->getThemeDir().'css/plugins.css')] = 'all';
		$this->css_files[_THEME_CSS_DIR_.'k2000.css?v='.filemtime($this->getThemeDir().'css/k2000.css')] = 'all';
		$this->css_files[_THEME_CSS_DIR_.'global.css?v='.filemtime($this->getThemeDir().'css/global.css')] = 'all';
		$this->css_files[_MODULE_DIR_.'antadissimulator/views/css/fonts.css'] = 'all';
		$this->css_files[_MODULE_DIR_.'antadissimulator/views/css/simulate.css'] = 'all';
		$this->css_files[_PS_BASE_URL_.__PS_BASE_URI__.'/js/jquery/ui/themes/base/jquery.ui.datepicker.css'] = 'all';
		$this->css_files[_MODULE_DIR_.'antadissimulator/views/colorpicker/jquery.simplecolorpicker.css'] = 'all';

		if (@filemtime($this->getThemeDir().'css/autoload/'))
		{
			foreach (scandir($this->getThemeDir().'css/autoload', 0) as $file)
			{
				if (preg_match('/^[^.].*\.css$/', $file))
				{
					$this->css_files[_THEME_CSS_DIR_.'autoload/'.$file.'?v='.filemtime($this->getThemeDir().'css/autoload/'.$file)] = 'all';
				}
			}
		}
		
		foreach($this->js_files as $key => $file)
		{
			if(strpos($file, _THEME_JS_DIR_) === false)
			{
				continue;
			}

			$this->js_files[$key] = $file.'?v='.filemtime($this->getThemeDir().'js/'.str_replace(_THEME_JS_DIR_, '', $file));
		}

		/****************************/

		$this->context->smarty->assign(array(
			'isMobile'=> $this->context->getMobileDevice(),
			'css_files' => $this->css_files,
			'js_files' => ($this->getLayout() && (bool)Configuration::get('PS_JS_DEFER')) ? array() : $this->js_files,
			'js_defer' => (bool)Configuration::get('PS_JS_DEFER'),
			'errors' => $this->errors,
			'confirmations' => $this->confirmations,
			'display_header' => $this->display_header,
			'display_footer' => $this->display_footer,
		));

		$layout = $this->getLayout();
		if ($layout)
		{
			if ($this->template)
				$template = $this->context->smarty->fetch($this->template);
			else // For retrocompatibility with 1.4 controller
			{
				ob_start();
				$this->displayContent();
				$template = ob_get_contents();
				ob_clean();

			}
			$template = $this->context->smarty->assign('template', $template);
			$this->smartyOutputContent($layout);
		}
		else
		{
			Tools::displayAsDeprecated('layout.tpl is missing in your theme directory');
			if ($this->display_header)
				$this->smartyOutputContent(_PS_THEME_DIR_.'header.tpl');

			if ($this->template)
				$this->smartyOutputContent($this->template);
			else // For retrocompatibility with 1.4 controller
				$this->displayContent();

			if ($this->display_footer)
				$this->smartyOutputContent(_PS_THEME_DIR_.'footer.tpl');
		}
		return true;
	}

}