summaryrefslogtreecommitdiffstats
path: root/application/controllers/CommandController.php
blob: de0ba5480b444ba26ec572cfec548b6c4abfe4fc (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
<?php

namespace Icinga\Module\Director\Controllers;

use gipfl\Web\Widget\Hint;
use Icinga\Module\Director\Objects\IcingaCommandArgument;
use Icinga\Module\Director\Web\Table\BranchedIcingaCommandArgumentTable;
use ipl\Html\Html;
use Icinga\Module\Director\Forms\IcingaCommandArgumentForm;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Resolver\CommandUsage;
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Web\Table\IcingaCommandArgumentTable;

class CommandController extends ObjectController
{
    /**
     * @throws \Icinga\Exception\AuthenticationException
     * @throws \Icinga\Exception\NotFoundError
     * @throws \Icinga\Security\SecurityException
     */
    public function init()
    {
        parent::init();
        $o = $this->object;
        if ($o && ! $o->isExternal()) {
            if ($this->getBranch()->isBranch()) {
                $urlParams = ['uuid' => $o->getUniqueId()->toString()];
            } else {
                $urlParams = ['name' => $o->getObjectName()];
            }
            $this->tabs()->add('arguments', [
                'url'       => 'director/command/arguments',
                'urlParams' => $urlParams,
                'label'     => 'Arguments'
            ]);
        }
    }

    /**
     * @throws \Icinga\Exception\NotFoundError
     * @throws \Zend_Db_Select_Exception
     */
    public function indexAction()
    {
        if (! $this->getRequest()->isApiRequest()) {
            $this->showUsage();
        }
        parent::indexAction();
    }

    /**
     * @throws \Icinga\Exception\NotFoundError
     * @throws \Icinga\Security\SecurityException
     * @throws \Zend_Db_Select_Exception
     */
    public function renderAction()
    {
        if ($this->object->isExternal()) {
            $this->showUsage();
        }

        parent::renderAction();
    }

    /**
     * @throws \Zend_Db_Select_Exception
     */
    protected function showUsage()
    {
        /** @var IcingaCommand $command */
        $command = $this->object;
        if ($command->isInUse()) {
            $usage = new CommandUsage($command);
            $this->content()->add(Hint::info(Html::sprintf(
                $this->translate('This Command is currently being used by %s'),
                Html::tag('span', null, $usage->getLinks())->setSeparator(', ')
            ))->addAttributes([
                'data-base-target' => '_next'
            ]));
        } else {
            $this->content()->add(Hint::warning($this->translate('This Command is currently not in use')));
        }
    }

    public function argumentsAction()
    {
        $p = $this->params;
        /** @var IcingaCommand $o */
        $o = $this->object;
        $this->tabs()->activate('arguments');
        $this->addTitle($this->translate('Command arguments: %s'), $o->getObjectName());
        $form = (new IcingaCommandArgumentForm)
            ->setBranch($this->getBranch())
            ->setCommandObject($o);
        if ($argument = $p->shift('argument')) {
            $this->addBackLink('director/command/arguments', [
                'name' => $p->get('name')
            ]);
            if ($this->branch->isBranch()) {
                $arguments = $o->arguments();
                $argument = $arguments->get($argument);
                // IcingaCommandArgument::create((array) $arguments->get($argument)->toFullPlainObject());
                // $argument->setBeingLoadedFromDb();
            } else {
                $argument = IcingaCommandArgument::load([
                    'command_id' => $o->get('id'),
                    'argument_name' => $argument
                ], $this->db());
            }
            $form->setObject($argument);
        }
        $form->handleRequest();
        $this->content()->add([$form]);
        if ($this->branch->isBranch()) {
            (new BranchedIcingaCommandArgumentTable($o, $this->getBranch()))->renderTo($this);
        } else {
            (new IcingaCommandArgumentTable($o, $this->getBranch()))->renderTo($this);
        }
    }

    protected function hasBasketSupport()
    {
        return true;
    }
}