- md5
- b91b977a342a74cdcd277873279fe9f6
- sha1
- a5e092f7ef31b4d61f9d614c019602c34284aa9e
- sha256
- 1c780eafd0742fda4401063e779a030f47673b3ae3c2a0ca10246a0f9bbc963e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
<?php
/**
* @package Joomla.Platform
* @subpackage Grid
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* JGrid class to dynamically generate HTML tables
*
* @since 11.3
*/
class JGrid
{
/**
* Array of columns
* @var array
* @since 11.3
*/
protected $columns = array();
/**
* Current active row
* @var int
* @since 11.3
*/
protected $activeRow = 0;
/**
* Rows of the table (including header and footer rows)
* @var array
* @since 11.3
*/
protected $rows = array();
/**
* Header and Footer row-IDs
* @var array
* @since 11.3
*/
protected $specialRows = array('header' => array(), 'footer' => array());
/**
* Associative array of attributes for the table-tag
* @var array
* @since 11.3
*/
protected $options;
/**
* Constructor for a JGrid object
*
* @param array $options Associative array of attributes for the table-tag
*
* @since 11.3
*/
public function __construct($options = array())
{
$this->setTableOptions($options, true);
}
/**
* Magic function to render this object as a table.
*
* @return string
*
* @since 11.3
*/
public function __toString()
{
return $this->toString();
}
/**
* Method to set the attributes for a table-tag
*
* @param array $options Associative array of attributes for the table-tag
* @param bool $replace Replace possibly existing attributes
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function setTableOptions($options = array(), $replace = false)
{
if ($replace)
{
$this->options = $options;
}
else
{
$this->options = array_merge($this->options, $options);
}
return $this;
}
/**
* Get the Attributes of the current table
*
* @return array Associative array of attributes
*
* @since 11.3
*/
public function getTableOptions()
{
return $this->options;
}
/**
* Add new column name to process
*
* @param string $name Internal column name
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function addColumn($name)
{
$this->columns[] = $name;
return $this;
}
/**
* Returns the list of internal columns
*
* @return array List of internal columns
*
* @since 11.3
*/
public function getColumns()
{
return $this->columns;
}
/**
* Delete column by name
*
* @param string $name Name of the column to be deleted
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function deleteColumn($name)
{
$index = array_search($name, $this->columns);
if ($index !== false)
{
unset($this->columns[$index]);
$this->columns = array_values($this->columns);
}
return $this;
}
/**
* Method to set a whole range of columns at once
* This can be used to re-order the columns, too
*
* @param array $columns List of internal column names
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function setColumns($columns)
{
$this->columns = array_values($columns);
return $this;
}
/**
* Adds a row to the table and sets the currently
* active row to the new row
*
* @param array $options Associative array of attributes for the row
* @param int $special 1 for a new row in the header, 2 for a new row in the footer
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function addRow($options = array(), $special = false)
{
$this->rows[]['_row'] = $options;
$this->activeRow = count($this->rows) - 1;
if ($special)
{
if ($special === 1)
{
$this->specialRows['header'][] = $this->activeRow;
}
else
{
$this->specialRows['footer'][] = $this->activeRow;
}
}
return $this;
}
/**
* Method to get the attributes of the currently active row
*
* @return array Associative array of attributes
*
* @since 11.3
*/
public function getRowOptions()
{
return $this->rows[$this->activeRow]['_row'];
}
/**
* Method to set the attributes of the currently active row
*
* @param array $options Associative array of attributes
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function setRowOptions($options)
{
$this->rows[$this->activeRow]['_row'] = $options;
return $this;
}
/**
* Get the currently active row ID
*
* @return int ID of the currently active row
*
* @since 11.3
*/
public function getActiveRow()
{
return $this->activeRow;
}
/**
* Set the currently active row
*
* @param int $id ID of the row to be set to current
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function setActiveRow($id)
{
$this->activeRow = (int) $id;
return $this;
}
/**
* Set cell content for a specific column for the
* currently active row
*
* @param string $name Name of the column
* @param string $content Content for the cell
* @param array $option Associative array of attributes for the td-element
* @param bool $replace If false, the content is appended to the current content of the cell
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function setRowCell($name, $content, $option = array(), $replace = true)
{
if ($replace || !isset($this->rows[$this->activeRow][$name]))
{
$cell = new stdClass;
$cell->options = $option;
$cell->content = $content;
$this->rows[$this->activeRow][$name] = $cell;
}
else
{
$this->rows[$this->activeRow][$name]->content .= $content;
$this->rows[$this->activeRow][$name]->options = $option;
}
return $this;
}
/**
* Get all data for a row
*
* @param int $id ID of the row to return
*
* @return array Array of columns of a table row
*
* @since 11.3
*/
public function getRow($id = false)
{
if ($id === false)
{
$id = $this->activeRow;
}
if (isset($this->rows[(int) $id]))
{
return $this->rows[(int) $id];
}
else
{
return false;
}
}
/**
* Get the IDs of all rows in the table
*
* @param int $special false for the standard rows, 1 for the header rows, 2 for the footer rows
*
* @return array Array of IDs
*
* @since 11.3
*/
public function getRows($special = false)
{
if ($special)
{
if ($special === 1)
{
return $this->specialRows['header'];
}
else
{
return $this->specialRows['footer'];
}
}
return array_diff(array_keys($this->rows), array_merge($this->specialRows['header'], $this->specialRows['footer']));
}
/**
* Delete a row from the object
*
* @param int $id ID of the row to be deleted
*
* @return JGrid This object for chaining
*
* @since 11.3
*/
public function deleteRow($id)
{
unset($this->rows[$id]);
if (in_array($id, $this->specialRows['header']))
{
unset($this->specialRows['header'][array_search($id, $this->specialRows['header'])]);
}
if (in_array($id, $this->specialRows['footer']))
{
unset($this->specialRows['footer'][array_search($id, $this->specialRows['footer'])]);
}
if ($this->activeRow == $id)
{
end($this->rows);
$this->activeRow = key($this->rows);
}
return $this;
}
/**
* Render the HTML table
*
* @return string The rendered HTML table
*
* @since 11.3
*/
public function toString()
{
$output = array();
$output[] = '<table' . $this->renderAttributes($this->getTableOptions()) . '>';
if (count($this->specialRows['header']))
{
$output[] = $this->renderArea($this->specialRows['header'], 'thead', 'th');
}
if (count($this->specialRows['footer']))
{
$output[] = $this->renderArea($this->specialRows['footer'], 'tfoot');
}
$ids = array_diff(array_keys($this->rows), array_merge($this->specialRows['header'], $this->specialRows['footer']));
if (count($ids))
{
$output[] = $this->renderArea($ids);
}
$output[] = '</table>';
return implode('', $output);
}
/**
* Render an area of the table
*
* @param array $ids IDs of the rows to render
* @param string $area Name of the area to render. Valid: tbody, tfoot, thead
* @param string $cell Name of the cell to render. Valid: td, th
*
* @return string The rendered table area
*
* @since 11.3
*/
protected function renderArea($ids, $area = 'tbody', $cell = 'td')
{
$output = array();
$output[] = '<' . $area . ">\n";
foreach ($ids as $id)
{
$output[] = "\t<tr" . $this->renderAttributes($this->rows[$id]['_row']) . ">\n";
foreach ($this->getColumns() as $name)
{
if (isset($this->rows[$id][$name]))
{
$column = $this->rows[$id][$name];
$output[] = "\t\t<" . $cell . $this->renderAttributes($column->options) . '>' . $column->content . '</' . $cell . ">\n";
}
}
$output[] = "\t</tr>\n";
}
$output[] = '</' . $area . '>';
return implode('', $output);
}
/**
* Renders an HTML attribute from an associative array
*
* @param array $attributes Associative array of attributes
*
* @return string The HTML attribute string
*
* @since 11.3
*/
protected function renderAttributes($attributes)
{
if (count((array) $attributes) == 0)
{
return '';
}
$return = array();
foreach ($attributes as $key => $option)
{
$return[] = $key . '="' . $option . '"';
}
return ' ' . implode(' ', $return);
}
}
The same content is in 29 releases, Joomla 3.5.0-beta2 to 3.7.0-alpha1. See them all, or every version of this file.