summaryrefslogtreecommitdiffstats
path: root/application/controllers/HostgroupController.php
blob: 14fd0c184e37f4349c5cfb37e5d5b92ec11706c2 (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
<?php

/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Controllers;

use Icinga\Exception\NotFoundError;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Hostgroupsummary;
use Icinga\Module\Icingadb\Redis\VolatileStateResults;
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Widget\ItemList\HostList;
use Icinga\Module\Icingadb\Widget\ItemTable\HostgroupTableRow;
use ipl\Html\Html;
use ipl\Stdlib\Filter;

class HostgroupController extends Controller
{
    /** @var Hostgroupsummary The host group object */
    protected $hostgroup;

    public function init()
    {
        $this->assertRouteAccess('hostgroups');

        $this->addTitleTab(t('Host Group'));

        $name = $this->params->getRequired('name');

        $query = Hostgroupsummary::on($this->getDb());

        foreach ($query->getUnions() as $unionPart) {
            $unionPart->filter(Filter::equal('hostgroup.name', $name));
        }

        $this->applyRestrictions($query);

        $hostgroup = $query->first();
        if ($hostgroup === null) {
            throw new NotFoundError(t('Host group not found'));
        }

        $this->hostgroup = $hostgroup;
        $this->setTitle($hostgroup->display_name);
    }

    public function indexAction()
    {
        $db = $this->getDb();

        $hosts = Host::on($db)->with(['state', 'state.last_comment', 'icon_image']);
        $hosts
            ->setResultSetClass(VolatileStateResults::class)
            ->filter(Filter::equal('hostgroup.id', $this->hostgroup->id));
        $this->applyRestrictions($hosts);

        $limitControl = $this->createLimitControl();
        $paginationControl = $this->createPaginationControl($hosts);
        $viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);

        $hostList = (new HostList($hosts->execute()))
            ->setViewMode($viewModeSwitcher->getViewMode());

        yield $this->export($hosts);

        // ICINGAWEB_EXPORT_FORMAT is not set yet and $this->format is inaccessible, yeah...
        if ($this->getRequest()->getParam('format') === 'pdf') {
            $this->addContent(new HostgroupTableRow($this->hostgroup));
            $this->addContent(Html::tag('h2', null, t('Hosts')));
        } else {
            $this->addControl(new HostgroupTableRow($this->hostgroup));
        }

        $this->addControl($paginationControl);
        $this->addControl($viewModeSwitcher);
        $this->addControl($limitControl);

        $this->addContent($hostList);

        $this->setAutorefreshInterval(10);
    }
}