IcingaCommandArgumentForm: clean up, store with cmd

This commit is contained in:
Thomas Gelf 2016-03-21 10:06:25 +01:00
parent bb8d4f18dd
commit 5d705ce47a
1 changed files with 76 additions and 47 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaCommandArgument;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaCommandArgumentForm extends DirectorObjectForm
@ -25,77 +26,105 @@ class IcingaCommandArgumentForm extends DirectorObjectForm
'description' => $this->translate('e.g. -H or --hostname, empty means "skip_key"')
));
if ($this->getRequest()->getPost('argument_format') === 'expression') {
$this->addElement('textarea', 'argument_value', array(
'label' => $this->translate('Value'),
'description' => $this->translate('e.g. 5%, $hostname$, $lower$%:$upper$%'),
'rows' => 3
));
} else {
$this->addElement('text', 'argument_value', array(
'label' => $this->translate('Value'),
'description' => $this->translate('e.g. ')
));
}
$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',
));
$this->addElement('text', 'set_if', array(
'label' => $this->translate('Condition (set_if)'),
if ($this->getSentOrObjectValue('argument_format') === 'expression') {
$this->addElement('textarea', 'argument_value', array(
'label' => $this->translate('Value'),
'description' => $this->translate(
'And Icinga DSL expression, e.g.: var cmd = macro("$cmd$");'
. ' return typeof(command) == String ...'),
'rows' => 3
));
} else {
$this->addElement('text', 'argument_value', array(
'label' => $this->translate('Value'),
'description' => $this->translate(
'e.g. 5%, $hostname$, $lower$%:$upper$%'
)
));
}
$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'),
'multiOptions' => array(
'string' => $this->translate('String'),
// 'expression' => $this->translate('Icinga DSL')
'expression' => $this->translate('Icinga DSL')
),
'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 {{ ... }}'
)
));
$this->addElement('text', 'set_if', array(
'label' => $this->translate('Condition (set_if)'),
'description' => $this->translate(
'Only set this parameter if the argument value resolves to a'
. ' numeric value. String values are not supported'
)
));
$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'
)
));
$this->addBoolean('required', array(
'label' => $this->translate('Required'),
'required' => false,
'description' => $this->translate('Whether this argument should be required')
));
$this->setButtons();
}
protected function beforeValidation($data = array())
public function onSuccess()
{
if (isset($data['argument_value']) && $value = $data['argument_value']) {
if (preg_match_all('/\$([a-z0-9_]+)\$/', $value, $m, PREG_PATTERN_ORDER)) {
foreach ($m[1] as $var) {
// $this->addCustomVariable($var);
}
}
$object = $this->object();
$cmd = $this->commandObject();
if ($object->hasBeenModified()) {
$cmd->arguments()->set(
$object->argument_name,
IcingaCommandArgument::create($this->getValues(), $this->db)
);
$msg = sprintf(
$this->translate('The argument %s has successfully been stored'),
$object->argument_name
);
$object->store($this->db);
} else {
$this->setHttpResponseCode(304);
$msg = $this->translate('No action taken, object has not been modified');
}
/*
$this->optionalBoolean(
'required',
$this->translate('Required'),
$this->translate('Whether this is a mandatory parameter')
$this->setSuccessUrl(
'director/director/command',
array('name' => $cmd->argument_name)
);
*/
}
protected function addCustomVariable($varname)
{
$a = new \Zend_Form_SubForm();
$a->addElement('simpleNote', 'title', array(
'label' => sprintf($this->translate('Custom Variable "%s"'), $varname),
));
$a->addElement('text', 'description', array(
'label' => $this->translate('Description'),
'required' => true,
));
$a->addElement('text', 'default_value', array(
'label' => $this->translate('Default value'),
));
$this->addSubForm($a, 'cv_' . $varname);
$this->redirectOnSuccess($msg);
}
}