Редакция 2 | Только различия | Учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 235 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * Smarty plugin
|
3 | * Smarty plugin
|
4 | * @package Smarty
|
4 | * @package Smarty
|
5 | * @subpackage plugins
|
5 | * @subpackage plugins
|
6 | */
|
6 | */
|
7 | 7 | ||
8 | 8 | ||
9 | /**
|
9 | /**
|
10 | * Smarty {html_table} function plugin
|
10 | * Smarty {html_table} function plugin
|
11 | *
|
11 | *
|
12 | * Type: function<br>
|
12 | * Type: function<br>
|
13 | * Name: html_table<br>
|
13 | * Name: html_table<br>
|
14 | * Date: Feb 17, 2003<br>
|
14 | * Date: Feb 17, 2003<br>
|
15 | * Purpose: make an html table from an array of data<br>
|
15 | * Purpose: make an html table from an array of data<br>
|
16 | * Input:<br>
|
16 | * Input:<br>
|
17 | * - loop = array to loop through
|
17 | * - loop = array to loop through
|
18 | * - cols = number of columns, comma separated list of column names
|
18 | * - cols = number of columns, comma separated list of column names
|
19 | * or array of column names
|
19 | * or array of column names
|
20 | * - rows = number of rows
|
20 | * - rows = number of rows
|
21 | * - table_attr = table attributes
|
21 | * - table_attr = table attributes
|
22 | * - th_attr = table heading attributes (arrays are cycled)
|
22 | * - th_attr = table heading attributes (arrays are cycled)
|
23 | * - tr_attr = table row attributes (arrays are cycled)
|
23 | * - tr_attr = table row attributes (arrays are cycled)
|
24 | * - td_attr = table cell attributes (arrays are cycled)
|
24 | * - td_attr = table cell attributes (arrays are cycled)
|
25 | * - trailpad = value to pad trailing cells with
|
25 | * - trailpad = value to pad trailing cells with
|
26 | * - caption = text for caption element
|
26 | * - caption = text for caption element
|
27 | * - vdir = vertical direction (default: "down", means top-to-bottom)
|
27 | * - vdir = vertical direction (default: "down", means top-to-bottom)
|
28 | * - hdir = horizontal direction (default: "right", means left-to-right)
|
28 | * - hdir = horizontal direction (default: "right", means left-to-right)
|
29 | * - inner = inner loop (default "cols": print $loop line by line,
|
29 | * - inner = inner loop (default "cols": print $loop line by line,
|
30 | * $loop will be printed column by column otherwise)
|
30 | * $loop will be printed column by column otherwise)
|
31 | *
|
31 | *
|
32 | *
|
32 | *
|
33 | * Examples:
|
33 | * Examples:
|
34 | * <pre>
|
34 | * <pre>
|
35 | * {table loop=$data}
|
35 | * {table loop=$data}
|
36 | * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
|
36 | * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
|
37 | * {table loop=$data cols="first,second,third" tr_attr=$colors}
|
37 | * {table loop=$data cols="first,second,third" tr_attr=$colors}
|
38 | * </pre>
|
38 | * </pre>
|
39 | * @author Monte Ohrt <monte at ohrt dot com>
|
39 | * @author Monte Ohrt <monte at ohrt dot com>
|
40 | * @author credit to Messju Mohr <messju at lammfellpuschen dot de>
|
40 | * @author credit to Messju Mohr <messju at lammfellpuschen dot de>
|
41 | * @author credit to boots <boots dot smarty at yahoo dot com>
|
41 | * @author credit to boots <boots dot smarty at yahoo dot com>
|
42 | * @version 1.1
|
42 | * @version 1.1
|
43 | * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
|
43 | * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
|
44 | * (Smarty online manual)
|
44 | * (Smarty online manual)
|
45 | * @param array
|
45 | * @param array
|
46 | * @param Smarty
|
46 | * @param Smarty
|
47 | * @return string
|
47 | * @return string
|
48 | */
|
48 | */
|
49 | function smarty_function_html_table($params, &$smarty) |
49 | function smarty_function_html_table($params, &$smarty) |
50 | {
|
50 | {
|
51 | $table_attr = 'border="1"'; |
51 | $table_attr = 'border="1"'; |
52 | $tr_attr = ''; |
52 | $tr_attr = ''; |
53 | $th_attr = ''; |
53 | $th_attr = ''; |
54 | $td_attr = ''; |
54 | $td_attr = ''; |
55 | $cols = $cols_count = 3; |
55 | $cols = $cols_count = 3; |
56 | $rows = 3; |
56 | $rows = 3; |
57 | $trailpad = ' '; |
57 | $trailpad = ' '; |
58 | $vdir = 'down'; |
58 | $vdir = 'down'; |
59 | $hdir = 'right'; |
59 | $hdir = 'right'; |
60 | $inner = 'cols'; |
60 | $inner = 'cols'; |
61 | $caption = ''; |
61 | $caption = ''; |
62 | 62 | ||
63 | if (!isset($params['loop'])) { |
63 | if (!isset($params['loop'])) { |
64 | $smarty->trigger_error("html_table: missing 'loop' parameter"); |
64 | $smarty->trigger_error("html_table: missing 'loop' parameter"); |
65 | return; |
65 | return; |
66 | }
|
66 | }
|
67 | 67 | ||
68 | foreach ($params as $_key=>$_value) { |
68 | foreach ($params as $_key=>$_value) { |
69 | switch ($_key) { |
69 | switch ($_key) { |
70 | case 'loop': |
70 | case 'loop': |
71 | $$_key = (array)$_value; |
71 | $$_key = (array)$_value; |
72 | break; |
72 | break; |
73 | 73 | ||
74 | case 'cols': |
74 | case 'cols': |
75 | if (is_array($_value) && !empty($_value)) { |
75 | if (is_array($_value) && !empty($_value)) { |
76 | $cols = $_value; |
76 | $cols = $_value; |
77 | $cols_count = count($_value); |
77 | $cols_count = count($_value); |
78 | } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) { |
78 | } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) { |
79 | $cols = explode(',', $_value); |
79 | $cols = explode(',', $_value); |
80 | $cols_count = count($cols); |
80 | $cols_count = count($cols); |
81 | } elseif (!empty($_value)) { |
81 | } elseif (!empty($_value)) { |
82 | $cols_count = (int)$_value; |
82 | $cols_count = (int)$_value; |
83 | } else { |
83 | } else { |
84 | $cols_count = $cols; |
84 | $cols_count = $cols; |
85 | }
|
85 | }
|
86 | break; |
86 | break; |
87 | 87 | ||
88 | case 'rows': |
88 | case 'rows': |
89 | $$_key = (int)$_value; |
89 | $$_key = (int)$_value; |
90 | break; |
90 | break; |
91 | 91 | ||
92 | case 'table_attr': |
92 | case 'table_attr': |
93 | case 'trailpad': |
93 | case 'trailpad': |
94 | case 'hdir': |
94 | case 'hdir': |
95 | case 'vdir': |
95 | case 'vdir': |
96 | case 'inner': |
96 | case 'inner': |
97 | case 'caption': |
97 | case 'caption': |
98 | $$_key = (string)$_value; |
98 | $$_key = (string)$_value; |
99 | break; |
99 | break; |
100 | 100 | ||
101 | case 'tr_attr': |
101 | case 'tr_attr': |
102 | case 'td_attr': |
102 | case 'td_attr': |
103 | case 'th_attr': |
103 | case 'th_attr': |
104 | $$_key = $_value; |
104 | $$_key = $_value; |
105 | break; |
105 | break; |
106 | }
|
106 | }
|
107 | }
|
107 | }
|
108 | 108 | ||
109 | $loop_count = count($loop); |
109 | $loop_count = count($loop); |
110 | if (empty($params['rows'])) { |
110 | if (empty($params['rows'])) { |
111 | /* no rows specified */
|
111 | /* no rows specified */
|
112 | $rows = ceil($loop_count/$cols_count); |
112 | $rows = ceil($loop_count/$cols_count); |
113 | } elseif (empty($params['cols'])) { |
113 | } elseif (empty($params['cols'])) { |
114 | if (!empty($params['rows'])) { |
114 | if (!empty($params['rows'])) { |
115 | /* no cols specified, but rows */
|
115 | /* no cols specified, but rows */
|
116 | $cols_count = ceil($loop_count/$rows); |
116 | $cols_count = ceil($loop_count/$rows); |
117 | }
|
117 | }
|
118 | }
|
118 | }
|
119 | 119 | ||
120 | $output = "<table $table_attr>\n"; |
120 | $output = "<table $table_attr>\n"; |
121 | 121 | ||
122 | if (!empty($caption)) { |
122 | if (!empty($caption)) { |
123 | $output .= '<caption>' . $caption . "</caption>\n"; |
123 | $output .= '<caption>' . $caption . "</caption>\n"; |
124 | }
|
124 | }
|
125 | 125 | ||
126 | if (is_array($cols)) { |
126 | if (is_array($cols)) { |
127 | $cols = ($hdir == 'right') ? $cols : array_reverse($cols); |
127 | $cols = ($hdir == 'right') ? $cols : array_reverse($cols); |
128 | $output .= "<thead><tr>\n"; |
128 | $output .= "<thead><tr>\n"; |
129 | 129 | ||
130 | for ($r=0; $r<$cols_count; $r++) { |
130 | for ($r=0; $r<$cols_count; $r++) { |
131 | $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>'; |
131 | $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>'; |
132 | $output .= $cols[$r]; |
132 | $output .= $cols[$r]; |
133 | $output .= "</th>\n"; |
133 | $output .= "</th>\n"; |
134 | }
|
134 | }
|
135 | $output .= "</tr></thead>\n"; |
135 | $output .= "</tr></thead>\n"; |
136 | }
|
136 | }
|
137 | 137 | ||
138 | $output .= "<tbody>\n"; |
138 | $output .= "<tbody>\n"; |
139 | for ($r=0; $r<$rows; $r++) { |
139 | for ($r=0; $r<$rows; $r++) { |
140 | $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n"; |
140 | $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n"; |
141 | $rx = ($vdir == 'down') ? $r*$cols_count : ($rows-1-$r)*$cols_count; |
141 | $rx = ($vdir == 'down') ? $r*$cols_count : ($rows-1-$r)*$cols_count; |
142 | 142 | ||
143 | for ($c=0; $c<$cols_count; $c++) { |
143 | for ($c=0; $c<$cols_count; $c++) { |
144 | $x = ($hdir == 'right') ? $rx+$c : $rx+$cols_count-1-$c; |
144 | $x = ($hdir == 'right') ? $rx+$c : $rx+$cols_count-1-$c; |
145 | if ($inner!='cols') { |
145 | if ($inner!='cols') { |
146 | /* shuffle x to loop over rows*/
|
146 | /* shuffle x to loop over rows*/
|
147 | $x = floor($x/$cols_count) + ($x%$cols_count)*$rows; |
147 | $x = floor($x/$cols_count) + ($x%$cols_count)*$rows; |
148 | }
|
148 | }
|
149 | 149 | ||
150 | if ($x<$loop_count) { |
150 | if ($x<$loop_count) { |
151 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n"; |
151 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n"; |
152 | } else { |
152 | } else { |
153 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n"; |
153 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n"; |
154 | }
|
154 | }
|
155 | }
|
155 | }
|
156 | $output .= "</tr>\n"; |
156 | $output .= "</tr>\n"; |
157 | }
|
157 | }
|
158 | $output .= "</tbody>\n"; |
158 | $output .= "</tbody>\n"; |
159 | $output .= "</table>\n"; |
159 | $output .= "</table>\n"; |
160 | 160 | ||
161 | return $output; |
161 | return $output; |
162 | }
|
162 | }
|
163 | 163 | ||
164 | function smarty_function_html_table_cycle($name, $var, $no) { |
164 | function smarty_function_html_table_cycle($name, $var, $no) { |
165 | if(!is_array($var)) { |
165 | if(!is_array($var)) { |
166 | $ret = $var; |
166 | $ret = $var; |
167 | } else { |
167 | } else { |
168 | $ret = $var[$no % count($var)]; |
168 | $ret = $var[$no % count($var)]; |
169 | }
|
169 | }
|
170 | 170 | ||
171 | return ($ret) ? ' '.$ret : ''; |
171 | return ($ret) ? ' '.$ret : ''; |
172 | }
|
172 | }
|
173 | 173 | ||
174 | 174 | ||
175 | /* vim: set expandtab: */
|
175 | /* vim: set expandtab: */
|
176 | 176 | ||
177 | ?>
|
177 | ?>
|
178 | 178 |