summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php
blob: 2f8df33a0a3419fd1a421744adadf494e50d3934 (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
<?php

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

namespace Icinga\Module\Icingadb\Hook\ActionsHook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
use Icinga\Module\Icingadb\Hook\HostActionsHook;
use Icinga\Module\Icingadb\Hook\ServiceActionsHook;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use InvalidArgumentException;
use ipl\Html\Attributes;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\Text;
use ipl\Orm\Model;
use ipl\Web\Widget\Link;

use function ipl\Stdlib\get_php_type;

abstract class ObjectActionsHook
{
    use HookUtils;

    /**
     * Load all actions for the given object
     *
     * @param Host|Service $object
     *
     * @return HtmlElement
     *
     * @throws InvalidArgumentException If the given model is not supported
     */
    final public static function loadActions(Model $object): HtmlElement
    {
        switch (true) {
            case $object instanceof Host:
                /** @var HostActionsHook $hook */
                $hookName = 'Icingadb\\HostActions';
                break;
            case $object instanceof Service:
                /** @var ServiceActionsHook $hook */
                $hookName = 'Icingadb\\ServiceActions';
                break;
            default:
                throw new InvalidArgumentException(
                    sprintf('%s is not a supported object type', get_php_type($object))
                );
        }

        $list = new HtmlElement('ul', Attributes::create(['class' => 'object-detail-actions']));
        foreach (Hook::all($hookName) as $hook) {
            try {
                foreach ($hook->getActionsForObject($object) as $link) {
                    if (! $link instanceof Link) {
                        continue;
                    }

                    // It may be ValidHtml, but modules shouldn't be able to break our views.
                    // That's why it needs to be rendered instantly, as any error will then
                    // be caught here.
                    $renderedLink = (string) $link;
                    $moduleName = $hook->getModule()->getName();

                    $list->addHtml(new HtmlElement('li', Attributes::create([
                        'class' => 'icinga-module module-' . $moduleName,
                        'data-icinga-module' => $moduleName
                    ]), HtmlString::create($renderedLink)));
                }
            } catch (Exception $e) {
                Logger::error("Failed to load object actions: %s\n%s", $e, $e->getTraceAsString());
                $list->addHtml(new HtmlElement('li', null, Text::create(IcingaException::describe($e))));
            }
        }

        return $list;
    }
}