Adjusted time.php to honor note on separate row option.
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_compile_for.php
1 <?php
2 /**
3  * Smarty Internal Plugin Compile For
4  * 
5  * Compiles the {for} {forelse} {/for} tags
6  * 
7  * @package Smarty
8  * @subpackage Compiler
9  * @author Uwe Tews 
10  */
11
12 /**
13  * Smarty Internal Plugin Compile For Class
14  */
15 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase {
16     /**
17      * Compiles code for the {for} tag
18      * 
19      * Smarty 3 does implement two different sytaxes:
20      * 
21      * - {for $var in $array}
22      * For looping over arrays or iterators
23      * 
24      * - {for $x=0; $x<$y; $x++}
25      * For general loops
26      * 
27      * The parser is gereration different sets of attribute by which this compiler can 
28      * determin which syntax is used.
29      * 
30      * @param array $args array with attributes from parser
31      * @param object $compiler compiler object
32      * @param array $parameter array with compilation parameter
33      * @return string compiled code
34      */
35     public function compile($args, $compiler, $parameter)
36     {
37         $this->compiler = $compiler; 
38         if ($parameter == 0) {
39                 $this->required_attributes = array('start','to');
40                 $this->optional_attributes = array('max','step');
41         } else {
42                 $this->required_attributes = array('start','ifexp','var','step');
43                 $this->optional_attributes = array();
44         }
45         // check and get attributes
46         $_attr = $this->_get_attributes($args);
47
48         $local_vars = array();
49
50         $output = "<?php ";
51         if ($parameter == 1) {
52             foreach ($_attr['start'] as $_statement) {
53                 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
54                 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n";
55                 $compiler->local_var[$_statement['var']] = true;
56                 $local_vars[] = $_statement['var'];
57             } 
58             $output .= "  if ($_attr[ifexp]){ for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[var]]->value$_attr[step]){\n";
59         } else {
60             $_statement = $_attr['start'];
61             $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
62             $compiler->local_var[$_statement['var']] = true;
63             $local_vars[] = $_statement['var'];
64             if (isset($_attr['step'])) {
65                 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = $_attr[step];";
66             } else {
67                 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = 1;";
68             } 
69             if (isset($_attr['max'])) {
70                 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)min(ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step)),$_attr[max]);\n";
71             } else {
72                 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step));\n";
73             } 
74             $output .= "if (\$_smarty_tpl->tpl_vars[$_statement[var]]->total > 0){\n";
75             $output .= "for (\$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value], \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration = 1;\$_smarty_tpl->tpl_vars[$_statement[var]]->iteration <= \$_smarty_tpl->tpl_vars[$_statement[var]]->total;\$_smarty_tpl->tpl_vars[$_statement[var]]->value += \$_smarty_tpl->tpl_vars[$_statement[var]]->step, \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration++){\n";
76             $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->first = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == 1;";
77             $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->last = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == \$_smarty_tpl->tpl_vars[$_statement[var]]->total;";
78         } 
79         $output .= "?>";
80
81         $this->_open_tag('for', array('for', $this->compiler->nocache, $local_vars)); 
82         // maybe nocache because of nocache variables
83         $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache; 
84         // return compiled code
85         return $output;
86     } 
87
88
89 /**
90  * Smarty Internal Plugin Compile Forelse Class
91  */
92 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase {
93     /**
94      * Compiles code for the {forelse} tag
95      * 
96      * @param array $args array with attributes from parser
97      * @param object $compiler compiler object
98      * @param array $parameter array with compilation parameter
99      * @return string compiled code
100      */
101     public function compile($args, $compiler, $parameter)
102     {
103         $this->compiler = $compiler; 
104         // check and get attributes
105         $_attr  = $this->_get_attributes($args);
106
107         list($_open_tag, $nocache, $local_vars) = $this->_close_tag(array('for'));
108         $this->_open_tag('forelse', array('forelse', $nocache, $local_vars));
109         return "<?php }} else { ?>";
110     } 
111
112
113 /**
114  * Smarty Internal Plugin Compile Forclose Class
115  */
116 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase {
117     /**
118      * Compiles code for the {/for} tag
119      * 
120      * @param array $args array with attributes from parser
121      * @param object $compiler compiler object
122      * @param array $parameter array with compilation parameter
123      * @return string compiled code
124      */
125     public function compile($args, $compiler, $parameter)
126     {
127         $this->compiler = $compiler; 
128         // check and get attributes
129         $_attr  = $this->_get_attributes($args); 
130         // must endblock be nocache?
131         if ($this->compiler->nocache) {
132             $this->compiler->tag_nocache = true;
133         } 
134
135         list($_open_tag, $this->compiler->nocache, $local_vars) = $this->_close_tag(array('for', 'forelse'));
136
137         foreach ($local_vars as $var) {
138             unset($compiler->local_var[$var]);
139         } 
140         if ($_open_tag == 'forelse')
141             return "<?php }  ?>";
142         else
143             return "<?php }} ?>";
144     } 
145
146
147 ?>