From 4ca6fd79bccf6a7993b522f854ab376d69d45fbf Mon Sep 17 00:00:00 2001 From: raviks789 Date: Tue, 27 Aug 2024 12:24:24 +0200 Subject: [PATCH] WIP3 --- .../Icingadb/CustomVarEnricher.php | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php b/library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php index 8b8d28525..8629e4d5a 100644 --- a/library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php +++ b/library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php @@ -47,7 +47,6 @@ public function enrichCustomVars(array &$vars, Model $object): array $varsToReplace = json_decode(json_encode($directorObject->getVars()), true) + json_decode(json_encode($directorObject->getInheritedVars()), true); - foreach ($varsToReplace as $varName => $customVar) { if (isset($vars[$varName])) { $newVars[] = $this->resolveCustomVarMapping($varName, $customVar, $connection); @@ -72,7 +71,7 @@ public function enrichCustomVars(array &$vars, Model $object): array * * @return array */ - protected function resolveCustomVarMapping(string $name, $val, DbConnection $conn, bool $grouping = true): array + protected function resolveCustomVarMapping(?string $name, $val, DbConnection $conn, bool $grouping = true): array { if (isset($this->fieldConfig[$name])) { /** @var DirectorDatafield $field */ @@ -83,24 +82,13 @@ protected function resolveCustomVarMapping(string $name, $val, DbConnection $con $label = $field->get('caption'); $newVarValue = []; foreach ($val as $nestedVarName => $nestedVarValue) { - if (isset($this->fieldConfig[$nestedVarName]) && is_array($nestedVarValue)) { - $childValues = []; - foreach ($nestedVarValue as $childName => $childValue) { - $childValues[] = $this->resolveCustomVarMapping($childName, $childValue, $conn, false); - } - - $newVarValue[] = [$nestedVarName => array_merge([], ...$childValues)]; - } else { - $newVarValue[] = $this->resolveCustomVarMapping( - $nestedVarName, - $nestedVarValue, - $conn, - false - ); - } + $newVarValue[$nestedVarName] = $this->buildDictionaryMap( + $nestedVarValue, + $conn + ); } - return [$label => array_merge([], ...$newVarValue)]; + return [$label => $newVarValue]; } elseif ($dataType === get_class(new DataTypeDatalist())) { if (isset($this->datalistMaps[$name])) { $val = $this->datalistMaps[$name][$val]; @@ -132,6 +120,15 @@ protected function resolveCustomVarMapping(string $name, $val, DbConnection $con return [$name => $val]; } + private function buildDictionaryMap($val, DbConnection $connection): array + { + foreach ($val as $childName => $childValue) { + $newValue[] = $this->resolveCustomVarMapping($childName, $childValue, $connection, false); + } + + return array_merge([], ...$newValue); + } + private function buildDataListMap(DbConnection $db) { $fieldsWithDataLists = [];