[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/System/includes/ -> class.Template.inc.php (source)

   1  <?php
   2    /**************************************************************************\
   3    * eGroupWare API - Template class                                          *
   4    * (C) Copyright 1999-2000 NetUSE GmbH Kristian Koehntopp                   *
   5    * ------------------------------------------------------------------------ *
   6    * http://www.egroupware.org/                                               *  
   7    * ------------------------------------------------------------------------ *
   8    * This program is free software; you can redistribute it and/or modify it  *
   9    * under the terms of the GNU Lesser General Public License as published    *
  10    * by the Free Software Foundation; either version 2.1 of the License, or   *
  11    * any later version.                                                       *
  12    \**************************************************************************/
  13  
  14    /* $Id: class.Template.inc.php,v 1.5 2005/11/26 13:01:24 bigmichi1 Exp $ */
  15  
  16      class Template
  17      {
  18          var $classname = 'Template';
  19  
  20          /* if set, echo assignments */
  21          var $debug = False;
  22  
  23          /* $file[handle] = 'filename'; */
  24          var $file = array();
  25  
  26          /* relative filenames are relative to this pathname */
  27          var $root = '';
  28  
  29          /* $varkeys[key] = 'key'; $varvals[key] = 'value'; */
  30          var $varkeys = array();
  31          var $varvals = array();
  32  
  33          /* 'remove'  => remove undefined variables
  34           * 'comment' => replace undefined variables with comments
  35           * 'keep'    => keep undefined variables
  36           */
  37          var $unknowns = 'remove';
  38  
  39          /* 'yes' => halt, 'report' => report error, continue, 'no' => ignore error quietly */
  40          var $halt_on_error = 'yes';
  41  
  42          /* last error message is retained here */
  43          var $last_error = '';
  44  
  45          /***************************************************************************/
  46          /* public: Constructor.
  47           * root:     template directory.
  48           * unknowns: how to handle unknown variables.
  49           */
  50  		function Template($root = '.', $unknowns = 'remove')
  51          {
  52              $this->set_root($root);
  53              $this->set_unknowns($unknowns);
  54          }
  55  
  56          /* public: setroot(pathname $root)
  57           * root:   new template directory.
  58           */
  59  		function set_root($root)
  60          {
  61              if (!is_dir($root))
  62              {
  63                  $this->halt("set_root: $root is not a directory.");
  64                  return false;
  65              }
  66              $this->root = $root;
  67              return true;
  68          }
  69  
  70          /* public: set_unknowns(enum $unknowns)
  71           * unknowns: 'remove', 'comment', 'keep'
  72           *
  73           */
  74  		function set_unknowns($unknowns = 'keep')
  75          {
  76              $this->unknowns = $unknowns;
  77          }
  78  
  79          /* public: set_file(array $filelist)
  80           * filelist: array of handle, filename pairs.
  81           *
  82           * public: set_file(string $handle, string $filename)
  83           * handle: handle for a filename,
  84           * filename: name of template file
  85           */
  86  		function set_file($handle, $filename = '')
  87          {
  88              if (!is_array($handle))
  89              {
  90                  if ($filename == '')
  91                  {
  92                      $this->halt("set_file: For handle $handle filename is empty.");
  93                      return false;
  94                  }
  95                  $this->file[$handle] = $this->filename($filename);
  96              }
  97              else
  98              {
  99                  reset($handle);
 100                  while(list($h, $f) = each($handle))
 101                  {
 102                      $this->file[$h] = $this->filename($f);
 103                  }
 104              }
 105          }
 106  
 107          /* public: set_block(string $parent, string $handle, string $name = '')
 108           * extract the template $handle from $parent, 
 109           * place variable {$name} instead.
 110           */
 111  		function set_block($parent, $handle, $name = '')
 112          {
 113              if (!$this->loadfile($parent))
 114              {
 115                  $this->halt("subst: unable to load $parent.");
 116                  return false;
 117              }
 118              if ($name == '')
 119              {
 120                  $name = $handle;
 121              }
 122              $str = $this->get_var($parent);
 123              $reg = "/<!--\s+BEGIN $handle\s+-->(.*)\n\s*<!--\s+END $handle\s+-->/sm";
 124              preg_match_all($reg, $str, $m);
 125              $str = preg_replace($reg, '{' . "$name}", $str);
 126              $this->set_var($handle, $m[1][0]);
 127              $this->set_var($parent, $str);
 128          }
 129  
 130          /* public: set_var(array $values)
 131           * values: array of variable name, value pairs.
 132           *
 133           * public: set_var(string $varname, string $value)
 134           * varname: name of a variable that is to be defined
 135           * value:   value of that variable
 136           */
 137  		function set_var($varname, $value = '')
 138          {
 139              if (!is_array($varname))
 140              {
 141                  if (!empty($varname))
 142                  {
 143                      if ($this->debug)
 144                      {
 145                          print "scalar: set *$varname* to *$value*<br>\n";
 146                      }
 147                      $this->varkeys[$varname] = $this->varname($varname);
 148                      $this->varvals[$varname] = str_replace('phpGroupWare','eGroupWare',$value);
 149                  }
 150              }
 151              else
 152              {
 153                  reset($varname);
 154                  while(list($k, $v) = each($varname))
 155                  {
 156                      if (!empty($k))
 157                      {
 158                          if ($this->debug)
 159                          {
 160                              print "array: set *$k* to *$v*<br>\n";
 161                          }
 162                          $this->varkeys[$k] = $this->varname($k);
 163                          $this->varvals[$k] = str_replace('phpGroupWare','eGroupWare',$v);
 164                      }
 165                  }
 166              }
 167          }
 168  
 169          /* public: subst(string $handle)
 170           * handle: handle of template where variables are to be substituted.
 171           */
 172  		function subst($handle)
 173          {
 174              if (!$this->loadfile($handle))
 175              {
 176                  $this->halt("subst: unable to load $handle.");
 177                  return false;
 178              }
 179  
 180              $str = $this->get_var($handle);
 181              reset($this->varkeys);
 182              while (list($k, $v) = each($this->varkeys))
 183              {
 184                  $str = str_replace($v, $this->varvals[$k], $str);
 185              }
 186              return $str;
 187          }
 188  
 189          /* public: psubst(string $handle)
 190           * handle: handle of template where variables are to be substituted.
 191           */
 192  		function psubst($handle)
 193          {
 194              print $this->subst($handle);
 195              return false;
 196          }
 197  
 198          /* public: parse(string $target, string $handle, boolean append)
 199           * public: parse(string $target, array  $handle, boolean append)
 200           * target: handle of variable to generate
 201           * handle: handle of template to substitute
 202           * append: append to target handle
 203           */
 204  		function parse($target, $handle, $append = false)
 205          {
 206              if (!is_array($handle))
 207              {
 208                  $str = $this->subst($handle);
 209                  if ($append)
 210                  {
 211                      $this->set_var($target, $this->get_var($target) . $str);
 212                  }
 213                  else
 214                  {
 215                      $this->set_var($target, $str);
 216                  }
 217              }
 218              else
 219              {
 220                  reset($handle);
 221                  while(list($i, $h) = each($handle))
 222                  {
 223                      $str = $this->subst($h);
 224                      $this->set_var($target, $str);
 225                  }
 226              }
 227              return $str;
 228          }
 229  
 230  		function pparse($target, $handle, $append = false)
 231          {
 232              print $this->parse($target, $handle, $append);
 233              return false;
 234          }
 235  
 236          /* This is short for finish parse */
 237          function fp($target, $handle, $append = False)
 238          {
 239              return $this->finish($this->parse($target, $handle, $append));
 240          }
 241  
 242          /* This is a short cut for print finish parse */
 243  		function pfp($target, $handle, $append = False)
 244          {
 245              echo $this->finish($this->parse($target, $handle, $append));
 246          }
 247  
 248          /* public: get_vars()
 249           */
 250  		function get_vars()
 251          {
 252              reset($this->varkeys);
 253              while(list($k, $v) = each($this->varkeys))
 254              {
 255                  $result[$k] = $this->varvals[$k];
 256              }
 257              return $result;
 258          }
 259  
 260          /* public: get_var(string varname)
 261           * varname: name of variable.
 262           *
 263           * public: get_var(array varname)
 264           * varname: array of variable names
 265           */
 266  		function get_var($varname)
 267          {
 268              if (!is_array($varname))
 269              {
 270                  return $this->varvals[$varname];
 271              }
 272              else
 273              {
 274                  reset($varname);
 275                  while(list($k, $v) = each($varname))
 276                  {
 277                      $result[$k] = $this->varvals[$k];
 278                  }
 279                  return $result;
 280              }
 281          }
 282  
 283          /* public: get_undefined($handle)
 284           * handle: handle of a template.
 285           */
 286  		function get_undefined($handle)
 287          {
 288              if (!$this->loadfile($handle))
 289              {
 290                  $this->halt("get_undefined: unable to load $handle.");
 291                  return false;
 292              }
 293  
 294              preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m);
 295              $m = $m[1];
 296              if (!is_array($m))
 297              {
 298                  return false;
 299              }
 300              reset($m);
 301              while(list($k, $v) = each($m))
 302              {
 303                  if (!isset($this->varkeys[$v]))
 304                  {
 305                      $result[$v] = $v;
 306                  }
 307              }
 308  
 309              if (count($result))
 310              {
 311                  return $result;
 312              }
 313              else
 314              {
 315                  return false;
 316              }
 317          }
 318  
 319          /* public: finish(string $str)
 320           * str: string to finish.
 321           */
 322  		function finish($str)
 323          {
 324              switch ($this->unknowns)
 325              {
 326                  case 'keep':
 327                      break;
 328                  case 'remove':
 329                      $str = preg_replace('/{[^ \t\r\n}]+}/', '', $str);
 330                      break;
 331                  case 'comment':
 332                      $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
 333                      break;
 334              }
 335  
 336              return $str;
 337          }
 338  
 339          /* public: p(string $varname)
 340           * varname: name of variable to print.
 341           */
 342          function p($varname)
 343          {
 344              print $this->finish($this->get_var($varname));
 345          }
 346  
 347  		function get($varname)
 348          {
 349              return $this->finish($this->get_var($varname));
 350          }
 351  
 352          /***************************************************************************/
 353          /* private: filename($filename)
 354           * filename: name to be completed.
 355           */
 356  		function filename($filename,$root='',$time=1)
 357          {
 358              if($root=='')
 359              {
 360                  $root=$this->root;
 361              }
 362              if (substr($filename, 0, 1) != '/')
 363              {
 364                  $new_filename = $root.'/'.$filename;
 365              }
 366              else
 367              {
 368                  $new_filename = $filename;
 369              }
 370  
 371              if (!file_exists($new_filename))
 372              {
 373                  if($time==2)
 374                  {
 375                      $this->halt("filename: file $new_filename does not exist.");
 376                  }
 377                  else
 378                  {
 379                      $new_root = str_replace($GLOBALS['egw_info']['server']['template_set'],'default',$root);
 380                      $new_filename = $this->filename(str_replace($root.'/','',$new_filename),$new_root,2);
 381                  }
 382              }
 383              return $new_filename;
 384          }
 385  
 386          /* private: varname($varname)
 387           * varname: name of a replacement variable to be protected.
 388           */
 389  		function varname($varname)
 390          {
 391              return '{'.$varname.'}';
 392          }
 393  
 394          /* private: loadfile(string $handle)
 395           * handle:  load file defined by handle, if it is not loaded yet.
 396           */
 397  		function loadfile($handle)
 398          {
 399              if (isset($this->varkeys[$handle]) and !empty($this->varvals[$handle]))
 400              {
 401                  return true;
 402              }
 403              if (!isset($this->file[$handle]))
 404              {
 405                  $this->halt("loadfile: $handle is not a valid handle.");
 406                  return false;
 407              }
 408              $filename = $this->file[$handle];
 409  
 410              $str = implode('', @file($filename));
 411              if (empty($str))
 412              {
 413                  $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
 414                  return false;
 415              }
 416  
 417              $this->set_var($handle, $str);
 418              return true;
 419          }
 420  
 421          /***************************************************************************/
 422          /* public: halt(string $msg)
 423           * msg:    error message to show.
 424           */
 425  		function halt($msg)
 426          {
 427              $this->last_error = $msg;
 428  
 429              if ($this->halt_on_error != 'no')
 430              {
 431                  $this->haltmsg($msg);
 432              }
 433  
 434              if ($this->halt_on_error == 'yes')
 435              {
 436                  echo('<b>Halted.</b>');
 437              }
 438  
 439              exit;
 440          }
 441  
 442          /* public, override: haltmsg($msg)
 443           * msg: error message to show.
 444           */
 445  		function haltmsg($msg)
 446          {
 447              printf("<b>Template Error:</b> %s<br>\n", $msg);
 448          }
 449      }


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7