Skip to content

Commit

Permalink
WIP3
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Aug 27, 2024
1 parent 0173ead commit 4ca6fd7
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand All @@ -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];
Expand Down Expand Up @@ -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 = [];
Expand Down

0 comments on commit 4ca6fd7

Please sign in to comment.