Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, October 16, 2026

Diff for phpPlatonLib/templateSystems/ObjTemplate/Attic/ObjTemplate.class.php between version 1.2 and 1.3

version 1.2, 2001/10/28 15:04:09 version 1.3, 2001/11/02 19:19:59
Line 11 
Line 11 
   
 class ObjTemplate  class ObjTemplate
 {  {
           var $cstr = array(
                           'begin' => '<!--',
                           'end' => '-->',
                           'classname' => 'ObjTemplate.class.php',
                           'version' => '0.1'
                           );
   
           var $tag = array(
                           'begin' => '%',
                           'delim' => ':',
                           'end' => '%'
                           );
   
           var $cmd = array( /* Both sides must be lowercase. */
                           'if' => 'if',
                           'else' => 'else',
                           'endif' => 'endif',
                           'file' => 'file'
                           );
   
           var $filename;
           var $array;
   
   
           function ObjTemplate($filename = '')
           {
                   $this->filename = $filename;
   
                   return $this->reload();
           }
   
           function set_tags($begin_tag = '', $end_tag = '')
           {
                   if (strlen($begin_tag) > 0)
                           $this->tag['begin'] = $begin_tag;
   
                   if (strlen($end_tag) > 0)
                           $this->tag['end'] = $end_tag;
           }
   
           function load($filename)
           {
                   if (file_exists($filename)) {
                           $this->array = $this->_string_transform(join('', file($filename)));
   
                           return true;
                   }
   
                   return false;
           }
   
           function reload()
           {
                   if (strlen($this->filename) > 0)
                           return $this->load($this->filename);
   
                   return false;
           }
   
           function add_before($string)
           {
                   return array_unshift($this->array, $this->_string_transform($string));
           }
   
           function add_after($string)
           {
                   return array_push($this->array, $this->_string_transform($string));
           }
   
           function is_key($key)
           {
                   return $this->_is_key(strtolower($key), $this->array);
           }
   
           function _is_key($key, $ar)
           {
                   /*
                    * TODO: optimalization
                    * try foreach() loop instead for() loop
                    */
   
                   for ($i = 0; $i < count($ar); $i++) {
                           if (is_array($ar[$i])) {
                                   //if (isset($ar[$i]['key'])) {
                                   if (! strcmp($key, $ar[$i]['key'])) {
                                           return true;
                                   }
                                   elseif (! strcmp($this->cmd['if'], $ar[$i]['key'])) {
                                           //echo "COMPARE: " . $this->cmd['if'] . " vs. ". $ar[$i]['key'] . "<br>";
                                           //echo "var_dump(): " . var_dump($ar[$i]['key']) . "<br>";
                                           if (is_array($ar[$i]['true'])) {
                                                   if (true == $this->_is_key($key, $ar[$i]['true']))
                                                           return true;
                                           }
                                           if (is_array($ar[$i]['false'])) {
                                                   if (true == $this->_is_key($key, $ar[$i]['false']))
                                                           return true;
                                           }
                                   }
                                   //}
                                   else {
                                           if (true == $this->_is_key($key, $ar[$i]))
                                                   return true;
                                   }
                           }
                   }
   
                 var $filename;                  return false;
                 var $html;          }
                 var $start_tag, $end_tag;  
   
   
                 function ObjTemplate($filename = '')  
                 {  
                                 $this->filename = $filename;  
   
                                 $this->set_tags();          function set($key, $value = "")
           {
                   if (is_array($key)) {
                           while (list($k, $v) = each($key)) {
                                   if (! empty($k)) {
                                           /* Empty values are allowed. Empty keys are NOT. */
   
                                 return $this->reload();                                          $this->set($k, $v);
                                   }
                           }
                 }                  }
                   else {
                 function set_tags($start_tag = '%', $end_tag = '%')                          $val_ar = $this->_string_transform($value, 1);
                 {                          $this->_set(strtolower($key), count($val_ar) > 1 ? $val_ar : $value, $this->array);
                                 $this->start_tag = $start_tag;                  }
                                 $this->end_tag = $end_tag;  
                   return;
           }
   
           function _set($key, $value, &$ar)
           {
                   for ($i = 0; $i < count($ar); $i++) {
                           if (is_array($ar[$i])) {
                                   if (! strcmp($key, $ar[$i]['key'])) {
                                           $ar[$i] = $value;
                                   }
                                   elseif (! strcmp($this->cmd['if'], $ar[$i]['key'])) {
                                           if (is_array($ar[$i]['true']))
                                                   $this->_set($key, $value, $ar[$i]['true']);
                                           if (is_array($ar[$i]['false']))
                                                   $this->_set($key, $value, $ar[$i]['false']);
                                   }
                                   else {
                                           $this->_set($key, $value, $ar[$i]);
                                   }
                           }
                 }                  }
   
                 function load($filename)                  return;
                 {          }
                                 if (file_exists($filename)) {  
                                                 $this->html = join('', file($filename));  
   
                                                 $this->_set_file();          function get_keys()
                                                 return 1;          {
                                 }                  return array_unique($this->_get_keys($this->array));
           }
   
                                 return 0;          function _get_keys($ar)
                 }          {
   
                 function reload()                  $ar_out = array();
                 {  
                                 if (isset($this->filename))  
                                                 return $this->load($this->filename);  
   
                                 return 0;                  for ($i = 0; $i < count($ar); $i++) {
                           if (is_array($ar[$i])) {
                                   if (! strcmp($this->cmd['if'], $ar[$i]['key'])) {
                                           if (is_array($ar[$i]['true']))
                                                   $ar_out = array_merge($ar_out, $this->_get_keys($ar[$i]['true']));
                                           if (is_array($ar[$i]['false']))
                                                   $ar_out = array_merge($ar_out, $this->_get_keys($ar[$i]['false']));
                                   }
                                   elseif (isset($ar[$i]['key'])) {
                                           array_push($ar_out, $ar[$i]['key']);
                                   }
                                   else {
                                           $ar_out = array_merge($ar_out, $this->_get_keys($ar[$i]));
                                   }
                           }
                 }                  }
   
                 function add_before($string)                  return $ar_out;
                 {          }
                                 $this->html = $string . $this->html;  
                 }  
   
                 function add_after($string)          function hide()
                 {          {
                                 $this->html .= $string;                  return $this->_get_content($this->array, 'hide');
           }
   
           function clean()
           {
                   return $this->_get_content($this->array, 'clean');
           }
   
           function html()
           {
                   return $this->_get_content($this->array, 'normal');
           }
   
           function _get_content($ar, $mode)
           {
   
                   $string = '';
   
                   for ($i = 0; $i < count($ar); $i++) {
                           if (is_array($ar[$i])) {
                                   if (! strcmp($this->cmd['if'], $ar[$i]['key'])) {
                                           /* TODO: :) */
                                           if (is_array($ar[$i]['true']));
                                           if (is_array($ar[$i]['false']));
                                   }
                                   elseif (isset($ar[$i]['key'])) {
                                           switch ($mode) {
                                                   case "clean":
                                                           ;
                                                   break;
                                                   case "hide":
                                                           $string .= $this->_generate_error("hide: " . $ar[$i]['key']);
                                                   break;
                                                   default:
                                                   $string .= $this->tag['begin'] . $ar[$i]['key'] . $this->tag['end'];
                                                   break;
                                           }
                                   }
                                   else {
                                           $string .= $this->_get_content($ar[$i], $mode);
                                   }
                           }
                           else {
                                   $string .= $ar[$i];
                           }
                 }                  }
   
                 function set($key, $value = "")                  return $string;
                 {          }
                                 if (gettype($key) == "array") {  
                                                 while ( list ($k, $v) = each ($key) ) {  
                                                                 if (!(empty($k))) {  
                                                                                 /* Empty values are allowed. Empty keys are NOT. */  
   
                                                                                 $this->set($k, $v);          function html_array($prefix)
                                                                 }          {
                                                 }                  $html = $this->html();
                                 }  
                                 else {  
   
                                                 $regexp = $this->start_tag . $key .                  $regexp =  $this->tag['start'] . $prefix . $this->tag['delim'] . ':([^%\b]+)' .
                                                                 ( isset($this->end_tag) ? $this->end_tag : '\b' );                          ( isset($this->tag['end']) ? $this->tag['end'] : '\b' ) . '';
   
                                                 if (isset($value)) {                  preg_match_all("/$regexp/is", $html, $matches);
                                                                 $this->html = preg_replace("/$regexp/i", $value, $this->html);  
                                                 }  
                                 }  
   
                                 $this->_set_file();                  return array(preg_split("/$regexp/is", $this->html), $matches[1]);
                 }          }
   
                 function _set_file()          function get_array()
                 {          {
                                 $regexp = $this->start_tag . 'file:([^%\b]+)' .                  return $this->array;
                                                 ( isset($this->end_tag) ? $this->end_tag : '\b' );          }
   
                                 while (preg_match("/$regexp/i", $this->html, $matches)){          /*
            *       Required improvements:
            *
            *       - remove empty text records (at the end of array)
            *       - return only string if array is not necessary
            *
            */
   
                                                 $filename = $matches[1];          function _string_transform($string, $debug = 0)
                                                 $filename_with_path = dirname($this->filename) . '/' . $filename;          {
                   $ar_out_idx = 0;
   
                                                 if (file_exists($filename_with_path)) {                  $regexp = $this->tag['begin'] . '(\w+(' . $this->tag['delim'] . '[\w\.]+)*)' .
                                                                 $value = join('', file($filename_with_path));                          ( isset($this->tag['end']) ? $this->tag['end'] : '\b' );
                                                 }  
                                                 else {  
                                                                 $value = "<!-- ObjTemplate.class.php: "  
                                                                                 ."file not found: $filename_with_path -->";  
                                                 }  
   
                                                 $filename = addslashes($filename);                  preg_match_all("/$regexp/is", $string, $m_tags);
                   $m_tags = $m_tags[1];
   
                                                 $regexp2 = $this->start_tag . "file:$filename" .                  $m_texts = preg_split("/$regexp/im", $string);
                                                                 ( isset($this->end_tag) ? $this->end_tag : '\b' );  
                                                 $this->html = preg_replace("/$regexp2/i", $value, $this->html);                  if ($debug) {
                                 }                          print "_string_transform() DEBUG: m_tags: ";
                           var_dump($m_tags);
                           print "<br>";
   
                           print "_string_transform() DEBUG: m_texts: ";
                           var_dump($m_texts);
                           print "<br>";
                 }                  }
   
                 function is_key($key)                  for ($i = 0; $i < count($m_texts); $i++) {
                 {                          if (strlen($m_texts[$i]) > 0) {
                                 $regexp = $this->start_tag . $key .                                  if ($ar_out_idx > 0 && ! is_array($ar_out[$ar_out_idx - 1])) {
                                                 ( isset($this->end_tag) ? $this->end_tag : '\b' );                                          $ar_out[$ar_out_idx - 1] .= $m_texts[$i];
                                   }
                                   else {
                                           $ar_out[$ar_out_idx] = $m_texts[$i];
                                           $ar_out_idx++;
                                   }
                           }
   
                                 return preg_match("/$regexp/i", $this->html);                          if (strlen($m_tags[$i]) > 0) {
                 }                                  $cmd_parts = explode($this->tag['delim'], $m_tags[$i], 2);
   
                 function get_keys()                                  // toto bol povodny if() - zrejme uz nude na nic, takze
                 {                                  // to mozes pokojne vymazat :)
                                 $regexp = $this->start_tag . '([\w][_\w\d]*)'.                                  //
                                                 ( isset($this->end_tag) ? $this->end_tag : '\b' );                                  //if (! strcasecmp($this->cmd['file'] . $this->tag['delim'],
                                   //                      substr($m_tags[$i], 0, 1 + strlen($this->cmd['file'])))) {
                                   if (! strcasecmp($this->cmd['file'], $cmd_parts[0])
                                                   && strlen($cmd_parts[1]) > 0) {
                                           $filename_with_path = dirname($this->filename) . '/' . $cmd_parts[1];
   
                                           if (file_exists($filename_with_path))
                                                   $ar_out[$ar_out_idx] =
                                                           $this->_string_transform(join('', file($filename_with_path)));
                                           else
                                                   $ar_out[$ar_out_idx] =
                                                           $this->_generate_error("file not found: $filename_with_path");
   
                                 preg_match_all("/$regexp/i", $this->html, $matches);                                          $ar_out_idx++;
                                   }
                                   elseif (! strcasecmp($this->cmd['if'], $cmd_parts[0])) {
                                           $ar_out[$ar_out_idx] = array(
                                                           'key' => $this->cmd['if'],
                                                           'conditions' => strtolower($cmd_parts[1])
                                                           );
                                           $tmp_str = '';
                                           $level = 0;
   
                                           for ($j = $i + 1; $j < count($m_texts); $j++) {
                                                   $cmd_parts2 = explode($this->tag['delim'], $m_tags[$j], 2);
                                                   $tmp_str .= $m_texts[$j];
   
                                 return $matches[1];                                                  if (! strcasecmp($this->cmd['if'], $cmd_parts2[0])) {
                 }                                                          $level++;
                                                   }
                                                   elseif (! $level && ! strcasecmp($this->cmd['else'], $cmd_parts2[0])) {
   
                 function hide()                                                          if (! isset($ar_out[$ar_out_idx]['true'])) {
                 {                                                                  $ar_out[$ar_out_idx]['true'] =
                                 $regexp = $this->start_tag . '\w+';                                                                          $this->_string_transform($tmp_str);
                                 $regexp .= ( isset($this->end_tag) ? $this->end_tag : '\b' );                                                                  $tmp_str = '';
   
                                 $this->html = preg_replace("/($regexp)/i",                                                                  continue;
                                                                 '<!-- ObjTemplate.class.php: hide: \\1 -->', $this->html);                                                          }
                                                   }
                                                   elseif (! strcasecmp($this->cmd['endif'], $cmd_parts2[0])) {
                                                           if ($level)
                                                                   $level--;
                                                           else
                                                                   break;
                                                   }
   
                                 return $this->html();                                                  $tmp_str .= $this->tag['begin'] . $m_tags[$j] . $this->tag['end'];
                 }                                          }
   
                 function clean()                                          if (isset($ar_out[$ar_out_idx]['true']))
                 {                                                  $ar_out[$ar_out_idx]['false'] = $this->_string_transform($tmp_str);
                                 $regexp = $this->start_tag . '\w+' .                                          else
                                                 ( isset($this->end_tag) ? $this->end_tag : '\b' );                                                  $ar_out[$ar_out_idx]['true'] = $this->_string_transform($tmp_str);
   
                                 $this->html = preg_replace("/$regexp/i", '', $this->html);                                          $i = $j;
                                   }
                                   else {
                                           $ar_out[$ar_out_idx] = array('key' => strtolower($m_tags[$i]));
                                   }
   
                                 return $this->html();                                  $ar_out_idx++;
                 }                          }
                           }
   
                 function html()                          return $ar_out;
                 {  
                                 return $this->html;  
                 }                  }
   
                 function html_array()                  function _generate_error($message = "unknown error")
                 {                  {
                                 $regexp =  $this->start_tag . 'include:([^%\b]+)' .                          return $this->cstr['begin']
                                                 ( isset($this->end_tag) ? $this->end_tag : '\b' ) . '';                                  . ' ' . $this->cstr['classname'] . ':'
                                   . ' ' . $message
                                 preg_match_all("/$regexp/is", $this->html, $matches);                                  . ' ' . $this->cstr['end'];
   
                                 return array(preg_split("/$regexp/is", $this->html), $matches[1]);  
                 }                  }
 }  
   
 ?>          }
   
           ?>

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top