blob: 1b25a441a6ddfc8561b25d5ec44e8a9a06faf46e [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
admin26364b92006-10-28 05:22:02 +000031 var $rows = array();
32 var $heading = array();
33 var $auto_heading = TRUE;
34 var $template = NULL;
35 var $newline = "\n";
36 var $empty_cells = "";
admin2f8ae022006-10-09 03:25:43 +000037
38
39 function CI_Table()
40 {
41 log_message('debug', "Table Class Initialized");
42 }
43
44 // --------------------------------------------------------------------
45
46 /**
47 * Set the template
48 *
49 * @access public
50 * @param array
51 * @return void
52 */
53 function set_template($template)
54 {
admin27991202006-10-11 01:38:08 +000055 if ( ! is_array($template))
admin2f8ae022006-10-09 03:25:43 +000056 {
57 return FALSE;
58 }
59
60 $this->template = $template;
61 }
62
63 // --------------------------------------------------------------------
64
65 /**
admina1931ad2006-10-09 04:17:38 +000066 * Set the table heading
admin2f8ae022006-10-09 03:25:43 +000067 *
68 * Can be passed as an array or discreet params
69 *
70 * @access public
71 * @param mixed
72 * @return void
73 */
admina1931ad2006-10-09 04:17:38 +000074 function set_heading()
admin2f8ae022006-10-09 03:25:43 +000075 {
76 $args = func_get_args();
77 $this->heading = (is_array($args[0])) ? $args[0] : $args;
78 }
79
80 // --------------------------------------------------------------------
81
82 /**
admin26364b92006-10-28 05:22:02 +000083 * Set columns. Takes a one-dimensional array as input and creates
84 * a multi-dimensional array with a depth equal to the number of
85 * columns. This allows a single array with many elements to be
86 * displayed in a table that has a fixed column count.
87 *
88 * @access public
89 * @param array
90 * @param int
91 * @return void
92 */
93 function make_columns($array = array(), $col_limit = 0)
94 {
95 if ( ! is_array($array) OR count($array) == 0)
96 {
97 return FALSE;
98 }
99
100 // Turn off the auto-heading feature since it's doubtful we
101 // will want headings from a one-dimensional array
102 $this->auto_heading = FALSE;
103
104 if ($col_limit == 0)
105 {
106 return $array;
107 }
108
109 $new = array();
110 while(count($array) > 0)
111 {
112 $temp = array_slice($array, 0, $col_limit);
113 $array = array_diff($array, $temp);
114
115 if (count($temp) < $col_limit)
116 {
117 for ($i = count($temp); $i < $col_limit; $i++)
118 {
119 $temp[] = '&nbsp;';
120 }
121 }
122
123 $new[] = $temp;
124 }
125
126 return $new;
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
admin2bc13852006-10-24 23:16:17 +0000132 * Set "empty" cells
133 *
134 * Can be passed as an array or discreet params
135 *
136 * @access public
137 * @param mixed
138 * @return void
139 */
140 function set_empty($value)
141 {
142 $this->empty_cells = $value;
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
admin2f8ae022006-10-09 03:25:43 +0000148 * Add a table row
149 *
150 * Can be passed as an array or discreet params
151 *
152 * @access public
153 * @param mixed
154 * @return void
155 */
156 function add_row()
157 {
158 $args = func_get_args();
159 $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Generate the table
166 *
167 * @access public
168 * @param mixed
169 * @return string
170 */
171 function generate($table_data = NULL)
172 {
173 // The table data can optionally be passed to this function
174 // either as a database result object or an array
175 if ( ! is_null($table_data))
176 {
177 if (is_object($table_data))
178 {
179 $this->_set_from_object($table_data);
180 }
181 elseif (is_array($table_data))
182 {
admin26364b92006-10-28 05:22:02 +0000183 $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
184 $this->_set_from_array($table_data, $set_heading);
admin2f8ae022006-10-09 03:25:43 +0000185 }
186 }
187
188 // Is there anything to display? No? Smite them!
189 if (count($this->heading) == 0 AND count($this->rows) == 0)
190 {
191 return 'Undefined table data';
192 }
193
adminbd6bee72006-10-21 19:39:00 +0000194 // Compile and validate the template date
admin2f8ae022006-10-09 03:25:43 +0000195 $this->_compile_template();
196
197
198 // Build the table!
199
200 $out = $this->template['table_open'];
201 $out .= $this->newline;
202
203 // Is there a table heading to display?
204 if (count($this->heading) > 0)
205 {
206 $out .= $this->template['heading_row_start'];
207 $out .= $this->newline;
208
209 foreach($this->heading as $heading)
210 {
211 $out .= $this->template['heading_cell_start'];
212 $out .= $heading;
213 $out .= $this->template['heading_cell_end'];
214 }
215
216 $out .= $this->template['heading_row_end'];
217 $out .= $this->newline;
218 }
219
220 // Build the table rows
221 if (count($this->rows) > 0)
222 {
223 $i = 1;
224 foreach($this->rows as $row)
225 {
226 if ( ! is_array($row))
227 {
228 break;
229 }
230
231 // We use modulus to alternate the row colors
admin26364b92006-10-28 05:22:02 +0000232 $name = (fmod($i++, 2)) ? '' : 'alt_';
admin2f8ae022006-10-09 03:25:43 +0000233
admin26364b92006-10-28 05:22:02 +0000234 $out .= $this->template['row_'.$name.'start'];
admin2f8ae022006-10-09 03:25:43 +0000235 $out .= $this->newline;
236
admin2bc13852006-10-24 23:16:17 +0000237 foreach($row as $cell)
admin2f8ae022006-10-09 03:25:43 +0000238 {
admin26364b92006-10-28 05:22:02 +0000239 $out .= $this->template['cell_'.$name.'start'];
admin2bc13852006-10-24 23:16:17 +0000240
241 if ($cell == "")
242 {
243 $out .= $this->empty_cells;
244 }
245 else
246 {
247 $out .= $cell;
248 }
249
admin26364b92006-10-28 05:22:02 +0000250 $out .= $this->template['cell_'.$name.'end'];
admin2f8ae022006-10-09 03:25:43 +0000251 }
252
admin26364b92006-10-28 05:22:02 +0000253 $out .= $this->template['row_'.$name.'end'];
admin2f8ae022006-10-09 03:25:43 +0000254 $out .= $this->newline;
255 }
256 }
257
258 $out .= $this->template['table_close'];
259
260 return $out;
261 }
admin26364b92006-10-28 05:22:02 +0000262
admin2f8ae022006-10-09 03:25:43 +0000263 // --------------------------------------------------------------------
264
265 /**
admin41cece92006-10-28 19:06:45 +0000266 * Clears the table arrays. Useful if multiple tables are beting generated
267 *
268 * @access public
269 * @return void
270 */
adminf3a62042006-10-29 20:24:11 +0000271 function clear()
admin41cece92006-10-28 19:06:45 +0000272 {
273 $this->rows = array();
274 $this->heading = array();
275 $this->auto_heading = TRUE;
276 }
277
278 // --------------------------------------------------------------------
279
280 /**
admin2f8ae022006-10-09 03:25:43 +0000281 * Set table data from a database result object
282 *
283 * @access public
284 * @param object
285 * @return void
286 */
287 function _set_from_object($query)
288 {
289 if ( ! is_object($query))
290 {
291 return FALSE;
292 }
293
294 // First generate the headings from the table column names
295 if (count($this->heading) == 0)
296 {
admin606f99c2006-10-11 23:48:41 +0000297 if ( ! method_exists($query, 'list_fields'))
admin2f8ae022006-10-09 03:25:43 +0000298 {
299 return FALSE;
300 }
301
admin606f99c2006-10-11 23:48:41 +0000302 $this->heading = $query->list_fields();
admin2f8ae022006-10-09 03:25:43 +0000303 }
304
305 // Next blast through the result array and build out the rows
admin40037182006-10-11 19:16:58 +0000306
307 if ($query->num_rows() > 0)
admin2f8ae022006-10-09 03:25:43 +0000308 {
admin40037182006-10-11 19:16:58 +0000309 foreach ($query->result_array() as $row)
310 {
311 $this->rows[] = $row;
312 }
admin2f8ae022006-10-09 03:25:43 +0000313 }
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Set table data from an array
320 *
321 * @access public
322 * @param array
323 * @return void
324 */
admin26364b92006-10-28 05:22:02 +0000325 function _set_from_array($data, $set_heading = TRUE)
admin2f8ae022006-10-09 03:25:43 +0000326 {
327 if ( ! is_array($data) OR count($data) == 0)
328 {
329 return FALSE;
330 }
331
332 $i = 0;
333 foreach ($data as $row)
334 {
335 if ( ! is_array($row))
336 {
337 $this->rows[] = $data;
338 break;
339 }
340
341 // If a heading hasn't already been set we'll use the first row of the array as the heading
admin26364b92006-10-28 05:22:02 +0000342 if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
admin2f8ae022006-10-09 03:25:43 +0000343 {
344 $this->heading = $row;
345 }
346 else
347 {
348 $this->rows[] = $row;
349 }
350
351 $i++;
352 }
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Compile Template
359 *
360 * @access private
361 * @return void
362 */
363 function _compile_template()
364 {
365 if ($this->template == NULL)
366 {
367 $this->template = $this->_default_template();
368 return;
369 }
370
371 $this->temp = $this->_default_template();
372 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)
373 {
374 if ( ! isset($this->template[$val]))
375 {
376 $this->template[$val] = $this->temp[$val];
377 }
378 }
379 }
380
381 // --------------------------------------------------------------------
382
383 /**
384 * Default Template
385 *
386 * @access private
387 * @return void
388 */
389 function _default_template()
390 {
391 return array (
392 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
393
394 'heading_row_start' => '<tr>',
395 'heading_row_end' => '</tr>',
396 'heading_cell_start' => '<th>',
397 'heading_cell_end' => '</th>',
398
399 'row_start' => '<tr>',
400 'row_end' => '</tr>',
401 'cell_start' => '<td>',
402 'cell_end' => '</td>',
403
404 'row_alt_start' => '<tr>',
405 'row_alt_end' => '</tr>',
406 'cell_alt_start' => '<td>',
407 'cell_alt_end' => '</td>',
408
409 'table_close' => '</table>'
410 );
411 }
admina1931ad2006-10-09 04:17:38 +0000412
admin2f8ae022006-10-09 03:25:43 +0000413
414}
415
416?>