go-utils
[ class tree: go-utils ] [ index: go-utils ] [ all elements ]

Source for file go_template_parser.class.inc.php

Documentation is available at go_template_parser.class.inc.php

  1. <?php
  2. /** 
  3.  * Copyright Intermesh
  4.  * 
  5.  * This file is part of Group-Office. You should have received a copy of the
  6.  * Group-Office license along with Group-Office. See the file /LICENSE.TXT
  7.  * 
  8.  * If you have questions write an e-mail to info@intermesh.nl
  9.  * 
  10.  * @copyright Copyright Intermesh
  11.  * @version $Id: go_template_parser.class.inc.php 1229 2008-10-25 13:13:38Z mschering $
  12.  * @author Merijn Schering <mschering@intermesh.nl>
  13.  */
  14.  
  15. /**
  16.  * Parses a template
  17.  * 
  18.  * @author Merijn Schering <mschering@intermesh.nl>
  19.  * @version $Id: go_template_parser.class.inc.php 1229 2008-10-25 13:13:38Z mschering $
  20.  * @copyright Copyright Intermesh
  21.  * @license AGPL Affero General Public License
  22.  * @package go.utils
  23.  * @uses db
  24.  * @since Group-Office 3.0
  25.  */
  26. {
  27.     var $open_tag_symbol = '&lt;';
  28.     var $close_tag_symbol = '&gt;';
  29.     
  30.     var $tags = array('gotpl');
  31.     var $fields;
  32.     
  33.     public function __construct($fields$values)
  34.     {
  35.         $this->fields=$fields;
  36.         $this->values=$values;
  37.                 
  38.     }
  39.     
  40.     private function get_tag($tag$content{
  41.         $start_pos strpos($content$this->open_tag_symbol.$tag);
  42.         if ($start_pos !== false{
  43.             $end_pos $this->get_end_pos($tag$content$start_pos);
  44.             $sub_start_pos =     strpos($content$this->open_tag_symbol.$tag$start_pos+strlen($this->open_tag_symbol.$tag));
  45.             
  46.             if($sub_start_pos!== false)
  47.             {
  48.                 $sub_end_pos $end_pos;
  49.                 
  50.                 //echo $sub_start_pos.' < '.$sub_end_pos."\n---\n";
  51.                 
  52.                 while($sub_start_pos<$sub_end_pos)
  53.                 {
  54.                     $sub_end_pos $this->get_end_pos($tag$content$sub_end_pos);
  55.                     $sub_start_pos =     strpos($content$this->open_tag_symbol.$tag$sub_start_pos+strlen($this->open_tag_symbol.$tag));
  56.                     
  57.                     if($sub_end_pos)
  58.                         $end_pos $sub_end_pos;
  59.                 }    
  60.             }
  61.             if($end_pos === false)
  62.             {
  63.                 return false;
  64.             }
  65.             $tag_length $end_pos-$start_pos;
  66.             return substr($content$start_pos$tag_length);
  67.         }
  68.         return false;
  69.     }
  70.     
  71.     
  72.     private function get_end_pos($tag$content$offset=0)
  73.     {
  74.         $end_pos strpos($content$this->open_tag_symbol.'/'.$tag.$this->close_tag_symbol$offset);
  75.         if($end_pos!==false)
  76.         {
  77.             $end_pos+=strlen($this->open_tag_symbol.'/'.$tag.$this->close_tag_symbol);
  78.         }
  79.         return $end_pos;        
  80.     }
  81.  
  82.     private function get_attributes($tag{
  83.         $attributes array ();
  84.         $in_value false;
  85.         $in_name false;
  86.         $name '';
  87.         $value '';
  88.         $length strlen($tag);
  89.         
  90.         $exit=false;
  91.         
  92.         for ($i 0$i $length$i ++{
  93.             
  94.             if($exit)
  95.             {
  96.                 break;
  97.             }
  98.             $char $tag[$i];
  99.             switch ($char{
  100.                 case '"' :
  101.                     if ($in_value{
  102.                         $in_value false;
  103.  
  104.                         $attributes[trim($name)trim($value);
  105.                         $name '';
  106.                         $value '';
  107.                     else {
  108.                         $in_value true;
  109.                     }
  110.  
  111.                     break;
  112.  
  113.                 case ' ' :
  114.                     if (!$in_value{
  115.                         $in_name true;
  116.                     else {
  117.                         $value .= $char;
  118.                     }
  119.                     break;
  120.  
  121.                 case '=' :
  122.                     $in_name false;
  123.                     if ($in_value{
  124.                         $value .= $char;
  125.                     }
  126.                     break;
  127.  
  128.                 default :
  129.                     if ($in_name{
  130.                         $name .= $char;
  131.                     }
  132.  
  133.                     if ($in_value{
  134.                         $value .= $char;
  135.                     }
  136.                     break;
  137.             }
  138.         }
  139.         return $attributes;
  140.     }
  141.  
  142.     
  143.     private function replace_template(&$content)
  144.     {
  145.         foreach($this->fields as $field)
  146.         {
  147.             $value = isset($this->values[$field]$this->values[$field'';
  148.             $content str_replace('{'.$field.'}'$value$content);
  149.         }
  150.     }
  151.     
  152.     public function parse(&$content)
  153.     {
  154.         $this->parse_tags($content);
  155.         $this->replace_template($content);
  156.     }
  157.     
  158.     
  159.     private function parse_tags(&$content)
  160.     {
  161.         
  162.         foreach($this->values as $varname=>$value)
  163.         {
  164.             $$varname $value;
  165.         }
  166.         
  167.         foreach($this->tags as $tagname)
  168.         {
  169.             while ($tag $this->get_tag($tagname$content)) {
  170.     
  171.                 $attributes $this->get_attributes($tag);
  172.                 
  173.                 $print false;
  174.                 //echo '$print = '.$attributes['if'].';'."\n\n";
  175.                 //eval('$print = '.html_entity_decode($attributes['if']).';');
  176.                 
  177.                 $print !empty($$attributes['if']);
  178.                 
  179.                 
  180.                 //var_dump($print);
  181.                 if($print)
  182.                 {
  183.                     $start_pos strpos($tag$this->close_tag_symbol);                    
  184.                     $tagcontent substr($tag$start_pos+strlen($this->close_tag_symbol));                    
  185.                     $tagcontent substr($tagcontent,0strlen($tagcontent)-strlen($this->open_tag_symbol.'/'.$tagname.$this->close_tag_symbol));    
  186.                     $this->parse_tags($tagcontent$conditions);                    
  187.                 }else
  188.                 {
  189.                     $tagcontent '';
  190.                 }
  191.                 
  192.             //    echo $tagcontent;
  193.                 
  194.                 //echo "\n\n########\n\n";
  195.                 
  196.                 $content str_replace($tag$tagcontent$content);
  197.             }
  198.         }
  199.         
  200.     }
  201.     
  202. }
  203. ?>

Documentation generated on Thu, 30 Oct 2008 14:13:31 +0100 by phpDocumentor 1.4.0