blob: 1afc17ce32265f2a77ca6367237282d782334d47 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.3.1
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * HTML Table Generating Class
20 *
21 * 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 ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/uri.html
28 */
29class CI_Table {
30
31 var $rows = array();
32 var $heading = array();
33 var $auto_heading = TRUE;
34 var $caption = NULL;
35 var $template = NULL;
36 var $newline = "\n";
37 var $empty_cells = "";
38
39
40 function CI_Table()
41 {
42 log_message('debug', "Table Class Initialized");
43 }
44
45 // --------------------------------------------------------------------
46
47 /**
48 * Set the template
49 *
50 * @access public
51 * @param array
52 * @return void
53 */
54 function set_template($template)
55 {
56 if ( ! is_array($template))
57 {
58 return FALSE;
59 }
60
61 $this->template = $template;
62 }
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Set the table heading
68 *
69 * Can be passed as an array or discreet params
70 *
71 * @access public
72 * @param mixed
73 * @return void
74 */
75 function set_heading()
76 {
77 $args = func_get_args();
78 $this->heading = (is_array($args[0])) ? $args[0] : $args;
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Set columns. Takes a one-dimensional array as input and creates
85 * a multi-dimensional array with a depth equal to the number of
86 * columns. This allows a single array with many elements to be
87 * displayed in a table that has a fixed column count.
88 *
89 * @access public
90 * @param array
91 * @param int
92 * @return void
93 */
94 function make_columns($array = array(), $col_limit = 0)
95 {
96 if ( ! is_array($array) OR count($array) == 0)
97 {
98 return FALSE;
99 }
100
101 // Turn off the auto-heading feature since it's doubtful we
102 // will want headings from a one-dimensional array
103 $this->auto_heading = FALSE;
104
105 if ($col_limit == 0)
106 {
107 return $array;
108 }
109
110 $new = array();
111 while(count($array) > 0)
112 {
113 $temp = array_splice($array, 0, $col_limit);
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 /**
132 * 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 /**
148 * 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 * Add a table caption
166 *
167 * @access public
168 * @param string
169 * @return void
170 */
171 function set_caption($caption)
172 {
173 $this->caption = $caption;
174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Generate the table
180 *
181 * @access public
182 * @param mixed
183 * @return string
184 */
185 function generate($table_data = NULL)
186 {
187 // The table data can optionally be passed to this function
188 // either as a database result object or an array
189 if ( ! is_null($table_data))
190 {
191 if (is_object($table_data))
192 {
193 $this->_set_from_object($table_data);
194 }
195 elseif (is_array($table_data))
196 {
197 $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
198 $this->_set_from_array($table_data, $set_heading);
199 }
200 }
201
202 // Is there anything to display? No? Smite them!
203 if (count($this->heading) == 0 AND count($this->rows) == 0)
204 {
205 return 'Undefined table data';
206 }
207
208 // Compile and validate the template date
209 $this->_compile_template();
210
211
212 // Build the table!
213
214 $out = $this->template['table_open'];
215 $out .= $this->newline;
216
217 // Add any caption here
218 if ($this->caption)
219 {
220 $out .= $this->newline;
221 $out .= '<caption>' . $this->caption . '</caption>';
222 $out .= $this->newline;
223 }
224
225 // Is there a table heading to display?
226 if (count($this->heading) > 0)
227 {
228 $out .= $this->template['heading_row_start'];
229 $out .= $this->newline;
230
231 foreach($this->heading as $heading)
232 {
233 $out .= $this->template['heading_cell_start'];
234 $out .= $heading;
235 $out .= $this->template['heading_cell_end'];
236 }
237
238 $out .= $this->template['heading_row_end'];
239 $out .= $this->newline;
240 }
241
242 // Build the table rows
243 if (count($this->rows) > 0)
244 {
245 $i = 1;
246 foreach($this->rows as $row)
247 {
248 if ( ! is_array($row))
249 {
250 break;
251 }
252
253 // We use modulus to alternate the row colors
254 $name = (fmod($i++, 2)) ? '' : 'alt_';
255
256 $out .= $this->template['row_'.$name.'start'];
257 $out .= $this->newline;
258
259 foreach($row as $cell)
260 {
261 $out .= $this->template['cell_'.$name.'start'];
262
263 if ($cell === "")
264 {
265 $out .= $this->empty_cells;
266 }
267 else
268 {
269 $out .= $cell;
270 }
271
272 $out .= $this->template['cell_'.$name.'end'];
273 }
274
275 $out .= $this->template['row_'.$name.'end'];
276 $out .= $this->newline;
277 }
278 }
279
280 $out .= $this->template['table_close'];
281
282 return $out;
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Clears the table arrays. Useful if multiple tables are being generated
289 *
290 * @access public
291 * @return void
292 */
293 function clear()
294 {
295 $this->rows = array();
296 $this->heading = array();
297 $this->auto_heading = TRUE;
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Set table data from a database result object
304 *
305 * @access public
306 * @param object
307 * @return void
308 */
309 function _set_from_object($query)
310 {
311 if ( ! is_object($query))
312 {
313 return FALSE;
314 }
315
316 // First generate the headings from the table column names
317 if (count($this->heading) == 0)
318 {
319 if ( ! method_exists($query, 'list_fields'))
320 {
321 return FALSE;
322 }
323
324 $this->heading = $query->list_fields();
325 }
326
327 // Next blast through the result array and build out the rows
328
329 if ($query->num_rows() > 0)
330 {
331 foreach ($query->result_array() as $row)
332 {
333 $this->rows[] = $row;
334 }
335 }
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Set table data from an array
342 *
343 * @access public
344 * @param array
345 * @return void
346 */
347 function _set_from_array($data, $set_heading = TRUE)
348 {
349 if ( ! is_array($data) OR count($data) == 0)
350 {
351 return FALSE;
352 }
353
354 $i = 0;
355 foreach ($data as $row)
356 {
357 if ( ! is_array($row))
358 {
359 $this->rows[] = $data;
360 break;
361 }
362
363 // If a heading hasn't already been set we'll use the first row of the array as the heading
364 if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
365 {
366 $this->heading = $row;
367 }
368 else
369 {
370 $this->rows[] = $row;
371 }
372
373 $i++;
374 }
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Compile Template
381 *
382 * @access private
383 * @return void
384 */
385 function _compile_template()
386 {
387 if ($this->template == NULL)
388 {
389 $this->template = $this->_default_template();
390 return;
391 }
392
393 $this->temp = $this->_default_template();
394 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)
395 {
396 if ( ! isset($this->template[$val]))
397 {
398 $this->template[$val] = $this->temp[$val];
399 }
400 }
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Default Template
407 *
408 * @access private
409 * @return void
410 */
411 function _default_template()
412 {
413 return array (
414 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
415
416 'heading_row_start' => '<tr>',
417 'heading_row_end' => '</tr>',
418 'heading_cell_start' => '<th>',
419 'heading_cell_end' => '</th>',
420
421 'row_start' => '<tr>',
422 'row_end' => '</tr>',
423 'cell_start' => '<td>',
424 'cell_end' => '</td>',
425
426 'row_alt_start' => '<tr>',
427 'row_alt_end' => '</tr>',
428 'cell_alt_start' => '<td>',
429 'cell_alt_end' => '</td>',
430
431 'table_close' => '</table>'
432 );
433 }
434
435
436}
437
438
439/* End of file Table.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000440/* Location: ./system/libraries/Table.php */