1 : <?php
2 : /**
3 : * Smarty Internal Plugin Compile Foreach
4 : *
5 : * Compiles the {foreach} {foreachelse} {/foreach} tags
6 : *
7 : * @package Smarty
8 : * @subpackage Compiler
9 : * @author Uwe Tews
10 : */
11 :
12 : /**
13 : * Smarty Internal Plugin Compile Foreach Class
14 : *
15 : * @package Smarty
16 : * @subpackage Compiler
17 : */
18 : class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
19 : /**
20 : * Attribute definition: Overwrites base class.
21 : *
22 : * @var array
23 : * @see Smarty_Internal_CompileBase
24 : */
25 : public $required_attributes = array('from', 'item');
26 : /**
27 : * Attribute definition: Overwrites base class.
28 : *
29 : * @var array
30 : * @see Smarty_Internal_CompileBase
31 : */
32 : public $optional_attributes = array('name', 'key');
33 : /**
34 : * Attribute definition: Overwrites base class.
35 : *
36 : * @var array
37 : * @see Smarty_Internal_CompileBase
38 : */
39 : public $shorttag_order = array('from','item','key','name');
40 :
41 : /**
42 : * Compiles code for the {foreach} tag
43 : *
44 : * @param array $args array with attributes from parser
45 : * @param object $compiler compiler object
46 : * @param array $parameter array with compilation parameter
47 : * @return string compiled code
48 : */
49 : public function compile($args, $compiler, $parameter)
50 : {
51 0 : $tpl = $compiler->template;
52 : // check and get attributes
53 0 : $_attr = $this->getAttributes($compiler, $args);
54 :
55 0 : $from = $_attr['from'];
56 0 : $item = $_attr['item'];
57 0 : if (!strncmp("\$_smarty_tpl->tpl_vars[$item]", $from, strlen($item) + 24)) {
58 0 : $compiler->trigger_template_error("item variable {$item} may not be the same variable as at 'from'", $compiler->lex->taglineno);
59 0 : }
60 :
61 0 : if (isset($_attr['key'])) {
62 0 : $key = $_attr['key'];
63 0 : } else {
64 0 : $key = null;
65 : }
66 :
67 0 : $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key));
68 : // maybe nocache because of nocache variables
69 0 : $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
70 :
71 0 : if (isset($_attr['name'])) {
72 0 : $name = $_attr['name'];
73 0 : $has_name = true;
74 0 : $SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.';
75 0 : } else {
76 0 : $name = null;
77 0 : $has_name = false;
78 : }
79 0 : $ItemVarName = '$' . trim($item, '\'"') . '@';
80 : // evaluates which Smarty variables and properties have to be computed
81 0 : if ($has_name) {
82 0 : $usesSmartyFirst = strpos($tpl->source->content, $SmartyVarName . 'first') !== false;
83 0 : $usesSmartyLast = strpos($tpl->source->content, $SmartyVarName . 'last') !== false;
84 0 : $usesSmartyIndex = strpos($tpl->source->content, $SmartyVarName . 'index') !== false;
85 0 : $usesSmartyIteration = strpos($tpl->source->content, $SmartyVarName . 'iteration') !== false;
86 0 : $usesSmartyShow = strpos($tpl->source->content, $SmartyVarName . 'show') !== false;
87 0 : $usesSmartyTotal = strpos($tpl->source->content, $SmartyVarName . 'total') !== false;
88 0 : } else {
89 0 : $usesSmartyFirst = false;
90 0 : $usesSmartyLast = false;
91 0 : $usesSmartyTotal = false;
92 0 : $usesSmartyShow = false;
93 : }
94 :
95 0 : $usesPropFirst = $usesSmartyFirst || strpos($tpl->source->content, $ItemVarName . 'first') !== false;
96 0 : $usesPropLast = $usesSmartyLast || strpos($tpl->source->content, $ItemVarName . 'last') !== false;
97 0 : $usesPropIndex = $usesPropFirst || strpos($tpl->source->content, $ItemVarName . 'index') !== false;
98 0 : $usesPropIteration = $usesPropLast || strpos($tpl->source->content, $ItemVarName . 'iteration') !== false;
99 0 : $usesPropShow = strpos($tpl->source->content, $ItemVarName . 'show') !== false;
100 0 : $usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($tpl->source->content, $ItemVarName . 'total') !== false;
101 : // generate output code
102 0 : $output = "<?php ";
103 0 : $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable; \$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
104 0 : if ($key != null) {
105 0 : $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
106 0 : }
107 0 : $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
108 0 : if ($usesPropTotal) {
109 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
110 0 : }
111 0 : if ($usesPropIteration) {
112 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
113 0 : }
114 0 : if ($usesPropIndex) {
115 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
116 0 : }
117 0 : if ($usesPropShow) {
118 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
119 0 : }
120 0 : if ($has_name) {
121 0 : if ($usesSmartyTotal) {
122 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
123 0 : }
124 0 : if ($usesSmartyIteration) {
125 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
126 0 : }
127 0 : if ($usesSmartyIndex) {
128 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
129 0 : }
130 0 : if ($usesSmartyShow) {
131 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['show']=(\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
132 0 : }
133 0 : }
134 0 : $output .= "foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
135 0 : if ($key != null) {
136 0 : $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
137 0 : }
138 0 : if ($usesPropIteration) {
139 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
140 0 : }
141 0 : if ($usesPropIndex) {
142 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
143 0 : }
144 0 : if ($usesPropFirst) {
145 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
146 0 : }
147 0 : if ($usesPropLast) {
148 0 : $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
149 0 : }
150 0 : if ($has_name) {
151 0 : if ($usesSmartyFirst) {
152 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
153 0 : }
154 0 : if ($usesSmartyIteration) {
155 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
156 0 : }
157 0 : if ($usesSmartyIndex) {
158 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
159 0 : }
160 0 : if ($usesSmartyLast) {
161 0 : $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
162 0 : }
163 0 : }
164 0 : $output .= "?>";
165 :
166 0 : return $output;
167 : }
168 : }
169 :
170 : /**
171 : * Smarty Internal Plugin Compile Foreachelse Class
172 : *
173 : * @package Smarty
174 : * @subpackage Compiler
175 : */
176 : class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase {
177 :
178 : /**
179 : * Compiles code for the {foreachelse} tag
180 : *
181 : * @param array $args array with attributes from parser
182 : * @param object $compiler compiler object
183 : * @param array $parameter array with compilation parameter
184 : * @return string compiled code
185 : */
186 : public function compile($args, $compiler, $parameter)
187 : {
188 : // check and get attributes
189 0 : $_attr = $this->getAttributes($compiler, $args);
190 :
191 0 : list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach'));
192 0 : $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key));
193 :
194 0 : return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
195 : }
196 :
197 : }
198 :
199 : /**
200 : * Smarty Internal Plugin Compile Foreachclose Class
201 : *
202 : * @package Smarty
203 : * @subpackage Compiler
204 : */
205 : class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase {
206 :
207 : /**
208 : * Compiles code for the {/foreach} tag
209 : *
210 : * @param array $args array with attributes from parser
211 : * @param object $compiler compiler object
212 : * @param array $parameter array with compilation parameter
213 : * @return string compiled code
214 : */
215 : public function compile($args, $compiler, $parameter)
216 : {
217 : // check and get attributes
218 0 : $_attr = $this->getAttributes($compiler, $args);
219 : // must endblock be nocache?
220 0 : if ($compiler->nocache) {
221 0 : $compiler->tag_nocache = true;
222 0 : }
223 :
224 0 : list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
225 :
226 0 : return "<?php } ?>";
227 : }
228 :
229 : }
230 :
|