From e1900eb3fcd345fe843e0b88fa5eacbd14a7c908 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Wed, 28 Aug 2024 11:56:36 +0200 Subject: [PATCH] Icingadb/CustomVarRenderer: Render key as an HtmlElement --- .../Icingadb/CustomVarRenderer.php | 86 +++++++++++++++++-- 1 file changed, 79 insertions(+), 7 deletions(-) diff --git a/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php b/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php index ca86e5836..39b39636a 100644 --- a/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php +++ b/library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php @@ -12,6 +12,11 @@ use Icinga\Module\Icingadb\Hook\CustomVarRendererHook; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; +use ipl\Html\Attributes; +use ipl\Html\Html; +use ipl\Html\HtmlElement; +use ipl\Html\Text; +use ipl\Html\ValidHtml; use ipl\Orm\Model; class CustomVarRenderer extends CustomVarRendererHook @@ -25,6 +30,8 @@ class CustomVarRenderer extends CustomVarRendererHook /** @var array Related dictionary field names */ protected $dictionaryNames = []; + protected $dictionaryLevel = 0; + /** * Get a database connection to the director database * @@ -120,7 +127,11 @@ public function prefetchForObject(Model $object): bool public function renderCustomVarKey(string $key) { if (isset($this->fieldConfig[$key]['label'])) { - return $this->fieldConfig[$key]['label']; + return new HtmlElement( + 'span', + Attributes::create(['title' => $this->fieldConfig[$key]['label'] . " [$key]"]), + Text::create($this->fieldConfig[$key]['label']) + ); } return null; @@ -157,21 +168,82 @@ public function identifyCustomVarGroup(string $key): ?string * * @param array $value * - * @return array + * @return ValidHtml */ - protected function renderDictionaryVal(array $value): array + protected function renderDictionaryVal(array $value): ValidHtml { - $newValue = []; + $value = (array) $value; + $numItems = count($value); + + $valueTable = new HtmlElement( + 'table', + Attributes::create(['class' => ['custom-var-table', 'name-value-table']]) + ); + + $valueBody = new HtmlElement('tbody'); + + $valueBody->addHtml( + new HtmlElement( + 'tr', + Attributes::create(['class' => "level-{$this->dictionaryLevel}"]), + new HtmlElement( + 'td', + null, + Text::create(sprintf(tp('%d item', '%d items', $numItems), $numItems)) + ) + ) + ); + + $this->dictionaryLevel++; foreach ($value as $key => $val) { - if (is_array($val)) { + if (is_array($val) || is_object($val)) { + $numItems = count((array) $val); + + $valueBody->addHtml( + new HtmlElement( + 'tr', + Attributes::create(['class' => "level-{$this->dictionaryLevel}"]), + new HtmlElement('th', null, Html::wantHtml($key)), + new HtmlElement( + 'td', + null, + Text::create(sprintf(tp('%d item', '%d items', $numItems), $numItems)) + ) + ) + ); + + $this->dictionaryLevel++; foreach ($val as $subKey => $subVal) { $label = $this->renderCustomVarKey($subKey) ?? $subKey; $subVal = $this->renderCustomVarValue($subKey, $subVal) ?? $subVal; - $newValue[$key][$label] = $subVal; + + if ($label !== null && $subVal !== null) { + $valueBody->addHtml( + new HtmlElement( + 'tr', + Attributes::create(['class' => "level-{$this->dictionaryLevel}"]), + new HtmlElement('th', null, Html::wantHtml($label)), + new HtmlElement('td', null, Html::wantHtml($subVal)) + ) + ); + } } + + $this->dictionaryLevel--; + } else { + $valueBody->addHtml( + new HtmlElement( + 'tr', + Attributes::create(['class' => "level-{$this->dictionaryLevel}"]), + new HtmlElement('th', null, Html::wantHtml($key)), + new HtmlElement('td', null, Html::wantHtml($val)) + ) + ); } } - return $newValue; + $this->dictionaryLevel--; + + return $valueTable->addHtml($valueBody); } }