Your IP : 216.73.216.201


Current Path : /home/balossw/www/temp/common/cms/includes/classes/
Upload File :
Current File : /home/balossw/www/temp/common/cms/includes/classes/template.class.php

<?php
/*---------------------------------------------------------------------------
		File name ............ template.class.php
		Author ............... Antoine Lemoine
		Creation date ........ 04.12.2007
		Modification date .... 13.06.2010
---------------------------------------------------------------------------*/


class Template
{
	var $path;
	var $filename;
	var $lang;
	var $model;
	var $lang_filename;
	var $lang_array;
	var $debug;

	function Template($filename=null, $lang_filename=null)
	{
		if (isset($filename))
		{
			if (!file_exists($filename))
			{
				trigger_error('Template() - file doesn\'t exist `'.strip_tags($filename).'`',E_USER_ERROR);
			}
		
			$fp = fopen($filename,'r');
			$size = filesize($filename);
			
			if ($size>0)
			{
				$this->model = fread($fp,filesize($filename));
			}
			else
			{
				$this->model = '';
			}
			fclose($fp);
			
			$infos = pathinfo($filename);
			$this->path = $infos['dirname'];
		}
		else
		{
			$this->model = '';
			$this->path = '';
		}

		$this->lang_filename = $lang_filename;
		$this->head = '';
		$this->_solvePaths();
		//$this->_traduct();
		$this->bloc_counter = 0;
	}

	function _eval($expression, $key, $data)
	{
		if ($this->debug) { echo '_eval('.$expression.','.$key.')'; }
		$match = preg_replace('#{([a-zA-Z0-9_.-]+)}#i','$data[$1]',$expression);
		@eval('$match='.$match.';');
		if ($this->debug) { echo ' = '.$match.'<br />'; }
		return $match;
	}

	function _parse($string, $begin_regex, $include_begin_delimiter, $end_regex, $include_end_delimiter, $callback, $key, $data)
	{
		$split_result = preg_split($begin_regex,$string,-1,PREG_SPLIT_DELIM_CAPTURE);
		
		for ($i=count($split_result)/2-1; $i>=0; $i--)
		{
			$new_string = ($include_begin_delimiter ? $split_result[2*$i] : '').$split_result[2*$i+1];
			$split_result2 = preg_split($end_regex,$new_string,-1,PREG_SPLIT_DELIM_CAPTURE);
			
			$bloc_content = array_shift($split_result2);
			$end_delimiter = array_shift($split_result2);
			
			$replacement = $this->$callback($bloc_content.($include_end_delimiter ? $end_delimiter : ''),$key,$data);
			
			if ($i>0)
			{
				$split_result[2*($i-1)+1] .= $replacement.implode('',$split_result2);
			}
		}
		
		return $split_result[0];
	}

	function _replaceAlternative($bloc, $key, $data)
	{
		$alternatives_contents = preg_split('#<!-- ?(?:if|else|elseif)(?: ?\(.*?\))? ?-->#si',$bloc,-1,PREG_SPLIT_NO_EMPTY);
		
		preg_match_all('#<!-- ?(if|elseif) ?\('.preg_quote($key).':(.*?)\) ?-->#si',$bloc,$result,PREG_SET_ORDER);

		$i = 0;
		foreach ($alternatives_contents as $alternative_content)
		{
			if (isset($result[$i]))
			{
				if ($this->_eval($result[$i][2],$key,$data)=='1')
				{
					return $alternative_content;
				}
			}
			else
			{
				return $alternative_content;
			}
			$i++;
		}
		return '';
	}

	function _populateBlocArray($bloc)
	{
		preg_match('#^<!-- ?bloc\((.*?)\) ?-->#',$bloc,$result);
		$bloc_id = $result[1];
		$this->bloc_array[$bloc_id] = preg_replace('#^<!--.*?-->(.*)$#s','$1',$bloc);
		return $bloc;
	}

	function _insertBloc($bloc, $key, $data)
	{
		$this->bloc_counter++;
		$bloc_content = preg_replace('#^<!-- ?bloc ?\(.*?\) ?-->(.*)<!-- ?endbloc ?\(.*?\) ?-->$#si','$1',$bloc);
		return '<!-- id('.$this->bloc_counter.') -->'.$this->_process($bloc_content,$key,$data).'<!-- endid('.$this->bloc_counter.') -->'.$bloc;
	}

	function insertBloc($bloc_name, $data, $id=null)
	{
		if (isset($id))
		{
			$split = preg_split('#(<!-- ?(?:end)?id ?\('.preg_quote($id).'\) ?-->)#si',$this->model,-1,PREG_SPLIT_DELIM_CAPTURE);
			$split[2] = $this->_parse(
				$split[2],
				'#(<!-- ?bloc ?\('.preg_quote($bloc_name).'\) ?-->)#',
				true,
				'#(<!-- ?endbloc ?\('.preg_quote($bloc_name).'\) ?-->)#',
				true,
				'_insertBloc',
				$bloc_name,
				$data
			);
			$this->model = implode($split);
		}
		else
		{
			$this->model = $this->_parse(
				$this->model,
				'#(<!-- ?bloc ?\('.preg_quote($bloc_name).'\) ?-->)#',
				true,
				'#(<!-- ?endbloc ?\('.preg_quote($bloc_name).'\) ?-->)#',
				true,
				'_insertBloc',
				$bloc_name,
				$data
			);
		}
		return $this->bloc_counter;
	}

	function callBloc($bloc_id, $key, $data)
	{
		$this->bloc_array = array();
		$bloc = $this->_parse($this->model,'#(<!-- ?bloc ?\(.*?\) ?-->)#',true,'#(<!-- ?endbloc ?\(.*?\) ?-->)#',false,'_populateBlocArray',$key,$data);
		if (isset($this->bloc_array[$bloc_id]))
		{
			return $this->bloc_array[$bloc_id];
		}
		else
		{
			return '';
		}
	}

	function _removeBloc($bloc)
	{
		return '';
	}

	function _removeBlocs()
	{
		$this->model = $this->_parse($this->model,'#(<!-- ?bloc ?\(.*?\) ?-->)#',false,'#(<!-- ?endbloc ?\(.*?\) ?-->)#',false,'_removeBloc',$key='',$data=array());
		$this->model = preg_replace('#<!-- ?(?:end)?id ?\(.*?\) ?-->#','',$this->model);
	}
	
	function _solvePaths()
	{
		$count = preg_match_all('#<!-- ?path ?\((.*?)\) ?-->#i',$this->model,$result,PREG_SET_ORDER);
		for ($i=0; $i<$count; $i++)
		{
			$this->model = str_replace($result[$i][0],$this->path.'/'.$result[$i][1],$this->model);
		}
		$count = preg_match_all('#<!-- ?common_path ?\((.*?)\) ?-->#i',$this->model,$result,PREG_SET_ORDER);
		for ($i=0; $i<$count; $i++)
		{
			$this->model = str_replace($result[$i][0],$global->path('common/'.$result[$i][1]),$this->model);
		}
	}

	function _parseLangFile()
	{
		$fp = fopen($this->lang_filename,'r');
		$content = fread($fp,filesize($this->lang_filename));
		fclose($fp);

		$lines = explode("\n",$content);
		if (!is_array($lines)) { $lines = array($lines); }
	
		$lang_array = array();
	
		for ($i=0; $i<count($lines); $i++)
		{
			$line = trim($lines[$i]);
			if ($line!='')
			{
				if (preg_match('/^([^=]+)=(.*)$/',$line,$result))
				{
					$lang_array[strtolower($result[1])] = $result[2];
				}
			}
		}
		$this->lang_array = $lang_array;
		return $lang_array;
	}

	/*
	function traduct($string=null)
	{
		if (isset($string))
		{
			if (isset($this->lang_array[$string]))
			{
				return $this->lang_array[$string];
			}
			return '*'.$string.'*';
		}
		else
		{
			$this->_traduct();
		}
	}

	function _traduct()
	{
		if (isset($this->lang_filename) && file_exists($this->lang_filename))
		{
			$lang_array = $this->_parseLangFile();
			if (isset($lang_array))
			{
				$this->process('lang',$lang_array);
			}
		}
	}
	*/

	function _head($bloc)
	{
		$this->head .= $bloc;
		return '';
	}

	function process($key, $data)
	{
		$this->model = $this->_process($this->model,$key,$data);
	}

	function _process($string, $key, $data)
	{
		$string = $this->_parse($string,'#(<!-- ?value ?\('.preg_quote($key).':)#',false,'#(\) ?-->)#',false,'_eval',$key,$data);
		$string = $this->_parse($string,'#(<!-- ?if ?\('.preg_quote($key).':.*?\) ?-->)#',true,'#(<!-- ?endif ?-->)#',false,'_replaceAlternative',$key,$data);
		return $string;
	}

	function appendTo(&$head, &$body)
	{
		$this->model = $this->_parse($this->model,'#(<!-- ?<head> ?-->)#',false,'#(<!-- ?</head> ?-->)#',false,'_head',null,array());
		$this->_removeBlocs();
		if (isset($head)) { $head .= $this->head; }
		$body .= $this->model;
	}

	function prependTo(&$head, &$body)
	{
		$this->model = $this->_parse($this->model,'#(<!-- ?<head> ?-->)#',false,'#(<!-- ?</head> ?-->)#',false,'_head',null,array());
		$this->_removeBlocs();
		if (isset($head)) { $head = $this->head.$head; }
		$body = $this->model.$body;
	}

	function toString()
	{
		$this->_removeBlocs();
		return $this->model;
	}
}

class TemplateManager
{
	var $root;
	var $lang;

	function TemplateManager($root, $lang)
	{
		if ($root!='' && $root{strlen($root)-1}!='/') { $root .= '/'; }
		$this->root = $root;
		$this->lang = $lang;
	}
	
	function createTemplate($filename, $path='')
	{
		global $global;
		if ($path!='' && $path{strlen($path)-1}!='/') { $path .= '/'; }
		return new Template($global->path('common/cms/media/'.$path.$filename),$global->path('common/cms/media/'.$path.'lang/'.$this->lang.'/'.$filename.'.'.$this->lang.'.lang'));
	}

	function createFreeTemplate($content)
	{
		$template = new Template();
		$template->model = $content;
		return $template;
	}
}

?>