2015-06-01 16:29:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2015-08-03 13:53:20 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaCommand;
|
2016-03-21 10:06:25 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaCommandArgument;
|
2015-06-01 16:29:03 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
|
|
|
|
|
|
|
class IcingaCommandArgumentForm extends DirectorObjectForm
|
|
|
|
{
|
2015-08-03 13:53:20 +02:00
|
|
|
protected $commandObject;
|
|
|
|
|
|
|
|
public function setCommandObject(IcingaCommand $object)
|
|
|
|
{
|
|
|
|
$this->commandObject = $object;
|
|
|
|
$this->setDb($object->getConnection());
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:29:03 +02:00
|
|
|
public function setup()
|
|
|
|
{
|
2015-08-03 13:53:20 +02:00
|
|
|
$this->addHidden('command_id', $this->commandObject->id);
|
2015-06-01 16:29:03 +02:00
|
|
|
|
|
|
|
$this->addElement('text', 'argument_name', array(
|
|
|
|
'label' => $this->translate('Argument name'),
|
2016-08-02 13:13:43 +02:00
|
|
|
'filters' => array('StringTrim'),
|
2015-06-01 16:29:03 +02:00
|
|
|
'description' => $this->translate('e.g. -H or --hostname, empty means "skip_key"')
|
|
|
|
));
|
|
|
|
|
2016-03-21 10:06:25 +01:00
|
|
|
$this->addElement('select', 'argument_format', array(
|
|
|
|
'label' => $this->translate('Value type'),
|
|
|
|
'multiOptions' => array(
|
|
|
|
'string' => $this->translate('String'),
|
|
|
|
'expression' => $this->translate('Icinga DSL')
|
|
|
|
),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether the argument value is a string (allowing macros like $host$)'
|
|
|
|
. ' or an Icinga DSL lambda function (will be enclosed with {{ ... }}'
|
|
|
|
),
|
|
|
|
'class' => 'autosubmit',
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($this->getSentOrObjectValue('argument_format') === 'expression') {
|
2015-08-03 13:53:20 +02:00
|
|
|
$this->addElement('textarea', 'argument_value', array(
|
|
|
|
'label' => $this->translate('Value'),
|
2016-03-21 10:06:25 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'And Icinga DSL expression, e.g.: var cmd = macro("$cmd$");'
|
2016-03-22 02:27:17 +01:00
|
|
|
. ' return typeof(command) == String ...'
|
|
|
|
),
|
2015-08-03 13:53:20 +02:00
|
|
|
'rows' => 3
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$this->addElement('text', 'argument_value', array(
|
|
|
|
'label' => $this->translate('Value'),
|
2016-03-21 10:06:25 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'e.g. 5%, $hostname$, $lower$%:$upper$%'
|
|
|
|
)
|
2015-08-03 13:53:20 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-03-21 10:06:25 +01:00
|
|
|
$this->addElement('text', 'sort_order', array(
|
|
|
|
'label' => $this->translate('Position'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Leave empty for non-positional arguments. Can be a positive or'
|
|
|
|
. ' negative number and influences argument ordering'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->addElement('select', 'set_if_format', array(
|
|
|
|
'label' => $this->translate('Condition format'),
|
2015-08-03 13:53:20 +02:00
|
|
|
'multiOptions' => array(
|
|
|
|
'string' => $this->translate('String'),
|
2015-12-02 03:14:15 +01:00
|
|
|
'expression' => $this->translate('Icinga DSL')
|
2015-08-03 13:53:20 +02:00
|
|
|
),
|
2016-03-21 10:06:25 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether the set_if parameter is a string (allowing macros like $host$)'
|
|
|
|
. ' or an Icinga DSL lambda function (will be enclosed with {{ ... }}'
|
|
|
|
)
|
2015-06-01 16:29:03 +02:00
|
|
|
));
|
2015-06-01 17:27:44 +02:00
|
|
|
|
2015-08-03 13:53:20 +02:00
|
|
|
$this->addElement('text', 'set_if', array(
|
|
|
|
'label' => $this->translate('Condition (set_if)'),
|
2016-03-21 10:06:25 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'Only set this parameter if the argument value resolves to a'
|
|
|
|
. ' numeric value. String values are not supported'
|
|
|
|
)
|
2015-08-03 13:53:20 +02:00
|
|
|
));
|
2015-06-08 14:39:12 +02:00
|
|
|
|
2016-03-21 10:06:25 +01:00
|
|
|
$this->addBoolean('repeat_key', array(
|
|
|
|
'label' => $this->translate('Repeat key'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether this parameter should be repeated when multiple values'
|
|
|
|
. ' (read: array) are given'
|
2015-08-03 13:53:20 +02:00
|
|
|
)
|
|
|
|
));
|
2015-11-17 18:25:13 +01:00
|
|
|
|
2016-03-21 10:06:25 +01:00
|
|
|
$this->addBoolean('required', array(
|
|
|
|
'label' => $this->translate('Required'),
|
|
|
|
'required' => false,
|
|
|
|
'description' => $this->translate('Whether this argument should be required')
|
|
|
|
));
|
|
|
|
|
2015-11-17 18:25:13 +01:00
|
|
|
$this->setButtons();
|
2015-08-03 13:53:20 +02:00
|
|
|
}
|
2015-06-08 14:39:12 +02:00
|
|
|
|
2016-08-01 13:06:52 +02:00
|
|
|
protected function deleteObject($object)
|
|
|
|
{
|
|
|
|
$cmd = $this->commandObject;
|
|
|
|
|
|
|
|
$msg = sprintf(
|
|
|
|
'%s argument "%s" has been removed',
|
|
|
|
$this->translate($this->getObjectName()),
|
|
|
|
$object->argument_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$url = $this->getSuccessUrl()->without('argument_id');
|
|
|
|
|
|
|
|
$cmd->arguments()->remove($object->argument_name);
|
|
|
|
if ($cmd->store()) {
|
|
|
|
$this->setSuccessUrl($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->redirectOnSuccess($msg);
|
|
|
|
}
|
2016-03-21 10:06:25 +01:00
|
|
|
|
|
|
|
public function onSuccess()
|
2015-06-08 14:39:12 +02:00
|
|
|
{
|
2016-03-21 10:06:25 +01:00
|
|
|
$object = $this->object();
|
2016-03-22 20:04:02 +01:00
|
|
|
$cmd = $this->commandObject;
|
2016-08-26 09:00:45 +02:00
|
|
|
if (! $object->hasBeenLoadedFromDb()) {
|
|
|
|
if ($object->argument_name === null) {
|
|
|
|
$object->skip_key = true;
|
|
|
|
$object->argument_name = $cmd->getNextSkippableKeyName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 10:06:25 +01:00
|
|
|
if ($object->hasBeenModified()) {
|
|
|
|
$cmd->arguments()->set(
|
|
|
|
$object->argument_name,
|
2016-08-26 09:00:45 +02:00
|
|
|
$object
|
2016-03-21 10:06:25 +01:00
|
|
|
);
|
|
|
|
$msg = sprintf(
|
|
|
|
$this->translate('The argument %s has successfully been stored'),
|
|
|
|
$object->argument_name
|
|
|
|
);
|
2016-08-01 12:45:47 +02:00
|
|
|
$cmd->store($this->db);
|
2016-03-21 10:06:25 +01:00
|
|
|
} else {
|
|
|
|
$this->setHttpResponseCode(304);
|
|
|
|
$msg = $this->translate('No action taken, object has not been modified');
|
2015-06-08 14:39:12 +02:00
|
|
|
}
|
2016-03-21 10:06:25 +01:00
|
|
|
$this->setSuccessUrl(
|
2016-06-16 22:42:56 +02:00
|
|
|
'director/command/arguments',
|
|
|
|
array('name' => $cmd->object_name)
|
2015-06-01 16:29:03 +02:00
|
|
|
);
|
2015-06-08 14:39:12 +02:00
|
|
|
|
2016-03-21 10:06:25 +01:00
|
|
|
$this->redirectOnSuccess($msg);
|
2015-06-08 14:39:12 +02:00
|
|
|
}
|
2015-06-01 16:29:03 +02:00
|
|
|
}
|