Code

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