1<?php
2/**
3 * Smarty Internal Plugin Compile If
4 * Compiles the {if} {else} {elseif} {/if} tags
5 *
6 * @package    Smarty
7 * @subpackage Compiler
8 * @author     Uwe Tews
9 */
10
11/**
12 * Smarty Internal Plugin Compile If Class
13 *
14 * @package    Smarty
15 * @subpackage Compiler
16 */
17class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase
18{
19    /**
20     * Compiles code for the {if} tag
21     *
22     * @param array                                 $args      array with attributes from parser
23     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
24     * @param array                                 $parameter array with compilation parameter
25     *
26     * @return string compiled code
27     * @throws \SmartyCompilerException
28     */
29    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
30    {
31        // check and get attributes
32        $_attr = $this->getAttributes($compiler, $args);
33        $this->openTag($compiler, 'if', array(1, $compiler->nocache));
34        // must whole block be nocache ?
35        $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
36
37        if (!array_key_exists("if condition", $parameter)) {
38            $compiler->trigger_template_error("missing if condition", null, true);
39        }
40
41        if (is_array($parameter['if condition'])) {
42            if ($compiler->nocache) {
43                $_nocache = ',true';
44                // create nocache var to make it know for further compiling
45                if (is_array($parameter['if condition']['var'])) {
46                    $var = trim($parameter['if condition']['var']['var'], "'");
47                } else {
48                    $var = trim($parameter['if condition']['var'], "'");
49                }
50                if (isset($compiler->template->tpl_vars[$var])) {
51                    $compiler->template->tpl_vars[$var]->nocache = true;
52                } else {
53                    $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
54                }
55            } else {
56                $_nocache = '';
57            }
58            if (is_array($parameter['if condition']['var'])) {
59                $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
60                    "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
61                    "]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . $parameter['if condition']['var']['var'] .
62                    "$_nocache);\n";
63                $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
64                    $parameter['if condition']['var']['smarty_internal_index'] . " = " .
65                    $parameter['if condition']['value'] . ") {?>";
66            } else {
67                $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] .
68                    "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] .
69                    "] = new Smarty_Variable(null{$_nocache});";
70                $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " .
71                    $parameter['if condition']['value'] . ") {?>";
72            }
73
74            return $_output;
75        } else {
76            return "<?php if ({$parameter['if condition']}) {?>";
77        }
78    }
79}
80
81/**
82 * Smarty Internal Plugin Compile Else Class
83 *
84 * @package    Smarty
85 * @subpackage Compiler
86 */
87class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase
88{
89    /**
90     * Compiles code for the {else} tag
91     *
92     * @param array                                 $args      array with attributes from parser
93     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
94     * @param array                                 $parameter array with compilation parameter
95     *
96     * @return string compiled code
97     */
98    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
99    {
100        list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
101        $this->openTag($compiler, 'else', array($nesting, $compiler->tag_nocache));
102
103        return "<?php } else { ?>";
104    }
105}
106
107/**
108 * Smarty Internal Plugin Compile ElseIf Class
109 *
110 * @package    Smarty
111 * @subpackage Compiler
112 */
113class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase
114{
115    /**
116     * Compiles code for the {elseif} tag
117     *
118     * @param array                                 $args      array with attributes from parser
119     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
120     * @param array                                 $parameter array with compilation parameter
121     *
122     * @return string compiled code
123     * @throws \SmartyCompilerException
124     */
125    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
126    {
127        // check and get attributes
128        $_attr = $this->getAttributes($compiler, $args);
129
130        list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
131
132        if (!array_key_exists("if condition", $parameter)) {
133            $compiler->trigger_template_error("missing elseif condition", null, true);
134        }
135
136        if (is_array($parameter['if condition'])) {
137            $condition_by_assign = true;
138            if ($compiler->nocache) {
139                $_nocache = ',true';
140                // create nocache var to make it know for further compiling
141                if (is_array($parameter['if condition']['var'])) {
142                    $var = trim($parameter['if condition']['var']['var'], "'");
143                } else {
144                    $var = trim($parameter['if condition']['var'], "'");
145                }
146                if (isset($compiler->template->tpl_vars[$var])) {
147                    $compiler->template->tpl_vars[$var]->nocache = true;
148                } else {
149                    $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
150                }
151            } else {
152                $_nocache = '';
153            }
154        } else {
155            $condition_by_assign = false;
156        }
157
158        if (empty($compiler->prefix_code)) {
159            if ($condition_by_assign) {
160                $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
161                if (is_array($parameter['if condition']['var'])) {
162                    $_output = "<?php } else { if (!isset(\$_smarty_tpl->tpl_vars[" .
163                        $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" .
164                        $parameter['if condition']['var']['var'] .
165                        "]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " .
166                        $parameter['if condition']['var']['var'] . "$_nocache);\n";
167                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
168                        $parameter['if condition']['var']['smarty_internal_index'] . " = " .
169                        $parameter['if condition']['value'] . ") {?>";
170                } else {
171                    $_output = "<?php  } else { if (!isset(\$_smarty_tpl->tpl_vars[" .
172                        $parameter['if condition']['var'] . "])) \$_smarty_tpl->tpl_vars[" .
173                        $parameter['if condition']['var'] . "] = new Smarty_Variable(null{$_nocache});";
174                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " .
175                        $parameter['if condition']['value'] . ") {?>";
176                }
177
178                return $_output;
179            } else {
180                $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache));
181
182                return "<?php } elseif ({$parameter['if condition']}) {?>";
183            }
184        } else {
185            $tmp = '';
186            foreach ($compiler->prefix_code as $code) {
187                $tmp = $compiler->appendCode($tmp, $code);
188            }
189            $compiler->prefix_code = array();
190            $tmp = $compiler->appendCode("<?php } else {?>", $tmp);
191            $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
192            if ($condition_by_assign) {
193                if (is_array($parameter['if condition']['var'])) {
194                    $_output = $compiler->appendCode($tmp, "<?php  if (!isset(\$_smarty_tpl->tpl_vars[" .
195                                                         $parameter['if condition']['var']['var'] .
196                                                         "]) || !is_array(\$_smarty_tpl->tpl_vars[" .
197                                                         $parameter['if condition']['var']['var'] .
198                                                         "]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " .
199                                                         $parameter['if condition']['var']['var'] . "$_nocache);\n");
200                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
201                        $parameter['if condition']['var']['smarty_internal_index'] . " = " .
202                        $parameter['if condition']['value'] . ") {?>";
203                } else {
204                    $_output = $compiler->appendCode($tmp, "<?php if (!isset(\$_smarty_tpl->tpl_vars[" .
205                                                         $parameter['if condition']['var'] .
206                                                         "])) \$_smarty_tpl->tpl_vars[" .
207                                                         $parameter['if condition']['var'] .
208                                                         "] = new Smarty_Variable(null{$_nocache});");
209                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " .
210                        $parameter['if condition']['value'] . ") {?>";
211                }
212
213                return $_output;
214            } else {
215                return $compiler->appendCode($tmp, "<?php if ({$parameter['if condition']}) {?>");
216            }
217        }
218    }
219}
220
221/**
222 * Smarty Internal Plugin Compile Ifclose Class
223 *
224 * @package    Smarty
225 * @subpackage Compiler
226 */
227class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase
228{
229    /**
230     * Compiles code for the {/if} tag
231     *
232     * @param array                                 $args      array with attributes from parser
233     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
234     * @param array                                 $parameter array with compilation parameter
235     *
236     * @return string compiled code
237     */
238    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
239    {
240        // must endblock be nocache?
241        if ($compiler->nocache) {
242            $compiler->tag_nocache = true;
243        }
244        list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif'));
245        $tmp = '';
246        for ($i = 0; $i < $nesting; $i ++) {
247            $tmp .= '}';
248        }
249
250        return "<?php {$tmp}?>";
251    }
252}
253