<?php

/* + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
 +                                                                   +
 * SimpleT                                                           *
 +                                                                   +
 * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */

require_once('HTML/Template/Simplet.php');

/**
 * Implementation of the interface for SimpleT
 *
 * @package SimpleTInterfaced
 * @link http://simplet.sourceforge.net/
 **/
class SimpleTInterfaced extends SimpleT_Binder implements Template_Interface
{
    
/**
     * To hold data to be used in template
     *
     * @access private
     * @var array
     */
    
private $_templateData;

    
/**
     * To hold a reference to the template
     *
     * @access private
     * @var ???
     */
    
private $_templateReference;

    
/**
     * Track whether any data was passed to the template
     *
     * @access private
     * @var boolean
     */
    
private $_freshData;

    
/**
     * Class constructor
     **/
    
function __construct()
    {
        require_once(
'PEAR.php');

        
parent::SimpleT_Binder();
        
$this->_templateData = array();
        
$this->_templateReference null;;
        
$this->_freshData false;
    }

    
/**
     * Open the template to be used.
     *
     * @access public
     * @param  object $template A SimpleT_ViewHandler instance
     * @return mixed  TRUE or a PEAR_Error object on error
     **/
    
public function openTemplate($template)
    {
        
// Type hinting can't be used here due to interface
        // declaration
        
if (!($template instanceof SimpleT_ViewHandler)) {
            return 
PEAR::raiseError('Argument 1 must be an object of class SimpleT_ViewHandler');
        }
        
$this->_templateReference $template;
        return 
true;
    }

    
/**
     * Set one or more value to be used by the template
     *
     * @access public
     * @param  mixed  $name Associative array or name/identifier of data
     * @param  mixed  $data Data associated identified by $name, when $name
     *                      is not an associative array.
     **/
    
public function setData($name$data null)
    {
        
$this->_freshData true;
        if (
is_array($name)) {
            
$this->_templateData array_merge($this->_templateData$name);
        } else {
            
$this->_templateData[$name] = $data;
        }
    }

    
/**
     * Set one value (by reference) to be used by the template
     *
     * @access public
     * @param  string $name Name/identifier of data
     * @param  mixed  $data Reference to data associated identified by $name
     **/
    
public function setDataByRef($name, &$data)
    {
        
$this->_freshData true;
        
$this->_templateData[$name] =& $data;
    }

    
/**
     * Clear all previously set data
     *
     * @access public
     **/
    
public function clearData()
    {
        
$this->_freshData false;
        
$this->_templateData = array();
    }

    
/**
     * Return the MIME type of the result generated by the template
     *
     * @access public
     * @return string MIME type
     **/
    
public function getMimeType() {
        return 
'text/html';
    }

    
/**
     * Output the result of the template
     *
     * @access public
     * @return mixed TRUE or PEAR_Error on error
     **/
    
public function output()
    {
        
$output $this->toString();
        if (
PEAR::isError($output)) {
            return 
$output;
        } else {
            echo 
$output;
        }
        return 
true;
    }

    
/**
     * Output the result of the template to a file
     *
     * @access public
     * @param  mixed  $filename Path to file or file resource
     * @return mixed  How many bytes where put or PEAR_Error object on error
     * @see fopen()
     **/
    
public function toFile($filename)
    {
        
$output $this->toString();
        if (
PEAR::isError($output)) {
            return 
$output;
        }

        if (
is_resource($filename)) {
            
$file $filename;

        } else {
            
$file = @fopen($filename'wb');
            if (!
$file) {
                return 
PEAR::raiseError('Couldn\'t open file to write template result to.');
            }
        }

        
$mgc get_magic_quotes_runtime();
        
set_magic_quotes_runtime(0);
        
$length = @fwrite($file$output);
        
set_magic_quotes_runtime($mgc);

        if (
$length === false) {
            
$return PEAR::raiseError('Couldn\'t write template result to file.');
        } else {
            
$return $length;
        }

        if (!
is_resource($filename)
            && !@
fclose($file)
            && 
is_integer($return)) {
            
$return PEAR::raiseError('Couldn\'t close file with template result.');
        }

        return 
$return;
    }

    
/**
     * Return the result of the template as a string
     *
     * @access public
     * @return mixed String or PEAR_Error object on error
     **/
    
public function toString()
    {
        if (
is_null($this->_templateReference)) {
            return 
PEAR::raiseError('You must call openTemplate() first.');
        }

        
// Don't regenerate the output of the template
        // if no new data was passed.
        
if ($this->_freshData) {
            
parent::setData($this->_templateData);
            
$this->setView($this->_templateReference);
            
$this->bind();
        }
        return 
$this->_templateReference->toString();
    }

// End [CLASS] SimpleTInterfaced


?>