From 00ca931cf3613c93e052cc7ec1260feea0abbd79 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Wed, 8 Jan 2025 18:37:17 +0100 Subject: [PATCH] GenericTable: rework timestamp formatting --- controllers/ManageErrors.php | 6 ++--- controllers/ManageUsers.php | 6 ++--- models/GenericTable.php | 49 +++++++++++++++--------------------- 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/controllers/ManageErrors.php b/controllers/ManageErrors.php index cdb75dc..75186aa 100644 --- a/controllers/ManageErrors.php +++ b/controllers/ManageErrors.php @@ -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, diff --git a/controllers/ManageUsers.php b/controllers/ManageUsers.php index 5e66946..8473596 100644 --- a/controllers/ManageUsers.php +++ b/controllers/ManageUsers.php @@ -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, diff --git a/models/GenericTable.php b/models/GenericTable.php index 2fcb900..ec76a1a 100644 --- a/models/GenericTable.php +++ b/models/GenericTable.php @@ -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)