summaryrefslogtreecommitdiffstats
path: root/application/controllers/ServicesetController.php
blob: 684d2fc8e3c69388c5e802df8d7aea8da16ba8a4 (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
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
<?php

namespace Icinga\Module\Director\Controllers;

use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Forms\IcingaServiceSetForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Table\IcingaHostsMatchingFilterTable;
use Icinga\Module\Director\Web\Table\IcingaServiceSetHostTable;
use Icinga\Module\Director\Web\Table\IcingaServiceSetServiceTable;
use gipfl\IcingaWeb2\Link;

class ServicesetController extends ObjectController
{
    /** @var IcingaHost */
    protected $host;

    protected function checkDirectorPermissions()
    {
        $this->assertPermission('director/servicesets');
    }

    public function init()
    {
        if (null !== ($host = $this->params->get('host'))) {
            $this->host = IcingaHost::load($host, $this->db());
        }

        parent::init();
        if ($this->object) {
            $this->addServiceSetTabs();
        }
    }

    protected function onObjectFormLoaded(DirectorObjectForm $form)
    {
        if ($this->host) {
            /** @var IcingaServiceSetForm $form */
            $form->setHost($this->host);
        }
    }

    public function addAction()
    {
        parent::addAction();
        if ($this->host) {
            $this->addTitle(
                $this->translate('Add a service set to "%s"'),
                $this->host->getObjectName()
            );
        }
    }

    public function servicesAction()
    {
        /** @var IcingaServiceSet $set */
        $set = $this->object;
        $name = $set->getObjectName();
        $this->tabs()->activate('services');
        $this->addTitle(
            $this->translate('Services in this set: %s'),
            $name
        );
        $this->actions()->add(Link::create(
            $this->translate('Add service'),
            'director/service/add',
            ['set' => $name],
            ['class' => 'icon-plus']
        ));

        IcingaServiceSetServiceTable::load($set)
            ->setBranch($this->getBranch())
            ->renderTo($this);
    }

    public function hostsAction()
    {
        /** @var IcingaServiceSet $set */
        $set = $this->object;
        $this->tabs()->activate('hosts');
        $this->addTitle(
            $this->translate('Hosts using this set: %s'),
            $set->getObjectName()
        );

        $table = IcingaServiceSetHostTable::load($set);
        if ($table->count()) {
            $table->renderTo($this);
        }
        $filter = $set->get('assign_filter');
        if ($filter !== null && \strlen($filter) > 0) {
            $this->content()->add(
                IcingaHostsMatchingFilterTable::load(Filter::fromQueryString($filter), $this->db())
            );
        }
    }

    protected function addServiceSetTabs()
    {
        $hexUuid = $this->object->getUniqueId()->toString();
        $tabs = $this->tabs();
        $tabs->add('services', [
            'url'       => 'director/serviceset/services',
            'urlParams' => ['uuid' => $hexUuid],
            'label'     => 'Services'
        ]);
        if ($this->branch->isBranch()) {
            return $this;
        }
        $tabs->add('hosts', [
            'url'       => 'director/serviceset/hosts',
            'urlParams' => ['uuid' => $hexUuid],
            'label'     => 'Hosts'
        ]);

        return $this;
    }

    protected function loadObject()
    {
        if ($this->object === null) {
            if (null !== ($name = $this->params->get('name'))) {
                $params = ['object_name' => $name];
                $db = $this->db();

                if ($this->host) {
                    $params['host_id'] = $this->host->get('id');
                }

                $this->object = IcingaServiceSet::load($params, $db);
            } else {
                parent::loadObject();
            }
        }

        return $this->object;
    }
}