From b082db707e5c6f681bc6114c9b16893d832f22d5 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Tue, 27 Aug 2024 16:16:46 +0200 Subject: [PATCH] Icingadb/CustomVarRenderer: Support rendering of dictionary values --- .../Icingadb/CustomVarRenderer.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php b/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php index d1db183d..78d81e2a 100644 --- a/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php +++ b/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php @@ -22,6 +22,9 @@ class CustomVarRenderer extends CustomVarRendererHook /** @var array Related datalists and their keys and values */ protected $datalistMaps = []; + /** @var array Related dictionary field names */ + protected $dictionaryNames = []; + /** * Get a database connection to the director database * @@ -85,6 +88,8 @@ class CustomVarRenderer extends CustomVarRendererHook if ($field->get('datatype') === 'Icinga\Module\Director\DataType\DataTypeDatalist') { $fieldsWithDataLists[$field->get('id')] = $field; + } elseif ($field->get('datatype') === 'Icinga\Module\Director\DataType\DataTypeDictionary') { + $this->dictionaryNames[] = $field->get('varname'); } } @@ -136,6 +141,8 @@ class CustomVarRenderer extends CustomVarRendererHook if (isset($this->datalistMaps[$key][$value])) { return $this->datalistMaps[$key][$value]; + } elseif ($value !== null && in_array($key, $this->dictionaryNames)) { + return $this->renderDictionaryVal((array) $value); } } @@ -150,4 +157,27 @@ class CustomVarRenderer extends CustomVarRendererHook return null; } + + /** + * Render the value of the dictionary + * + * @param array $value + * + * @return array + */ + protected function renderDictionaryVal(array $value): array + { + $newValue = []; + foreach ($value as $key => $val) { + if (is_array($val)) { + foreach ($val as $subKey => $subVal) { + $label = $this->renderCustomVarKey($subKey) ?? $subKey; + $subVal = $this->renderCustomVarValue($subKey, $subVal) ?? $subVal; + $newValue[$key][$label] = $subVal; + } + } + } + + return $newValue; + } }