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