home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Template / Flexy.php next >
Encoding:
PHP Script  |  2008-07-02  |  28.6 KB  |  823 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author:  Alan Knowles <alan@akbkhome.com>
  17. // | Original Author: Wolfram Kriesing <wolfram@kriesing.de>             |
  18. // +----------------------------------------------------------------------+
  19. //
  20.  
  21. /**
  22. *   @package    HTML_Template_Flexy
  23. */
  24. // prevent disaster when used with xdebug! 
  25. @ini_set('xdebug.max_nesting_level', 1000);
  26.  
  27. /*
  28. * Global variable - used to store active options when compiling a template.
  29. */
  30. $GLOBALS['_HTML_TEMPLATE_FLEXY'] = array(); 
  31.  
  32. // ERRORS:
  33.  
  34. /**
  35.  * Workaround for stupid depreciation of is_a...
  36.  */
  37. function HTML_Template_Flexy_is_a($obj, $class)  // which f***wit depreciated is_a....
  38. {
  39.     if (version_compare(phpversion(),"5","<")) {
  40.        return is_a($obj, $class);
  41.        
  42.     } 
  43.     $test=false; 
  44.     @eval("\$test = \$obj instanceof ".$class.";");
  45.     return $test;
  46.  
  47. }
  48.  
  49.  
  50.  
  51. define('HTML_TEMPLATE_FLEXY_ERROR_SYNTAX',-1);  // syntax error in template.
  52. define('HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS',-2);  // bad arguments to methods.
  53. define('HTML_TEMPLATE_FLEXY_ERROR_FILE',-2);  // file access problem
  54.  
  55. define('HTML_TEMPLATE_FLEXY_ERROR_RETURN',1);  // RETURN ERRORS
  56. define('HTML_TEMPLATE_FLEXY_ERROR_DIE',8);  // FATAL DEATH
  57. /**
  58. * A Flexible Template engine - based on simpletemplate  
  59. *
  60. * @abstract Long Description
  61. *  Have a look at the package description for details.
  62. *
  63. * usage: 
  64. * $template = new HTML_Template_Flexy($options);
  65. * $template->compiler('/name/of/template.html');
  66. * $data =new StdClass
  67. * $data->text = 'xxxx';
  68. * $template->outputObject($data,$elements)
  69. *
  70. * Notes:
  71. * $options can be blank if so, it is read from 
  72. * PEAR::getStaticProperty('HTML_Template_Flexy','options');
  73. *
  74. * the first argument to outputObject is an object (which could even be an 
  75. * associateve array cast to an object) - I normally send it the controller class.
  76. * the seconde argument '$elements' is an array of HTML_Template_Flexy_Elements
  77. * eg. array('name'=> new HTML_Template_Flexy_Element('',array('value'=>'fred blogs'));
  78. *
  79. *
  80. *
  81. * @version    $Id: Flexy.php,v 1.100 2008/02/27 05:56:32 alan_k Exp $
  82. */
  83.  
  84.  
  85.  
  86. class HTML_Template_Flexy  
  87. {
  88.  
  89.     /* 
  90.     *   @var    array   $options    the options for initializing the template class
  91.     */
  92.     var $options = array(   
  93.         'compileDir'    =>  '',         // where do you want to write to.. (defaults to session.save_path)
  94.         'templateDir'   =>  '',         // where are your templates
  95.         
  96.         // where the template comes from. ------------------------------------------
  97.         'multiSource'   => false,       // Allow same template to exist in multiple places
  98.                                         // So you can have user themes....
  99.         'templateDirOrder' => '',       // set to 'reverse' to assume that first template
  100.         
  101.          
  102.         'debug'         => false,       // prints a few messages
  103.         
  104.         
  105.         // compiling conditions ------------------------------------------
  106.         'compiler'      => 'Flexy',  // which compiler to use. (Flexy,Regex, Raw,Xipe)
  107.         'forceCompile'  =>  false,      // only suggested for debugging
  108.  
  109.         // regex Compiler       ------------------------------------------
  110.         'filters'       => array(),     // used by regex compiler.
  111.         
  112.         // standard Compiler    ------------------------------------------
  113.         'nonHTML'       => false,       // dont parse HTML tags (eg. email templates)
  114.         'allowPHP'      => false,       // allow PHP in template (use true=allow, 'delete' = remove it.)
  115.         
  116.         'flexyIgnore'   => 0,           // turn on/off the tag to element code
  117.         'numberFormat'  => ",2,'.',','",  // default number format  {xxx:n} format = eg. 1,200.00 
  118.         
  119.         'url_rewrite'   => '',          // url rewriting ability:
  120.                                         // eg. "images/:test1/images/,js/:test1/js"
  121.                                         // changes href="images/xxx" to href="test1/images/xxx"
  122.                                         // and src="js/xxx.js" to src="test1/js/xxx.js"
  123.                                         
  124.         'compileToString' => false,     // should the compiler return a string 
  125.                                         // rather than writing to a file.
  126.         'privates'      => false,       // allow access to _variables (eg. suido privates
  127.         'globals'       => false,       // allow access to _GET/_POST/_REQUEST/GLOBALS/_COOKIES/_SESSION
  128.  
  129.         'globalfunctions' => false,     // allow GLOBALS.date(#d/m/Y#) to have access to all PHP's methods
  130.                                         // warning dont use unless you trust the template authors
  131.                                         // exec() becomes exposed.
  132.          
  133.         // get text/transalation suppport ------------------------------------------
  134.         //  (flexy compiler only)
  135.         'locale'        => 'en',        // works with gettext or File_Gettext
  136.         'textdomain'    => '',          // for gettext emulation with File_Gettext
  137.                                         // eg. 'messages' (or you can use the template name.
  138.         'textdomainDir' => '',          // eg. /var/www/site.com/locale
  139.                                         // so the french po file is:
  140.                                         // /var/www/site.com/local/fr/LC_MESSAGE/{textdomain}.po
  141.         
  142.         'Translation2'  => false,       // to make Translation2 a provider.
  143.                                         // rather than gettext.
  144.                                         // set to:
  145.                                         //  'Translation2' => array(
  146.                                         //         'driver' => 'dataobjectsimple',
  147.                                         //         'options' => array()
  148.                                         //  );
  149.                                         // or the slower way.. 
  150.                                         //   = as it requires loading the code..
  151.                                         //
  152.                                         //  'Translation2' => new Translation2('dataobjectsimple','')
  153.                                         
  154.       
  155.         'charset'       => 'ISO-8859-1',    // charset used with htmlspecialchars to render data.
  156.                                             // experimental
  157.         
  158.         // output options           ------------------------------------------
  159.         'strict'        => false,       // All elements in the template must be defined - 
  160.                                         // makes php E_NOTICE warnings appear when outputing template.
  161.                                         
  162.         'fatalError'    => HTML_TEMPLATE_FLEXY_ERROR_DIE,       // default behavior is to die on errors in template.
  163.         
  164.         'plugins'       => array(),     // load classes to be made available via the plugin method
  165.                                         // eg. = array('Savant') - loads the Savant methods.
  166.                                         // = array('MyClass_Plugins' => 'MyClass/Plugins.php')
  167.                                         //    Class, and where to include it from..
  168.     ); 
  169.     /**
  170.     * The compiled template filename (Full path)
  171.     *
  172.     * @var string
  173.     * @access public
  174.     */
  175.     var $compiledTemplate;
  176.     /**
  177.     * The source template filename (Full path)
  178.     *
  179.     * @var string
  180.     * @access public
  181.     */
  182.     
  183.     
  184.     var $currentTemplate;
  185.     
  186.     /**
  187.     * The getTextStrings Filename
  188.     *
  189.     * @var string
  190.     * @access public
  191.     */
  192.     var $getTextStringsFile;
  193.     /**
  194.     * The serialized elements array file.
  195.     *
  196.     * @var string
  197.     * @access public
  198.     */
  199.     var $elementsFile;
  200.     
  201.      
  202.     /**
  203.     * Array of HTML_elements which is displayed on the template
  204.     * 
  205.     * Technically it's private (eg. only the template uses it..)
  206.     * 
  207.     *
  208.     * @var array of  HTML_Template_Flexy_Elements
  209.     * @access private
  210.     */
  211.     var $elements = array();
  212.     /**
  213.     *   Constructor 
  214.     *
  215.     *   Initializes the Template engine, for each instance, accepts options or
  216.     *   reads from PEAR::getStaticProperty('HTML_Template_Flexy','options');
  217.     *
  218.     *   @access public
  219.     *   @param    array    $options (Optional)
  220.     */
  221.       
  222.     function HTML_Template_Flexy( $options=array() )
  223.     {
  224.         
  225.         $baseoptions = array();
  226.         if (class_exists('PEAR')) {
  227.             $baseoptions = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
  228.         }
  229.         if ($baseoptions ) {
  230.             foreach( $baseoptions as  $key=>$aOption)  {
  231.                 $this->options[$key] = $aOption;
  232.             }
  233.         }
  234.         
  235.         foreach( $options as $key=>$aOption)  {
  236.            $this->options[$key] = $aOption;
  237.         }
  238.         
  239.         $filters = $this->options['filters'];
  240.         if (is_string($filters)) {
  241.             $this->options['filters']= explode(',',$filters);
  242.         }
  243.         
  244.         if (is_string($this->options['templateDir'])) {
  245.             $this->options['templateDir'] = explode(PATH_SEPARATOR,$this->options['templateDir'] );
  246.         }
  247.          
  248.        
  249.     }
  250.  
  251.    
  252.       
  253.  
  254.  
  255.     /**
  256.     *   compile the template
  257.     *
  258.     *   @access     public
  259.     *   @version    01/12/03
  260.     *   @author     Wolfram Kriesing <wolfram@kriesing.de>
  261.     *   @param      string  $file   relative to the 'templateDir' which you set when calling the constructor
  262.     *   @return     boolean true on success. (or string, if compileToString) PEAR_Error on failure..
  263.     */
  264.     function compile( $file )
  265.     {
  266.         if (!$file) {
  267.             return $this->raiseError('HTML_Template_Flexy::compile no file selected',
  268.                 HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS,HTML_TEMPLATE_FLEXY_ERROR_DIE);
  269.         }
  270.         
  271.         if (!@$this->options['locale']) {
  272.             $this->options['locale']='en';
  273.         }
  274.         
  275.         
  276.         //Remove the slash if there is one in front, just to be safe.
  277.         $file = ltrim($file,DIRECTORY_SEPARATOR);
  278.         
  279.         
  280.         if (strpos($file,'#')) {
  281.             list($file,$this->options['output.block']) = explode('#', $file);
  282.         }
  283.         
  284.         $parts = array();
  285.         $tmplDirUsed = false;
  286.         
  287.         // PART A mulitlanguage support: ( part B is gettext support in the engine..) 
  288.         //    - user created language version of template.
  289.         //    - compile('abcdef.html') will check for compile('abcdef.en.html') 
  290.         //       (eg. when locale=en)
  291.         
  292.         $this->currentTemplate  = false;
  293.         
  294.         if (preg_match('/(.*)(\.[a-z]+)$/i',$file,$parts)) {
  295.             $newfile = $parts[1].'.'.$this->options['locale'] .$parts[2];
  296.             foreach ($this->options['templateDir'] as $tmplDir) {
  297.                 if (@!file_exists($tmplDir . DIRECTORY_SEPARATOR .$newfile)) {
  298.                     continue;
  299.                 }
  300.                 $file = $newfile;
  301.                 $this->currentTemplate = $tmplDir . DIRECTORY_SEPARATOR .$newfile;
  302.                 $tmplDirUsed = $tmplDir;
  303.             }
  304.         }
  305.         
  306.         // look in all the posible locations for the template directory..
  307.         if ($this->currentTemplate  === false) {
  308.             $dirs = array_unique($this->options['templateDir']);
  309.             if ($this->options['templateDirOrder'] == 'reverse') {
  310.                 $dirs = array_reverse($dirs);
  311.             }
  312.             foreach ($dirs as $tmplDir) {
  313.                 if (!@file_exists($tmplDir . DIRECTORY_SEPARATOR . $file))  {
  314.                     continue;
  315.                 }
  316.                  
  317.                     
  318.                 if (!$this->options['multiSource'] && ($this->currentTemplate  !== false)) {
  319.                     return $this->raiseError("You have more than one template Named {$file} in your paths, found in both".
  320.                         "<BR>{$this->currentTemplate }<BR>{$tmplDir}" . DIRECTORY_SEPARATOR . $file,  
  321.                         HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS , HTML_TEMPLATE_FLEXY_ERROR_DIE);
  322.                     
  323.                 }
  324.                 
  325.                 $this->currentTemplate = $tmplDir . DIRECTORY_SEPARATOR . $file;
  326.                 $tmplDirUsed = $tmplDir;
  327.             }
  328.         }
  329.         if ($this->currentTemplate === false)  {
  330.             // check if the compile dir has been created
  331.             return $this->raiseError("Could not find Template {$file} in any of the directories<br>" . 
  332.                 implode("<BR>",$this->options['templateDir']) ,
  333.                 HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS, HTML_TEMPLATE_FLEXY_ERROR_DIE);
  334.         }
  335.         
  336.         
  337.         // Savant compatible compiler 
  338.         
  339.         if ( is_string( $this->options['compiler'] ) && ($this->options['compiler'] == 'Raw')) {
  340.             $this->compiledTemplate = $this->currentTemplate;
  341.             $this->debug("Using Raw Compiler");
  342.             return true;
  343.         }
  344.         
  345.         
  346.         
  347.         
  348.         // now for the compile target 
  349.         
  350.         //If you are working with mulitple source folders and $options['multiSource'] is set
  351.         //the template folder will be:
  352.         // compiled_tempaltes/{templatedir_basename}_{md5_of_dir}/
  353.         
  354.         
  355.         $compileSuffix = ((count($this->options['templateDir']) > 1) && $this->options['multiSource']) ? 
  356.             DIRECTORY_SEPARATOR  .basename($tmplDirUsed) . '_' .md5($tmplDirUsed) : '';
  357.         
  358.         
  359.         $compileDest = @$this->options['compileDir'];
  360.         
  361.         $isTmp = false;
  362.         // Use a default compile directory if one has not been set. 
  363.         if (!@$compileDest) {
  364.             // Use session.save_path + 'compiled_templates_' + md5(of sourcedir)
  365.             $compileDest = ini_get('session.save_path') .  DIRECTORY_SEPARATOR . 'flexy_compiled_templates';
  366.             if (!file_exists($compileDest)) {
  367.                 require_once 'System.php';
  368.                 System::mkdir(array('-p',$compileDest));
  369.             }
  370.             $isTmp = true;
  371.         
  372.         }
  373.         
  374.          
  375.         
  376.         // we generally just keep the directory structure as the application uses it,
  377.         // so we dont get into conflict with names
  378.         // if we have multi sources we do md5 the basedir..
  379.         
  380.        
  381.         $base = $compileDest . $compileSuffix . DIRECTORY_SEPARATOR .$file;
  382.         $fullFile = $this->compiledTemplate    = $base .'.'.$this->options['locale'].'.php';
  383.         $this->getTextStringsFile  = $base .'.gettext.serial';
  384.         $this->elementsFile        = $base .'.elements.serial';
  385.         if (isset($this->options['output.block'])) {
  386.             $this->compiledTemplate    .= '#'.$this->options['output.block'];
  387.         }
  388.           
  389.         $recompile = false;
  390.         
  391.         $isuptodate = file_exists($this->compiledTemplate)   ?
  392.             (filemtime($this->currentTemplate) == filemtime( $this->compiledTemplate)) : 0;
  393.             
  394.         if( @$this->options['forceCompile'] || !$isuptodate ) {
  395.             $recompile = true;
  396.         } else {
  397.             $this->debug("File looks like it is uptodate.");
  398.             return true;
  399.         }
  400.         
  401.         
  402.         
  403.         
  404.         if( !@is_dir($compileDest) || !is_writeable($compileDest)) {
  405.             require_once 'System.php';
  406.             
  407.             System::mkdir(array('-p',$compileDest));
  408.         }
  409.         if( !@is_dir($compileDest) || !is_writeable($compileDest)) {
  410.             return $this->raiseError(   "can not write to 'compileDir', which is <b>'$compileDest'</b><br>".
  411.                             "Please give write and enter-rights to it",
  412.                             HTML_TEMPLATE_FLEXY_ERROR_FILE, HTML_TEMPLATE_FLEXY_ERROR_DIE);
  413.         }
  414.         
  415.         if (!file_exists(dirname($this->compiledTemplate))) {
  416.             require_once 'System.php';
  417.             System::mkdir(array('-p','-m', 0770, dirname($this->compiledTemplate)));
  418.         }
  419.          
  420.         // Compile the template in $file. 
  421.         
  422.         require_once 'HTML/Template/Flexy/Compiler.php';
  423.         $compiler = HTML_Template_Flexy_Compiler::factory($this->options);
  424.         $ret = $compiler->compile($this);
  425.         if (HTML_Template_Flexy_is_a($ret,'PEAR_Error')) {
  426.             return $this->raiseError('HTML_Template_Flexy fatal error:' .$ret->message,
  427.                 $ret->code,  HTML_TEMPLATE_FLEXY_ERROR_DIE);
  428.         }
  429.         return $ret;
  430.         
  431.         //return $this->$method();
  432.         
  433.     }
  434.  
  435.      /**
  436.     *  compiles all templates
  437.     *  Used for offline batch compilation (eg. if your server doesn't have write access to the filesystem).
  438.     *
  439.     *   @access     public
  440.     *   @author     Alan Knowles <alan@akbkhome.com>
  441.     *
  442.     */
  443.     function compileAll($dir = '',$regex='/.html$/')
  444.     {
  445.         
  446.         require_once 'HTML/Template/Flexy/Compiler.php';
  447.         $c = new HTML_Template_Flexy_Compiler;
  448.         $c->compileAll($this,$dir,$regex);
  449.     } 
  450.     
  451.     /**
  452.     *   Outputs an object as $t 
  453.     *
  454.     *   for example the using simpletags the object's variable $t->test
  455.     *   would map to {test}
  456.     *
  457.     *   @version    01/12/14
  458.     *   @access     public
  459.     *   @author     Alan Knowles
  460.     *   @param    object   to output  
  461.     *   @param    array  HTML_Template_Flexy_Elements (or any object that implements toHtml())
  462.     *   @return     none
  463.     */
  464.     
  465.     
  466.     function outputObject(&$t,$elements=array()) 
  467.     {
  468.         if (!is_array($elements)) {
  469.             return $this->raiseError(
  470.                 'second Argument to HTML_Template_Flexy::outputObject() was an '.gettype($elements) . ', not an array',
  471.                 HTML_TEMPLATE_FLEXY_ERROR_INVALIDARGS ,HTML_TEMPLATE_FLEXY_ERROR_DIE);
  472.         }
  473.         if (@$this->options['debug']) {
  474.             echo "output $this->compiledTemplate<BR>";
  475.         }
  476.   
  477.         // this may disappear later it's a Backwards Compatibility fudge to try 
  478.         // and deal with the first stupid design decision to not use a second argument
  479.         // to the method.
  480.        
  481.         if (count($this->elements) && !count($elements)) {
  482.             $elements = $this->elements;
  483.         }
  484.         // end depreciated code
  485.         
  486.         
  487.         $this->elements = $this->getElements();
  488.         
  489.         // Overlay values from $elements to $this->elements (which is created from the template)
  490.         // Remove keys with no corresponding value.
  491.         foreach($elements as $k=>$v) {
  492.             // Remove key-value pair from $this->elements if hasn't a value in $elements.
  493.             if (!$v) {
  494.                 unset($this->elements[$k]);
  495.             }
  496.             // Add key-value pair to $this->$elements if it's not there already.
  497.             if (!isset($this->elements[$k])) {
  498.                 $this->elements[$k] = $v;
  499.                 continue;
  500.             }
  501.             // Call the clever element merger - that understands form values and 
  502.             // how to display them...
  503.             $this->elements[$k] = $this->mergeElement($this->elements[$k] ,$v);
  504.         }
  505.         //echo '<PRE>'; print_r(array($elements,$this->elements));
  506.       
  507.         
  508.         // we use PHP's error handler to hide errors in the template.
  509.         // use $options['strict'] - if you want to force declaration of
  510.         // all variables in the template
  511.         
  512.         
  513.         $_error_reporting = false;
  514.         if (!$this->options['strict']) {
  515.             $_error_reporting = error_reporting(E_ALL ^ E_NOTICE);
  516.         }
  517.         if (!is_readable($this->compiledTemplate)) {
  518.               return $this->raiseError( "Could not open the template: <b>'{$this->compiledTemplate}'</b><BR>".
  519.                             "Please check the file permissions on the directory and file ",
  520.                             HTML_TEMPLATE_FLEXY_ERROR_FILE, HTML_TEMPLATE_FLEXY_ERROR_DIE);
  521.         }
  522.         
  523.         // are we using the assign api!
  524.         
  525.         if (isset($this->assign)) {
  526.             if (!$t) {
  527.                 $t = (object) $this->assign->variables;
  528.             }
  529.             extract($this->assign->variables);
  530.             foreach(array_keys($this->assign->references) as $_k) {
  531.                 $$_k = &$this->assign->references[$_k];
  532.             }
  533.         }
  534.         // used by Flexy Elements etc..
  535.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['options']  = $this->options;
  536.         
  537.         include($this->compiledTemplate);
  538.         
  539.         // Return the error handler to its previous state. 
  540.         
  541.         if ($_error_reporting !== false) {
  542.             error_reporting($_error_reporting);
  543.         }
  544.     }
  545.     /**
  546.     *   Outputs an object as $t, buffers the result and returns it.
  547.     *
  548.     *   See outputObject($t) for more details.
  549.     *
  550.     *   @version    01/12/14
  551.     *   @access     public
  552.     *   @author     Alan Knowles
  553.     *   @param      object object to output as $t
  554.     *   @return     string - result
  555.     */
  556.     function bufferedOutputObject(&$t,$elements=array()) 
  557.     {
  558.         ob_start();
  559.         $this->outputObject($t,$elements);
  560.         $data = ob_get_contents();
  561.         ob_end_clean();
  562.         return $data;
  563.     }
  564.     /**
  565.     * static version which does new, compile and output all in one go.
  566.     *
  567.     *   See outputObject($t) for more details.
  568.     *
  569.     *   @version    01/12/14
  570.     *   @access     public
  571.     *   @author     Alan Knowles
  572.     *   @param      object object to output as $t
  573.     *   @param      filename of template
  574.     *   @return     string - result
  575.     */
  576.     function &staticQuickTemplate($file,&$t) 
  577.     {
  578.         $template = new HTML_Template_Flexy;
  579.         $template->compile($file);
  580.         $template->outputObject($t);
  581.     }
  582.     
  583.     /**
  584.     *   if debugging is on, print the debug info to the screen
  585.     *
  586.     *   @access     public
  587.     *   @author     Alan Knowles <alan@akbkhome.com>
  588.     *   @param      string  $string       output to display
  589.     *   @return     none
  590.     */
  591.     function debug($string) 
  592.     {  
  593.         
  594.         
  595.         if (HTML_Template_Flexy_is_a($this,'HTML_Template_Flexy')) {
  596.             if (!$this->options['debug']) {
  597.                 return;
  598.             }
  599.         } else if (empty($GLOBALS['_HTML_TEMPLATE_FLEXY']['debug'])) {
  600.             return;
  601.         }
  602.         
  603.         echo "<PRE><B>FLEXY DEBUG:</B> $string</PRE>";
  604.         
  605.     }
  606.  
  607.    
  608.  
  609.     
  610.     /**
  611.      * A general Utility method that merges HTML_Template_Flexy_Elements
  612.      * Static method - no native debug avaiable..
  613.      *
  614.      * @param    HTML_Template_Flexy_Element   $original  (eg. from getElements())
  615.      * @param    HTML_Template_Flexy_Element   $new (with data to replace/merge)
  616.      * @return   HTML_Template_Flexy_Element   the combined/merged data.
  617.      * @static
  618.      * @access   public
  619.      */
  620.      
  621.     function mergeElement($original,$new)
  622.     {
  623.      
  624.         // no original - return new
  625.         if (!$original) {
  626.             return $new;
  627.         }
  628.         // no new - return original
  629.         if (!$new) {
  630.             return $original;
  631.         }
  632.         // If the properties of $original differ from those of $new and 
  633.         // they are set on $new, set them to $new's. Otherwise leave them 
  634.         // as they are.
  635.  
  636.         if ($new->tag && ($new->tag != $original->tag)) {
  637.             $original->tag = $new->tag;
  638.         }
  639.         
  640.         if ($new->override !== false) {
  641.             $original->override = $new->override;
  642.         }
  643.         
  644.         if (count($new->children)) {
  645.             //echo "<PRE> COPY CHILDREN"; print_r($from->children);
  646.             $original->children = $new->children;
  647.         }
  648.         
  649.         if (is_array($new->attributes)) {
  650.         
  651.             foreach ($new->attributes as $key => $value) {
  652.                 $original->attributes[$key] = $value;
  653.             }
  654.         }
  655.         // originals never have prefixes or suffixes..
  656.         $original->prefix = $new->prefix;
  657.         $original->suffix = $new->suffix;  
  658.  
  659.         if ($new->value !== null) {
  660.             $original->setValue($new->value);
  661.         } 
  662.        
  663.         return $original;
  664.         
  665.     }  
  666.      
  667.      
  668.     /**
  669.     * Get an array of elements from the template
  670.     *
  671.     * All <form> elements (eg. <input><textarea) etc.) and anything marked as 
  672.     * dynamic  (eg. flexy:dynamic="yes") are converted in to elements
  673.     * (simliar to XML_Tree_Node) 
  674.     * you can use this to build the default $elements array that is used by
  675.     * outputObject() - or just create them and they will be overlayed when you
  676.     * run outputObject()
  677.     *
  678.     *
  679.     * @return   array   of HTML_Template_Flexy_Element sDescription
  680.     * @access   public
  681.     */
  682.     
  683.     function getElements() {
  684.     
  685.         if ($this->elementsFile && file_exists($this->elementsFile)) {
  686.             require_once 'HTML/Template/Flexy/Element.php';
  687.             return unserialize(file_get_contents($this->elementsFile));
  688.         }
  689.         return array();
  690.     }
  691.     
  692.     
  693.     /**
  694.     * Lazy loading of PEAR, and the error handler..
  695.     * This should load HTML_Template_Flexy_Error really..
  696.     * 
  697.     * @param   string message
  698.     * @param   int      error type.
  699.     * @param   int      an equivalant to pear error return|die etc.
  700.     *
  701.     * @return   object      pear error.
  702.     * @access   public
  703.     */
  704.   
  705.     
  706.     function raiseError($message, $type = null, $fatal = HTML_TEMPLATE_FLEXY_ERROR_RETURN ) 
  707.     {
  708.         HTML_Template_Flexy::debug("<B>HTML_Template_Flexy::raiseError</B>$message");
  709.         require_once 'PEAR.php';
  710.         if (HTML_Template_Flexy_is_a($this,'HTML_Template_Flexy') &&  ($fatal == HTML_TEMPLATE_FLEXY_ERROR_DIE)) {
  711.             // rewrite DIE!
  712.             return PEAR::raiseError($message, $type, $this->options['fatalError']);
  713.         }
  714.         if (isset($GLOBALS['_HTML_TEMPLATE_FLEXY']['fatalError']) &&  ($fatal == HTML_TEMPLATE_FLEXY_ERROR_DIE)) {
  715.             
  716.             return PEAR::raiseError($message, $type,$GLOBALS['_HTML_TEMPLATE_FLEXY']['fatalError']);
  717.         }
  718.         return PEAR::raiseError($message, $type, $fatal);
  719.     }
  720.  
  721.  
  722.     /**
  723.     * 
  724.     * Assign API - 
  725.     * 
  726.     * read the docs on HTML_Template_Flexy_Assign::assign()
  727.     *
  728.     * @param   varargs ....
  729.     * 
  730.     *
  731.     * @return   mixed    PEAR_Error or true?
  732.     * @access   public
  733.     * @see  HTML_Template_Flexy_Assign::assign()
  734.     * @status alpha
  735.     */
  736.   
  737.     function setData() {
  738.         require_once 'HTML/Template/Flexy/Assign.php';
  739.         // load assigner..
  740.         if (!isset($this->assign)) {
  741.             $this->assign = new HTML_Template_Flexy_Assign;
  742.         }
  743.         return $this->assign->assign(func_get_args());
  744.     }
  745.     /**
  746.     * 
  747.     * Assign API - by Reference
  748.     * 
  749.     * read the docs on HTML_Template_Flexy_Assign::assign()
  750.     *
  751.     * @param  key  string
  752.     * @param  value mixed
  753.     * 
  754.     * @return   mixed    PEAR_Error or true?
  755.     * @access   public
  756.     * @see  HTML_Template_Flexy_Assign::assign()
  757.     * @status alpha
  758.     */
  759.         
  760.     function setDataByRef($k,&$v) {
  761.         require_once 'HTML/Template/Flexy/Assign.php';
  762.         // load assigner..
  763.         if (!isset($this->assign)) {
  764.             $this->assign = new HTML_Template_Flexy_Assign;
  765.         }
  766.         $this->assign->assignRef($k,$v);
  767.     } 
  768.     /**
  769.     * 
  770.     * Plugin (used by templates as $this->plugin(...) or {this.plugin(#...#,#....#)}
  771.     * 
  772.     * read the docs on HTML_Template_Flexy_Plugin()
  773.     *
  774.     * @param  varargs ....
  775.     * 
  776.     * @return   mixed    PEAR_Error or true?
  777.     * @access   public
  778.     * @see  HTML_Template_Flexy_Plugin
  779.     * @status alpha
  780.     */
  781.     function plugin() {
  782.         require_once 'HTML/Template/Flexy/Plugin.php';
  783.         // load pluginManager.
  784.         if (!isset($this->plugin)) {
  785.             $this->plugin = new HTML_Template_Flexy_Plugin;
  786.             $this->plugin->flexy = &$this;
  787.         }
  788.         return $this->plugin->call(func_get_args());
  789.     } 
  790.     /**
  791.     * 
  792.     * output / display ? - outputs an object, without copy by references..
  793.     * 
  794.     * @param  optional mixed object to output
  795.     * 
  796.     * @return   mixed    PEAR_Error or true?
  797.     * @access   public
  798.     * @see  HTML_Template_Flexy::ouptutObject
  799.     * @status alpha
  800.     */
  801.     function output($object = false) 
  802.     {
  803.         return $this->outputObject($object);
  804.     }
  805.     
  806.     /**
  807.     * 
  808.     * render the template with data..
  809.     * 
  810.     * @param  optional mixed object to output
  811.     * 
  812.     * @return   mixed    PEAR_Error or true?
  813.     * @access   public
  814.     * @see  HTML_Template_Flexy::ouptutObject
  815.     * @status alpha
  816.     */
  817.     function toString($object = false) 
  818.     {
  819.         return $this->bufferedOutputObject($object);
  820.     }
  821.     
  822. }