GenericTable: rework timestamp formatting

This commit is contained in:
Aaron van Geffen 2025-01-08 18:37:17 +01:00
parent 7c25d628e1
commit 00ca931cf3
3 changed files with 24 additions and 37 deletions

View File

@ -70,10 +70,8 @@ class ManageErrors extends HTMLController
'time' => [
'format' => [
'type' => 'timestamp',
'data' => [
'timestamp' => 'time',
'pattern' => 'long',
],
'pattern' => 'long',
'value' => 'time',
],
'header' => 'Time',
'is_sortable' => true,

View File

@ -60,10 +60,8 @@ class ManageUsers extends HTMLController
'last_action_time' => [
'format' => [
'type' => 'timestamp',
'data' => [
'timestamp' => 'last_action_time',
'pattern' => 'long',
],
'pattern' => 'long',
'value' => 'last_action_time',
],
'header' => 'Last activity',
'is_sortable' => true,

View File

@ -217,39 +217,30 @@ class GenericTable
private function processFormatting($options, $rowData)
{
// Parse the basic value first.
switch ($options['type'])
if ($options['type'] === 'timestamp')
{
// Timestamps get custom treatment.
case 'timestamp':
if (empty($options['data']['pattern']) || $options['data']['pattern'] === 'long')
$pattern = 'Y-m-d H:i';
elseif ($options['data']['pattern'] === 'short')
$pattern = 'Y-m-d';
else
$pattern = $options['data']['pattern'];
if (empty($options['pattern']) || $options['pattern'] === 'long')
$pattern = 'Y-m-d H:i';
elseif ($options['pattern'] === 'short')
$pattern = 'Y-m-d';
else
$pattern = $options['pattern'];
if (!isset($rowData[$options['data']['timestamp']]))
$timestamp = 0;
elseif (!is_numeric($rowData[$options['data']['timestamp']]))
$timestamp = strtotime($rowData[$options['data']['timestamp']]);
else
$timestamp = (int) $rowData[$options['data']['timestamp']];
assert(isset($rowData[$options['value']]));
if (!is_numeric($rowData[$options['value']]))
$timestamp = strtotime($rowData[$options['value']]);
else
$timestamp = (int) $rowData[$options['value']];
if (isset($options['data']['if_null']) && $timestamp == 0)
$value = $options['data']['if_null'];
else
$value = date($pattern, $timestamp);
break;
if (isset($options['if_null']) && $timestamp == 0)
$value = $options['if_null'];
else
$value = date($pattern, $timestamp);
return $value;
}
// TODO: deprecated
if (!empty($options['link']))
{
$value = $this->processLink($options['link'], $value, $rowData);
}
return $value;
else
throw ValueError('Unexpected formatter type: ' . $options['type']);
}
private function processLink($template, $value, array $rowData)