HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file Flexy.php

Documentation is available at Flexy.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 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. // | Authors: Alan Knowles <alan@akbkhome.com>                            |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Flexy.php,v 1.19 2005/07/14 00:30:39 alan_k Exp $
  20. //
  21. //  Base Compiler Class
  22. //  Standard 'Original Flavour' Flexy compiler
  23.  
  24. // this does the main conversion, (eg. for {vars and methods}) 
  25. // it relays into Compiler/Tag & Compiler/Flexy for tags and namespace handling.
  26.  
  27.  
  28.  
  29.  
  30. require_once 'HTML/Template/Flexy/Tokenizer.php';
  31. require_once 'HTML/Template/Flexy/Token.php';
  32.  
  33. class HTML_Template_Flexy_Compiler_Flexy extends HTML_Template_Flexy_Compiler {
  34.     
  35.     
  36.         
  37.     /**
  38.     * The current template (Full path)
  39.     *
  40.     * @var string 
  41.     * @access public
  42.     */
  43.     var $currentTemplate;
  44.     /**
  45.     * The compile method.
  46.     * 
  47.     * @params   object HTML_Template_Flexy
  48.     * @params   string|false string to compile of false to use a file.
  49.     * @return   string   filename of template
  50.     * @access   public
  51.     */
  52.     function compile(&$flexy$string=false
  53.     {
  54.         // read the entire file into one variable
  55.         
  56.         // note this should be moved to new HTML_Template_Flexy_Token
  57.         // and that can then manage all the tokens in one place..
  58.         global $_HTML_TEMPLATE_FLEXY_COMPILER;
  59.         
  60.         $this->currentTemplate  $flexy->currentTemplate;
  61.         
  62.         
  63.         $gettextStrings &$_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings'];
  64.         $gettextStrings = array()// reset it.
  65.         
  66.         if (@$this->options['debug']{
  67.             echo "compiling template $flexy->currentTemplate<BR>";
  68.             
  69.         }
  70.          
  71.         // reset the elements.
  72.         $flexy->_elements = array();
  73.         
  74.         // replace this with a singleton??
  75.         
  76.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']  $this->options;
  77.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']        = array();
  78.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']        $flexy->currentTemplate;
  79.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput']    '';
  80.         $GLOBALS['_HTML_TEMPLATE_FLEXY']['compiledTemplate']$flexy->compiledTemplate;
  81.         
  82.         
  83.         // initialize Translation 2, and 
  84.         $this->initializeTranslator();
  85.         
  86.         
  87.         // load the template!
  88.         $data $string;
  89.         $res = false;
  90.         if ($string === false{
  91.             $data file_get_contents($flexy->currentTemplate);
  92.         }
  93.          
  94.         // PRE PROCESS {_(.....)} translation markers.
  95.         if (strpos($data'{_('!== false{
  96.             $data $this->preProcessTranslation($data);
  97.         }
  98.         
  99.         // Tree generation!!!
  100.         
  101.         
  102.         
  103.         if (!$this->options['forceCompile'&& isset($_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)])) {
  104.             $res $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)];
  105.         else {
  106.         
  107.              
  108.             $tokenizer = new HTML_Template_Flexy_Tokenizer($data);
  109.             $tokenizer->fileName = $flexy->currentTemplate;
  110.             
  111.             
  112.               
  113.             //$tokenizer->debug=1;
  114.             $tokenizer->options['ignore_html'$this->options['nonHTML'];
  115.             
  116.           
  117.             require_once 'HTML/Template/Flexy/Token.php';
  118.             $res = HTML_Template_Flexy_Token::buildTokens($tokenizer);
  119.             if (is_a($res'PEAR_Error')) {
  120.                 return $res;
  121.             }       
  122.             $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)$res;
  123.             
  124.         }
  125.         
  126.         
  127.         // technically we shouldnt get here as we dont cache errors..
  128.         if (is_a($res'PEAR_Error')) {
  129.             return $res;
  130.         }   
  131.         
  132.         // turn tokens into Template..
  133.         
  134.         $data $res->compile($this);
  135.         
  136.         if (is_a($data'PEAR_Error')) {
  137.             return $data;
  138.         }
  139.         
  140.         $data $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'$data;
  141.         
  142.         if (   $flexy->options['debug'> 1{
  143.             echo "<B>Result: </B><PRE>".htmlspecialchars($data)."</PRE><BR>\n";
  144.         }
  145.  
  146.         if ($this->options['nonHTML']{
  147.            $data =  str_replace("?>\n""?>\n\n"$data);
  148.         }
  149.         
  150.          
  151.         
  152.         
  153.         // at this point we are into writing stuff...
  154.         if ($flexy->options['compileToString']{
  155.             if (   $flexy->options['debug']{
  156.                 echo "<B>Returning string:<BR>\n";
  157.             }
  158.  
  159.             $flexy->elements =  $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'];
  160.             return $data;
  161.         }
  162.         
  163.         
  164.         
  165.         
  166.         // error checking?
  167.         $file  $flexy->compiledTemplate;
  168.         if (isset($flexy->options['output.block'])) {
  169.             list($file$partexplode('#'$file);
  170.         }
  171.         
  172.         if( ($cfp fopen($file'w')) ) {
  173.             if ($flexy->options['debug']{
  174.                 echo "<B>Writing: </B>$file<BR>\n";
  175.             }
  176.             fwrite($cfp$data);
  177.             fclose($cfp);
  178.             
  179.             chmod($file0775);
  180.             // make the timestamp of the two items match.
  181.             clearstatcache();
  182.             touch($filefilemtime($flexy->currentTemplate));
  183.             if ($file != $flexy->compiledTemplate{
  184.                 chmod($flexy->compiledTemplate0775);
  185.                 // make the timestamp of the two items match.
  186.                 clearstatcache();
  187.                 touch($flexy->compiledTemplatefilemtime($flexy->currentTemplate));
  188.             }
  189.              
  190.             
  191.         else {
  192.             return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to write to '.$flexy->compiledTemplate,
  193.         }
  194.         // gettext strings
  195.          
  196.         if (file_exists($flexy->getTextStringsFile)) {
  197.             unlink($flexy->getTextStringsFile);
  198.         }
  199.         
  200.         if($gettextStrings && ($cfp fopen$flexy->getTextStringsFile'w') ) ) {
  201.             
  202.             fwrite($cfpserialize(array_unique($gettextStrings)));
  203.             fclose($cfp);
  204.             chmod($flexy->getTextStringsFile0664);
  205.         }
  206.         
  207.         // elements
  208.         if (file_exists($flexy->elementsFile)) {
  209.             unlink($flexy->elementsFile);
  210.         }
  211.         
  212.         if$GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'&&
  213.             ($cfp fopen$flexy->elementsFile'w') ) ) {
  214.             fwrite($cfpserialize$GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']));
  215.             fclose($cfp);
  216.             chmod($flexy->elementsFile0664);
  217.             // now clear it.
  218.         
  219.         }
  220.         
  221.         return true;
  222.     }
  223.     
  224.     
  225.     /**
  226.     * Initilalize the translation methods.
  227.     *
  228.     * Loads Translation2 if required.
  229.     * 
  230.      *
  231.     * @return   none 
  232.     * @access   public
  233.     */
  234.     function initializeTranslator({
  235.     
  236.         if (is_array($this->options['Translation2'])) {
  237.             require_once 'Translation2.php';
  238.             $this->options['Translation2'=  &Translation2::factory(
  239.                 $this->options['Translation2']['driver'],
  240.                 isset($this->options['Translation2']['options']$this->options['Translation2']['options': array(),
  241.                 isset($this->options['Translation2']['params']$this->options['Translation2']['params': array()
  242.             );
  243.         }
  244.                 
  245.         if (is_a($this->options['Translation2']'Translation2')) {
  246.             $this->options['Translation2']->setLang($this->options['locale']);
  247.             // fixme - needs to be more specific to which template to use..
  248.             foreach ($this->options['templateDir'as $tt{
  249.                 $n basename($this->currentTemplate);
  250.                 if (substr($this->currentTemplate0strlen($tt)) == $tt{
  251.                     $n substr($this->currentTemplatestrlen($tt)+1);
  252.                 }
  253.                 //echo $n;
  254.             }
  255.             $this->options['Translation2']->setPageID($n);
  256.         else {
  257.             setlocale(LC_ALL$this->options['locale']);
  258.         }
  259.         
  260.     }
  261.     
  262.     
  263.     
  264.     /**
  265.     * do the early tranlsation of {_(......)_} text
  266.     *
  267.     * 
  268.     * @param    input string
  269.     * @return   output string
  270.     * @access   public
  271.     */
  272.     function preProcessTranslation($data{
  273.         global $_HTML_TEMPLATE_FLEXY_COMPILER;
  274.         $matches = array();
  275.         $lmatches explode ('{_('$data);
  276.         array_shift($lmatches);
  277.         // shift the first..
  278.         foreach ($lmatches as $k{
  279.             if (false === strpos($k')_}')) {
  280.                 continue;
  281.             }
  282.             $x explode(')_}'$k);
  283.             $matches[$x[0];
  284.         }
  285.     
  286.     
  287.        //echo '<PRE>';print_r($matches);
  288.         // we may need to do some house cleaning here...
  289.         $_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings'$matches;
  290.         
  291.         
  292.         // replace them now..  
  293.         // ** leaving in the tag (which should be ignored by the parser..
  294.         // we then get rid of the tags during the toString method in this class.
  295.         foreach($matches as $v{
  296.             $data str_replace('{_('.$v.')_}''{_('.$this->translateString($v).')_}'$data);
  297.         }
  298.         return $data;
  299.     }    
  300.  
  301.     
  302.     
  303.     
  304.     
  305.     /**
  306.     * Flag indicating compiler is inside {_( .... )_} block, and should not
  307.     * add to the gettextstrings array.
  308.     *
  309.     * @var boolean 
  310.     * @access public
  311.     */
  312.     var $inGetTextBlock = false;
  313.     
  314.     /**
  315.     * This is the base toString Method, it relays into toString{TokenName}
  316.     *
  317.     * @param    object    HTML_Template_Flexy_Token_* 
  318.     * 
  319.     * @return   string     string to build a template
  320.     * @access   public
  321.     * @see      toString*
  322.     */
  323.   
  324.  
  325.     function toString($element
  326.     {
  327.         static $len = 26; // strlen('HTML_Template_Flexy_Token_');
  328.         if ($this->options['debug'> 1{
  329.             $x $element;
  330.             unset($x->children);
  331.             //echo htmlspecialchars(print_r($x,true))."<BR>\n";
  332.         }
  333.         if ($element->token == 'GetTextStart'{
  334.             $this->inGetTextBlock = true;
  335.             return '';
  336.         }
  337.         if ($element->token == 'GetTextEnd'{
  338.             $this->inGetTextBlock = false;
  339.             return '';
  340.         }
  341.         
  342.             
  343.         $class get_class($element);
  344.         if (strlen($class>= $len{
  345.             $type substr($class$len);
  346.             return $this->{'toString'.$type}($element);
  347.         }
  348.         
  349.         $ret $element->value;
  350.         $add $element->compileChildren($this);
  351.         if (is_a($add'PEAR_Error')) {
  352.             return $add;
  353.         }
  354.         $ret .= $add;
  355.         
  356.         if ($element->close{
  357.             $add $element->close->compile($this);
  358.             if (is_a($add'PEAR_Error')) {
  359.                 return $add;
  360.             }
  361.             $ret .= $add;
  362.         }
  363.         
  364.         return $ret;
  365.     }
  366.  
  367.  
  368.     /**
  369.     *   HTML_Template_Flexy_Token_Else toString
  370.     *
  371.     * @param    object    HTML_Template_Flexy_Token_Else 
  372.     * 
  373.     * @return   string     string to build a template
  374.     * @access   public
  375.     * @see      toString*
  376.     */
  377.   
  378.  
  379.     function toStringElse($element
  380.      {
  381.         // pushpull states to make sure we are in an area.. - should really check to see 
  382.         // if the state it is pulling is a if...
  383.         if ($element->pullState(=== false{
  384.             return $this->appendHTML(
  385.                 "<font color=\"red\">Unmatched {else:} on line: {$element->line}</font>"
  386.                 );
  387.         }
  388.         $element->pushState();
  389.         return $this->appendPhp("} else {");
  390.     }
  391.     
  392.     /**
  393.     *   HTML_Template_Flexy_Token_End toString
  394.     *
  395.     * @param    object    HTML_Template_Flexy_Token_Else 
  396.     * 
  397.     * @return   string     string to build a template
  398.     * @access   public
  399.     * @see      toString*
  400.     */
  401.   
  402.     function toStringEnd($element
  403.     {
  404.         // pushpull states to make sure we are in an area.. - should really check to see 
  405.         // if the state it is pulling is a if...
  406.         if ($element->pullState(=== false{
  407.             return $this->appendHTML(
  408.                 "<font color=\"red\">Unmatched {end:} on line: {$element->line}</font>"
  409.                 );
  410.         }
  411.          
  412.         return $this->appendPhp("}");
  413.     }
  414.  
  415.     /**
  416.     *   HTML_Template_Flexy_Token_EndTag toString
  417.     *
  418.     * @param    object    HTML_Template_Flexy_Token_EndTag 
  419.     * 
  420.     * @return   string     string to build a template
  421.     * @access   public
  422.     * @see      toString*
  423.     */
  424.   
  425.  
  426.  
  427.     function toStringEndTag($element
  428.     {
  429.         return $this->toStringTag($element);
  430.     }
  431.         
  432.     
  433.     
  434.     /**
  435.     *   HTML_Template_Flexy_Token_Foreach toString
  436.     *
  437.     * @param    object    HTML_Template_Flexy_Token_Foreach 
  438.     * 
  439.     * @return   string     string to build a template
  440.     * @access   public
  441.     * @see      toString*
  442.     */
  443.   
  444.     
  445.     function toStringForeach($element
  446.     {
  447.     
  448.         $loopon $element->toVar($element->loopOn);
  449.         if (is_a($loopon'PEAR_Error')) {
  450.             return $loopon;
  451.         }
  452.         
  453.         $ret 'if ($this->options[\'strict\'] || ('.
  454.             'is_array('$loopon')  || ' .
  455.             'is_object(' $loopon  '))) ' .
  456.             'foreach(' $loopon  " ";
  457.             
  458.         $ret .= "as \${$element->key}";
  459.         
  460.         if ($element->value{
  461.             $ret .=  " => \${$element->value}";
  462.         }
  463.         $ret .= ") {";
  464.         
  465.         $element->pushState();
  466.         $element->pushVar($element->key);
  467.         $element->pushVar($element->value);
  468.         return $this->appendPhp($ret);
  469.     }
  470.     /**
  471.     *   HTML_Template_Flexy_Token_If toString
  472.     *
  473.     * @param    object    HTML_Template_Flexy_Token_If 
  474.     * 
  475.     * @return   string     string to build a template
  476.     * @access   public
  477.     * @see      toString*
  478.     */
  479.   
  480.     function toStringIf($element
  481.     {
  482.         
  483.         $var $element->toVar($element->condition);
  484.         if (is_a($var'PEAR_Error')) {
  485.             return $var;
  486.         }
  487.         
  488.         $ret "if (".$element->isNegative . $var .")  {";
  489.         $element->pushState();
  490.         return $this->appendPhp($ret);
  491.     }
  492.  
  493.    /**
  494.     *  get Modifier Wrapper
  495.     *
  496.     * converts :h, :u, :r , .....
  497.     * @param    object    HTML_Template_Flexy_Token_Method|Var
  498.     * 
  499.     * @return   array prefix,suffix
  500.     * @access   public
  501.     * @see      toString*
  502.     */
  503.  
  504.     function getModifierWrapper($element
  505.     {
  506.         $prefix 'echo ';
  507.         
  508.         $suffix '';
  509.         $modifier strlen(trim($element->modifier)) $element->modifier : ' ';
  510.         
  511.         switch ($modifier{
  512.             case 'h':
  513.                 break;
  514.             case 'u':
  515.                 $prefix 'echo urlencode(';
  516.                 $suffix ')';
  517.                 break;
  518.             case 'r':
  519.                 $prefix 'echo \'<pre>\'; echo htmlspecialchars(print_r(';
  520.                 $suffix ',true)); echo \'</pre>\';';
  521.                 break;                
  522.             case 'n'
  523.                 // blank or value..
  524.                 $numberformat @$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'];
  525.                 $prefix 'echo number_format(';
  526.                 $suffix $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'')';
  527.                 break;
  528.             case 'b'// nl2br + htmlspecialchars
  529.                 $prefix 'echo nl2br(htmlspecialchars(';
  530.                 
  531.                 // add language ?
  532.                 $suffix '))';
  533.                 break;
  534.             case ' ':
  535.                 $prefix 'echo htmlspecialchars(';
  536.                 // add language ?
  537.                 $suffix ')';
  538.                 break;
  539.             default:
  540.                $prefix 'echo $this->plugin("'.trim($element->modifier.'",';
  541.                $suffix ')'
  542.             
  543.             
  544.         }
  545.         
  546.         return array($prefix$suffix);
  547.     }
  548.  
  549.  
  550.  
  551.   /**
  552.     *   HTML_Template_Flexy_Token_Var toString
  553.     *
  554.     * @param    object    HTML_Template_Flexy_Token_Method 
  555.     * 
  556.     * @return   string     string to build a template
  557.     * @access   public
  558.     * @see      toString*
  559.     */
  560.   
  561.     function toStringVar($element
  562.     {
  563.         // ignore modifier at present!!
  564.         
  565.         $var $element->toVar($element->value);
  566.         if (is_a($var'PEAR_Error')) {
  567.             return $var;
  568.         }
  569.         list($prefix$suffix$this->getModifierWrapper($element);
  570.         return $this->appendPhp$prefix $var $suffix .';');
  571.     }
  572.    /**
  573.     *   HTML_Template_Flexy_Token_Method toString
  574.     *
  575.     * @param    object    HTML_Template_Flexy_Token_Method 
  576.     * 
  577.     * @return   string     string to build a template
  578.     * @access   public
  579.     * @see      toString*
  580.     */
  581.   
  582.     function toStringMethod($element
  583.     {
  584.  
  585.               
  586.         // set up the modifier at present!!
  587.          
  588.         list($prefix$suffix$this->getModifierWrapper($element);
  589.         
  590.         // add the '!' to if
  591.         
  592.         if ($element->isConditional{
  593.             $prefix 'if ('.$element->isNegative;
  594.             $element->pushState();
  595.             $suffix ')';
  596.         }  
  597.         
  598.         
  599.         // check that method exists..
  600.         // if (method_exists($object,'method');
  601.         $bits explode('.'$element->method);
  602.         $method array_pop($bits);
  603.         
  604.         $object implode('.'$bits);
  605.         
  606.         $var $element->toVar($object);
  607.         if (is_a($var'PEAR_Error')) {
  608.             return $var;
  609.         }
  610.         
  611.         if (($object == 'GLOBALS'&& 
  612.             $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['globalfunctions']{
  613.             // we should check if they something weird like: GLOBALS.xxxx[sdf](....)
  614.             $var $method;
  615.         else {
  616.             $prefix 'if ($this->options[\'strict\'] || (isset('.$var.
  617.                 ') && method_exists('.$var .", '{$method}'))) " . $prefix;
  618.             $var $element->toVar($element->method);
  619.         }
  620.         
  621.  
  622.         if (is_a($var'PEAR_Error')) {
  623.             return $var;
  624.         }
  625.         
  626.         $ret  =  $prefix;
  627.         $ret .=  $var "(";
  628.         $s =0;
  629.          
  630.        
  631.          
  632.         foreach($element->args as $a{
  633.              
  634.             if ($s{
  635.                 $ret .= ",";
  636.             }
  637.             $s =1;
  638.             if ($a{0== '#'{
  639.                 if (is_numeric(substr($a1-1))) {
  640.                     $ret .= substr($a1-1);
  641.                 else {
  642.                     $ret .= '"'addslashes(substr($a1-1)) '"';
  643.                 }
  644.                 continue;
  645.             }
  646.             
  647.             $var $element->toVar($a);
  648.             if (is_a($var'PEAR_Error')) {
  649.                 return $var;
  650.             }
  651.             $ret .= $var;
  652.             
  653.         }
  654.         $ret .= ")" $suffix;
  655.         
  656.         if ($element->isConditional{
  657.             $ret .= ' { ';
  658.         else {
  659.             $ret .= ";";
  660.         }
  661.         
  662.         
  663.         
  664.         return $this->appendPhp($ret)
  665.         
  666.          
  667.  
  668.    }
  669.    /**
  670.     *   HTML_Template_Flexy_Token_Processing toString
  671.     *
  672.     * @param    object    HTML_Template_Flexy_Token_Processing 
  673.     * 
  674.     * @return   string     string to build a template
  675.     * @access   public
  676.     * @see      toString*
  677.     */
  678.  
  679.  
  680.     function toStringProcessing($element
  681.     {
  682.         // if it's XML then quote it..
  683.         if (strtoupper(substr($element->value23)) == 'XML'
  684.             return $this->appendPhp("echo '" str_replace("'""\\"."'"$element->value"';");
  685.         }
  686.         // otherwise it's PHP code - so echo it..
  687.         return $element->value;
  688.     }
  689.     
  690.     /**
  691.     *   HTML_Template_Flexy_Token_Text toString
  692.     *
  693.     * @param    object    HTML_Template_Flexy_Token_Text 
  694.     * 
  695.     * @return   string     string to build a template
  696.     * @access   public
  697.     * @see      toString*
  698.     */
  699.  
  700.  
  701.  
  702.     function toStringText($element
  703.     {
  704.         
  705.         // first get rid of stuff thats not translated etc.
  706.         // empty strings => output.
  707.         // comments -> just output
  708.         // our special tags -> output..
  709.         
  710.         if (!strlen(trim($element->value) )) {
  711.             return $this->appendHtml($element->value);
  712.         }
  713.         // dont add comments to translation lists.
  714.          
  715.         if (substr($element->value04== '<!--'{
  716.             return $this->appendHtml($element->value);
  717.         }
  718.         // ignore anything wrapped with {_( .... )_}
  719.         if ($this->inGetTextBlock{
  720.             return $this->appendHtml($element->value);
  721.         }
  722.         
  723.         
  724.         if (!$element->isWord()) {
  725.             return $this->appendHtml($element->value);
  726.         }
  727.         
  728.         // grab the white space at start and end (and keep it!
  729.         
  730.         $value ltrim($element->value);
  731.         $front substr($element->value0-strlen($value));
  732.         $value rtrim($element->value);
  733.         $rear  substr($element->valuestrlen($value));
  734.         $value trim($element->value);
  735.         
  736.         
  737.         // convert to escaped chars.. (limited..)
  738.         //$value = strtr($value,$cleanArray);
  739.         
  740.         $this->addStringToGettext($value);
  741.         $value $this->translateString($value);
  742.         // its a simple word!
  743.         return $this->appendHtml($front $value $rear);
  744.         
  745.     }
  746.     
  747.     
  748.     
  749.       /**
  750.     *   HTML_Template_Flexy_Token_Cdata toString
  751.     *
  752.     * @param    object    HTML_Template_Flexy_Token_Cdata ?
  753.     * 
  754.     * @return   string     string to build a template
  755.     * @access   public
  756.     * @see      toString*
  757.     */
  758.  
  759.  
  760.  
  761.     function toStringCdata($element
  762.     {
  763.         return $this->appendHtml($element->value);
  764.     }
  765.     
  766.     
  767.     
  768.     
  769.     
  770.     
  771.     
  772.     
  773.     
  774.     
  775.     /**
  776.     * addStringToGettext
  777.     *
  778.     * Adds a string to the gettext array.
  779.     * 
  780.     * @param   mixed        preferably.. string to store
  781.     *
  782.     * @return   none 
  783.     * @access   public
  784.     */
  785.     
  786.     function addStringToGettext($string
  787.     {
  788.     
  789.         
  790.         
  791.         
  792.         if (!is_string($string)) {
  793.             return;
  794.         }
  795.         
  796.         if (!preg_match('/[a-z]+/i'$string)) {
  797.             return;
  798.         }
  799.         $string trim($string);
  800.         
  801.         if (substr($string04== '<!--'{
  802.             return;
  803.         }
  804.         
  805.         $GLOBALS['_HTML_TEMPLATE_FLEXY_COMPILER']['gettextStrings'][$string;
  806.     }
  807.     
  808.     
  809.     /**
  810.     * translateString - a gettextWrapper
  811.     *
  812.     * tries to do gettext or falls back on File_Gettext
  813.     * This has !!!NO!!! error handling - if it fails you just get english..
  814.     * no questions asked!!!
  815.     * 
  816.     * @param   string       string to translate
  817.     *
  818.     * @return   string      translated string..
  819.     * @access   public
  820.     */
  821.   
  822.     function translateString($string)
  823.     {
  824.          
  825.         
  826.         
  827.         if (is_a($this->options['Translation2']'Translation2')) {
  828.             $result $this->options['Translation2']->get($string);
  829.             if (!empty($result)) {
  830.                 return $result;
  831.             }
  832.             return $string;
  833.         }
  834.         
  835.         // note this stuff may have been broken by removing the \n replacement code 
  836.         // since i dont have a test for it... it may remain broken..
  837.         // use Translation2 - it has gettext backend support
  838.         // and should sort out the mess that \n etc. entail.
  839.         
  840.         
  841.         $prefix basename($GLOBALS['_HTML_TEMPLATE_FLEXY']['filename']).':';
  842.         if (@$this->options['debug']{
  843.             echo __CLASS__.":TRANSLATING $string<BR>\n";
  844.         }
  845.         
  846.         if (function_exists('gettext'&& !$this->options['textdomain']{
  847.             if (@$this->options['debug']{
  848.                 echo __CLASS__.":USING GETTEXT?<BR>";
  849.             }
  850.             $t gettext($string);
  851.             
  852.             if ($t != $string{
  853.                 return $t;
  854.             }
  855.             $tt gettext($prefix.$string);
  856.             if ($tt != $prefix.$string{
  857.                 return $tt;
  858.             }
  859.                 // give up it's not translated anywhere...
  860.             return $string;
  861.              
  862.         }
  863.         if (!$this->options['textdomain'|| !$this->options['textdomainDir']{
  864.             // text domain is not set..
  865.             if (@$this->options['debug']{
  866.                 echo __CLASS__.":MISSING textdomain settings<BR>";
  867.             }
  868.             return $string;
  869.         }
  870.         $pofile $this->options['textdomainDir'
  871.                 '/' $this->options['locale'
  872.                 '/LC_MESSAGES/' $this->options['textdomain''.po';
  873.         
  874.         
  875.         // did we try to load it already..
  876.         if (@$GLOBALS['_'.__CLASS__]['PO'][$pofile=== false{
  877.             if (@$this->options['debug']{
  878.                 echo __CLASS__.":LOAD failed (Cached):<BR>";
  879.             }
  880.             return $string;
  881.         }
  882.         if (!@$GLOBALS['_'.__CLASS__]['PO'][$pofile]{
  883.             // default - cant load it..
  884.             $GLOBALS['_'.__CLASS__]['PO'][$pofile= false;
  885.             if (!file_exists($pofile)) {
  886.                  if (@$this->options['debug']{
  887.                 echo __CLASS__.":LOAD failed: {$pofile}<BR>";
  888.             }
  889.                 return $string;
  890.             }
  891.             
  892.             if (!@include_once 'File/Gettext.php'{
  893.                 if (@$this->options['debug']{
  894.                     echo __CLASS__.":LOAD no File_gettext:<BR>";
  895.                 }
  896.                 return $string;
  897.             }
  898.             
  899.             $GLOBALS['_'.__CLASS__]['PO'][$pofile= File_Gettext::factory('PO'$pofile);
  900.             $GLOBALS['_'.__CLASS__]['PO'][$pofile]->load();
  901.             //echo '<PRE>'.htmlspecialchars(print_r($GLOBALS['_'.__CLASS__]['PO'][$pofile]->strings,true));
  902.             
  903.         }
  904.         $po &$GLOBALS['_'.__CLASS__]['PO'][$pofile];
  905.         // we should have it loaded now...
  906.         // this is odd - data is a bit messed up with CR's
  907.         $string str_replace('\n'"\n"$string);
  908.         
  909.         if (isset($po->strings[$prefix.$string])) {
  910.             return $po->strings[$prefix.$string];
  911.         }
  912.         
  913.         if (!isset($po->strings[$string])) {
  914.             if (@$this->options['debug']{
  915.                     echo __CLASS__.":no match:<BR>";
  916.             }
  917.             return $string;
  918.         }
  919.         if (@$this->options['debug']{
  920.             echo __CLASS__.":MATCHED: {$po->strings[$string]}<BR>";
  921.         }
  922.         
  923.         // finally we have a match!!!
  924.         return $po->strings[$string];
  925.         
  926.     
  927.      /**
  928.     *   HTML_Template_Flexy_Token_Tag toString
  929.     *
  930.     * @param    object    HTML_Template_Flexy_Token_Tag 
  931.     * 
  932.     * @return   string     string to build a template
  933.     * @access   public
  934.     * @see      toString*
  935.     */
  936.   
  937.     function toStringTag($element{
  938.         
  939.         $original $element->getAttribute('ALT');
  940.         if (($element->tag == 'IMG'&& is_string($original&& strlen($original)) {
  941.             $this->addStringToGettext($original);
  942.             $quote $element->ucAttributes['ALT']{0};
  943.             $element->ucAttributes['ALT'$quote  $this->translateString($original)$quote;
  944.         }
  945.         $original $element->getAttribute('TITLE');
  946.         if (($element->tag == 'A'&& is_string($original&& strlen($original)) {
  947.             $this->addStringToGettext($original);
  948.             $quote $element->ucAttributes['TITLE']{0};
  949.             $element->ucAttributes['TITLE'$quote  $this->translateString($original)$quote;
  950.         }
  951.         
  952.         
  953.         if (strpos($element->tag':'=== false{
  954.             $namespace 'Tag';
  955.         else {
  956.             $bits =  explode(':'$element->tag);
  957.             $namespace $bits[0];
  958.         }
  959.         if ($namespace{0== '/'{
  960.             $namespace substr($namespace1);
  961.         }
  962.         if (empty($this->tagHandlers[$namespace])) {
  963.             
  964.             require_once 'HTML/Template/Flexy/Compiler/Flexy/Tag.php';
  965.             $this->tagHandlers[$namespace&HTML_Template_Flexy_Compiler_Flexy_Tag::factory($namespace$this);
  966.             if (!$this->tagHandlers[$namespace{
  967.                 return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to create Namespace Handler '.$namespace 
  968.                     ' in file ' $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'],
  969.                     HTML_TEMPLATE_FLEXY_ERROR_SYNTAXHTML_TEMPLATE_FLEXY_ERROR_RETURN);
  970.             }
  971.                 
  972.         }
  973.         return $this->tagHandlers[$namespace]->toString($element);
  974.         
  975.         
  976.     }
  977.     
  978.  
  979. }

Documentation generated on Mon, 11 Mar 2019 14:17:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.