source: trunk/include/smarty/libs/sysplugins/smarty_internal_data.php @ 23485

Last change on this file since 23485 was 23485, checked in by rvelices, 11 years ago

Smarty EOL style LF svn property

  • Property svn:eol-style set to LF
File size: 16.9 KB
Line 
1<?php
2/**
3 * Smarty Internal Plugin Data
4 *
5 * This file contains the basic classes and methodes for template and variable creation
6 *
7 * @package Smarty
8 * @subpackage Template
9 * @author Uwe Tews
10 */
11
12/**
13 * Base class with template and variable methodes
14 *
15 * @package Smarty
16 * @subpackage Template
17 */
18class Smarty_Internal_Data {
19
20    /**
21     * name of class used for templates
22     *
23     * @var string
24     */
25    public $template_class = 'Smarty_Internal_Template';
26    /**
27     * template variables
28     *
29     * @var array
30     */
31    public $tpl_vars = array();
32    /**
33     * parent template (if any)
34     *
35     * @var Smarty_Internal_Template
36     */
37    public $parent = null;
38    /**
39     * configuration settings
40     *
41     * @var array
42     */
43    public $config_vars = array();
44
45    /**
46     * assigns a Smarty variable
47     *
48     * @param array|string $tpl_var the template variable name(s)
49     * @param mixed        $value   the value to assign
50     * @param boolean      $nocache if true any output of this variable will be not cached
51     * @param boolean $scope the scope the variable will have  (local,parent or root)
52     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
53     */
54    public function assign($tpl_var, $value = null, $nocache = false)
55    {
56        if (is_array($tpl_var)) {
57            foreach ($tpl_var as $_key => $_val) {
58                if ($_key != '') {
59                    $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache);
60                }
61            }
62        } else {
63            if ($tpl_var != '') {
64                $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache);
65            }
66        }
67
68        return $this;
69    }
70
71    /**
72     * assigns a global Smarty variable
73     *
74     * @param string $varname the global variable name
75     * @param mixed  $value   the value to assign
76     * @param boolean $nocache if true any output of this variable will be not cached
77     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
78     */
79    public function assignGlobal($varname, $value = null, $nocache = false)
80    {
81        if ($varname != '') {
82            Smarty::$global_tpl_vars[$varname] = new Smarty_variable($value, $nocache);
83            $ptr = $this;
84            while ($ptr instanceof Smarty_Internal_Template) {
85                $ptr->tpl_vars[$varname] = clone Smarty::$global_tpl_vars[$varname];
86                $ptr = $ptr->parent;
87            }
88        }
89
90        return $this;
91    }
92    /**
93     * assigns values to template variables by reference
94     *
95     * @param string $tpl_var the template variable name
96     * @param mixed $ &$value the referenced value to assign
97     * @param boolean $nocache if true any output of this variable will be not cached
98     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
99     */
100    public function assignByRef($tpl_var, &$value, $nocache = false)
101    {
102        if ($tpl_var != '') {
103            $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
104            $this->tpl_vars[$tpl_var]->value = &$value;
105        }
106
107        return $this;
108    }
109
110    /**
111     * appends values to template variables
112     *
113     * @param array|string $tpl_var the template variable name(s)
114     * @param mixed        $value   the value to append
115     * @param boolean      $merge   flag if array elements shall be merged
116     * @param boolean $nocache if true any output of this variable will be not cached
117     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
118     */
119    public function append($tpl_var, $value = null, $merge = false, $nocache = false)
120    {
121        if (is_array($tpl_var)) {
122            // $tpl_var is an array, ignore $value
123            foreach ($tpl_var as $_key => $_val) {
124                if ($_key != '') {
125                    if (!isset($this->tpl_vars[$_key])) {
126                        $tpl_var_inst = $this->getVariable($_key, null, true, false);
127                        if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
128                            $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache);
129                        } else {
130                            $this->tpl_vars[$_key] = clone $tpl_var_inst;
131                        }
132                    }
133                    if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
134                        settype($this->tpl_vars[$_key]->value, 'array');
135                    }
136                    if ($merge && is_array($_val)) {
137                        foreach($_val as $_mkey => $_mval) {
138                            $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
139                        }
140                    } else {
141                        $this->tpl_vars[$_key]->value[] = $_val;
142                    }
143                }
144            }
145        } else {
146            if ($tpl_var != '' && isset($value)) {
147                if (!isset($this->tpl_vars[$tpl_var])) {
148                    $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
149                    if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
150                        $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
151                    } else {
152                        $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
153                    }
154                }
155                if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
156                    settype($this->tpl_vars[$tpl_var]->value, 'array');
157                }
158                if ($merge && is_array($value)) {
159                    foreach($value as $_mkey => $_mval) {
160                        $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
161                    }
162                } else {
163                    $this->tpl_vars[$tpl_var]->value[] = $value;
164                }
165            }
166        }
167
168        return $this;
169    }
170
171    /**
172     * appends values to template variables by reference
173     *
174     * @param string $tpl_var the template variable name
175     * @param mixed  &$value  the referenced value to append
176     * @param boolean $merge  flag if array elements shall be merged
177     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
178     */
179    public function appendByRef($tpl_var, &$value, $merge = false)
180    {
181        if ($tpl_var != '' && isset($value)) {
182            if (!isset($this->tpl_vars[$tpl_var])) {
183                $this->tpl_vars[$tpl_var] = new Smarty_variable();
184            }
185            if (!is_array($this->tpl_vars[$tpl_var]->value)) {
186                settype($this->tpl_vars[$tpl_var]->value, 'array');
187            }
188            if ($merge && is_array($value)) {
189                foreach($value as $_key => $_val) {
190                    $this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
191                }
192            } else {
193                $this->tpl_vars[$tpl_var]->value[] = &$value;
194            }
195        }
196
197        return $this;
198    }
199
200    /**
201     * Returns a single or all template variables
202     *
203     * @param string  $varname        variable name or null
204     * @param string  $_ptr           optional pointer to data object
205     * @param boolean $search_parents include parent templates?
206     * @return string variable value or or array of variables
207     */
208    public function getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
209    {
210        if (isset($varname)) {
211            $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
212            if (is_object($_var)) {
213                return $_var->value;
214            } else {
215                return null;
216            }
217        } else {
218            $_result = array();
219            if ($_ptr === null) {
220                $_ptr = $this;
221            } while ($_ptr !== null) {
222                foreach ($_ptr->tpl_vars AS $key => $var) {
223                    if (!array_key_exists($key, $_result)) {
224                        $_result[$key] = $var->value;
225                    }
226                }
227                // not found, try at parent
228                if ($search_parents) {
229                    $_ptr = $_ptr->parent;
230                } else {
231                    $_ptr = null;
232                }
233            }
234            if ($search_parents && isset(Smarty::$global_tpl_vars)) {
235                foreach (Smarty::$global_tpl_vars AS $key => $var) {
236                    if (!array_key_exists($key, $_result)) {
237                        $_result[$key] = $var->value;
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     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
250     */
251    public function clearAssign($tpl_var)
252    {
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     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
267     */
268    public function clearAllAssign()
269    {
270        $this->tpl_vars = array();
271        return $this;
272    }
273
274    /**
275     * load a config file, optionally load just selected sections
276     *
277     * @param string $config_file filename
278     * @param mixed  $sections    array of section names, single section or null
279     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
280     */
281    public function configLoad($config_file, $sections = null)
282    {
283        // load Config class
284        $config = new Smarty_Internal_Config($config_file, $this->smarty, $this);
285        $config->loadConfigVars($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     * @return object the object of the variable
296     */
297    public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
298    {
299        if ($_ptr === null) {
300            $_ptr = $this;
301        } while ($_ptr !== null) {
302            if (isset($_ptr->tpl_vars[$variable])) {
303                // found it, return it
304                return $_ptr->tpl_vars[$variable];
305            }
306            // not found, try at parent
307            if ($search_parents) {
308                $_ptr = $_ptr->parent;
309            } else {
310                $_ptr = null;
311            }
312        }
313        if (isset(Smarty::$global_tpl_vars[$variable])) {
314            // found it, return it
315            return Smarty::$global_tpl_vars[$variable];
316        }
317        if ($this->smarty->error_unassigned && $error_enable) {
318            // force a notice
319            $x = $$variable;
320        }
321        return new Undefined_Smarty_Variable;
322    }
323
324    /**
325     * gets  a config variable
326     *
327     * @param string $variable the name of the config variable
328     * @return mixed the value of the config variable
329     */
330    public function getConfigVariable($variable, $error_enable = true)
331    {
332        $_ptr = $this;
333        while ($_ptr !== null) {
334            if (isset($_ptr->config_vars[$variable])) {
335                // found it, return it
336                return $_ptr->config_vars[$variable];
337            }
338            // not found, try at parent
339            $_ptr = $_ptr->parent;
340        }
341        if ($this->smarty->error_unassigned && $error_enable) {
342            // force a notice
343            $x = $$variable;
344        }
345        return null;
346    }
347
348    /**
349     * gets  a stream variable
350     *
351     * @param string $variable the stream of the variable
352     * @return mixed the value of the stream variable
353     */
354    public function getStreamVariable($variable)
355    {
356        $_result = '';
357        $fp = fopen($variable, 'r+');
358        if ($fp) {
359            while (!feof($fp) && ($current_line = fgets($fp)) !== false ) {
360                $_result .= $current_line;
361            }
362            fclose($fp);
363            return $_result;
364        }
365
366        if ($this->smarty->error_unassigned) {
367            throw new SmartyException('Undefined stream variable "' . $variable . '"');
368        } else {
369            return null;
370        }
371    }
372
373    /**
374     * Returns a single or all config variables
375     *
376     * @param string $varname variable name or null
377     * @return string variable value or or array of variables
378     */
379    public function getConfigVars($varname = null, $search_parents = true)
380    {
381        $_ptr = $this;
382        $var_array = array();
383        while ($_ptr !== null) {
384            if (isset($varname)) {
385                if (isset($_ptr->config_vars[$varname])) {
386                    return $_ptr->config_vars[$varname];
387                }
388            } else {
389                $var_array = array_merge($_ptr->config_vars, $var_array);
390            }
391             // not found, try at parent
392            if ($search_parents) {
393                $_ptr = $_ptr->parent;
394            } else {
395                $_ptr = null;
396            }
397        }
398        if (isset($varname)) {
399            return '';
400        } else {
401            return $var_array;
402        }
403    }
404
405    /**
406     * Deassigns a single or all config variables
407     *
408     * @param string $varname variable name or null
409     * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
410     */
411    public function clearConfig($varname = null)
412    {
413        if (isset($varname)) {
414            unset($this->config_vars[$varname]);
415        } else {
416            $this->config_vars = array();
417        }
418        return $this;
419    }
420
421}
422
423/**
424 * class for the Smarty data object
425 *
426 * The Smarty data object will hold Smarty variables in the current scope
427 *
428 * @package Smarty
429 * @subpackage Template
430 */
431class Smarty_Data extends Smarty_Internal_Data {
432
433    /**
434     * Smarty object
435     *
436     * @var Smarty
437     */
438    public $smarty = null;
439
440    /**
441     * create Smarty data object
442     *
443     * @param Smarty|array $_parent  parent template
444     * @param Smarty       $smarty   global smarty instance
445     */
446    public function __construct ($_parent = null, $smarty = null)
447    {
448        $this->smarty = $smarty;
449        if (is_object($_parent)) {
450            // when object set up back pointer
451            $this->parent = $_parent;
452        } elseif (is_array($_parent)) {
453            // set up variable values
454            foreach ($_parent as $_key => $_val) {
455                $this->tpl_vars[$_key] = new Smarty_variable($_val);
456            }
457        } elseif ($_parent != null) {
458            throw new SmartyException("Wrong type for template variables");
459        }
460    }
461
462}
463
464/**
465 * class for the Smarty variable object
466 *
467 * This class defines the Smarty variable object
468 *
469 * @package Smarty
470 * @subpackage Template
471 */
472class Smarty_Variable {
473
474    /**
475     * template variable
476     *
477     * @var mixed
478     */
479    public $value = null;
480    /**
481     * if true any output of this variable will be not cached
482     *
483     * @var boolean
484     */
485    public $nocache = false;
486    /**
487     * the scope the variable will have  (local,parent or root)
488     *
489     * @var int
490     */
491    public $scope = Smarty::SCOPE_LOCAL;
492
493    /**
494     * create Smarty variable object
495     *
496     * @param mixed   $value   the value to assign
497     * @param boolean $nocache if true any output of this variable will be not cached
498     * @param int     $scope   the scope the variable will have  (local,parent or root)
499     */
500    public function __construct($value = null, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
501    {
502        $this->value = $value;
503        $this->nocache = $nocache;
504        $this->scope = $scope;
505    }
506
507    /**
508     * <<magic>> String conversion
509     *
510     * @return string
511     */
512    public function __toString()
513    {
514        return (string) $this->value;
515    }
516
517}
518
519/**
520 * class for undefined variable object
521 *
522 * This class defines an object for undefined variable handling
523 *
524 * @package Smarty
525 * @subpackage Template
526 */
527class Undefined_Smarty_Variable {
528
529    /**
530     * Returns FALSE for 'nocache' and NULL otherwise.
531     *
532     * @param string $name
533     * @return bool
534     */
535    public function __get($name)
536    {
537        if ($name == 'nocache') {
538            return false;
539        } else {
540            return null;
541        }
542    }
543
544    /**
545     * Always returns an empty string.
546     *
547     * @return string
548     */
549    public function __toString()
550    {
551        return "";
552    }
553
554}
555
556?>
Note: See TracBrowser for help on using the repository browser.