<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

class layout extends Smarty {
	public $currentComGroupPath;
	public $allMetaTags;
	public $metaTagStyle;
	public $shopProducers;
	public $theme_style = '';
	public $theme_dir = '';
	public $maincontent = '';
	public $metaTagObject;
	public $base_object;

	function start($base_object) {
		$this->base_object = $base_object;

		// get domain and theme configuration
		include_once './core/domain.class.php';
		$domain_object = new Domain($this->base_object);
		$domain = $domain_object->get_domain_config($_SERVER['SERVER_NAME']);
		if (!$domain) {
			echo "Keine Konfiguration gefunden!";
			exit();
		}

		// get default domain config user language
		include_once './core/language.class.php';
		$language_object = new Language($base_object);
		if (!isset($_SESSION['user_lang'])) {
			$language = $language_object->load($domain->standard_language_id);
			$_SESSION['user_lang'] = $language['iso_639_1'];
		}
		$this->assign('i18n_config', isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '');
		$this->assign('user_lang', $_SESSION['user_lang']);

		include_once './core/shop_theme.class.php';
		$theme_object = new Shop_theme($this->base_object);
		$theme = $theme_object->get_by_id($domain->standard_theme_id);
		$this->assign('domain_config', $domain);
		$this->assign('theme_config', $theme);
		$this->assign('SERVER_NAME', $_SERVER['SERVER_NAME']);

		// get text for templates
		include_once './core/text_translation.class.php';
		$text_class = new Text_translation($this->base_object);
		$text = $text_class->get_text($_SESSION['user_lang']);
		$this->assign('template_text', $text);

		// set content charset
		header('content-type: text/html; charset=utf-8');

		$this->__construct();
		$this->compile_id   = SHOP_SYSTEM.'_'.$base_object->config->shopConfiguration['THEME'];
		$this->compile_dir  = ROOT_DIR.'/tmp/smarty_compile';
		$this->cache_dir	= ROOT_DIR.'/tmp/smarty_cache';

		$tpl_paths = array();
		$tpl_paths[] = ROOT_DIR.'/web/'.SHOP_SYSTEM.'/themes/'.$base_object->config->shopConfiguration['THEME'].'/templates';

		if ($theme->parent_theme) {
			$tpl_paths[] = ROOT_DIR.'/themes/'.$theme->parent_theme.'/templates';
		}
		$this->template_dir = $tpl_paths;

//		if ($theme->parent_theme) {
//			$this->template_dir = ROOT_DIR.'/themes/'.$theme->parent_theme.'/templates';
//		} else {
//			$this->template_dir = ROOT_DIR.'/web/'.SHOP_SYSTEM.'/themes/'.$base_object->config->shopConfiguration['THEME'].'/templates';
//		}

		//$smarty->caching	= true;
		$this->debugging	= false;

		$this->theme_dir = STANDARD_DIR.'web/'.SHOP_SYSTEM.'/themes/'.$base_object->config->shopConfiguration['THEME'];

		// important variables
		$this->assign('PARENT_THEME_DIR', STANDARD_DIR.'themes/'.$theme->parent_theme);
		$this->assign('FULL_PARENT_THEME_DIR', ROOT_DIR.STANDARD_DIR.'themes/'.$theme->parent_theme);
		$this->assign('THEME_DIR', $this->theme_dir);
		$this->assign('SHOP_DIR', STANDARD_DIR.'web/'.SHOP_SYSTEM.'/');
		$this->assign('SUB_DIR', STANDARD_DIR);
		$this->assign('currentURI', $_SERVER['REQUEST_URI']);
		$this->assign('system_configuration', $base_object->config->shopConfiguration);

		$currentSURI = preg_replace("@[\?|\&].*@", "", $_SERVER['REQUEST_URI']);
		$currentSURI = preg_replace("@Seite-[0-9]+/@si", "", $currentSURI);
		$this->assign('currentSURI', $currentSURI);

		$this->assign('servername', $_SERVER['SERVER_NAME']);

		if ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) {
			$this->assign('serverProtocol', 'https');
		} else {
			$this->assign('serverProtocol', 'http');
		}

		return;
	}

	function _fetch ($template) {
		$content = '';

		if (isset($this->base_object->config->shopConfiguration['customized_templates'][$template])) {
			// Save current tpl path
			$formerTplDir = $this->template_dir;

			// Change tpl path
			$this->template_dir = str_replace(array('//', '\\'), array('/', '/'), ROOT_DIR.'/web/'.SHOP_SYSTEM.'/themes/'.$this->base_object->config->shopConfiguration['THEME'].'/templates');
			$content = $this->fetch($template);

			// Restore tpl path
			$this->template_dir = $formerTplDir;
		} else {
			$content = $this->fetch($template);
		}

		return $content;
	}

	public function show_site() {
		$this->assign('maincontent', $this->maincontent);

		$this->assign('items_in_memory', isset($_COOKIE['article_memorize']));

		echo $this->_fetch('main.tpl');
	}

	public static function i18n_format_date($format, $date) {
		$date = mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4));
		return date($format, $date);
	}

	public static function i18n_format_price($price) {
		return $price.' EUR';
	}

}

?>