summaryrefslogtreecommitdiffstats
path: root/application/views/helpers/Tree.php
blob: a016b681874df6a80e374511f488891fe9ea2cab (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
107
108
109
110
<?php
/* Icinga Web 2 Top Level View | (c) 2017 Icinga Development Team | GPLv2+ */

use Icinga\Module\Toplevelview\Tree\TLVTreeNode;
use Icinga\Web\Url;

class Zend_View_Helper_Tree extends Zend_View_Helper_Abstract
{
    /** @var \Icinga\Web\View */
    public $view;

    public function tree(TLVTreeNode $node, $classes = array(), $level = 0)
    {
        $htm = '';
        $htmExtra = '';
        $title = $node->getTitle();
        $type = $node->getType();

        $cssClasses = join(' ', $classes);

        $status = $node->getStatus();
        $statusClass = $status->getOverall();

        if ($type === 'host') {
            $icon = 'host';
            $url = Url::fromPath(
                'monitoring/host/show',
                array(
                    'host' => $node->get('host')
                )
            );
        } elseif ($type === 'service') {
            $icon = 'service';
            $url = Url::fromPath(
                'monitoring/service/show',
                array(
                    'host'    => $node->get('host'),
                    'service' => $node->get('service')
                )
            );
        } elseif ($type === 'hostgroup') {
            $icon = 'cubes';
            $url = Url::fromPath(
                'monitoring/list/services',
                array(
                    'hostgroup' => $node->get('hostgroup'),
                    'sort'      => 'service_severity',
                    'dir'       => 'desc',
                )
            );

            if (($h = $status->getMeta('hosts_unhandled')) > 0) {
                $hostTitle = '(<strong>'
                    . sprintf($this->view->translatePlural('%s unhandled host', '%s unhandled hosts', $h), $h)
                    . '</strong>)';
            } else {
                $h = $status->getMeta('hosts_total');
                $hostTitle = '(' . sprintf($this->view->translatePlural('%s host', '%s hosts', $h), $h) . ')';
            }

            $htmExtra .= ' ' . $this->view->qlink(
                $hostTitle,
                'monitoring/list/hosts',
                array(
                    'hostgroup' => $node->get('hostgroup'),
                    'sort'      => 'host_severity',
                    'dir'       => 'desc',
                ),
                null,
                false
            );
        } else {
            $icon = null;
            $url = Url::fromPath(
                'toplevelview/show/tree',
                array(
                    'name' => $node->getRoot()->getConfig()->getName(),
                    'id'   => $node->getFullId()
                )
            );
        }

        if ($type !== 'node') {
            $htm .= "<div class=\"tlv-node-icinga tlv-node-\$type tlv-status-tile action $statusClass $cssClasses\""
                . " data-base-target=\"_next\" href=\"$url\">";
            $htm .= $this->view->icon($icon) . ' ';
            $htm .= $this->view->qlink($title, $url);
            $htm .= $htmExtra;
            $htm .= ' ' . $this->view->badges($status, false);
            $htm .= '</div>';
        } else {
            $htm .= "<div class=\"tlv-tree-node tlv-status-section tlv-collapsible $statusClass $cssClasses\"";
            $htm .= " title=\"$title\">";
            $htm .= '<div class="tlv-tree-title">';
            $htm .= $this->view->badges($status, false, $level === 0 ? true : false);
            $htm .= '<i class="icon icon-bycss tlv-collapse-handle"></i> ';
            $htm .= $this->view->qlink($title, $url);
            $htm .= $htmExtra;
            $htm .= '</div>';
            if ($node->hasChildren()) {
                foreach ($node->getChildren() as $child) {
                    $htm .= $this->tree($child, $classes, $level + 1);
                }
            }
            $htm .= '</div>';
        }

        return $htm;
    }
}