blob: da7bbbecfe0b68bc0c0fbe2c5eff7797c2cafe7f [file] [log] [blame]
admin2f8ae022006-10-09 03:25:43 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
admin2f8ae022006-10-09 03:25:43 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.3.1
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
admin2f8ae022006-10-09 03:25:43 +000016// ------------------------------------------------------------------------
17
18/**
19 * HTML Table Generating Class
admine334c472006-10-21 19:44:22 +000020 *
admin2f8ae022006-10-09 03:25:43 +000021 * Lets you create tables manually or from database result objects, or arrays.
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category HTML Tables
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/libraries/uri.html
28 */
29class CI_Table {
30
admin2bc13852006-10-24 23:16:17 +000031 var $rows = array();
32 var $heading = array();
33 var $template = NULL;
34 var $newline = "\n";
35 var $empty_cells = "";
admin2f8ae022006-10-09 03:25:43 +000036
37
38 function CI_Table()
39 {
40 log_message('debug', "Table Class Initialized");
41 }
42
43 // --------------------------------------------------------------------
44
45 /**
46 * Set the template
47 *
48 * @access public
49 * @param array
50 * @return void
51 */
52 function set_template($template)
53 {
admin27991202006-10-11 01:38:08 +000054 if ( ! is_array($template))
admin2f8ae022006-10-09 03:25:43 +000055 {
56 return FALSE;
57 }
58
59 $this->template = $template;
60 }
61
62 // --------------------------------------------------------------------
63
64 /**
admina1931ad2006-10-09 04:17:38 +000065 * Set the table heading
admin2f8ae022006-10-09 03:25:43 +000066 *
67 * Can be passed as an array or discreet params
68 *
69 * @access public
70 * @param mixed
71 * @return void
72 */
admina1931ad2006-10-09 04:17:38 +000073 function set_heading()
admin2f8ae022006-10-09 03:25:43 +000074 {
75 $args = func_get_args();
76 $this->heading = (is_array($args[0])) ? $args[0] : $args;
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
admin2bc13852006-10-24 23:16:17 +000082 * Set "empty" cells
83 *
84 * Can be passed as an array or discreet params
85 *
86 * @access public
87 * @param mixed
88 * @return void
89 */
90 function set_empty($value)
91 {
92 $this->empty_cells = $value;
93 }
94
95 // --------------------------------------------------------------------
96
97 /**
admin2f8ae022006-10-09 03:25:43 +000098 * Add a table row
99 *
100 * Can be passed as an array or discreet params
101 *
102 * @access public
103 * @param mixed
104 * @return void
105 */
106 function add_row()
107 {
108 $args = func_get_args();
109 $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
110 }
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Generate the table
116 *
117 * @access public
118 * @param mixed
119 * @return string
120 */
121 function generate($table_data = NULL)
122 {
123 // The table data can optionally be passed to this function
124 // either as a database result object or an array
125 if ( ! is_null($table_data))
126 {
127 if (is_object($table_data))
128 {
129 $this->_set_from_object($table_data);
130 }
131 elseif (is_array($table_data))
132 {
133 $this->_set_from_array($table_data);
134 }
135 }
136
137 // Is there anything to display? No? Smite them!
138 if (count($this->heading) == 0 AND count($this->rows) == 0)
139 {
140 return 'Undefined table data';
141 }
142
adminbd6bee72006-10-21 19:39:00 +0000143 // Compile and validate the template date
admin2f8ae022006-10-09 03:25:43 +0000144 $this->_compile_template();
145
146
147 // Build the table!
148
149 $out = $this->template['table_open'];
150 $out .= $this->newline;
151
152 // Is there a table heading to display?
153 if (count($this->heading) > 0)
154 {
155 $out .= $this->template['heading_row_start'];
156 $out .= $this->newline;
157
158 foreach($this->heading as $heading)
159 {
160 $out .= $this->template['heading_cell_start'];
161 $out .= $heading;
162 $out .= $this->template['heading_cell_end'];
163 }
164
165 $out .= $this->template['heading_row_end'];
166 $out .= $this->newline;
167 }
168
169 // Build the table rows
170 if (count($this->rows) > 0)
171 {
172 $i = 1;
173 foreach($this->rows as $row)
174 {
175 if ( ! is_array($row))
176 {
177 break;
178 }
179
180 // We use modulus to alternate the row colors
181 $alt = (fmod($i++, 2)) ? '' : 'alt_';
182
183 $out .= $this->template['row_'.$alt.'start'];
184 $out .= $this->newline;
185
admin2bc13852006-10-24 23:16:17 +0000186 foreach($row as $cell)
admin2f8ae022006-10-09 03:25:43 +0000187 {
188 $out .= $this->template['cell_'.$alt.'start'];
admin2bc13852006-10-24 23:16:17 +0000189
190 if ($cell == "")
191 {
192 $out .= $this->empty_cells;
193 }
194 else
195 {
196 $out .= $cell;
197 }
198
admin2f8ae022006-10-09 03:25:43 +0000199 $out .= $this->template['cell_'.$alt.'end'];
200 }
201
202 $out .= $this->template['row_'.$alt.'end'];
203 $out .= $this->newline;
204 }
205 }
206
207 $out .= $this->template['table_close'];
208
209 return $out;
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Set table data from a database result object
216 *
217 * @access public
218 * @param object
219 * @return void
220 */
221 function _set_from_object($query)
222 {
223 if ( ! is_object($query))
224 {
225 return FALSE;
226 }
227
228 // First generate the headings from the table column names
229 if (count($this->heading) == 0)
230 {
admin606f99c2006-10-11 23:48:41 +0000231 if ( ! method_exists($query, 'list_fields'))
admin2f8ae022006-10-09 03:25:43 +0000232 {
233 return FALSE;
234 }
235
admin606f99c2006-10-11 23:48:41 +0000236 $this->heading = $query->list_fields();
admin2f8ae022006-10-09 03:25:43 +0000237 }
238
239 // Next blast through the result array and build out the rows
admin40037182006-10-11 19:16:58 +0000240
241 if ($query->num_rows() > 0)
admin2f8ae022006-10-09 03:25:43 +0000242 {
admin40037182006-10-11 19:16:58 +0000243 foreach ($query->result_array() as $row)
244 {
245 $this->rows[] = $row;
246 }
admin2f8ae022006-10-09 03:25:43 +0000247 }
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * Set table data from an array
254 *
255 * @access public
256 * @param array
257 * @return void
258 */
259 function _set_from_array($data)
260 {
261 if ( ! is_array($data) OR count($data) == 0)
262 {
263 return FALSE;
264 }
265
266 $i = 0;
267 foreach ($data as $row)
268 {
269 if ( ! is_array($row))
270 {
271 $this->rows[] = $data;
272 break;
273 }
274
275 // If a heading hasn't already been set we'll use the first row of the array as the heading
276 if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0)
277 {
278 $this->heading = $row;
279 }
280 else
281 {
282 $this->rows[] = $row;
283 }
284
285 $i++;
286 }
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Compile Template
293 *
294 * @access private
295 * @return void
296 */
297 function _compile_template()
298 {
299 if ($this->template == NULL)
300 {
301 $this->template = $this->_default_template();
302 return;
303 }
304
305 $this->temp = $this->_default_template();
306 foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val)
307 {
308 if ( ! isset($this->template[$val]))
309 {
310 $this->template[$val] = $this->temp[$val];
311 }
312 }
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Default Template
319 *
320 * @access private
321 * @return void
322 */
323 function _default_template()
324 {
325 return array (
326 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
327
328 'heading_row_start' => '<tr>',
329 'heading_row_end' => '</tr>',
330 'heading_cell_start' => '<th>',
331 'heading_cell_end' => '</th>',
332
333 'row_start' => '<tr>',
334 'row_end' => '</tr>',
335 'cell_start' => '<td>',
336 'cell_end' => '</td>',
337
338 'row_alt_start' => '<tr>',
339 'row_alt_end' => '</tr>',
340 'cell_alt_start' => '<td>',
341 'cell_alt_end' => '</td>',
342
343 'table_close' => '</table>'
344 );
345 }
admina1931ad2006-10-09 04:17:38 +0000346
admin2f8ae022006-10-09 03:25:43 +0000347
348}
349
350?>