mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-25 02:47:39 +02:00
IcingaObjectFieldForm: easier field handling
This makes it much easier to deal with command fields, as they are auto-proposed in a very helpful way.
This commit is contained in:
parent
7d96201afc
commit
2d40ffa1e0
@ -3,6 +3,7 @@
|
|||||||
namespace Icinga\Module\Director\Forms;
|
namespace Icinga\Module\Director\Forms;
|
||||||
|
|
||||||
use Icinga\Module\Director\Objects\IcingaObject;
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
|
use Icinga\Module\Director\Objects\DirectorDatafield;
|
||||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||||
|
|
||||||
class IcingaObjectFieldForm extends DirectorObjectForm
|
class IcingaObjectFieldForm extends DirectorObjectForm
|
||||||
@ -33,11 +34,58 @@ class IcingaObjectFieldForm extends DirectorObjectForm
|
|||||||
. ' a specific set, shown as a dropdown.'
|
. ' a specific set, shown as a dropdown.'
|
||||||
);
|
);
|
||||||
|
|
||||||
$fields = $this->db->enumDatafields();
|
// TODO: remove assigned ones!
|
||||||
|
$existingFields = $this->db->enumDatafields();
|
||||||
|
$blacklistedVars = array();
|
||||||
|
$suggestedFields = array();
|
||||||
|
|
||||||
|
foreach ($existingFields as $id => $field) {
|
||||||
|
if (preg_match('/ \(([^\)]+)\)$/', $field, $m)) {
|
||||||
|
$blacklistedVars['$' . $m[1] . '$'] = $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: think about imported existing vars without fields
|
||||||
|
$argumentVars = array();
|
||||||
|
if ($this->icingaObject->supportsArguments()) {
|
||||||
|
foreach ($this->icingaObject->arguments() as $arg) {
|
||||||
|
if ($arg->argument_format === 'string') {
|
||||||
|
$val = $arg->argument_value;
|
||||||
|
// TODO: create var::extractMacros or so
|
||||||
|
if (preg_match('/^\$[^\$]+\$$/', $val)) {
|
||||||
|
if (array_key_exists($val, $blacklistedVars)) {
|
||||||
|
$id = $blacklistedVars[$val];
|
||||||
|
$suggestedFields[$id] = $existingFields[$id];
|
||||||
|
unset($existingFields[$id]);
|
||||||
|
} else {
|
||||||
|
$argumentVars[$val] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare combined fields array
|
||||||
|
$fields = array();
|
||||||
|
if (! empty($suggestedFields)) {
|
||||||
|
asort($existingFields);
|
||||||
|
$fields[$this->translate('Suggested fields')] = $suggestedFields;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($argumentVars)) {
|
||||||
|
ksort($argumentVars);
|
||||||
|
$fields[$this->translate('Argument macros')] = $argumentVars;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($existingFields)) {
|
||||||
|
$fields[$this->translate('Other available fields')] = $existingFields;
|
||||||
|
}
|
||||||
|
|
||||||
$this->addElement('select', 'datafield_id', array(
|
$this->addElement('select', 'datafield_id', array(
|
||||||
'label' => 'Field',
|
'label' => 'Field',
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'description' => 'Field to assign',
|
'description' => 'Field to assign',
|
||||||
|
'class' => 'autosubmit',
|
||||||
'multiOptions' => $this->optionalEnum($fields)
|
'multiOptions' => $this->optionalEnum($fields)
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -51,6 +99,15 @@ class IcingaObjectFieldForm extends DirectorObjectForm
|
|||||||
$this->getElement('datafield_id')->addError($msg);
|
$this->getElement('datafield_id')->addError($msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (($id = $this->getSentValue('datafield_id')) && ! ctype_digit($id)) {
|
||||||
|
$this->addElement('text', 'caption', array(
|
||||||
|
'label' => $this->translate('Caption'),
|
||||||
|
'required' => true,
|
||||||
|
'ignore' => true,
|
||||||
|
'description' => $this->translate('The caption which should be displayed')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
$this->addElement('select', 'is_required', array(
|
$this->addElement('select', 'is_required', array(
|
||||||
'label' => $this->translate('Mandatory'),
|
'label' => $this->translate('Mandatory'),
|
||||||
'description' => $this->translate('Whether this field should be mandatory'),
|
'description' => $this->translate('Whether this field should be mandatory'),
|
||||||
@ -85,4 +142,22 @@ class IcingaObjectFieldForm extends DirectorObjectForm
|
|||||||
$this->redirectOnSuccess($this->translate('Field has been removed'));
|
$this->redirectOnSuccess($this->translate('Field has been removed'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onSuccess()
|
||||||
|
{
|
||||||
|
$fieldId = $this->getValue('datafield_id');
|
||||||
|
if (! ctype_digit($fieldId)) {
|
||||||
|
|
||||||
|
$field = DirectorDatafield::create(array(
|
||||||
|
'varname' => trim($fieldId, '$'),
|
||||||
|
'caption' => $this->getValue('caption'),
|
||||||
|
'datatype' => 'Icinga\Module\Director\DataType\DataTypeString'
|
||||||
|
));
|
||||||
|
$field->store($this->getDb());
|
||||||
|
$this->setElementValue('datafield_id', $field->id);
|
||||||
|
$this->object()->datafield_id = $field->id;
|
||||||
|
|
||||||
|
}
|
||||||
|
return parent::onSuccess();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user