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' => [ 'pattern' => 'long',
'timestamp' => 'time', 'value' => 'time',
'pattern' => 'long',
],
], ],
'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' => [ 'pattern' => 'long',
'timestamp' => 'last_action_time', 'value' => 'last_action_time',
'pattern' => 'long',
],
], ],
'header' => 'Last activity', 'header' => 'Last activity',
'is_sortable' => true, 'is_sortable' => true,

View File

@ -217,39 +217,30 @@ 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': $pattern = 'Y-m-d H:i';
if (empty($options['data']['pattern']) || $options['data']['pattern'] === 'long') elseif ($options['pattern'] === 'short')
$pattern = 'Y-m-d H:i'; $pattern = 'Y-m-d';
elseif ($options['data']['pattern'] === 'short') else
$pattern = 'Y-m-d'; $pattern = $options['pattern'];
else
$pattern = $options['data']['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['value']];
$timestamp = (int) $rowData[$options['data']['timestamp']];
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;
return $value;
} }
else
// TODO: deprecated throw ValueError('Unexpected formatter type: ' . $options['type']);
if (!empty($options['link']))
{
$value = $this->processLink($options['link'], $value, $rowData);
}
return $value;
} }
private function processLink($template, $value, array $rowData) private function processLink($template, $value, array $rowData)