* * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ namespace CodeIgniter\View; use CodeIgniter\Database\BaseResult; /** * HTML Table Generating Class * * Lets you create tables manually or from database result objects, or arrays. * * @see \CodeIgniter\View\TableTest */ class Table { /** * Data for table rows * * @var array */ public $rows = []; /** * Data for table heading * * @var array */ public $heading = []; /** * Data for table footing * * @var array */ public $footing = []; /** * Whether or not to automatically create the table header * * @var bool */ public $autoHeading = true; /** * Table caption * * @var string|null */ public $caption; /** * Table layout template * * @var array */ public $template; /** * Newline setting * * @var string */ public $newline = "\n"; /** * Contents of empty cells * * @var string */ public $emptyCells = ''; /** * Callback for custom table layout * * @var callable|null */ public $function; /** * Order each inserted row by heading keys */ private bool $syncRowsWithHeading = false; /** * Set the template from the table config file if it exists * * @param array $config (default: array()) */ public function __construct($config = []) { // initialize config foreach ($config as $key => $val) { $this->template[$key] = $val; } } /** * Set the template * * @param array $template * * @return bool */ public function setTemplate($template) { if (! is_array($template)) { return false; } $this->template = $template; return true; } /** * Set the table heading * * Can be passed as an array or discreet params * * @return Table */ public function setHeading() { $this->heading = $this->_prepArgs(func_get_args()); return $this; } /** * Set the table footing * * Can be passed as an array or discreet params * * @return Table */ public function setFooting() { $this->footing = $this->_prepArgs(func_get_args()); return $this; } /** * Set columns. Takes a one-dimensional array as input and creates * a multi-dimensional array with a depth equal to the number of * columns. This allows a single array with many elements to be * displayed in a table that has a fixed column count. * * @param array $array * @param int $columnLimit * * @return array|false */ public function makeColumns($array = [], $columnLimit = 0) { if (! is_array($array) || $array === [] || ! is_int($columnLimit)) { return false; } // Turn off the auto-heading feature since it's doubtful we // will want headings from a one-dimensional array $this->autoHeading = false; $this->syncRowsWithHeading = false; if ($columnLimit === 0) { return $array; } $new = []; do { $temp = array_splice($array, 0, $columnLimit); if (count($temp) < $columnLimit) { for ($i = count($temp); $i < $columnLimit; $i++) { $temp[] = ' '; } } $new[] = $temp; } while ($array !== []); return $new; } /** * Set "empty" cells * * @param string $value * * @return Table */ public function setEmpty($value) { $this->emptyCells = $value; return $this; } /** * Add a table row * * Can be passed as an array or discreet params * * @return Table */ public function addRow() { $tmpRow = $this->_prepArgs(func_get_args()); if ($this->syncRowsWithHeading && $this->heading !== []) { // each key has an index $keyIndex = array_flip(array_keys($this->heading)); // figure out which keys need to be added $missingKeys = array_diff_key($keyIndex, $tmpRow); // Remove all keys which don't exist in $keyIndex $tmpRow = array_filter($tmpRow, static fn ($k) => array_key_exists($k, $keyIndex), ARRAY_FILTER_USE_KEY); // add missing keys to row, but use $this->emptyCells $tmpRow = array_merge($tmpRow, array_map(fn ($v) => ['data' => $this->emptyCells], $missingKeys)); // order keys by $keyIndex values uksort($tmpRow, static fn ($k1, $k2) => $keyIndex[$k1] <=> $keyIndex[$k2]); } $this->rows[] = $tmpRow; return $this; } /** * Set to true if each row column should be synced by keys defined in heading. * * If a row has a key which does not exist in heading, it will be filtered out * If a row does not have a key which exists in heading, the field will stay empty * * @return $this */ public function setSyncRowsWithHeading(bool $orderByKey) { $this->syncRowsWithHeading = $orderByKey; return $this; } /** * Prep Args * * Ensures a standard associative array format for all cell data * * @return array */ protected function _prepArgs(array $args) { // If there is no $args[0], skip this and treat as an associative array // This can happen if there is only a single key, for example this is passed to table->generate // array(array('foo'=>'bar')) if (isset($args[0]) && count($args) === 1 && is_array($args[0])) { $args = $args[0]; } foreach ($args as $key => $val) { if (! is_array($val)) { $args[$key] = ['data' => $val]; } } return $args; } /** * Add a table caption * * @param string $caption * * @return Table */ public function setCaption($caption) { $this->caption = $caption; return $this; } /** * Generate the table * * @param array|BaseResult|null $tableData * * @return string */ public function generate($tableData = null) { // The table data can optionally be passed to this function // either as a database result object or an array if ($tableData !== null && $tableData !== []) { if ($tableData instanceof BaseResult) { $this->_setFromDBResult($tableData); } elseif (is_array($tableData)) { $this->_setFromArray($tableData); } } // Is there anything to display? No? Smite them! if ($this->heading === [] && $this->rows === []) { return 'Undefined table data'; } // Compile and validate the template date $this->_compileTemplate(); // Validate a possibly existing custom cell manipulation function if (isset($this->function) && ! is_callable($this->function)) { $this->function = null; } // Build the table! $out = $this->template['table_open'] . $this->newline; // Add any caption here if ($this->caption) { $out .= '
', 'heading_cell_end' => ' | ', 'tfoot_open' => '', 'tfoot_close' => '', 'footing_row_start' => '
---|
', 'footing_cell_end' => ' | ', 'tbody_open' => '', 'tbody_close' => '', 'row_start' => '
', 'cell_end' => ' | ', 'row_alt_start' => '
', 'cell_alt_end' => ' | ', 'table_close' => '