12 defined(
'JPATH_PLATFORM') or die;
28 protected $columns = array();
35 protected $activeRow = 0;
42 protected $rows = array();
49 protected $specialRows = array(
'header' => array(),
'footer' => array());
65 public function __construct($options = array())
67 $this->setTableOptions($options,
true);
77 public function __toString()
79 return $this->toString();
92 public function setTableOptions($options = array(), $replace =
false)
96 $this->options = $options;
100 $this->options = array_merge($this->options, $options);
112 public function getTableOptions()
114 return $this->options;
126 public function addColumn($name)
128 $this->columns[] = $name;
140 public function getColumns()
142 return $this->columns;
154 public function deleteColumn($name)
156 $index = array_search($name, $this->columns);
157 if ($index !==
false)
159 unset($this->columns[$index]);
160 $this->columns = array_values($this->columns);
176 public function setColumns($columns)
178 $this->columns = array_values($columns);
194 public function addRow($options = array(), $special =
false)
196 $this->rows[][
'_row'] = $options;
197 $this->activeRow = count($this->rows) - 1;
202 $this->specialRows[
'header'][] = $this->activeRow;
206 $this->specialRows[
'footer'][] = $this->activeRow;
220 public function getRowOptions()
222 return $this->rows[$this->activeRow][
'_row'];
234 public function setRowOptions($options)
236 $this->rows[$this->activeRow][
'_row'] = $options;
248 public function getActiveRow()
250 return $this->activeRow;
262 public function setActiveRow($id)
264 $this->activeRow = (int) $id;
281 public function setRowCell($name, $content, $option = array(), $replace =
true)
283 if ($replace || !isset($this->rows[$this->activeRow][$name]))
285 $cell =
new stdClass;
286 $cell->options = $option;
287 $cell->content = $content;
288 $this->rows[$this->activeRow][$name] = $cell;
292 $this->rows[$this->activeRow][$name]->content .= $content;
293 $this->rows[$this->activeRow][$name]->options = $option;
308 public function getRow($id =
false)
312 $id = $this->activeRow;
315 if (isset($this->rows[(
int) $id]))
317 return $this->rows[(int) $id];
334 public function getRows($special =
false)
340 return $this->specialRows[
'header'];
344 return $this->specialRows[
'footer'];
347 return array_diff(array_keys($this->rows), array_merge($this->specialRows[
'header'], $this->specialRows[
'footer']));
359 public function deleteRow($id)
361 unset($this->rows[$id]);
363 if (in_array($id, $this->specialRows[
'header']))
365 unset($this->specialRows[
'header'][array_search($id, $this->specialRows[
'header'])]);
368 if (in_array($id, $this->specialRows[
'footer']))
370 unset($this->specialRows[
'footer'][array_search($id, $this->specialRows[
'footer'])]);
373 if ($this->activeRow == $id)
376 $this->activeRow = key($this->rows);
389 public function toString()
392 $output[] =
'<table' . $this->renderAttributes($this->getTableOptions()) .
'>';
394 if (count($this->specialRows[
'header']))
396 $output[] = $this->renderArea($this->specialRows[
'header'],
'thead',
'th');
399 if (count($this->specialRows[
'footer']))
401 $output[] = $this->renderArea($this->specialRows[
'footer'],
'tfoot');
404 $ids = array_diff(array_keys($this->rows), array_merge($this->specialRows[
'header'], $this->specialRows[
'footer']));
407 $output[] = $this->renderArea($ids);
410 $output[] =
'</table>';
411 return implode(
'', $output);
425 protected function renderArea($ids, $area =
'tbody', $cell =
'td')
428 $output[] =
'<' . $area .
">\n";
429 foreach ($ids as $id)
431 $output[] =
"\t<tr" . $this->renderAttributes($this->rows[$id][
'_row']) .
">\n";
432 foreach ($this->getColumns() as $name)
434 if (isset($this->rows[$id][$name]))
436 $column = $this->rows[$id][$name];
437 $output[] =
"\t\t<" . $cell . $this->renderAttributes($column->options) .
'>' . $column->content .
'</' . $cell .
">\n";
441 $output[] =
"\t</tr>\n";
443 $output[] =
'</' . $area .
'>';
445 return implode(
'', $output);
457 protected function renderAttributes($attributes)
459 if (count((array) $attributes) == 0)
464 foreach ($attributes as $key => $option)
466 $return[] = $key .
'="' . $option .
'"';
468 return ' ' . implode(
' ', $return);