From a7a99fcdfe972dacc1a6beada7607e73a1689d05 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 7 Jan 2025 17:08:39 +0100 Subject: [PATCH] mvc: implement reusable grid template using form definitions. for https://github.com/opnsense/core/issues/8187 Although this might not be the final version, it adds the relevant metadata to the form definitions and offers a very simple template which can be used as a partial(). Example input field: sysctl.tunable text 1 grid_view entries contain a sequence which may overwrite the default order and can overwrite any data-* attribute available, e.g. false would remove the sortable option from the field. --- .../OPNsense/Base/ControllerBase.php | 64 +++++++++++-------- .../layout_partials/base_bootgrid_table.volt | 29 +++++++++ 2 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 src/opnsense/mvc/app/views/layout_partials/base_bootgrid_table.volt diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Base/ControllerBase.php b/src/opnsense/mvc/app/controllers/OPNsense/Base/ControllerBase.php index c069780b9..0692b7190 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Base/ControllerBase.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Base/ControllerBase.php @@ -234,7 +234,7 @@ class ControllerBase extends ControllerRoot * Extract grid fields from form definition * @return array */ - public function getFormGrid($formname) + public function getFormGrid($formname, $grid_id=null, $edit_alert_id=null) { /* collect all fields, sort by sequence */ $all_data = []; @@ -242,42 +242,54 @@ class ControllerBase extends ControllerRoot foreach ($this->getFormXML($formname) as $rootkey => $rootnode) { if ($rootkey == 'field') { $record = [ - 'sequence' => '9999999', - 'visible' => 'false', - 'ignore' => 'false', - 'sortable' => 'false', - 'width' => '', + 'column-id' => '', 'label' => '', - 'id' => '', - 'type' => 'string' + 'visible' => 'true', + 'sortable' => 'true', + 'identifier' => 'false', + 'type' => 'string' /* XXX: convert to type + formatter using source type? */ ]; foreach ($rootnode as $key => $item) { - if (isset($record[$key])) { - switch ($key) { - case 'label': - $record[$key] = gettext((string)$item); - break; - case 'type': - /* XXX: convert to type + formatter */ - $record[$key] = 'string'; - default: - $record[$key] = (string)$item; - break; - } + switch ($key) { + case 'label': + $record['label'] = gettext((string)$item); + break; + case 'id': + $record['column-id'] = end(explode('.', (string)$item)); + break; } } /* iterate field->grid_view items */ + $this_sequence = '9999999'; foreach ($rootnode->grid_view->children() as $key => $item) { - $record[$key] = (string)$item; - } - if (strtolower($record['ignore']) == 'false') { - $record['fieldname'] = end(explode('.', $record['id'])); - $all_data[sprintf("%010d.%03d", $record['sequence'], $idx++)] = $record; + if ($key == 'ignore' && $item != 'false') { + /* ignore field as requested */ + continue 2; + } elseif ($key == 'sequence') { + $this_sequence = (string)$item; + } else { + $record[$key] = (string)$item; + } } + $all_data[sprintf("%010d.%03d", $this_sequence, $idx++)] = $record; } } + /* prepend identifier */ + $all_data[sprintf("%010d.%03d", 0, 0)] = [ + 'column-id' => 'uuid', + 'label' => gettext('ID'), + 'type' => 'string', + 'identifier' => 'true', + 'visible' => 'false' + ]; ksort($all_data); - return array_values($all_data); + $basename = $grid_id ?? $formname; + return [ + 'table_id' => $basename, + 'edit_dialog_id' => 'dialog_' . $basename, + 'edit_alert_id' => $edit_alert_id == null ? 'change_message_' . $basename : $edit_alert_id, + 'fields' => array_values($all_data) + ]; } /** diff --git a/src/opnsense/mvc/app/views/layout_partials/base_bootgrid_table.volt b/src/opnsense/mvc/app/views/layout_partials/base_bootgrid_table.volt new file mode 100644 index 000000000..779c2f5b4 --- /dev/null +++ b/src/opnsense/mvc/app/views/layout_partials/base_bootgrid_table.volt @@ -0,0 +1,29 @@ +{# requires getFormGrid() input to render #} + + + + {% for field in fields %} + + {% endfor %} + + + + + + + + + + +
{{field['label']}} + {{ lang._('Commands') }} +
+ + + + +