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

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