1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
<?php
/* Icinga Web 2 | (c) 2021 Icinga GmbH | GPLv2+ */
namespace Icinga\Module\Monitoring\Web\Widget;
use Icinga\Module\Monitoring\Hook\CustomVarRendererHook;
use Icinga\Module\Monitoring\Object\MonitoredObject;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Web\Widget\Icon;
use Closure;
class CustomVarTable extends BaseHtmlElement
{
/** @var iterable The variables */
protected $data;
/** @var ?MonitoredObject The object the variables are bound to */
protected $object;
/** @var Closure Callback to apply hooks */
protected $hookApplier;
/** @var array The groups as identified by hooks */
protected $groups = [];
/** @var string Header title */
protected $headerTitle;
/** @var int The nesting level */
protected $level = 0;
protected $tag = 'table';
/** @var HtmlElement The table body */
protected $body;
protected $defaultAttributes = [
'class' => ['custom-var-table', 'name-value-table']
];
/**
* Create a new CustomVarTable
*
* @param iterable $data
* @param ?MonitoredObject $object
*/
public function __construct($data, MonitoredObject $object = null)
{
$this->data = $data;
$this->object = $object;
$this->body = new HtmlElement('tbody');
}
/**
* Set the header to show
*
* @param string $title
*
* @return $this
*/
protected function setHeader($title)
{
$this->headerTitle = (string) $title;
return $this;
}
/**
* Add a new row to the body
*
* @param mixed $name
* @param mixed $value
*
* @return void
*/
protected function addRow($name, $value)
{
$this->body->addHtml(new HtmlElement(
'tr',
Attributes::create(['class' => "level-{$this->level}"]),
new HtmlElement('th', null, Html::wantHtml($name)),
new HtmlElement('td', null, Html::wantHtml($value))
));
}
/**
* Render a variable
*
* @param mixed $name
* @param mixed $value
*
* @return void
*/
protected function renderVar($name, $value)
{
if ($this->object !== null && $this->level === 0) {
list($name, $value, $group) = call_user_func($this->hookApplier, $name, $value);
if ($group !== null) {
$this->groups[$group][] = [$name, $value];
return;
}
}
$isArray = is_array($value);
if (! $isArray && $value instanceof \stdClass) {
$value = (array) $value;
$isArray = true;
}
switch (true) {
case $isArray && is_int(key($value)):
$this->renderArray($name, $value);
break;
case $isArray:
$this->renderObject($name, $value);
break;
default:
$this->renderScalar($name, $value);
}
}
/**
* Render an array
*
* @param mixed $name
* @param array $array
*
* @return void
*/
protected function renderArray($name, array $array)
{
$numItems = count($array);
$name = (new HtmlDocument())->addHtml(
Html::wantHtml($name),
Text::create(' (Array)')
);
$this->addRow($name, sprintf(tp('%d item', '%d items', $numItems), $numItems));
++$this->level;
ksort($array);
foreach ($array as $key => $value) {
$this->renderVar("[$key]", $value);
}
--$this->level;
}
/**
* Render an object (associative array)
*
* @param mixed $name
* @param array $object
*
* @return void
*/
protected function renderObject($name, array $object)
{
$numItems = count($object);
$this->addRow($name, sprintf(tp('%d item', '%d items', $numItems), $numItems));
++$this->level;
ksort($object);
foreach ($object as $key => $value) {
$this->renderVar($key, $value);
}
--$this->level;
}
/**
* Render a scalar
*
* @param mixed $name
* @param mixed $value
*
* @return void
*/
protected function renderScalar($name, $value)
{
if ($value === '') {
$value = new HtmlElement('span', Attributes::create(['class' => 'empty']), Text::create(t('empty string')));
}
$this->addRow($name, $value);
}
/**
* Render a group
*
* @param string $name
* @param iterable $entries
*
* @return void
*/
protected function renderGroup($name, $entries)
{
$table = new self($entries);
/** @var HtmlDocument $wrapper */
$wrapper = $this->getWrapper();
if ($wrapper === null) {
$wrapper = new HtmlDocument();
$wrapper->addHtml($this);
$this->prependWrapper($wrapper);
}
$wrapper->addHtml($table->setHeader($name));
}
protected function assemble()
{
if ($this->object !== null) {
$this->hookApplier = CustomVarRendererHook::prepareForObject($this->object);
}
if ($this->headerTitle !== null) {
$this->getAttributes()
->add('class', 'collapsible')
->add('data-visible-height', 0)
->add('data-state-collapses', true)
->add('data-toggle-element', 'thead')
->add(
'id',
preg_replace('/\s+/', '-', strtolower($this->headerTitle)) . '-customvars'
);
$this->addHtml(new HtmlElement('thead', null, new HtmlElement(
'tr',
null,
new HtmlElement(
'th',
Attributes::create(['colspan' => 2]),
new HtmlElement(
'span',
null,
new Icon('angle-right'),
new Icon('angle-down')
),
Text::create($this->headerTitle)
)
)));
}
if (is_array($this->data)) {
ksort($this->data);
}
foreach ($this->data as $name => $value) {
$this->renderVar($name, $value);
}
$this->addHtml($this->body);
// Hooks can return objects as replacement for keys, hence a generator is needed for group entries
$genGenerator = function ($entries) {
foreach ($entries as list($key, $value)) {
yield $key => $value;
}
};
foreach ($this->groups as $group => $entries) {
$this->renderGroup($group, $genGenerator($entries));
}
}
}
|