From 10c81a4eea87a17a180ff93cc9c3aa2cc6d72d85 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Thu, 4 Apr 2024 18:19:12 +0200 Subject: [PATCH] mvc: refactor grid search to fetch descriptive values from the model instead of trying to reconstruct them. This makes it easier for special model field types to translate values into human readable formats, also when not being presented to the user like that in a selector. --- .../app/library/OPNsense/Base/UIModelGrid.php | 17 +---------------- .../OPNsense/Base/FieldTypes/BaseField.php | 11 +++++++++++ .../OPNsense/Base/FieldTypes/BaseListField.php | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/opnsense/mvc/app/library/OPNsense/Base/UIModelGrid.php b/src/opnsense/mvc/app/library/OPNsense/Base/UIModelGrid.php index 9abb99a1d..8c2a99b70 100644 --- a/src/opnsense/mvc/app/library/OPNsense/Base/UIModelGrid.php +++ b/src/opnsense/mvc/app/library/OPNsense/Base/UIModelGrid.php @@ -127,22 +127,7 @@ class UIModelGrid $row['uuid'] = $record->getAttributes()['uuid']; foreach ($fields as $fieldname) { if ($record->$fieldname != null) { - $row[$fieldname] = $record->$fieldname->getNodeData(); - if (is_array($row[$fieldname])) { - $listItems = $row[$fieldname]; - $row[$fieldname] = ''; - foreach ($listItems as $fieldValue) { - if ($fieldValue['selected'] == 1) { - if ($row[$fieldname] != "") { - $row[$fieldname] .= ','; - } - $row[$fieldname] .= $fieldValue['value']; - } - } - if ($row[$fieldname] === null) { - $row[$fieldname] = ""; - } - } + $row[$fieldname] = $record->$fieldname->getDescription(); } } diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php index 900e9f590..ed3b7f05d 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php @@ -651,6 +651,17 @@ abstract class BaseField return (string)$this; } + /** + * Return descriptive value of the item. + * For simple types this is usually the internal value, complex types may return what this value represents. + * (descriptions of selected items) + * @return null|string + */ + public function getDescription() + { + return (string)$this; + } + /** * update model with data returning missing repeating tag types. * @param $data array structure containing new model content diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseListField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseListField.php index b456bd3d8..c09df3df0 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseListField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseListField.php @@ -122,6 +122,20 @@ abstract class BaseListField extends BaseField return $result; } + /** + * {@inheritdoc} + */ + public function getDescription() + { + $items = []; + foreach ($this->getNodeData() as $fieldValue) { + if ($fieldValue['selected'] == 1) { + $items[] = $fieldValue['value']; + } + } + return implode(',', $items); + } + /** * {@inheritdoc}