2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* GenericTable.php
|
|
|
|
* Contains key class GenericTable.
|
|
|
|
*
|
2023-03-11 13:20:59 +01:00
|
|
|
* Global Data Lab code (C) Radboud University Nijmegen
|
|
|
|
* Programming (C) Aaron van Geffen, 2015-2021
|
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;
|
|
|
|
private $tableIsSortable = false;
|
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)
|
|
|
|
{
|
|
|
|
// Make sure we're actually sorting on something sortable.
|
|
|
|
if (!isset($options['sort_order']) || (!empty($options['sort_order']) && empty($options['columns'][$options['sort_order']]['is_sortable'])))
|
|
|
|
$options['sort_order'] = '';
|
|
|
|
|
|
|
|
// Order in which direction?
|
2019-09-29 14:47:56 +02:00
|
|
|
if (!empty($options['sort_direction']) && !in_array($options['sort_direction'], ['up', 'down']))
|
2016-09-01 23:13:23 +02:00
|
|
|
$options['sort_direction'] = 'up';
|
|
|
|
|
|
|
|
// Make sure we know whether we can actually sort on something.
|
|
|
|
$this->tableIsSortable = !empty($options['base_url']);
|
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
// How much data do we have?
|
2021-02-17 16:09:38 +01:00
|
|
|
$this->recordCount = $options['get_count'](...(!empty($options['get_count_params']) ? $options['get_count_params'] : []));
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
// How much data do we need to retrieve?
|
2016-09-01 23:13:23 +02:00
|
|
|
$this->items_per_page = !empty($options['items_per_page']) ? $options['items_per_page'] : 30;
|
|
|
|
|
|
|
|
// Figure out where to start.
|
|
|
|
$this->start = empty($options['start']) || !is_numeric($options['start']) || $options['start'] < 0 || $options['start'] > $this->recordCount ? 0 : $options['start'];
|
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
// Figure out where we are on the whole, too.
|
2022-07-14 16:45:17 +02:00
|
|
|
$numPages = max(1, ceil($this->recordCount / $this->items_per_page));
|
2021-02-17 16:41:58 +01:00
|
|
|
$this->currentPage = min(ceil($this->start / $this->items_per_page) + 1, $numPages);
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
// Let's bear a few things in mind...
|
|
|
|
$this->base_url = $options['base_url'];
|
|
|
|
|
|
|
|
// Gather parameters for the data gather function first.
|
2019-09-29 14:47:56 +02:00
|
|
|
$parameters = [$this->start, $this->items_per_page, $options['sort_order'], $options['sort_direction']];
|
2016-09-01 23:13:23 +02:00
|
|
|
if (!empty($options['get_data_params']) && is_array($options['get_data_params']))
|
|
|
|
$parameters = array_merge($parameters, $options['get_data_params']);
|
|
|
|
|
|
|
|
// Okay, let's fetch the data!
|
2021-02-17 16:09:38 +01:00
|
|
|
$data = $options['get_data'](...$parameters);
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2021-02-17 20:35:30 +01:00
|
|
|
// Extract data into local variables.
|
|
|
|
$rawRowData = $data['rows'];
|
2016-09-01 23:13:23 +02:00
|
|
|
$this->sort_order = $data['order'];
|
|
|
|
$this->sort_direction = $data['direction'];
|
|
|
|
unset($data);
|
|
|
|
|
|
|
|
// Okay, now for the column headers...
|
|
|
|
$this->generateColumnHeaders($options);
|
|
|
|
|
2021-02-17 16:41:58 +01:00
|
|
|
// Should we create a page index?
|
|
|
|
$needsPageIndex = !empty($this->items_per_page) && $this->recordCount > $this->items_per_page;
|
|
|
|
if ($needsPageIndex)
|
|
|
|
$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.
|
|
|
|
if (!empty($rawRowData))
|
|
|
|
$this->processAllRows($rawRowData, $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
|
|
|
}
|
|
|
|
|
|
|
|
private function generateColumnHeaders($options)
|
|
|
|
{
|
|
|
|
foreach ($options['columns'] as $key => $column)
|
|
|
|
{
|
|
|
|
if (empty($column['header']))
|
|
|
|
continue;
|
|
|
|
|
2021-02-17 20:38:27 +01:00
|
|
|
$isSortable = $this->tableIsSortable && !empty($column['is_sortable']);
|
|
|
|
$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':
|
2021-02-17 22:44:26 +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
|
|
|
}
|