[ Index ]
 

Code source de SugarCRM 5.0.0beta1

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

title

Body

[fermer]

/XTemplate/ -> xtpl.php (source)

   1  <?php
   2  
   3  class XTemplate {
   4  
   5  /*

   6      xtemplate class 0.2.4-3

   7      html generation with templates - fast & easy

   8      copyright (c) 2000 barnab�s debreceni [cranx@users.sourceforge.net]

   9      code optimization by Ivar Smolin <okul@linux.ee> 14-march-2001

  10      latest stable & CVS version always available @ http://sourceforge.net/projects/xtpl

  11  

  12      tested with php 3.0.11 and 4.0.4pl1

  13  

  14      This program is free software; you can redistribute it and/or

  15      modify it under the terms of the GNU Lesser General Public License

  16      version 2.1 as published by the Free Software Foundation.

  17  

  18      This library is distributed in the hope that it will be useful,

  19      but WITHOUT ANY WARRANTY; without even the implied warranty of

  20      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

  21      GNU Lesser General Public License for more details at

  22      http://www.gnu.org/copyleft/lgpl.html

  23  

  24      You should have received a copy of the GNU General Public License

  25      along with this program; if not, write to the Free Software

  26      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  27  

  28  

  29  */
  30  
  31  /***[ variables ]***********************************************************/

  32  
  33  var $filecontents="";                                /* raw contents of template file */
  34  var $blocks=array();                                /* unparsed blocks */
  35  var $parsed_blocks=array();                    /* parsed blocks */
  36  var $block_parse_order=array();            /* block parsing order for recursive parsing (sometimes reverse:) */
  37  var $sub_blocks=array();                        /* store sub-block names for fast resetting */
  38  var $VARS=array();                                    /* variables array */
  39  var $alternate_include_directory = "";
  40  
  41  var $file_delim="/\{FILE\s*\"([^\"]+)\"\s*\}/m";  /* regexp for file includes */
  42  var $block_start_delim="<!-- ";            /* block start delimiter */
  43  var $block_end_delim="-->";                    /* block end delimiter */
  44  var $block_start_word="BEGIN:";            /* block start word */
  45  var $block_end_word="END:";                    /* block end word */
  46  
  47  /* this makes the delimiters look like: <!-- BEGIN: block_name --> if you use my syntax. */

  48  
  49  var $NULL_STRING=array(""=>"");                /* null string for unassigned vars */
  50  var $NULL_BLOCK=array(""=>"");    /* null string for unassigned blocks */
  51  var $mainblock="";
  52  var $ERROR="";
  53  var $AUTORESET=1;                                        /* auto-reset sub blocks */
  54  
  55  /***[ constructor ]*********************************************************/

  56  
  57  function XTemplate ($file, $alt_include = "", $mainblock="main") {
  58      $this->alternate_include_directory = $alt_include;
  59      $this->mainblock=$mainblock;
  60      $this->filecontents=$this->r_getfile($file);    /* read in template file */
  61      //if(substr_count($file, 'backup') == 1)_ppd($this->filecontents);

  62      $this->blocks=$this->maketree($this->filecontents,$mainblock);    /* preprocess some stuff */
  63      //$this->scan_globals();

  64  }
  65  
  66  
  67  /***************************************************************************/

  68  /***[ public stuff ]********************************************************/

  69  /***************************************************************************/

  70  
  71  
  72  /***[ assign ]**************************************************************/

  73  /*

  74      assign a variable

  75  */
  76  
  77  function assign ($name,$val="") {
  78      if (gettype($name)=="array")
  79          while (list($k,$v)=each($name))
  80              $this->VARS[$k]=$v;
  81      else
  82          $this->VARS[$name]=$val;
  83  }
  84  
  85  function append ($varname, $name,$val="") {
  86      if(!isset($this->VARS[$varname])){
  87          $this->VARS[$varname] = array();
  88      }
  89     if(is_array($this->VARS[$varname])){
  90         $this->VARS[$varname][$name] = $val;
  91      }       
  92  }
  93  
  94  /***[ parse ]***************************************************************/

  95  /*

  96      parse a block

  97  */
  98  
  99  function parse ($bname) {
 100      global $sugar_version, $sugar_config;
 101      
 102      $this->assign('SUGAR_VERSION', $sugar_version);
 103      $this->assign('JS_CUSTOM_VERSION', $sugar_config['js_custom_version']);
 104      
 105      if(empty($this->blocks[$bname]))
 106          return;
 107  
 108      $copy=$this->blocks[$bname];
 109      if (!isset($this->blocks[$bname]))
 110          $this->set_error ("parse: blockname [$bname] does not exist");
 111      preg_match_all("/\{([A-Za-z0-9\._]+?)}/",$this->blocks[$bname],$var_array);
 112      $var_array=$var_array[1];
 113      while (list($k,$v)=each($var_array)) {
 114          $sub=explode(".",$v);
 115          if ($sub[0]=="_BLOCK_") {
 116              unset($sub[0]);
 117              $bname2=implode(".",$sub);
 118  
 119              if(array_key_exists($bname2, $this->parsed_blocks))
 120              {
 121                  $var=$this->parsed_blocks[$bname2];
 122              }
 123              else
 124              {
 125                  $var = null;
 126              }
 127  
 128              $nul=(!isset($this->NULL_BLOCK[$bname2])) ? $this->NULL_BLOCK[""] : $this->NULL_BLOCK[$bname2];
 129              $var=(empty($var))?$nul:trim($var);
 130              // Commented out due to regular expression issue with '$' in replacement string.

 131              //$copy=preg_replace("/\{".$v."\}/","$var",$copy);

 132              // This should be faster and work better for '$'

 133              $copy=str_replace("{".$v."}",$var,$copy);
 134          } else {
 135              $var=$this->VARS;
 136  
 137              while(list($k1,$v1)=each($sub))
 138              {
 139                  if(is_array($var) && array_key_exists($v1, $var))
 140                  {
 141                      $var=$var[$v1];
 142                  }
 143                  else
 144                  {
 145                      $var = null;
 146                  }
 147              }
 148  
 149              $nul=(!isset($this->NULL_STRING[$v])) ? ($this->NULL_STRING[""]) : ($this->NULL_STRING[$v]);
 150              $var=(!isset($var))?$nul:$var;
 151              // Commented out due to regular expression issue with '$' in replacement string.

 152              //$copy=preg_replace("/\{$v\}/","$var",$copy);

 153              // This should be faster and work better for '$'

 154  
 155              // this was periodically returning an array to string conversion error....

 156              if(!is_array($var))
 157              {
 158                  $copy=str_replace("{".$v."}",$var,$copy);
 159              }
 160          }
 161      }
 162  
 163      if(array_key_exists($bname, $this->parsed_blocks))
 164      {
 165          $this->parsed_blocks[$bname].=$copy;
 166      }
 167      else
 168      {
 169          $this->parsed_blocks[$bname]=$copy;
 170      }
 171  
 172      // reset sub-blocks

 173      if ($this->AUTORESET && (!empty($this->sub_blocks[$bname]))) {
 174          reset($this->sub_blocks[$bname]);
 175          while (list($k,$v)=each($this->sub_blocks[$bname]))
 176              $this->reset($v);
 177      }
 178  }
 179  
 180  /***[ exists ]**************************************************************/

 181  /*

 182      returns true if a block exists otherwise returns false.

 183  */
 184  function exists($bname){
 185      return (!empty($this->parsed_blocks[$bname])) || (!empty($this->blocks[$bname]));
 186  }
 187  
 188  
 189  /***[ var_exists ]**************************************************************/

 190  /*

 191      returns true if a block exists otherwise returns false.

 192  */
 193  function var_exists($bname,$vname){
 194      if(!empty($this->blocks[$bname])){
 195          return substr_count($this->blocks[$bname], '{'. $vname . '}') >0;
 196      }
 197      return false;
 198  }
 199  
 200  
 201  /***[ rparse ]**************************************************************/

 202  /*

 203      returns the parsed text for a block, including all sub-blocks.

 204  */
 205  
 206  function rparse($bname) {
 207      if (!empty($this->sub_blocks[$bname])) {
 208          reset($this->sub_blocks[$bname]);
 209          while (list($k,$v)=each($this->sub_blocks[$bname]))
 210              if (!empty($v))
 211                  $this->rparse($v,$indent."\t");
 212      }
 213      $this->parse($bname);
 214  }
 215  
 216  /***[ insert_loop ]*********************************************************/

 217  /*

 218      inserts a loop ( call assign & parse )

 219  */
 220  
 221  function insert_loop($bname,$var,$value="") {
 222      $this->assign($var,$value);
 223      $this->parse($bname);
 224  }
 225  
 226  /***[ text ]****************************************************************/

 227  /*

 228      returns the parsed text for a block

 229  */
 230  
 231  function text($bname) {
 232     
 233      //_pp($bname);

 234      //_ppd($this->parsed_blocks);

 235      return $this->parsed_blocks[isset($bname) ? $bname :$this->mainblock];
 236  }
 237  
 238  /***[ out ]*****************************************************************/

 239  /*

 240      prints the parsed text

 241  */
 242  
 243  function out ($bname) {
 244      global $focus;
 245      
 246      if(isset($focus)){
 247          global $action;
 248          
 249          if($focus && is_subclass_of($focus, 'SugarBean') && !$focus->ACLAccess($action)){
 250              
 251              ACLController::displayNoAccess(true);
 252          
 253              sugar_die('');
 254              return;
 255      }}
 256      echo $this->text($bname);
 257  }
 258  
 259  /***[ reset ]***************************************************************/

 260  /*

 261      resets the parsed text

 262  */
 263  
 264  function reset ($bname) {
 265      $this->parsed_blocks[$bname]="";
 266  }
 267  
 268  /***[ parsed ]**************************************************************/

 269  /*

 270      returns true if block was parsed, false if not

 271  */
 272  
 273  function parsed ($bname) {
 274      return (!empty($this->parsed_blocks[$bname]));
 275  }
 276  
 277  /***[ SetNullString ]*******************************************************/

 278  /*

 279      sets the string to replace in case the var was not assigned

 280  */
 281  
 282  function SetNullString($str,$varname="") {
 283      $this->NULL_STRING[$varname]=$str;
 284  }
 285  
 286  /***[ SetNullBlock ]********************************************************/

 287  /*

 288      sets the string to replace in case the block was not parsed

 289  */
 290  
 291  function SetNullBlock($str,$bname="") {
 292      $this->NULL_BLOCK[$bname]=$str;
 293  }
 294  
 295  /***[ set_autoreset ]*******************************************************/

 296  /*

 297      sets AUTORESET to 1. (default is 1)

 298      if set to 1, parse() automatically resets the parsed blocks' sub blocks

 299      (for multiple level blocks)

 300  */
 301  
 302  function set_autoreset() {
 303      $this->AUTORESET=1;
 304  }
 305  
 306  /***[ clear_autoreset ]*****************************************************/

 307  /*

 308      sets AUTORESET to 0. (default is 1)

 309      if set to 1, parse() automatically resets the parsed blocks' sub blocks

 310      (for multiple level blocks)

 311  */
 312  
 313  function clear_autoreset() {
 314      $this->AUTORESET=0;
 315  }
 316  
 317  /***[ scan_globals ]********************************************************/

 318  /*

 319      scans global variables

 320  */
 321  
 322  function scan_globals() {
 323      reset($GLOBALS);
 324      while (list($k,$v)=each($GLOBALS))
 325          $GLOB[$k]=$v;
 326      $this->assign("PHP",$GLOB);    /* access global variables as {PHP.HTTP_HOST} in your template! */
 327  }
 328  
 329  /******

 330  

 331          WARNING

 332          PUBLIC FUNCTIONS BELOW THIS LINE DIDN'T GET TESTED

 333  

 334  ******/
 335  
 336  
 337  /***************************************************************************/

 338  /***[ private stuff ]*******************************************************/

 339  /***************************************************************************/

 340  
 341  /***[ maketree ]************************************************************/

 342  /*

 343      generates the array containing to-be-parsed stuff:

 344    $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc.

 345      also builds the reverse parse order.

 346  */
 347  
 348  
 349  function maketree($con,$block) {
 350      $con2=explode($this->block_start_delim,$con);
 351      $level=0;
 352      $block_names=array();
 353      $blocks=array();
 354      reset($con2);
 355      while(list($k,$v)=each($con2)) {
 356          $patt="($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)";
 357          if (preg_match_all("/$patt/ims",$v,$res, PREG_SET_ORDER)) {
 358              // $res[0][1] = BEGIN or END

 359              // $res[0][2] = block name

 360              // $res[0][3] = kinda content

 361              if ($res[0][1]==$this->block_start_word) {
 362                  $parent_name=implode(".",$block_names);
 363                  $block_names[++$level]=$res[0][2];                            /* add one level - array("main","table","row")*/
 364                  $cur_block_name=implode(".",$block_names);    /* make block name (main.table.row) */
 365                  $this->block_parse_order[]=$cur_block_name;    /* build block parsing order (reverse) */
 366  
 367                  if(array_key_exists($cur_block_name, $blocks))
 368                  {
 369                      $blocks[$cur_block_name].=$res[0][3];                /* add contents */
 370                  }
 371                  else
 372                  {
 373                      $blocks[$cur_block_name]=$res[0][3];                /* add contents */
 374                  }
 375  
 376                  /* add {_BLOCK_.blockname} string to parent block */

 377                  if(array_key_exists($parent_name, $blocks))
 378                  {
 379                      $blocks[$parent_name].="{_BLOCK_.$cur_block_name}";
 380                  }
 381                  else
 382                  {
 383                      $blocks[$parent_name]="{_BLOCK_.$cur_block_name}";
 384                  }
 385  
 386                  $this->sub_blocks[$parent_name][]=$cur_block_name;        /* store sub block names for autoresetting and recursive parsing */
 387                  $this->sub_blocks[$cur_block_name][]="";        /* store sub block names for autoresetting */
 388              } else if ($res[0][1]==$this->block_end_word) {
 389                  unset($block_names[$level--]);
 390                  $parent_name=implode(".",$block_names);
 391                  $blocks[$parent_name].=$res[0][3];    /* add rest of block to parent block */
 392                }
 393          } else { /* no block delimiters found */
 394              $index = implode(".",$block_names);
 395              if(array_key_exists($index, $blocks))
 396              {
 397                  $blocks[].=$this->block_start_delim.$v;
 398              }
 399              else
 400              {
 401                  $blocks[]=$this->block_start_delim.$v;
 402              }
 403          }
 404      }
 405      return $blocks;
 406  }
 407  
 408  
 409  
 410  /***[ error stuff ]*********************************************************/

 411  /*

 412      sets and gets error

 413  */
 414  
 415  function get_error()    {
 416      return ($this->ERROR=="")?0:$this->ERROR;
 417  }
 418  
 419  
 420  function set_error($str)    {
 421      $this->ERROR=$str;
 422  }
 423  
 424  /***[ getfile ]*************************************************************/

 425  /*

 426      returns the contents of a file

 427  */
 428  
 429  function getfile($file) {
 430      if (!isset($file)) {
 431          $this->set_error("!isset file name!");
 432          return "";
 433      }
 434  
 435      // Pick which folder we should include from

 436      // Prefer the local directory, then try the theme directory.

 437      if (!is_file($file))
 438          $file = $this->alternate_include_directory.$file;
 439  
 440      if(is_file($file))
 441      {
 442          if (!($fh=fopen($file,"r"))) {
 443              $this->set_error("Cannot open file: $file");
 444              return "";
 445          }
 446  
 447          $file_text=fread($fh,filesize($file));
 448          fclose($fh);
 449      } else {
 450          $this->set_error("[$file] does not exist");
 451          $file_text="<b>__XTemplate fatal error: file [$file] does not exist__</b>";
 452      }
 453  
 454      return $file_text;
 455  }
 456  
 457  /***[ r_getfile ]***********************************************************/

 458  /*

 459      recursively gets the content of a file with {FILE "filename.tpl"} directives

 460  */
 461  
 462  
 463  function r_getfile($file) {
 464      $text=$this->getfile($file);
 465      while (preg_match($this->file_delim,$text,$res)) {
 466          $text2=$this->getfile($res[1]);
 467          $text=preg_replace("'".preg_quote($res[0])."'",$text2,$text);
 468      }
 469      return $text;
 470  }
 471  
 472  } /* end of XTemplate class. */
 473  
 474  ?>


Généré le : Tue Sep 11 10:48:47 2007 par Balluche grâce à PHPXref 0.7