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