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

namespace Icinga\Module\Director\Web\Tree;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\Resolver\TemplateTree;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Widget\ControlsAndContent;

class TemplateTreeRenderer extends BaseHtmlElement
{
    use TranslationHelper;

    protected $tag = 'ul';

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

    protected $tree;

    public function __construct(TemplateTree $tree)
    {
        $this->tree = $tree;
    }

    public static function showType($type, ControlsAndContent $controller, Db $db)
    {
        $controller->content()->add(
            new static(new TemplateTree($type, $db))
        );
    }

    public function renderContent()
    {
        $this->add(
            $this->dumpTree(
                array(
                    'name' => $this->translate('Templates'),
                    'children' => $this->tree->getTree()
                )
            )
        );

        return parent::renderContent();
    }

    protected function dumpTree($tree, $level = 0)
    {
        $hasChildren = ! empty($tree['children']);
        $type = $this->tree->getType();

        $li = Html::tag('li');
        if (! $hasChildren) {
            $li->getAttributes()->add('class', 'collapsed');
        }

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

        if ($level === 0) {
            $li->add(Html::tag('a', [
                'name'  => $tree['name'],
                'class' => 'icon-globe'
            ], $tree['name']));
        } else {
            $li->add(Link::create(
                $tree['name'],
                "director/${type}template/usage",
                array('name' => $tree['name']),
                array('class' => 'icon-' .$type)
            ));
        }

        if ($hasChildren) {
            $li->add(
                $ul = Html::tag('ul')
            );
            foreach ($tree['children'] as $child) {
                $ul->add($this->dumpTree($child, $level + 1));
            }
        }

        return $li;
    }
}