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 0802260 commit a4d9588
Showing 1 changed file with 95 additions and 8 deletions.
103 changes: 95 additions & 8 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,14 @@ class CustomVarRenderer extends CustomVarRendererHook
/** @var array Related dictionary field names */
protected $dictionaryNames = [];

protected $dictionaryLevel = 0;

/** @var ValidHtml Table for dictionary fields */
private $dictionaryTable;

/** @var ValidHtml Table body for dictionary fields */
private $dictionaryBody;

/**
* Get a database connection to the director database
*
Expand Down Expand Up @@ -120,7 +133,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 All @@ -136,7 +153,7 @@ public function renderCustomVarValue(string $key, $value)
if (isset($this->datalistMaps[$key][$value])) {
return $this->datalistMaps[$key][$value];
} elseif (in_array($key, $this->dictionaryNames)) {
return $this->renderDictionaryVal($value);
return $this->renderDictionaryVal((array) $value);
}
}

Expand All @@ -157,21 +174,91 @@ 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 = [];
if ($this->dictionaryLevel === 0) {
$this->dictionaryTable = new HtmlElement(
'table',
Attributes::create(['class' => ['custom-var-table', 'name-value-table']])
);

$this->dictionaryBody = new HtmlElement('tbody');
}

$this->dictionaryLevel++;

foreach ($value as $key => $val) {
if (is_array($val)) {
if (is_array($val) || is_object($val)) {
$numItems = count((array) $val);

$this->dictionaryBody->addHtml(

Check failure on line 196 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 196 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 196 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 196 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 196 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 196 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 196 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::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;
if (in_array($subKey, $this->dictionaryNames)) {
$subVal = (array) $subVal;
$childrenCount = count($subVal);
$this->dictionaryBody->addHtml(

Check failure on line 215 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 215 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 215 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 215 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 215 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 215 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 215 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().
new HtmlElement(
'tr',
Attributes::create(['class' => "level-{$this->dictionaryLevel}"]),
new HtmlElement('th', null, Html::wantHtml($label)),
new HtmlElement(
'td',
null,
Text::create(sprintf(tp('%d item', '%d items', $childrenCount), $childrenCount))
)
)
);
}

$subVal = $this->renderCustomVarValue($subKey, $subVal) ?? $subVal;
$newValue[$key][$label] = $subVal;

if (! in_array($subKey, $this->dictionaryNames)) {
$this->dictionaryBody->addHtml(

Check failure on line 232 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 232 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 232 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 232 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 232 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 232 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 232 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::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 {
$this->dictionaryBody->addHtml(

Check failure on line 245 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 245 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 245 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 245 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 245 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 245 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 245 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::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--;

if ($this->dictionaryLevel === 0) {
return $this->dictionaryTable->addHtml($this->dictionaryBody);

Check failure on line 259 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 259 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 259 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 259 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 259 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 259 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().

Check failure on line 259 in library/Director/ProvidedHook/Icingadb/CustomVarRenderer.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Call to an undefined method ipl\Html\ValidHtml::addHtml().
}

return null;
}
}

0 comments on commit a4d9588

Please sign in to comment.