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

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