2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* GenericTable.php
|
|
|
|
* Contains key class GenericTable.
|
|
|
|
*
|
2023-04-08 21:32:38 +02:00
|
|
|
* Kabuki CMS (C) 2013-2023, Aaron van Geffen
|
2016-09-01 23:13:23 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
class GenericTable
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2021-02-17 20:08:22 +01:00
|
|
|
private $header = [];
|
|
|
|
private $body = [];
|
|
|
|
private $pageIndex = null;
|
|
|
|
private $currentPage = 1;
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2021-02-17 20:08:22 +01:00
|
|
|
private $title;
|
|
|
|
private $title_class;
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
public $form_above;
|
|
|
|
public $form_below;
|
2023-03-11 13:20:59 +01:00
|
|
|
private $table_class;
|
2022-12-25 13:50:33 +01:00
|
|
|
private $sort_direction;
|
|
|
|
private $sort_order;
|
|
|
|
private $base_url;
|
|
|
|
private $start;
|
|
|
|
private $items_per_page;
|
|
|
|
private $recordCount;
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function __construct($options)
|
|
|
|
{
|
2024-12-19 15:00:00 +01:00
|
|
|
$this->initOrder($options);
|
|
|
|
$this->initPagination($options);
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2024-12-19 15:00:00 +01:00
|
|
|
$data = $options['get_data']($this->start, $this->items_per_page,
|
|
|
|
$this->sort_order, $this->sort_direction);
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
// Okay, now for the column headers...
|
|
|
|
$this->generateColumnHeaders($options);
|
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
// Should we create a page index?
|
2024-12-19 15:00:00 +01:00
|
|
|
if ($this->recordCount > $this->items_per_page)
|
2021-02-17 16:41:58 +01:00
|
|
|
$this->generatePageIndex($options);
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2021-02-17 20:35:30 +01:00
|
|
|
// Process the data to be shown into rows.
|
2024-12-19 15:00:00 +01:00
|
|
|
if (!empty($data))
|
|
|
|
$this->processAllRows($data, $options);
|
2016-09-01 23:13:23 +02:00
|
|
|
else
|
2021-02-17 20:35:30 +01:00
|
|
|
$this->body = $options['no_items_label'] ?? '';
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2023-03-11 13:20:59 +01:00
|
|
|
$this->table_class = $options['table_class'] ?? '';
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
// Got a title?
|
2021-02-17 16:09:49 +01:00
|
|
|
$this->title = $options['title'] ?? '';
|
|
|
|
$this->title_class = $options['title_class'] ?? '';
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
// Maybe even a form or two?
|
2021-02-17 16:09:49 +01:00
|
|
|
$this->form_above = $options['form_above'] ?? $options['form'] ?? null;
|
|
|
|
$this->form_below = $options['form_below'] ?? $options['form'] ?? null;
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
2024-12-19 15:00:00 +01:00
|
|
|
private function initOrder($options)
|
|
|
|
{
|
|
|
|
assert(isset($options['default_sort_order']));
|
|
|
|
assert(isset($options['default_sort_direction']));
|
|
|
|
|
|
|
|
// Validate sort order (column)
|
|
|
|
$this->sort_order = $options['sort_order'];
|
|
|
|
if (empty($this->sort_order) || empty($options['columns'][$this->sort_order]['is_sortable']))
|
|
|
|
$this->sort_order = $options['default_sort_order'];
|
|
|
|
|
|
|
|
// Validate sort direction
|
|
|
|
$this->sort_direction = $options['sort_direction'];
|
|
|
|
if (empty($this->sort_direction) || !in_array($this->sort_direction, ['up', 'down']))
|
|
|
|
$this->sort_direction = $options['default_sort_direction'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function initPagination(array $options)
|
|
|
|
{
|
|
|
|
assert(isset($options['base_url']));
|
|
|
|
assert(isset($options['items_per_page']));
|
|
|
|
|
|
|
|
$this->base_url = $options['base_url'];
|
|
|
|
|
|
|
|
$this->recordCount = $options['get_count']();
|
|
|
|
$this->items_per_page = !empty($options['items_per_page']) ? $options['items_per_page'] : 30;
|
|
|
|
|
|
|
|
$this->start = empty($options['start']) || !is_numeric($options['start']) || $options['start'] < 0 || $options['start'] > $this->recordCount ? 0 : $options['start'];
|
|
|
|
|
|
|
|
$numPages = max(1, ceil($this->recordCount / $this->items_per_page));
|
|
|
|
$this->currentPage = min(ceil($this->start / $this->items_per_page) + 1, $numPages);
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
private function generateColumnHeaders($options)
|
|
|
|
{
|
|
|
|
foreach ($options['columns'] as $key => $column)
|
|
|
|
{
|
|
|
|
if (empty($column['header']))
|
|
|
|
continue;
|
|
|
|
|
2024-12-19 12:01:00 +01:00
|
|
|
$isSortable = !empty($column['is_sortable']);
|
2021-02-17 20:38:27 +01:00
|
|
|
$sortDirection = $key == $this->sort_order && $this->sort_direction === 'up' ? 'down' : 'up';
|
|
|
|
|
2019-09-29 14:47:56 +02:00
|
|
|
$header = [
|
2016-09-01 23:13:23 +02:00
|
|
|
'class' => isset($column['class']) ? $column['class'] : '',
|
2023-03-11 13:20:59 +01:00
|
|
|
'cell_class' => isset($column['cell_class']) ? $column['cell_class'] : null,
|
2016-09-01 23:13:23 +02:00
|
|
|
'colspan' => !empty($column['header_colspan']) ? $column['header_colspan'] : 1,
|
2021-02-17 20:38:27 +01:00
|
|
|
'href' => $isSortable ? $this->getLink($this->start, $key, $sortDirection) : null,
|
2016-09-01 23:13:23 +02:00
|
|
|
'label' => $column['header'],
|
|
|
|
'scope' => 'col',
|
|
|
|
'sort_mode' => $key == $this->sort_order ? $this->sort_direction : null,
|
|
|
|
'width' => !empty($column['header_width']) && is_int($column['header_width']) ? $column['header_width'] : null,
|
2019-09-29 14:47:56 +02:00
|
|
|
];
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
$this->header[] = $header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
private function generatePageIndex($options)
|
|
|
|
{
|
|
|
|
$this->pageIndex = new PageIndex([
|
|
|
|
'base_url' => $this->base_url,
|
|
|
|
'index_class' => $options['index_class'] ?? '',
|
|
|
|
'items_per_page' => $this->items_per_page,
|
2021-02-17 20:06:33 +01:00
|
|
|
'linkBuilder' => [$this, 'getLink'],
|
2021-02-17 16:41:58 +01:00
|
|
|
'recordCount' => $this->recordCount,
|
|
|
|
'sort_direction' => $this->sort_direction,
|
|
|
|
'sort_order' => $this->sort_order,
|
|
|
|
'start' => $this->start,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-09-02 14:19:47 +02:00
|
|
|
public function getLink($start = null, $order = null, $dir = null)
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
if ($start === null)
|
|
|
|
$start = $this->start;
|
|
|
|
if ($order === null)
|
|
|
|
$order = $this->sort_order;
|
|
|
|
if ($dir === null)
|
|
|
|
$dir = $this->sort_direction;
|
|
|
|
|
|
|
|
return $this->base_url . (strpos($this->base_url, '?') ? '&' : '?') . 'start=' . $start . '&order=' . $order. '&dir=' . $dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOffset()
|
|
|
|
{
|
|
|
|
return $this->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHeader()
|
|
|
|
{
|
|
|
|
return $this->header;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBody()
|
|
|
|
{
|
|
|
|
return $this->body;
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
public function getCurrentPage()
|
|
|
|
{
|
|
|
|
return $this->currentPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPageIndex()
|
|
|
|
{
|
|
|
|
return $this->pageIndex;
|
|
|
|
}
|
|
|
|
|
2023-03-11 13:20:59 +01:00
|
|
|
public function getTableClass()
|
|
|
|
{
|
|
|
|
return $this->table_class;
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTitleClass()
|
|
|
|
{
|
|
|
|
return $this->title_class;
|
|
|
|
}
|
2021-02-17 20:35:30 +01:00
|
|
|
|
|
|
|
private function processAllRows($rows, $options)
|
|
|
|
{
|
|
|
|
foreach ($rows as $i => $row)
|
|
|
|
{
|
|
|
|
$newRow = [
|
|
|
|
'cells' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($options['columns'] as $column)
|
|
|
|
{
|
|
|
|
// Process data for this particular cell.
|
|
|
|
if (isset($column['parse']))
|
|
|
|
$value = self::processCell($column['parse'], $row);
|
|
|
|
else
|
|
|
|
$value = $row[$column['value']];
|
|
|
|
|
|
|
|
// Append the cell to the row.
|
|
|
|
$newRow['cells'][] = [
|
2023-03-11 13:20:59 +01:00
|
|
|
'class' => $column['cell_class'] ?? '',
|
2021-02-17 20:35:30 +01:00
|
|
|
'value' => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the new row in the body.
|
|
|
|
$this->body[] = $newRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processCell($options, $rowData)
|
|
|
|
{
|
|
|
|
if (!isset($options['type']))
|
|
|
|
$options['type'] = 'value';
|
|
|
|
|
|
|
|
// Parse the basic value first.
|
|
|
|
switch ($options['type'])
|
|
|
|
{
|
|
|
|
// Basic option: simply take a use a particular data property.
|
|
|
|
case 'value':
|
2024-12-19 15:00:00 +01:00
|
|
|
$value = htmlspecialchars($rowData[$options['data']] ?? '');
|
2021-02-17 20:35:30 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Processing via a lambda function.
|
|
|
|
case 'function':
|
|
|
|
$value = $options['data']($rowData);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Using sprintf to fill out a particular pattern.
|
|
|
|
case 'sprintf':
|
|
|
|
$parameters = [$options['data']['pattern']];
|
|
|
|
foreach ($options['data']['arguments'] as $identifier)
|
|
|
|
$parameters[] = $rowData[$identifier];
|
|
|
|
|
|
|
|
$value = sprintf(...$parameters);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Timestamps get custom treatment.
|
|
|
|
case 'timestamp':
|
|
|
|
if (empty($options['data']['pattern']) || $options['data']['pattern'] === 'long')
|
2022-05-07 13:25:19 +02:00
|
|
|
$pattern = 'Y-m-d H:i';
|
2021-02-17 20:35:30 +01:00
|
|
|
elseif ($options['data']['pattern'] === 'short')
|
2022-05-07 13:25:19 +02:00
|
|
|
$pattern = 'Y-m-d';
|
2021-02-17 20:35:30 +01:00
|
|
|
else
|
|
|
|
$pattern = $options['data']['pattern'];
|
|
|
|
|
2022-07-14 16:45:32 +02:00
|
|
|
if (!isset($rowData[$options['data']['timestamp']]))
|
|
|
|
$timestamp = 0;
|
|
|
|
elseif (!is_numeric($rowData[$options['data']['timestamp']]))
|
2021-02-17 20:35:30 +01:00
|
|
|
$timestamp = strtotime($rowData[$options['data']['timestamp']]);
|
|
|
|
else
|
|
|
|
$timestamp = (int) $rowData[$options['data']['timestamp']];
|
|
|
|
|
|
|
|
if (isset($options['data']['if_null']) && $timestamp == 0)
|
|
|
|
$value = $options['data']['if_null'];
|
|
|
|
else
|
2022-05-07 13:25:19 +02:00
|
|
|
$value = date($pattern, $timestamp);
|
2021-02-17 20:35:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a link, if requested.
|
|
|
|
if (!empty($options['link']))
|
|
|
|
{
|
|
|
|
// First, generate the replacement variables.
|
|
|
|
$keys = array_keys($rowData);
|
|
|
|
$values = array_values($rowData);
|
|
|
|
foreach ($keys as $keyKey => $keyValue)
|
|
|
|
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
|
|
|
|
|
|
|
|
$value = '<a href="' . str_replace($keys, $values, $options['link']) . '">' . $value . '</a>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|