summaryrefslogtreecommitdiffstats
path: root/library/Director/ProvidedHook/IcingaDbCubeLinks.php
blob: f3fe4029940752b6d79e6da7c06044fc984b7d5d (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
<?php

namespace Icinga\Module\Director\ProvidedHook;

use Icinga\Data\Filter\Filter;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Cube\Hook\IcingaDbActionsHook;
use Icinga\Module\Cube\IcingaDb\IcingaDbCube;
use Icinga\Module\Cube\IcingaDb\IcingaDbHostStatusCube;
use ipl\Stdlib\Filter\Condition;

class IcingaDbCubeLinks extends IcingaDbActionsHook
{
    /**
     * @inheritDoc
     * @param IcingaDbCube $cube
     * @throws ProgrammingError
     */
    public function createActionLinks(IcingaDbCube $cube)
    {
        if (! $cube instanceof IcingaDbHostStatusCube) {
            return;
        }

        $filterChain = $cube->getObjectsFilter();

        if ($filterChain->count() === 1) {
            $url = 'director/host/edit?';
            /** @var Condition $rule */
            $rule = $filterChain->getIterator()->current();
            /** @var string $name */
            $name = $rule->getValue();
            $params = ['name' => $name];

            $title = t('Modify a host');
            $description = sprintf(t('This allows you to modify properties for "%s"'), $name);
        } else {
            $params = null;

            $urlFilter = Filter::matchAny();
            /** @var Condition $filter */
            foreach ($filterChain as $filter) {
                $urlFilter->addFilter(
                    Filter::matchAny(
                        Filter::expression(
                            'name',
                            '=',
                            $filter->getValue()
                        )
                    )
                );
            }

            $url = 'director/hosts/edit?' . $urlFilter->toQueryString();

            $title = sprintf(t('Modify %d hosts'), $filterChain->count());
            $description = t(
                'This allows you to modify properties for all chosen hosts at once'
            );
        }

        $this->addActionLink(
            $this->makeUrl($url, $params),
            $title,
            $description,
            'wrench'
        );
    }
}