]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.html_table.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.html_table.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
22
23
24 */
25
26
27 /**
28  * Smarty plugin
29  * @package Smarty
30  * @subpackage plugins
31  */
32
33
34 /**
35  * Smarty {html_table} function plugin
36  *
37  * Type:     function<br>
38  * Name:     html_table<br>
39  * Date:     Feb 17, 2003<br>
40  * Purpose:  make an html table from an array of data<br>
41  * Input:<br>
42  *         - loop = array to loop through
43  *         - cols = number of columns
44  *         - rows = number of rows
45  *         - table_attr = table attributes
46  *         - tr_attr = table row attributes (arrays are cycled)
47  *         - td_attr = table cell attributes (arrays are cycled)
48  *         - trailpad = value to pad trailing cells with
49  *         - vdir = vertical direction (default: "down", means top-to-bottom)
50  *         - hdir = horizontal direction (default: "right", means left-to-right)
51  *         - inner = inner loop (default "cols": print $loop line by line,
52  *                   $loop will be printed column by column otherwise)
53  *
54  *
55  * Examples:
56  * <pre>
57  * {table loop=$data}
58  * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
59  * {table loop=$data cols=4 tr_attr=$colors}
60  * </pre>
61  * @author   Monte Ohrt <monte at ohrt dot com>
62  * @version  1.0
63  * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
64  *          (Smarty online manual)
65  * @param array
66  * @param Smarty
67  * @return string
68  */
69 function smarty_function_html_table($params, &$smarty)
70 {
71     $table_attr = 'border="1"';
72     $tr_attr = '';
73     $td_attr = '';
74     $cols = 3;
75     $rows = 3;
76     $trailpad = '&nbsp;';
77     $vdir = 'down';
78     $hdir = 'right';
79     $inner = 'cols';
80
81     if (!isset($params['loop'])) {
82         $smarty->trigger_error("html_table: missing 'loop' parameter");
83         return;
84     }
85
86     foreach ($params as $_key=>$_value) {
87         switch ($_key) {
88             case 'loop':
89                 $$_key = (array)$_value;
90                 break;
91
92             case 'cols':
93             case 'rows':
94                 $$_key = (int)$_value;
95                 break;
96
97             case 'table_attr':
98             case 'trailpad':
99             case 'hdir':
100             case 'vdir':
101             case 'inner':
102                 $$_key = (string)$_value;
103                 break;
104
105             case 'tr_attr':
106             case 'td_attr':
107                 $$_key = $_value;
108                 break;
109         }
110     }
111
112     $loop_count = count($loop);
113     if (empty($params['rows'])) {
114         /* no rows specified */
115         $rows = ceil($loop_count/$cols);
116     } elseif (empty($params['cols'])) {
117         if (!empty($params['rows'])) {
118             /* no cols specified, but rows */
119             $cols = ceil($loop_count/$rows);
120         }
121     }
122
123     $output = "<table $table_attr>\n";
124
125     for ($r=0; $r<$rows; $r++) {
126         $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
127         $rx =  ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols;
128
129         for ($c=0; $c<$cols; $c++) {
130             $x =  ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c;
131             if ($inner!='cols') {
132                 /* shuffle x to loop over rows*/
133                 $x = floor($x/$cols) + ($x%$cols)*$rows;
134             }
135
136             if ($x<$loop_count) {
137                 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
138             } else {
139                 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
140             }
141         }
142         $output .= "</tr>\n";
143     }
144     $output .= "</table>\n";
145     
146     return $output;
147 }
148
149 function smarty_function_html_table_cycle($name, $var, $no) {
150     if(!is_array($var)) {
151         $ret = $var;
152     } else {
153         $ret = $var[$no % count($var)];
154     }
155     
156     return ($ret) ? ' '.$ret : '';
157 }
158
159
160 /* vim: set expandtab: */
161
162 ?>