<?php
/*
EasyGUI XP is free software released under the terms of the following BSD license.

Copyright (c) 2005,2006 Gaetan Lauff (glauff@plansoft.de)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

- Neither the name of the developer nor the names of its contributors may 
be used to endorse or promote products derived from this software without 
specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * Template Representation in EasyGUI XP
 *
 * This class is used to represent a template in the EasyGUI System
 * As you see we have numerous properties to set. This class is only used by the framework
 * You never have to deal with this class, but if you have a basic knowledge about what happens
 * here EasyGUI will increase your application development to a whole new level
 * Please refer to the EasyGUI XP Developper Manual to get more information
 * on how to extend the framework for fit your needs
 * For more Information on how to extend the Framework with your own classes refer to the
 * excellent EasyGUI XP Developers Guide which you can find on http://www.plansoft.de
 *
 * Let me explain:
 * As the template is initialized the framework searches in the database for
 * a template description. If none is found default values are set.
 *
 * The TRANSFORMER describes the class which transforms the input to the output.
 * The ENGINE describes the class which replaces variables in templates
 * And finally the stream defines the way the output is sent to the client.
 *
 * By default the template transformer is the builtin ek_tpltransformer which transforms
 * EasyKat Screen Designer (eks, xml, xsd) Files to XHTML 1.0 Output. Quite ennough.
 * 
 * If you want to use the another transformer you can write your own and transform any
 * input format to any output format you'd like, without loosing the power of the framework.
 * Imagine: you write your own pdf-converter for a Database Query. 
 * So you can send PDF Files to the client instead of HTML.
 * 
 * The Same procedure can be applied on the template engine and the stream.
 * For each of these you have the possibility to write your own, or use others.
 * Perhaps you want to use Smarty as a template engine.
 * or you want to compress files or ...
 *
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @package template
 * @version $Id: class.ek_template.php 46 2006-03-02 11:54:20Z glauff $
 */
class EK_Template
{
	/**
	 * Filename of Template
	 * @var string
	 */
	var $tplFile;
	
	/**
	 * Path to Template
	 * @var string
	 */
	var $tplPath;
	
	/**
	 * Transformer for Template
	 * @var	string
	 */
	var $transformer;
	
	/**
	 * Template Engine
	 * @var	string
	 */
	var $engine;
	
	/**
	 * Output Stream
	 * Defaults to stdout
	 * @var string
	 */
	var $stream;
	
	/**
	 * Template
	 * @var string
	 */
	var $tpl;
	
	/**
	 * Output Filename
	 * @var	string
	 */
	var $outFile;

	/**
	 * Extension
	 * @var string
	 */
	var $tplType;
	
	/**
	 *  Compress Outfile
	 */
	var $compress;
	
	/**
	 * Template Values
	 *
	 * @var array
	 */
	var $values;
	
	/**
	 * Used Map for the Template
	 *
	 * @var string
	 */
	var $map;
	
	/**
	 * Constructor
	 *
	 * @param	string	$transformer	template transformer
	 * @param	string	$engine			template engine
	 * @param	string	$steam			template stream
	 *
	 * @access  private
	 */
	function EK_Template( $transformer = null, $engine = null, $stream = 'ek', $map = 'ek' )
	{
		$this->transformer 	= $transformer;
		$this->engine 		= $engine;
		$this->stream		= $stream;
		$this->map			= $map;
		
		$this->outFile		= null;
		
		$this->values		= array();
	}
	
	/**
	 *  A little Helper Function to get the template with replaced Values
	 *
	 */
	function fetch() 
	{
		foreach ( $this->values as $key => $value ) {
			$this->tpl = str_replace( "%" . $key . "%", $value, $this->tpl );
		}
		
		$values = array();
		
		preg_match_all('/\%([^\%\"]+)\%/U', $this->tpl, $values, PREG_PATTERN_ORDER);
		
		foreach( $values[1] as $id ) {
			if ( defined( $id ) ) {
				$this->tpl = str_replace( "%".$id."%", constant( $id ), $this->tpl );
			}		
		}
		
		return $this->tpl;
	} 
	
	/**
	 * Returns the Template Map
	 *
	 * @return string
	 */
	function getMap()
	{
		return $this->map;
	}
	/**
	 * Returns Template File Extension.
	 *
	 * Used to determine which transformer to use in default case ( xml, xsd )
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function getExtension()
	{
		if (!empty( $this->tplFile ) ) {
			$info = pathinfo( $this->tplFile );
		
			if (strtolower( $info['extension'] ) == 'xml' || strtolower( $info['extension'] ) == 'xsd' ) {
				return 'eks';
			}
			
			return $info['extension'];
		}
		
		return 'eks';
	}
	
	/**
	 * Returns the template object itself
	 *
	 * @return	object
	 *
	 * @access 	public
	 */
	function & getObject()
	{
		return $this;
	}
	
	/**
	 * Returns the name of the template file
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function getTplFile()
	{
		return $this->tplFile;
	}
	
	/**
	 * Returns the template path
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function getTplPath()
	{
		return $this->tplPath;
	}
	
	/**
	 * Returns the location of the template file
	 *
	 * @return 	string
	 *
	 * @access 	public
	 */
	function getTplLocation() 
	{
		return $this->getTplPath();
	}
	
	/**
	 * Returns the transformer name
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function getTransformer() 
	{
		return $this->transformer;
	}
	
	/**
	 * Returns the template engine name
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function getEngine()
	{
		return $this->engine;
	}
	
	/**
	 * Returns the stream type
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function getStream()
	{
		return $this->stream;
	}
	
	/**
	 * Returns the raw string representation of Template
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function & getTemplate()
	{
		return $this->tpl;
	}
	
	/**
	 * Returns the outfile
	 *
	 * @return	string
	 *
	 * @access 	public
	 */
	function getOutfile()
	{
		return $this->outFile;
	}
	
	/**
	 * Sets the template file
	 *
	 * @param	string
	 *
	 * @access 	public
	 */
	function setTplFile( $file )
	{
		$this->tplFile = $file;
	}
	
	/**
	 * Sets the template path
	 *
	 * @param	string
	 * @return 	void
	 *
	 * @access 	public
	 */
	function setTplPath ( $path )
	{
		$this->tplPath = $path;
	}
	
	/**
	 * Sets the transformer name
	 *
	 * @param	string	$transformer	Transformer Name
	 * @return 	void
	 *
	 * @access 	public
	 */
	function setTransformer( $transformer )
	{
		$this->transformer = $transformer;
	}
	
	/**
	 * Sets template engine
	 *
	 * @param	string	$engine	Engine Name
	 * @return 	void
	 *
	 * @access 	public
	 */
	function setEngine( $engine )
	{
		$this->engine = $engine;
	}
	
	/**
	 * Sets the stream type
	 * valid parameters are file, compress.zlib, ftp ... etc
	 * @see www.php.net/documentation for streams
	 *
	 * @param	string	$stream	Stream Type
	 * @return 	void
	 *
	 * @access 	public
	 */
	function setStream( $stream )
	{
		$this->stream = $stream;
	}
	
	/**
	 * Sets the string representation of the template.
	 *
	 * @param	string	$tpl	Template as string
	 *
	 * @access 	public
	 */
	function setTemplate( $tpl )
	{
		$this->tpl = $tpl;
	}
	
	/**
	 * Sets the output filename
	 *
	 * @param	string	$filename	Name of output file
	 *
	 * @access 	public
	 */
	function setDownloadfile( $filename, $compress = true )
	{
		$this->outFile 	= $filename;
		$this->compress = $compress;
		$this->setStream( 'download' );
	}
	
	/**
	 * Sets the output filename
	 *
	 * @param string $filename
	 */
	function setOutfile( $filename )
	{
		$this->outFile = $filename;
	}
	
	/**
	 * Sets a template value
	 *
	 * @param string $id
	 * @param mixed $val
	 */
	function setValue( $id , $val )
	{
		$this->values[$id] = $val;
	}
	
	/**
	 * Sets the Template Values
	 *
	 * @param array $values
	 */
	function setValues( $values )
	{
		foreach ( $values as $key => $value ) {
			$this->values[$key] = $value;
		}
	}
	
	/**
	 * Returns a template value if its set
	 *
	 * @param string $id
	 * @return mixed value if it's set, FALSE otherwise
	 */
	function getValue( $id )
	{
		if (isset($this->values[$id])) {
			return $this->values[$id];
		}
		
		return false;
	}
}
?>