summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Controller/TemplateController.php
blob: 4f0898a4a027979aaa24fcaec581037b9609e52b (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

namespace Icinga\Module\Director\Web\Controller;

use gipfl\Web\Widget\Hint;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Controller\Extension\DirectorDb;
use Icinga\Module\Director\Web\Table\ApplyRulesTable;
use Icinga\Module\Director\Web\Table\ObjectsTable;
use Icinga\Module\Director\Web\Table\ObjectsTableSetMembers;
use Icinga\Module\Director\Web\Table\TemplatesTable;
use Icinga\Module\Director\Web\Table\TemplateUsageTable;
use Icinga\Module\Director\Web\Tabs\ObjectTabs;
use Icinga\Module\Director\Web\Widget\UnorderedList;
use ipl\Html\FormattedString;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\CompatController;

abstract class TemplateController extends CompatController
{
    use DirectorDb;

    use BranchHelper;

    /** @var IcingaObject */
    protected $template;

    public function objectsAction()
    {
        $template = $this->requireTemplate();
        $plural = $this->getTranslatedPluralType();
        $this
            ->addSingleTab($plural)
            ->setAutorefreshInterval(10)
            ->addTitle(
                $this->translate('%s based on %s'),
                $plural,
                $template->getObjectName()
            )->addBackToUsageLink($template);

        ObjectsTable::create($this->getType(), $this->db(), $this->Auth())
            ->setBranch($this->getBranch())
            ->setBaseObjectUrl($this->getBaseObjectUrl())
            ->filterTemplate($template, $this->getInheritance())
            ->renderTo($this);
    }

    public function setmembersAction()
    {
        $template = $this->requireTemplate();
        $plural = $this->getTranslatedPluralType();
        $this
            ->addSingleTab($plural)
            ->setAutorefreshInterval(10)
            ->addTitle(
                $this->translate('%s in service sets based on %s'),
                $plural,
                $template->getObjectName()
            )->addBackToUsageLink($template);

        ObjectsTableSetMembers::create($this->getType(), $this->db(), $this->Auth())
            ->setBaseObjectUrl($this->getBaseObjectUrl())
            ->setBranch($this->getBranch())
            ->filterTemplate($template, $this->getInheritance())
            ->renderTo($this);
    }

    public function applyrulesAction()
    {
        $type = $this->getType();
        $template = $this->requireTemplate();
        $this
            ->addSingleTab(sprintf($this->translate('Applied %s'), $this->getTranslatedPluralType()))
            ->setAutorefreshInterval(10)
            ->addTitle(
                $this->translate('Notification Apply Rules based on %s'),
                $template->getObjectName()
            )->addBackToUsageLink($template);

        ApplyRulesTable::create($type, $this->db())
            ->setBaseObjectUrl($this->getBaseObjectUrl())
            ->setBranch($this->getBranch())
            ->filterTemplate($template, $this->params->get('inheritance', 'direct'))
            ->renderTo($this);
    }

    public function templatesAction()
    {
        $template = $this->requireTemplate();
        $typeName = $this->getTranslatedType();
        $this
            ->addSingleTab(sprintf($this->translate('%s Templates'), $typeName))
            ->setAutorefreshInterval(10)
            ->addTitle(
                $this->translate('%s templates based on %s'),
                $typeName,
                $template->getObjectName()
            )->addBackToUsageLink($template);

        TemplatesTable::create($this->getType(), $this->db())
            ->filterTemplate($template, $this->getInheritance())
            ->renderTo($this);
    }

    protected function getInheritance()
    {
        return $this->params->get('inheritance', 'direct');
    }

    protected function addBackToUsageLink(IcingaObject $template)
    {
        $type = $this->getType();
        $this->actions()->add(
            Link::create(
                $this->translate('Back'),
                "director/{$type}template/usage",
                ['name' => $template->getObjectName()],
                ['class' => 'icon-left-big']
            )
        );

        return $this;
    }

    public function usageAction()
    {
        $template = $this->requireTemplate();
        if (! $template->isTemplate() && $template instanceof IcingaCommand) {
            $this->redirectNow($this->url()->setPath('director/command'));
        }
        $templateName = $template->getObjectName();

        $type = $this->getType();
        $this->tabs(new ObjectTabs($type, $this->Auth(), $template))->activate('modify');
        $this
            ->addTitle($this->translate('Template: %s'), $templateName)
            ->setAutorefreshInterval(10);

        $this->actions()->add([
            Link::create(
                $this->translate('Modify'),
                "director/$type/edit",
                ['uuid' => $template->getUniqueId()->toString()],
                ['class' => 'icon-edit']
            )
        ]);
        if ($template instanceof ExportInterface) {
            $this->actions()->add(Link::create(
                $this->translate('Add to Basket'),
                'director/basket/add',
                [
                    'type'  => ucfirst($this->getType()) . 'Template',
                    'names' => $template->getUniqueIdentifier()
                ],
                ['class' => 'icon-tag']
            ));
        }

        $list = new UnorderedList([], [
            'class' => 'vertical-action-list'
        ]);

        $auth = $this->Auth();

        if ($type !== 'notification') {
            $list->addItem(new FormattedString(
                $this->translate('Create a new %s inheriting from this template'),
                [Link::create(
                    $this->translate('Object'),
                    "director/$type/add",
                    ['imports' => $templateName, 'type' => 'object']
                )]
            ));
        }
        if ($auth->hasPermission(Permission::ADMIN)) {
            $list->addItem(new FormattedString(
                $this->translate('Create a new %s inheriting from this one'),
                [Link::create(
                    $this->translate('Template'),
                    "director/$type/add",
                    ['imports' => $templateName, 'type' => 'template']
                )]
            ));
        }
        if ($template->supportsApplyRules()) {
            $list->addItem(new FormattedString(
                $this->translate('Create a new %s inheriting from this template'),
                [Link::create(
                    $this->translate('Apply Rule'),
                    "director/$type/add",
                    ['imports' => $templateName, 'type' => 'apply']
                )]
            ));
        }

        $typeName = $this->getTranslatedType();
        $this->content()->add(Html::sprintf(
            $this->translate(
                'This is the "%s" %s Template. Based on this, you might want to:'
            ),
            $typeName,
            $templateName
        ))->add(
            $list
        )->add(
            Html::tag('h2', null, $this->translate('Current Template Usage'))
        );

        try {
            $this->content()->add(
                TemplateUsageTable::forTemplate($template, $this->Auth(), $this->getBranch())
            );
        } catch (NestingError $e) {
            $this->content()->add(Hint::error($e->getMessage()));
        }
    }

    protected function getType()
    {
        return $this->template()->getShortTableName();
    }

    protected function getPluralType()
    {
        return preg_replace(
            '/cys$/',
            'cies',
            $this->template()->getShortTableName() . 's'
        );
    }

    protected function getTranslatedType()
    {
        return $this->translate(ucfirst($this->getType()));
    }

    protected function getTranslatedPluralType()
    {
        return $this->translate(ucfirst($this->getPluralType()));
    }

    protected function getBaseObjectUrl()
    {
        return $this->getType();
    }

    /**
     * @return IcingaObject
     */
    protected function template()
    {
        if ($this->template === null) {
            $this->template = $this->requireTemplate();
        }

        return $this->template;
    }

    /**
     * @return IcingaObject
     */
    abstract protected function requireTemplate();
}