Skip to content

Commit

Permalink
Icingadb/CustomVarRenderer: Render key as an HtmlElement
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Aug 28, 2024
1 parent b0c50d9 commit 01653a9
Showing 1 changed file with 79 additions and 7 deletions.
86 changes: 79 additions & 7 deletions library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 01653a9

Please sign in to comment.