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

Source for file Standard.php

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

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