summaryrefslogtreecommitdiffstats
path: root/library/Director/Resolver/CommandUsage.php
blob: 7e3e0c5a539295d9176f86192d95d4df991987e7 (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
<?php

namespace Icinga\Module\Director\Resolver;

use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use Icinga\Module\Director\Objects\IcingaCommand;
use InvalidArgumentException;

class CommandUsage
{
    use TranslationHelper;

    /** @var IcingaCommand */
    protected $command;

    /** @var \Zend_Db_Adapter_Abstract */
    protected $db;

    /**
     * CommandUsageTable constructor.
     * @param IcingaCommand $command
     */
    public function __construct(IcingaCommand $command)
    {
        if ($command->isTemplate()) {
            throw new InvalidArgumentException(
                'CommandUsageTable expects object or external_object, got a template'
            );
        }

        $this->command = $command;
        $this->db = $command->getDb();
    }

    /**
     * @return array
     */
    public function getLinks()
    {
        $name = $this->command->getObjectName();
        $links = [];
        $map = [
            'host'         => ['check_command', 'event_command'],
            'service'      => ['check_command', 'event_command'],
            'notification' => ['command'],
        ];
        $types = [
            'host' => [
                'object'   => $this->translate('%d Host(s)'),
                'template' => $this->translate('%d Host Template(s)'),
            ],
            'service' => [
                'object'   => $this->translate('%d Service(s)'),
                'template' => $this->translate('%d Service Template(s)'),
                'apply'     => $this->translate('%d Service Apply Rule(s)'),
            ],
            'notification' => [
                'object'   => $this->translate('%d Notification(s)'),
                'template' => $this->translate('%d Notification Template(s)'),
                'apply'     => $this->translate('%d Notification Apply Rule(s)'),
            ],
        ];

        $urlSuffix = [
            'object'   => '',
            'template' => '/templates',
            'apply'    => '/applyrules',
        ];

        foreach ($map as $type => $relations) {
            $res = $this->fetchFor($type, $relations, array_keys($types[$type]));
            foreach ($types[$type] as $objectType => $caption) {
                if ($res->$objectType > 0) {
                    $suffix = $urlSuffix[$objectType];
                    $links[] = Link::create(
                        sprintf($caption, $res->$objectType),
                        "director/${type}s$suffix",
                        ['command' => $name]
                    );
                }
            }
        }

        return $links;
    }

    protected function fetchFor($table, $rels, $objectTypes)
    {
        $id = $this->command->getAutoincId();

        $columns = [];
        foreach ($objectTypes as $type) {
            $columns[$type] = "COALESCE(SUM(CASE WHEN object_type = '$type' THEN 1 ELSE 0 END), 0)";
        }
        $query = $this->db->select()->from("icinga_$table", $columns);

        foreach ($rels as $rel) {
            $query->orWhere("${rel}_id = ?", $id);
        }

        return $this->db->fetchRow($query);
    }
}