Code

fixed property
[gosa.git] / gosa-core / include / smarty / sysplugins / smarty_internal_compile_foreach.php
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  * Smarty Internal Plugin Compile Foreach Class
13  */
14 class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
15     /**
16      * Compiles code for the {foreach} tag
17      * 
18      * @param array $args array with attributes from parser
19      * @param object $compiler compiler object
20      * @return string compiled code
21      */
22     public function compile($args, $compiler)
23     {
24         $this->compiler = $compiler;
25         $this->required_attributes = array('from', 'item');
26         $this->optional_attributes = array('name', 'key');
27         $tpl = $compiler->template; 
28         // check and get attributes
29         $_attr = $this->_get_attributes($args);
31         $this->_open_tag('foreach', array('foreach', $this->compiler->nocache)); 
32         // maybe nocache because of nocache variables
33         $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
35         $from = $_attr['from'];
36         $item = $_attr['item'];
38         if (isset($_attr['key'])) {
39             $key = $_attr['key'];
40         } else {
41             $key = null;
42         } 
44         if (isset($_attr['name'])) {
45             $name = $_attr['name'];
46             $has_name = true;
47             $SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.';
48         } else {
49             $name = null;
50             $has_name = false;
51         } 
52         $ItemVarName = '$' . trim($item, '\'"') . '@'; 
53         // evaluates which Smarty variables and properties have to be computed
54         if ($has_name) {
55             $usesSmartyFirst = strpos($tpl->template_source, $SmartyVarName . 'first') !== false;
56             $usesSmartyLast = strpos($tpl->template_source, $SmartyVarName . 'last') !== false;
57             $usesSmartyIndex = strpos($tpl->template_source, $SmartyVarName . 'index') !== false;
58             $usesSmartyIteration = strpos($tpl->template_source, $SmartyVarName . 'iteration') !== false;
59             $usesSmartyShow = strpos($tpl->template_source, $SmartyVarName . 'show') !== false;
60             $usesSmartyTotal = $usesSmartyLast || strpos($tpl->template_source, $SmartyVarName . 'total') !== false;
61         } else {
62             $usesSmartyFirst = false;
63             $usesSmartyLast = false;
64             $usesSmartyTotal = false;
65         } 
67         $usesPropFirst = $usesSmartyFirst || strpos($tpl->template_source, $ItemVarName . 'first') !== false;
68         $usesPropLast = $usesSmartyLast || strpos($tpl->template_source, $ItemVarName . 'last') !== false;
69         $usesPropIndex = $usesPropFirst || strpos($tpl->template_source, $ItemVarName . 'index') !== false;
70         $usesPropIteration = $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'iteration') !== false;
71         $usesPropShow = strpos($tpl->template_source, $ItemVarName . 'show') !== false;
72         $usesPropTotal = $usesSmartyTotal || $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'total') !== false; 
73         // generate output code
74         $output = "<?php ";
75         $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
76         $compiler->local_var[$item] = true;
77         if ($key != null) {
78             $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
79             $compiler->local_var[$key] = true;
80         } 
81         $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
82         if ($usesPropTotal) {
83             $output .= " \$_smarty_tpl->tpl_vars[$item]->total=count(\$_from);\n";
84         } 
85         if ($usesPropIteration) {
86             $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
87         } 
88         if ($usesPropIndex) {
89             $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
90         } 
91         if ($has_name) {
92             if ($usesSmartyTotal) {
93                 $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
94             } 
95             if ($usesSmartyIteration) {
96                 $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
97             } 
98             if ($usesSmartyIndex) {
99                 $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
100             } 
101         } 
102         $output .= "if (count(\$_from) > 0){\n";
103         $output .= "    foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
104         if ($key != null) {
105             $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
106         } 
107         if ($usesPropIteration) {
108             $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
109         } 
110         if ($usesPropIndex) {
111             $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
112         } 
113         if ($usesPropFirst) {
114             $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
115         } 
116         if ($usesPropLast) {
117             $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
118         } 
119         if ($has_name) {
120             if ($usesSmartyFirst) {
121                 $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
122             } 
123             if ($usesSmartyIteration) {
124                 $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
125             } 
126             if ($usesSmartyIndex) {
127                 $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
128             } 
129             if ($usesSmartyLast) {
130                 $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
131             } 
132         } 
133         $output .= "?>";
135         return $output;
136     } 
137
139 /**
140  * Smarty Internal Plugin Compile Foreachelse Class
141  */
142 class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase {
143     /**
144      * Compiles code for the {foreachelse} tag
145      * 
146      * @param array $args array with attributes from parser
147      * @param object $compiler compiler object
148      * @return string compiled code
149      */
150     public function compile($args, $compiler)
151     {
152         $this->compiler = $compiler; 
153         // check and get attributes
154         $_attr = $this->_get_attributes($args);
156         list($_open_tag, $nocache) = $this->_close_tag(array('foreach'));
157         $this->_open_tag('foreachelse', array('foreachelse', $nocache));
159         return "<?php }} else { ?>";
160     } 
161
163 /**
164  * Smarty Internal Plugin Compile Foreachclose Class
165  */
166 class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase {
167     /**
168      * Compiles code for the {/foreach} tag
169      * 
170      * @param array $args array with attributes from parser
171      * @param object $compiler compiler object
172      * @return string compiled code
173      */
174     public function compile($args, $compiler)
175     {
176         $this->compiler = $compiler; 
177         // check and get attributes
178         $_attr = $this->_get_attributes($args); 
179         // must endblock be nocache?
180         if ($this->compiler->nocache) {
181             $this->compiler->tag_nocache = true;
182         } 
184         list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('foreach', 'foreachelse'));
186         if ($_open_tag == 'foreachelse')
187             return "<?php } ?>";
188         else
189             return "<?php }} ?>";
190     } 
191
193 ?>