1<?php
2
3/**
4 * Smarty Method GetTemplateVars
5 *
6 * Smarty::getTemplateVars() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_GetTemplateVars
13{
14    /**
15     * Valid for all objects
16     *
17     * @var int
18     */
19    public $objMap = 7;
20
21    /**
22     * Returns a single or all template variables
23     *
24     * @api  Smarty::getTemplateVars()
25     * @link http://www.smarty.net/docs/en/api.get.template.vars.tpl
26     *
27     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
28     * @param string                                                  $varName       variable name or null
29     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr          optional pointer to data object
30     * @param bool                                                    $searchParents include parent templates?
31     *
32     * @return mixed variable value or or array of variables
33     */
34    public function getTemplateVars(
35        Smarty_Internal_Data $data,
36        $varName = null,
37        Smarty_Internal_Data $_ptr = null,
38        $searchParents = true
39    ) {
40        if (isset($varName)) {
41            $_var = $this->_getVariable($data, $varName, $_ptr, $searchParents, false);
42            if (is_object($_var)) {
43                return $_var->value;
44            } else {
45                return null;
46            }
47        } else {
48            $_result = array();
49            if ($_ptr === null) {
50                $_ptr = $data;
51            }
52            while ($_ptr !== null) {
53                foreach ($_ptr->tpl_vars as $key => $var) {
54                    if (!array_key_exists($key, $_result)) {
55                        $_result[ $key ] = $var->value;
56                    }
57                }
58                // not found, try at parent
59                if ($searchParents && isset($_ptr->parent)) {
60                    $_ptr = $_ptr->parent;
61                } else {
62                    $_ptr = null;
63                }
64            }
65            if ($searchParents && isset(Smarty::$global_tpl_vars)) {
66                foreach (Smarty::$global_tpl_vars as $key => $var) {
67                    if (!array_key_exists($key, $_result)) {
68                        $_result[ $key ] = $var->value;
69                    }
70                }
71            }
72            return $_result;
73        }
74    }
75
76    /**
77     * gets the object of a Smarty variable
78     *
79     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
80     * @param string                                                  $varName       the name of the Smarty variable
81     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr          optional pointer to data object
82     * @param bool                                                    $searchParents search also in parent data
83     * @param bool                                                    $errorEnable
84     *
85     * @return \Smarty_Variable
86     */
87    public function _getVariable(
88        Smarty_Internal_Data $data,
89        $varName,
90        Smarty_Internal_Data $_ptr = null,
91        $searchParents = true,
92        $errorEnable = true
93    ) {
94        if ($_ptr === null) {
95            $_ptr = $data;
96        }
97        while ($_ptr !== null) {
98            if (isset($_ptr->tpl_vars[ $varName ])) {
99                // found it, return it
100                return $_ptr->tpl_vars[ $varName ];
101            }
102            // not found, try at parent
103            if ($searchParents && isset($_ptr->parent)) {
104                $_ptr = $_ptr->parent;
105            } else {
106                $_ptr = null;
107            }
108        }
109        if (isset(Smarty::$global_tpl_vars[ $varName ])) {
110            // found it, return it
111            return Smarty::$global_tpl_vars[ $varName ];
112        }
113        if ($errorEnable && $data->_getSmartyObj()->error_unassigned) {
114            // force a notice
115            $x = $$varName;
116        }
117        return new Smarty_Undefined_Variable;
118    }
119}
120