Adjusted time.php to honor note on separate row option.
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_compile_section.php
1 <?php
2 /**
3  * Smarty Internal Plugin Compile Section
4  * 
5  * Compiles the {section} {sectionelse} {/section} tags
6  * 
7  * @package Smarty
8  * @subpackage Compiler
9  * @author Uwe Tews 
10  */
11
12 /**
13  * Smarty Internal Plugin Compile Section Class
14  */
15 class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase {
16         // attribute definitions
17     public $required_attributes = array('name', 'loop');
18         public $shorttag_order = array('name', 'loop');
19     public $optional_attributes = array('start', 'step', 'max', 'show'); 
20
21     /**
22      * Compiles code for the {section} tag
23      * 
24      * @param array $args array with attributes from parser
25      * @param object $compiler compiler object
26      * @return string compiled code
27      */
28     public function compile($args, $compiler)
29     {
30         $this->compiler = $compiler;
31         // check and get attributes
32         $_attr = $this->_get_attributes($args);
33
34         $this->_open_tag('section', array('section',$this->compiler->nocache));
35         // maybe nocache because of nocache variables
36         $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
37
38         $output = "<?php ";
39
40         $section_name = $_attr['name'];
41         
42         $output .= "unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
43         $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
44
45         foreach ($_attr as $attr_name => $attr_value) {
46             switch ($attr_name) {
47                 case 'loop':
48                     $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
49                     break;
50
51                 case 'show':
52                     if (is_bool($attr_value))
53                         $show_attr_value = $attr_value ? 'true' : 'false';
54                     else
55                         $show_attr_value = "(bool)$attr_value";
56                     $output .= "{$section_props}['show'] = $show_attr_value;\n";
57                     break;
58
59                 case 'name':
60                     $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
61                     break;
62
63                 case 'max':
64                 case 'start':
65                     $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
66                     break;
67
68                 case 'step':
69                     $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
70                     break;
71             } 
72         } 
73
74         if (!isset($_attr['show']))
75             $output .= "{$section_props}['show'] = true;\n";
76
77         if (!isset($_attr['loop']))
78             $output .= "{$section_props}['loop'] = 1;\n";
79
80         if (!isset($_attr['max']))
81             $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
82         else
83             $output .= "if ({$section_props}['max'] < 0)\n" . "    {$section_props}['max'] = {$section_props}['loop'];\n";
84
85         if (!isset($_attr['step']))
86             $output .= "{$section_props}['step'] = 1;\n";
87
88         if (!isset($_attr['start']))
89             $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
90         else {
91             $output .= "if ({$section_props}['start'] < 0)\n" . "    {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" . "else\n" . "    {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
92         } 
93
94         $output .= "if ({$section_props}['show']) {\n";
95         if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
96             $output .= "    {$section_props}['total'] = {$section_props}['loop'];\n";
97         } else {
98             $output .= "    {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
99         } 
100         $output .= "    if ({$section_props}['total'] == 0)\n" . "        {$section_props}['show'] = false;\n" . "} else\n" . "    {$section_props}['total'] = 0;\n";
101
102         $output .= "if ({$section_props}['show']):\n";
103         $output .= "
104             for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
105                  {$section_props}['iteration'] <= {$section_props}['total'];
106                  {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
107         $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
108         $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
109         $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
110         $output .= "{$section_props}['first']      = ({$section_props}['iteration'] == 1);\n";
111         $output .= "{$section_props}['last']       = ({$section_props}['iteration'] == {$section_props}['total']);\n";
112
113         $output .= "?>";
114         return $output;
115     } 
116
117
118 /**
119 * Smarty Internal Plugin Compile Sectionelse Class
120 */
121 class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase {
122     /**
123      * Compiles code for the {sectionelse} tag
124      * 
125      * @param array $args array with attributes from parser
126      * @param object $compiler compiler object
127      * @return string compiled code
128      */
129     public function compile($args, $compiler)
130     {
131         $this->compiler = $compiler; 
132         // check and get attributes
133         $_attr = $this->_get_attributes($args);
134
135         list($_open_tag, $nocache) = $this->_close_tag(array('section'));
136         $this->_open_tag('sectionelse',array('sectionelse', $nocache));
137
138         return "<?php endfor; else: ?>";
139     } 
140
141
142 /**
143  * Smarty Internal Plugin Compile Sectionclose Class
144  */
145 class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase {
146     /**
147      * Compiles code for the {/section} tag
148      * 
149      * @param array $args array with attributes from parser
150      * @param object $compiler compiler object
151      * @return string compiled code
152      */
153     public function compile($args, $compiler)
154     {
155         $this->compiler = $compiler; 
156         // check and get attributes
157         $_attr = $this->_get_attributes($args);
158
159         // must endblock be nocache?
160         if ($this->compiler->nocache) {
161                  $this->compiler->tag_nocache = true;
162         }
163
164         list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('section', 'sectionelse'));
165
166         if ($_open_tag == 'sectionelse')
167             return "<?php endif; ?>";
168         else
169             return "<?php endfor; endif; ?>";
170     } 
171
172
173 ?>