summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/CoreApiFieldsTable.php
blob: 24a6521515cdb2dcaac80568348edf3e3b347629 (plain)
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
<?php

namespace Icinga\Module\Director\Web\Table;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use ipl\Html\Table;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Url;

class CoreApiFieldsTable extends Table
{
    use TranslationHelper;

    protected $defaultAttributes = [
        'class' => ['common-table'/*, 'table-row-selectable'*/],
        //'data-base-target' => '_next',
    ];

    protected $fields;

    /** @var Url */
    protected $url;

    public function __construct($fields, Url $url)
    {
        $this->url = $url;
        $this->fields = $fields;
    }

    public function assemble()
    {
        if (empty($this->fields)) {
            return;
        }
        $this->add(Html::tag('thead', Html::tag('tr', Html::wrapEach($this->getColumnsToBeRendered(), 'th'))));
        foreach ($this->fields as $name => $field) {
            $tr = $this::tr([
                $this::td($name),
                $this::td(Link::create(
                    $field->type,
                    $this->url->with('type', $field->type)
                )),
                $this::td($field->id)
                // $this::td($field->array_rank),
                // $this::td($this->renderKeyValue($field->attributes))
            ]);
            $this->addAttributeColumns($tr, $field->attributes);
            $this->add($tr);
        }
    }

    protected function addAttributeColumns(BaseHtmlElement $tr, $attrs)
    {
        $tr->add([
            $this->makeBooleanColumn($attrs->state),
            $this->makeBooleanColumn($attrs->config),
            $this->makeBooleanColumn($attrs->required),
            $this->makeBooleanColumn(isset($attrs->deprecated) ? $attrs->deprecated : null),
            $this->makeBooleanColumn($attrs->no_user_modify),
            $this->makeBooleanColumn($attrs->no_user_view),
            $this->makeBooleanColumn($attrs->navigation),
        ]);
    }

    protected function makeBooleanColumn($value)
    {
        if ($value === null) {
            return $this::td('-');
        }

        return $this::td($value ? Html::tag('strong', 'true') : 'false');
    }

    public function getColumnsToBeRendered()
    {
        return [
            $this->translate('Name'),
            $this->translate('Type'),
            $this->translate('Id'),
            // $this->translate('Array Rank'),
            // $this->translate('Attributes')
            $this->translate('State'),
            $this->translate('Config'),
            $this->translate('Required'),
            $this->translate('Deprecated'),
            $this->translate('Protected'),
            $this->translate('Hidden'),
            $this->translate('Nav'),
        ];
    }

    protected function renderKeyValue($values)
    {
        $parts = [];
        foreach ((array) $values as $key => $value) {
            if (is_bool($value)) {
                $value = $value ? 'true' : 'false';
            }
            $parts[] = "$key: $value";
        }

        return implode(', ', $parts);
    }
}