library/smarty/libs/sysplugins/smarty_internal_data.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Data
       
     4  * This file contains the basic classes and methods for template and variable creation
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Template
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Base class with template and variable methods
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Template
       
    16  */
       
    17 class Smarty_Internal_Data {
       
    18     /**
       
    19      * name of class used for templates
       
    20      *
       
    21      * @var string
       
    22      */
       
    23     public $template_class = 'Smarty_Internal_Template';
       
    24     /**
       
    25      * template variables
       
    26      *
       
    27      * @var array
       
    28      */
       
    29     public $tpl_vars = array();
       
    30     /**
       
    31      * parent template (if any)
       
    32      *
       
    33      * @var Smarty_Internal_Template
       
    34      */
       
    35     public $parent = null;
       
    36     /**
       
    37      * configuration settings
       
    38      *
       
    39      * @var array
       
    40      */
       
    41     public $config_vars = array();
       
    42 
       
    43     /**
       
    44      * assigns a Smarty variable
       
    45      *
       
    46      * @param  array|string $tpl_var the template variable name(s)
       
    47      * @param  mixed $value the value to assign
       
    48      * @param  boolean $nocache if true any output of this variable will be not cached
       
    49      *
       
    50      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
    51      */
       
    52     public function assign($tpl_var, $value = null, $nocache = false) {
       
    53         if (is_array($tpl_var)) {
       
    54             foreach ($tpl_var as $_key => $_val) {
       
    55                 if ($_key != '') {
       
    56                     $this->tpl_vars[$_key] = new Smarty_Variable($_val, $nocache);
       
    57                 }
       
    58             }
       
    59         } else {
       
    60             if ($tpl_var != '') {
       
    61                 $this->tpl_vars[$tpl_var] = new Smarty_Variable($value, $nocache);
       
    62             }
       
    63         }
       
    64 
       
    65         return $this;
       
    66     }
       
    67 
       
    68     /**
       
    69      * assigns a global Smarty variable
       
    70      *
       
    71      * @param  string $varname the global variable name
       
    72      * @param  mixed $value the value to assign
       
    73      * @param  boolean $nocache if true any output of this variable will be not cached
       
    74      *
       
    75      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
    76      */
       
    77     public function assignGlobal($varname, $value = null, $nocache = false) {
       
    78         if ($varname != '') {
       
    79             Smarty::$global_tpl_vars[$varname] = new Smarty_Variable($value, $nocache);
       
    80             $ptr = $this;
       
    81             while ($ptr instanceof Smarty_Internal_Template) {
       
    82                 $ptr->tpl_vars[$varname] = clone Smarty::$global_tpl_vars[$varname];
       
    83                 $ptr = $ptr->parent;
       
    84             }
       
    85         }
       
    86 
       
    87         return $this;
       
    88     }
       
    89 
       
    90     /**
       
    91      * assigns values to template variables by reference
       
    92      *
       
    93      * @param string $tpl_var the template variable name
       
    94      * @param          $value
       
    95      * @param  boolean $nocache if true any output of this variable will be not cached
       
    96      *
       
    97      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
    98      */
       
    99     public function assignByRef($tpl_var, &$value, $nocache = false) {
       
   100         if ($tpl_var != '') {
       
   101             $this->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
       
   102             $this->tpl_vars[$tpl_var]->value = &$value;
       
   103         }
       
   104 
       
   105         return $this;
       
   106     }
       
   107 
       
   108     /**
       
   109      * appends values to template variables
       
   110      *
       
   111      * @param  array|string $tpl_var the template variable name(s)
       
   112      * @param  mixed $value the value to append
       
   113      * @param  boolean $merge flag if array elements shall be merged
       
   114      * @param  boolean $nocache if true any output of this variable will be not cached
       
   115      *
       
   116      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
   117      */
       
   118     public function append($tpl_var, $value = null, $merge = false, $nocache = false) {
       
   119         if (is_array($tpl_var)) {
       
   120             // $tpl_var is an array, ignore $value
       
   121             foreach ($tpl_var as $_key => $_val) {
       
   122                 if ($_key != '') {
       
   123                     if (!isset($this->tpl_vars[$_key])) {
       
   124                         $tpl_var_inst = $this->getVariable($_key, null, true, false);
       
   125                         if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
       
   126                             $this->tpl_vars[$_key] = new Smarty_Variable(null, $nocache);
       
   127                         } else {
       
   128                             $this->tpl_vars[$_key] = clone $tpl_var_inst;
       
   129                         }
       
   130                     }
       
   131                     if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
       
   132                         settype($this->tpl_vars[$_key]->value, 'array');
       
   133                     }
       
   134                     if ($merge && is_array($_val)) {
       
   135                         foreach ($_val as $_mkey => $_mval) {
       
   136                             $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
       
   137                         }
       
   138                     } else {
       
   139                         $this->tpl_vars[$_key]->value[] = $_val;
       
   140                     }
       
   141                 }
       
   142             }
       
   143         } else {
       
   144             if ($tpl_var != '' && isset($value)) {
       
   145                 if (!isset($this->tpl_vars[$tpl_var])) {
       
   146                     $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
       
   147                     if ($tpl_var_inst instanceof Smarty_Undefined_Variable) {
       
   148                         $this->tpl_vars[$tpl_var] = new Smarty_Variable(null, $nocache);
       
   149                     } else {
       
   150                         $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
       
   151                     }
       
   152                 }
       
   153                 if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
       
   154                     settype($this->tpl_vars[$tpl_var]->value, 'array');
       
   155                 }
       
   156                 if ($merge && is_array($value)) {
       
   157                     foreach ($value as $_mkey => $_mval) {
       
   158                         $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
       
   159                     }
       
   160                 } else {
       
   161                     $this->tpl_vars[$tpl_var]->value[] = $value;
       
   162                 }
       
   163             }
       
   164         }
       
   165 
       
   166         return $this;
       
   167     }
       
   168 
       
   169     /**
       
   170      * appends values to template variables by reference
       
   171      *
       
   172      * @param  string $tpl_var the template variable name
       
   173      * @param  mixed &$value the referenced value to append
       
   174      * @param  boolean $merge flag if array elements shall be merged
       
   175      *
       
   176      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
   177      */
       
   178     public function appendByRef($tpl_var, &$value, $merge = false) {
       
   179         if ($tpl_var != '' && isset($value)) {
       
   180             if (!isset($this->tpl_vars[$tpl_var])) {
       
   181                 $this->tpl_vars[$tpl_var] = new Smarty_Variable();
       
   182             }
       
   183             if (!is_array($this->tpl_vars[$tpl_var]->value)) {
       
   184                 settype($this->tpl_vars[$tpl_var]->value, 'array');
       
   185             }
       
   186             if ($merge && is_array($value)) {
       
   187                 foreach ($value as $_key => $_val) {
       
   188                     $this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
       
   189                 }
       
   190             } else {
       
   191                 $this->tpl_vars[$tpl_var]->value[] = &$value;
       
   192             }
       
   193         }
       
   194 
       
   195         return $this;
       
   196     }
       
   197 
       
   198     /**
       
   199      * Returns a single or all template variables
       
   200      *
       
   201      * @param  string $varname variable name or null
       
   202      * @param  object $_ptr optional pointer to data object
       
   203      * @param  boolean $search_parents include parent templates?
       
   204      *
       
   205      * @return string  variable value or or array of variables
       
   206      */
       
   207     public function getTemplateVars($varname = null, $_ptr = null, $search_parents = true) {
       
   208         if (isset($varname)) {
       
   209             $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
       
   210             if (is_object($_var)) {
       
   211                 return $_var->value;
       
   212             } else {
       
   213                 return null;
       
   214             }
       
   215         } else {
       
   216             $_result = array();
       
   217             if ($_ptr === null) {
       
   218                 $_ptr = $this;
       
   219             }
       
   220             while ($_ptr !== null) {
       
   221                 foreach ($_ptr->tpl_vars AS $key => $var) {
       
   222                     if (!array_key_exists($key, $_result)) {
       
   223                         $_result[$key] = $var->value;
       
   224                     }
       
   225                 }
       
   226                 // not found, try at parent
       
   227                 if ($search_parents) {
       
   228                     $_ptr = $_ptr->parent;
       
   229                 } else {
       
   230                     $_ptr = null;
       
   231                 }
       
   232             }
       
   233             if ($search_parents && isset(Smarty::$global_tpl_vars)) {
       
   234                 foreach (Smarty::$global_tpl_vars AS $key => $var) {
       
   235                     if (!array_key_exists($key, $_result)) {
       
   236                         $_result[$key] = $var->value;
       
   237                     }
       
   238                 }
       
   239             }
       
   240 
       
   241             return $_result;
       
   242         }
       
   243     }
       
   244 
       
   245     /**
       
   246      * clear the given assigned template variable.
       
   247      *
       
   248      * @param  string|array $tpl_var the template variable(s) to clear
       
   249      *
       
   250      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
   251      */
       
   252     public function clearAssign($tpl_var) {
       
   253         if (is_array($tpl_var)) {
       
   254             foreach ($tpl_var as $curr_var) {
       
   255                 unset($this->tpl_vars[$curr_var]);
       
   256             }
       
   257         } else {
       
   258             unset($this->tpl_vars[$tpl_var]);
       
   259         }
       
   260 
       
   261         return $this;
       
   262     }
       
   263 
       
   264     /**
       
   265      * clear all the assigned template variables.
       
   266      *
       
   267      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
   268      */
       
   269     public function clearAllAssign() {
       
   270         $this->tpl_vars = array();
       
   271 
       
   272         return $this;
       
   273     }
       
   274 
       
   275     /**
       
   276      * load a config file, optionally load just selected sections
       
   277      *
       
   278      * @param  string $config_file filename
       
   279      * @param  mixed $sections array of section names, single section or null
       
   280      *
       
   281      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
   282      */
       
   283     public function configLoad($config_file, $sections = null) {
       
   284         // load Config class
       
   285         Smarty_Internal_Extension_Config::configLoad($this, $config_file, $sections);
       
   286         return $this;
       
   287     }
       
   288 
       
   289     /**
       
   290      * gets the object of a Smarty variable
       
   291      *
       
   292      * @param  string $variable the name of the Smarty variable
       
   293      * @param  object $_ptr optional pointer to data object
       
   294      * @param  boolean $search_parents search also in parent data
       
   295      * @param bool $error_enable
       
   296      *
       
   297      * @return object  the object of the variable
       
   298      */
       
   299     public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true) {
       
   300         if ($_ptr === null) {
       
   301             $_ptr = $this;
       
   302         }
       
   303         while ($_ptr !== null) {
       
   304             if (isset($_ptr->tpl_vars[$variable])) {
       
   305                 // found it, return it
       
   306                 return $_ptr->tpl_vars[$variable];
       
   307             }
       
   308             // not found, try at parent
       
   309             if ($search_parents) {
       
   310                 $_ptr = $_ptr->parent;
       
   311             } else {
       
   312                 $_ptr = null;
       
   313             }
       
   314         }
       
   315         if (isset(Smarty::$global_tpl_vars[$variable])) {
       
   316             // found it, return it
       
   317             return Smarty::$global_tpl_vars[$variable];
       
   318         }
       
   319         $smarty = isset($this->smarty) ? $this->smarty : $this;
       
   320         if ($smarty->error_unassigned && $error_enable) {
       
   321             // force a notice
       
   322             $x = $$variable;
       
   323         }
       
   324 
       
   325         return new Smarty_Undefined_Variable;
       
   326     }
       
   327 
       
   328     /**
       
   329      * gets  a config variable
       
   330      *
       
   331      * @param  string $variable the name of the config variable
       
   332      * @param bool $error_enable
       
   333      *
       
   334      * @return mixed  the value of the config variable
       
   335      */
       
   336     public function getConfigVariable($variable, $error_enable = true) {
       
   337         return Smarty_Internal_Extension_Config::getConfigVariable($this, $variable, $error_enable = true);
       
   338     }
       
   339 
       
   340     /**
       
   341      * Returns a single or all config variables
       
   342      *
       
   343      * @param  string $varname variable name or null
       
   344      * @param bool $search_parents
       
   345      *
       
   346      * @return string variable value or or array of variables
       
   347      */
       
   348     public function getConfigVars($varname = null, $search_parents = true) {
       
   349         return Smarty_Internal_Extension_Config::getConfigVars($this, $varname, $search_parents);
       
   350     }
       
   351 
       
   352     /**
       
   353      * Deassigns a single or all config variables
       
   354      *
       
   355      * @param  string $varname variable name or null
       
   356      *
       
   357      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
   358      */
       
   359     public function clearConfig($varname = null) {
       
   360         return Smarty_Internal_Extension_Config::clearConfig($this, $varname);
       
   361     }
       
   362 
       
   363     /**
       
   364      * gets  a stream variable
       
   365      *
       
   366      * @param  string $variable the stream of the variable
       
   367      *
       
   368      * @throws SmartyException
       
   369      * @return mixed  the value of the stream variable
       
   370      */
       
   371     public function getStreamVariable($variable) {
       
   372         $_result = '';
       
   373         $fp = fopen($variable, 'r+');
       
   374         if ($fp) {
       
   375             while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
       
   376                 $_result .= $current_line;
       
   377             }
       
   378             fclose($fp);
       
   379 
       
   380             return $_result;
       
   381         }
       
   382         $smarty = isset($this->smarty) ? $this->smarty : $this;
       
   383         if ($smarty->error_unassigned) {
       
   384             throw new SmartyException('Undefined stream variable "' . $variable . '"');
       
   385         } else {
       
   386             return null;
       
   387         }
       
   388     }
       
   389 }