<?php

/* + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
 +                                                                   +
 * Savant                                                            *
 +                                                                   +
 * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */

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

/**
 * Implementation of the interface for Savant
 *
 * @package SavantInterfaced
 * @link http://www.phpsavant.com/
 **/
class SavantInterfaced extends Savant implements Template_Interface
{
    
/**
     * To hold a reference to the template
     *
     * @access private
     * @var string
     */
    
private $_templateReference;

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

        
parent::Savant();
        
$this->_templateReference null;
    }

    
/**
     * Open the template to be used.
     *
     * @access public
     * @param  string $template A template file name
     * @return mixed  TRUE or a PEAR_Error object on error
     **/
    
public function openTemplate($template)
    {
        
$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)
    {
        if (
is_array($name)) {
            
$this->assign($name);
        } else {
            
$this->assign($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->assignRef($name$data);
    }

    
/**
     * Clear all previously set data
     *
     * @access public
     **/
    
public function clearData()
    {
        
$this->_token_vars = array();
        
$this->_token_refs = 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()
    {
        
$return $this->display($this->_templateReference);
        if (
PEAR::isError($return)) {
            return 
$return;
        }
        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()
    {
        
$output $this->fetch($this->_templateReference);
        return 
$output;
    }

// End [CLASS] SavantInterfaced


?>