carbonPHP Initial commit v2.0
[carbonphp.git] / Source / carbon / libraries / Table.php
blobb3191575b42ca24a88ed998ae10b629bc62f2ec8
1 <?php
2 /*------------------------------------------------------------
3 * CarbonPHP framework (C) Tom Bell
4 * http://tombell.org.uk
5 *------------------------------------------------------------*/
7 if (!defined('CARBON_PATH'))
9 exit('Direct script access is not allowed.');
12 class Carbon_Table
14 private $rows = array();
15 private $heading = array();
16 private $auto_heading = true;
17 private $caption = null;
18 private $template = null;
19 private $newline = "\n";
20 private $empty_cells = '';
22 public function __construct()
24 log_message('debug', 'Table class initialiased.');
27 public function set_template($template)
29 if (!is_array($template))
31 return false;
34 $this->template = $template;
36 return true;
39 public function set_heading()
41 $args = func_get_args();
42 $this->heading = (is_array($args[0])) ? $args[0] : $args;
45 public function make_columns($array = array(), $col_limit = 0)
47 if (!is_array($array) || count($array) == 0)
49 return false;
52 $this->auto_heading = false;
54 if ($col_limit == 0)
56 return $array;
59 $new = array();
61 while (count($array) > 0)
63 $temp = array_slice($array, 0, $col_limit);
64 $array = array_diff($array, $temp);
66 if (count($temp) < $col_limit)
68 for ($i = count($temp); $i < $col_limit; $i++)
70 $temp[] = '&nbsp';
74 $new[] = $temp;
77 return $new;
80 public function set_empty($value)
82 $this->empty_cells = $value;
85 public function add_row()
87 $args = func_get_args();
88 $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
91 public function set_caption($caption)
93 $this->caption = $caption;
96 public function generate($table_data = null)
98 if (!is_null($table_data))
100 if (is_object($table_data))
102 $this->c_set_from_object($table_data);
104 elseif (is_array($table_data))
106 $set_heading = (count($this->heading) == 0 && $this->auto_heading == false) ? false : true;
107 $this->c_set_from_array($table_data, $set_heading);
111 if (count($this->heading) == 0 && count($this->rows) == 0)
113 return 'Undefined table data';
116 $this->c_compile_template();
118 $out = $this->template['table_open'];
119 $out .= $this->newline;
121 if ($this->caption)
123 $out .= $this->newline;
124 $out .= '<caption>' . $this->caption . '</caption>';
125 $out .= $this->newline;
128 if (count($this->heading) > 0)
130 $out .= $this->template['heading_row_start'];
131 $out .= $this->newline;
133 foreach ($this->heading as $heading)
135 $out .= $this->template['heading_cell_start'];
136 $out .= $heading;
137 $out .= $this->template['heading_cell_end'];
140 $out .= $this->template['heading_row_end'];
141 $out .= $this->newline;
144 if (count($this->rows) > 0)
146 $i = 1;
148 foreach ($this->rows as $row)
150 if (!is_array($row))
152 break;
155 $name = (fmod($i++, 2)) ? '' : 'alt_';
157 $out .= $this->template['row_' . $name . 'start'];
158 $out .= $this->newline;
160 foreach ($row as $cell)
162 $out .= $this->template['cell_' . $name . 'start'];
164 if ($cell == '')
166 $out .= $this->empty_cells;
168 else
170 $out .= $cell;
173 $out .= $this->template['cell_' . $name . 'end'];
176 $out .= $this->template['row_' . $name . 'end'];
177 $out .= $this->newline;
181 $out .= $this->template['table_close'];
183 return $out;
186 public function clear()
188 $this->rows = array();
189 $this->heading = array();
190 $this->auto_heading = true;
193 private function c_set_from_object($query)
195 if (!is_object($query))
197 return false;
200 if (count($this->heading) == 0)
202 if (!method_exists($query, 'list_fields'))
204 return false;
207 $this->heading = $query->list_fields();
210 if ($query->num_rows() > 0)
212 foreach ($query->result_array() as $row)
214 $this->rows[] = $row;
219 private function c_set_from_array($data, $set_heading = true)
221 if (!is_array($data) || count($data) == 0)
223 return false;
226 $i = 0;
228 foreach ($data as $row)
230 if (!is_array($row))
232 $this->rows[] = $data;
233 break;
236 if ($i == 0 && count($data) > 1 && count($this->heading) == 0 && $set_heading == true)
238 $this->heading = $row;
240 else
242 $this->rows[] = $row;
245 $i++;
249 private function c_compile_template()
251 if ($this->template == null)
253 $this->template = $this->c_default_template();
254 return true;
257 $this->temp = $this->c_default_template();
258 $template = array(
259 'table_open',
260 'heading_row_start',
261 'heading_row_end',
262 'heading_cell_start',
263 'heading_cell_end',
264 'row_start',
265 'row_end',
266 'cell_start',
267 'cell_end',
268 'row_alt_start',
269 'row_alt_end',
270 'cell_alt_start',
271 'call_alt_end',
272 'table_close'
275 foreach ($template as $value)
277 if (!isset($this->template[$value]))
279 $this->template[$value] = $this->temp[$value];
284 private function c_default_template()
286 $template = array(
287 'table_open' => '<table border="0" cellingpadding="4" cellspacing="0">',
288 'heading_row_start' => '<tr>',
289 'heading_row_end' => '</tr>',
290 'heading_cell_start' => '<th>',
291 'heading_cell_end' => '</th>',
292 'row_start' => '<tr>',
293 'row_end' => '</tr>',
294 'cell_start' => '<td>',
295 'cell_end' => '</td>',
296 'row_alt_start' => '<tr>',
297 'row_alt_end' => '</tr>',
298 'cell_alt_start' => '<td>',
299 'cell_alt_end' => '</td>',
300 'table_close' => '</table>'
303 return $template;