summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Tree/InspectTreeRenderer.php
blob: 54a177f4dc39f6b37bfd1ec5feba5dbb0f8c317b (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
<?php

namespace Icinga\Module\Director\Web\Tree;

use Icinga\Module\Director\Objects\IcingaEndpoint;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;

class InspectTreeRenderer extends BaseHtmlElement
{
    use TranslationHelper;

    protected $tag = 'ul';

    protected $defaultAttributes = [
        'class'            => 'tree',
        'data-base-target' => '_next',
    ];

    protected $tree;

    /** @var IcingaEndpoint */
    protected $endpoint;

    public function __construct(IcingaEndpoint $endpoint)
    {
        $this->endpoint = $endpoint;
    }

    protected function getNodes()
    {
        $rootNodes = array();
        $types = $this->endpoint->api()->getTypes();
        foreach ($types as $name => $type) {
            if (property_exists($type, 'base')) {
                $base = $type->base;
                if (! property_exists($types[$base], 'children')) {
                    $types[$base]->children = array();
                }

                $types[$base]->children[$name] = $type;
            } else {
                $rootNodes[$name] = $type;
            }
        }

        return $rootNodes;
    }

    public function assemble()
    {
        $this->add($this->renderNodes($this->getNodes()));
    }

    protected function renderNodes($nodes, $showLinks = false, $level = 0)
    {
        $result = [];
        foreach ($nodes as $child) {
            $result[] = $this->renderNode($child, $showLinks, $level + 1);
        }

        if ($level === 0) {
            return $result;
        } else {
            return Html::tag('ul', null, $result);
        }
    }

    protected function renderNode($node, $forceLinks = false, $level = 0)
    {
        $name = $node->name;
        $showLinks = $forceLinks || $name === 'ConfigObject';
        $hasChildren = property_exists($node, 'children');
        $li = Html::tag('li');
        if (! $hasChildren) {
            $li->getAttributes()->add('class', 'collapsed');
        }

        if ($hasChildren) {
            $li->add(Html::tag('span', ['class' => 'handle']));
        }

        $class = $node->abstract ? 'icon-sitemap' : 'icon-doc-text';
        $li->add(Link::create($name, 'director/inspect/type', [
            'endpoint' => $this->endpoint->getObjectName(),
            'type'     => $name
        ], ['class' => $class]));

        if ($hasChildren) {
            $li->add($this->renderNodes($node->children, $showLinks, $level + 1));
        }

        return $li;
    }
}