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' => [ 'time' => [
'format' => [ 'format' => [
'type' => 'timestamp', 'type' => 'timestamp',
'data' => [
'timestamp' => 'time',
'pattern' => 'long', 'pattern' => 'long',
], 'value' => 'time',
], ],
'header' => 'Time', 'header' => 'Time',
'is_sortable' => true, 'is_sortable' => true,

View File

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

View File

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