icingaweb2-module-director/application/controllers/CommandController.php

127 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2015-10-20 22:34:04 +02:00
namespace Icinga\Module\Director\Controllers;
2020-10-26 18:50:13 +01:00
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;
2015-10-20 22:34:04 +02:00
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Web\Table\IcingaCommandArgumentTable;
2015-08-03 13:42:19 +02:00
2015-10-20 22:34:04 +02:00
class CommandController extends ObjectController
{
/**
* @throws \Icinga\Exception\AuthenticationException
* @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Security\SecurityException
*/
2015-08-03 13:42:19 +02:00
public function init()
{
parent::init();
2017-07-04 21:22:20 +02:00
$o = $this->object;
if ($o && ! $o->isExternal()) {
if ($this->getBranch()->isBranch()) {
$urlParams = ['uuid' => $o->getUniqueId()->toString()];
} else {
$urlParams = ['name' => $o->getObjectName()];
}
2017-07-04 21:22:20 +02:00
$this->tabs()->add('arguments', [
2015-08-03 13:42:19 +02:00
'url' => 'director/command/arguments',
'urlParams' => $urlParams,
2015-08-03 13:42:19 +02:00
'label' => 'Arguments'
2017-07-04 21:22:20 +02:00
]);
2015-08-03 13:42:19 +02:00
}
}
/**
* @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);
2020-10-26 18:50:13 +01:00
$this->content()->add(Hint::info(Html::sprintf(
$this->translate('This Command is currently being used by %s'),
Html::tag('span', null, $usage->getLinks())->setSeparator(', ')
2020-10-26 18:50:13 +01:00
))->addAttributes([
'data-base-target' => '_next'
]));
} else {
2020-10-26 18:50:13 +01:00
$this->content()->add(Hint::warning($this->translate('This Command is currently not in use')));
}
}
2015-08-03 13:42:19 +02:00
public function argumentsAction()
{
2017-07-04 21:22:20 +02:00
$p = $this->params;
/** @var IcingaCommand $o */
2017-07-04 21:22:20 +02:00
$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);
2015-08-03 13:42:19 +02:00
}
$form->handleRequest();
2017-07-21 08:23:52 +02:00
$this->content()->add([$form]);
if ($this->branch->isBranch()) {
(new BranchedIcingaCommandArgumentTable($o, $this->getBranch()))->renderTo($this);
} else {
(new IcingaCommandArgumentTable($o, $this->getBranch()))->renderTo($this);
}
2015-08-03 13:42:19 +02:00
}
protected function hasBasketSupport()
{
return true;
}
}