summaryrefslogtreecommitdiffstats
path: root/application/forms/IcingaObjectFieldForm.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/forms/IcingaObjectFieldForm.php')
-rw-r--r--application/forms/IcingaObjectFieldForm.php158
1 files changed, 63 insertions, 95 deletions
diff --git a/application/forms/IcingaObjectFieldForm.php b/application/forms/IcingaObjectFieldForm.php
index 537c95e..3b503fc 100644
--- a/application/forms/IcingaObjectFieldForm.php
+++ b/application/forms/IcingaObjectFieldForm.php
@@ -2,6 +2,9 @@
namespace Icinga\Module\Director\Forms;
+use Icinga\Module\Director\DataType\DataTypeBoolean;
+use Icinga\Module\Director\DataType\DataTypeString;
+use Icinga\Module\Director\Field\FormFieldSuggestion;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
@@ -15,6 +18,9 @@ class IcingaObjectFieldForm extends DirectorObjectForm
/** @var IcingaObject Please note that $object would conflict with logic in parent class */
protected $icingaObject;
+ /** @var FormFieldSuggestion */
+ protected $fieldSuggestion;
+
public function setIcingaObject($object)
{
$this->icingaObject = $object;
@@ -36,22 +42,9 @@ class IcingaObjectFieldForm extends DirectorObjectForm
. ' a specific set, shown as a dropdown.'
);
- // 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
// TODO: extract vars from command line (-> dummy)
// TODO: do not suggest chosen ones
- $argumentVars = array();
- $argumentVarDescriptions = array();
if ($object instanceof IcingaCommand) {
$command = $object;
} elseif ($object->hasProperty('check_command_id')) {
@@ -60,56 +53,16 @@ class IcingaObjectFieldForm extends DirectorObjectForm
$command = null;
}
- if ($command) {
- foreach ($command->arguments() as $arg) {
- if ($arg->argument_format === 'string') {
- $val = $arg->argument_value;
- // TODO: create var::extractMacros or so
-
- if (preg_match_all('/(\$[a-z0-9_]+\$)/i', $val, $m, PREG_PATTERN_ORDER)) {
- foreach ($m[1] as $val) {
- if (array_key_exists($val, $blacklistedVars)) {
- $id = $blacklistedVars[$val];
-
- // Hint: if not set it might already have been
- // removed in this loop
- if (array_key_exists($id, $existingFields)) {
- $suggestedFields[$id] = $existingFields[$id];
- unset($existingFields[$id]);
- }
- } else {
- $argumentVars[$val] = $val;
- $argumentVarDescriptions[$val] = $arg->description;
- }
- }
- }
- }
- }
- }
-
- // 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;
- }
+ $suggestions = $this->fieldSuggestion = new FormFieldSuggestion($command, $this->db->enumDatafields());
+ $fields = $suggestions->getCommandFields();
- $this->addElement('select', 'datafield_id', array(
+ $this->addElement('select', 'datafield_id', [
'label' => 'Field',
'required' => true,
'description' => 'Field to assign',
'class' => 'autosubmit',
'multiOptions' => $this->optionalEnum($fields)
- ));
+ ]);
if (empty($fields)) {
// TODO: show message depending on permissions
@@ -121,67 +74,58 @@ class IcingaObjectFieldForm extends DirectorObjectForm
}
if (($id = $this->getSentValue('datafield_id')) && ! ctype_digit($id)) {
- $this->addElement('text', 'caption', array(
+ $this->addElement('text', 'caption', [
'label' => $this->translate('Caption'),
'required' => true,
'ignore' => true,
'value' => trim($id, '$'),
- 'description' => $this->translate('The caption which should be displayed')
- ));
+ 'description' => $this->translate(
+ 'The caption which should be displayed to your users when this field'
+ . ' is shown'
+ )
+ ]);
- $this->addElement('textarea', 'description', array(
+ $this->addElement('textarea', 'description', [
'label' => $this->translate('Description'),
- 'description' => $this->translate('A description about the field'),
+ 'description' => $this->translate(
+ 'An extended description for this field. Will be shown as soon as a'
+ . ' user puts the focus on this field'
+ ),
'ignore' => true,
- 'value' => array_key_exists($id, $argumentVarDescriptions) ? $argumentVarDescriptions[$id] : null,
+ 'value' => $command ? $suggestions->getDescription($id) : null,
'rows' => '3',
- ));
+ ]);
}
- $this->addElement('select', 'is_required', array(
+ $this->addElement('select', 'is_required', [
'label' => $this->translate('Mandatory'),
'description' => $this->translate('Whether this field should be mandatory'),
'required' => true,
- 'multiOptions' => array(
+ 'multiOptions' => [
'n' => $this->translate('Optional'),
'y' => $this->translate('Mandatory'),
- )
- ));
-
- $filterFields = array();
- $prefix = null;
- if ($object instanceof IcingaHost) {
- $prefix = 'host.vars.';
- } elseif ($object instanceof IcingaService) {
- $prefix = 'service.vars.';
- }
+ ]
+ ]);
- if ($prefix) {
- $loader = new IcingaObjectFieldLoader($object);
- $fields = $loader->getFields();
-
- foreach ($fields as $varName => $field) {
- $filterFields[$prefix . $field->varname] = $field->caption;
- }
-
- $this->addFilterElement('var_filter', array(
+ if ($filterFields = $this->getFilterFields($object)) {
+ $this->addFilterElement('var_filter', [
'description' => $this->translate(
'You might want to show this field only when certain conditions are met.'
. ' Otherwise it will not be available and values eventually set before'
. ' will be cleared once stored'
),
'columns' => $filterFields,
- ));
+ ]);
- $this->addDisplayGroup(array($this->getElement('var_filter')), 'field_filter', array(
- 'decorators' => array(
+ $this->addDisplayGroup([$this->getElement('var_filter')], 'field_filter', [
+ 'decorators' => [
'FormElements',
- array('HtmlTag', array('tag' => 'dl')),
+ ['HtmlTag', ['tag' => 'dl']],
'Fieldset',
- ),
+ ],
'order' => 30,
'legend' => $this->translate('Show based on filter')
- ));
+ ]);
}
$this->setButtons();
@@ -202,18 +146,42 @@ class IcingaObjectFieldForm extends DirectorObjectForm
$fieldId = $this->getValue('datafield_id');
if (! ctype_digit($fieldId)) {
- $field = DirectorDatafield::create(array(
+ $field = DirectorDatafield::create([
'varname' => trim($fieldId, '$'),
'caption' => $this->getValue('caption'),
'description' => $this->getValue('description'),
- 'datatype' => 'Icinga\Module\Director\DataType\DataTypeString',
- ));
+ 'datatype' => $this->fieldSuggestion && $this->fieldSuggestion->isBoolean($fieldId)
+ ? DataTypeBoolean::class
+ : DataTypeString::class
+ ]);
$field->store($this->getDb());
$this->setElementValue('datafield_id', $field->get('id'));
$this->object()->set('datafield_id', $field->get('id'));
}
$this->object()->set('var_filter', $this->getValue('var_filter'));
- return parent::onSuccess();
+ parent::onSuccess();
+ }
+
+ protected static function getFilterFields(IcingaObject $object): array
+ {
+ $filterFields = [];
+ $prefix = null;
+ if ($object instanceof IcingaHost) {
+ $prefix = 'host.vars.';
+ } elseif ($object instanceof IcingaService) {
+ $prefix = 'service.vars.';
+ }
+
+ if ($prefix) {
+ $loader = new IcingaObjectFieldLoader($object);
+ $fields = $loader->getFields();
+
+ foreach ($fields as $varName => $field) {
+ $filterFields[$prefix . $field->get('varname')] = $field->get('caption');
+ }
+ }
+
+ return $filterFields;
}
}