init
[garradin.git] / include / libs / template_lite / internal / compile.section_start.php
1 <?php
2 /**
3 * Template Lite section_start compile plugin converted from Smarty
4 *
5 * Type: compile
6 * Name: section_start
7 */
8
9 function compile_section_start($arguments, &$object)
10 {
11 $attrs = $object->_parse_arguments($arguments);
12 $arg_list = array();
13
14 $output = '<?php ';
15 $section_name = $attrs['name'];
16 if (empty($section_name))
17 {
18 throw new Template_Exception("missing section name", $object);
19 }
20
21 $output .= "if (isset(\$this->_sections['$section_name'])) unset(\$this->_sections['$section_name']);\n";
22 $section_props = "\$this->_sections['$section_name']";
23
24 foreach ($attrs as $attr_name => $attr_value)
25 {
26 switch ($attr_name)
27 {
28 case 'loop':
29 $output .= "{$section_props}['loop'] = is_array($attr_value) ? count($attr_value) : max(0, (int)$attr_value);\n";
30 break;
31
32 case 'show':
33 if (is_bool($attr_value))
34 {
35 $show_attr_value = $attr_value ? 'true' : 'false';
36 }
37 else
38 {
39 $show_attr_value = "(bool)$attr_value";
40 }
41 $output .= "{$section_props}['show'] = $show_attr_value;\n";
42 break;
43
44 case 'name':
45 $output .= "{$section_props}['$attr_name'] = '$attr_value';\n";
46 break;
47
48 case 'max':
49 case 'start':
50 $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
51 break;
52
53 case 'step':
54 $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
55 break;
56
57 default:
58 throw new Template_Exception("unknown section attribute - '$attr_name'", $object);
59 break;
60 }
61 }
62
63 if (!isset($attrs['show']))
64 {
65 $output .= "{$section_props}['show'] = true;\n";
66 }
67
68 if (!isset($attrs['loop']))
69 {
70 $output .= "{$section_props}['loop'] = 1;\n";
71 }
72
73 if (!isset($attrs['max']))
74 {
75 $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
76 }
77 else
78 {
79 $output .= "if ({$section_props}['max'] < 0)\n" .
80 " {$section_props}['max'] = {$section_props}['loop'];\n";
81 }
82
83 if (!isset($attrs['step']))
84 {
85 $output .= "{$section_props}['step'] = 1;\n";
86 }
87
88 if (!isset($attrs['start']))
89 {
90 $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
91 }
92 else
93 {
94 $output .= "if ({$section_props}['start'] < 0)\n" .
95 " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
96 "else\n" .
97 " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
98 }
99
100 $output .= "if ({$section_props}['show']) {\n";
101 if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max']))
102 {
103 $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
104 }
105 else
106 {
107 $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";
108 }
109 $output .= " if ({$section_props}['total'] == 0)\n" .
110 " {$section_props}['show'] = false;\n" .
111 "} else\n" .
112 " {$section_props}['total'] = 0;\n";
113
114 $output .= "if ({$section_props}['show']):\n";
115 $output .= "
116 for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
117 {$section_props}['iteration'] <= {$section_props}['total'];
118 {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
119 $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
120 $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
121 $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
122 $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
123 $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
124
125 $output .= "?>";
126
127 return $output;
128 }
129 ?>