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

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