blob: 01c45c73ba494b13156161b958f072746791f391 [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 /**
266 * Set table data from a database result object
267 *
268 * @access public
269 * @param object
270 * @return void
271 */
272 function _set_from_object($query)
273 {
274 if ( ! is_object($query))
275 {
276 return FALSE;
277 }
278
279 // First generate the headings from the table column names
280 if (count($this->heading) == 0)
281 {
admin606f99c2006-10-11 23:48:41 +0000282 if ( ! method_exists($query, 'list_fields'))
admin2f8ae022006-10-09 03:25:43 +0000283 {
284 return FALSE;
285 }
286
admin606f99c2006-10-11 23:48:41 +0000287 $this->heading = $query->list_fields();
admin2f8ae022006-10-09 03:25:43 +0000288 }
289
290 // Next blast through the result array and build out the rows
admin40037182006-10-11 19:16:58 +0000291
292 if ($query->num_rows() > 0)
admin2f8ae022006-10-09 03:25:43 +0000293 {
admin40037182006-10-11 19:16:58 +0000294 foreach ($query->result_array() as $row)
295 {
296 $this->rows[] = $row;
297 }
admin2f8ae022006-10-09 03:25:43 +0000298 }
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Set table data from an array
305 *
306 * @access public
307 * @param array
308 * @return void
309 */
admin26364b92006-10-28 05:22:02 +0000310 function _set_from_array($data, $set_heading = TRUE)
admin2f8ae022006-10-09 03:25:43 +0000311 {
312 if ( ! is_array($data) OR count($data) == 0)
313 {
314 return FALSE;
315 }
316
317 $i = 0;
318 foreach ($data as $row)
319 {
320 if ( ! is_array($row))
321 {
322 $this->rows[] = $data;
323 break;
324 }
325
326 // 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 +0000327 if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
admin2f8ae022006-10-09 03:25:43 +0000328 {
329 $this->heading = $row;
330 }
331 else
332 {
333 $this->rows[] = $row;
334 }
335
336 $i++;
337 }
338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Compile Template
344 *
345 * @access private
346 * @return void
347 */
348 function _compile_template()
349 {
350 if ($this->template == NULL)
351 {
352 $this->template = $this->_default_template();
353 return;
354 }
355
356 $this->temp = $this->_default_template();
357 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)
358 {
359 if ( ! isset($this->template[$val]))
360 {
361 $this->template[$val] = $this->temp[$val];
362 }
363 }
364 }
365
366 // --------------------------------------------------------------------
367
368 /**
369 * Default Template
370 *
371 * @access private
372 * @return void
373 */
374 function _default_template()
375 {
376 return array (
377 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
378
379 'heading_row_start' => '<tr>',
380 'heading_row_end' => '</tr>',
381 'heading_cell_start' => '<th>',
382 'heading_cell_end' => '</th>',
383
384 'row_start' => '<tr>',
385 'row_end' => '</tr>',
386 'cell_start' => '<td>',
387 'cell_end' => '</td>',
388
389 'row_alt_start' => '<tr>',
390 'row_alt_end' => '</tr>',
391 'cell_alt_start' => '<td>',
392 'cell_alt_end' => '</td>',
393
394 'table_close' => '</table>'
395 );
396 }
admina1931ad2006-10-09 04:17:38 +0000397
admin2f8ae022006-10-09 03:25:43 +0000398
399}
400
401?>