[ Index ]
 

Code source de eGroupWare 1.2.106-2

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

title

Body

[fermer]

/phpgwapi/inc/ -> class.Template.inc.php (source)

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


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7