summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Hook/TabHook/HookActions.php
blob: d2801a58d7495b251fe27808acf64fc250dc081b (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
142
143
144
145
146
147
148
<?php

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

namespace Icinga\Module\Icingadb\Hook\TabHook;

use Exception;
use Generator;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Module\Icingadb\Hook\TabHook;
use ipl\Html\ValidHtml;
use ipl\Orm\Model;
use ipl\Stdlib\Str;

/**
 * Trait HookActions
 */
trait HookActions
{
    /** @var Model The object to load tabs for */
    protected $objectToLoadTabsFor;

    /** @var TabHook[] Loaded tab hooks */
    protected $tabHooks;

    /**
     * Get default control elements
     *
     * @return ValidHtml[]
     */
    abstract protected function getDefaultTabControls(): array;

    public function __call($methodName, $args)
    {
        if (substr($methodName, -6) === 'Action') {
            $hookName = substr($methodName, 0, -6);

            $hooks = $this->loadTabHooks();
            if (isset($hooks[$hookName])) {
                $this->showTabHook($hooks[$hookName]);
                return;
            }
        }

        parent::__call($methodName, $args);
    }

    /**
     * Register the object for which to load additional tabs
     *
     * @param Model $object
     *
     * @return void
     */
    protected function loadTabsForObject(Model $object)
    {
        $this->objectToLoadTabsFor = $object;
    }

    /**
     * Load tab hooks
     *
     * @return array<string, TabHook>
     */
    protected function loadTabHooks(): array
    {
        if ($this->objectToLoadTabsFor === null) {
            return [];
        } elseif ($this->tabHooks !== null) {
            return $this->tabHooks;
        }

        $this->tabHooks = [];
        foreach (Hook::all('Icingadb\\Tab') as $hook) {
            /** @var TabHook $hook */
            try {
                if ($hook->shouldBeShown($this->objectToLoadTabsFor)) {
                    $this->tabHooks[Str::camel($hook->getName())] = $hook;
                }
            } catch (Exception $e) {
                Logger::error("Failed to load tab hook: %s\n%s", $e, $e->getTraceAsString());
            }
        }

        return $this->tabHooks;
    }

    /**
     * Load additional tabs
     *
     * @return Generator<string, array{label: string, url: string}>
     */
    protected function loadAdditionalTabs(): Generator
    {
        foreach ($this->loadTabHooks() as $hook) {
            yield $hook->getName() => [
                'label' => $hook->getLabel(),
                'url'   => 'icingadb/' . $this->getRequest()->getControllerName() . '/' . $hook->getName()
            ];
        }
    }

    /**
     * Render the given tab hook
     *
     * @param TabHook $hook
     *
     * @return void
     */
    protected function showTabHook(TabHook $hook)
    {
        $moduleName = $hook->getModule()->getName();

        foreach ($hook->getControls($this->objectToLoadTabsFor) as $control) {
            $this->addControl($control);
        }

        if (! empty($this->controls->getContent())) {
            $this->controls->addAttributes([
                'class'                 => ['icinga-module', 'module-' . $moduleName],
                'data-icinga-module'    => $moduleName
            ]);
        } else {
            foreach ($this->getDefaultTabControls() as $control) {
                $this->addControl($control);
            }
        }

        foreach ($hook->getContent($this->objectToLoadTabsFor) as $content) {
            $this->addContent($content);
        }

        $this->content->addAttributes([
            'class'                 => ['icinga-module', 'module-' . $moduleName],
            'data-icinga-module'    => $moduleName
        ]);

        foreach ($hook->getFooter($this->objectToLoadTabsFor) as $footer) {
            $this->addFooter($footer);
        }

        $this->footer->addAttributes([
            'class'                 => ['icinga-module', 'module-' . $moduleName],
            'data-icinga-module'    => $moduleName
        ]);
    }
}