From 2d40ffa1e066a04291a4e2071d709fca140fd8af Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 4 Feb 2016 10:05:32 +0100 Subject: [PATCH] IcingaObjectFieldForm: easier field handling This makes it much easier to deal with command fields, as they are auto-proposed in a very helpful way. --- application/forms/IcingaObjectFieldForm.php | 77 ++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/application/forms/IcingaObjectFieldForm.php b/application/forms/IcingaObjectFieldForm.php index ed619fcc..195875bb 100644 --- a/application/forms/IcingaObjectFieldForm.php +++ b/application/forms/IcingaObjectFieldForm.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\Forms; use Icinga\Module\Director\Objects\IcingaObject; +use Icinga\Module\Director\Objects\DirectorDatafield; use Icinga\Module\Director\Web\Form\DirectorObjectForm; class IcingaObjectFieldForm extends DirectorObjectForm @@ -33,11 +34,58 @@ class IcingaObjectFieldForm extends DirectorObjectForm . ' 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( 'label' => 'Field', 'required' => true, 'description' => 'Field to assign', + 'class' => 'autosubmit', 'multiOptions' => $this->optionalEnum($fields) )); @@ -51,6 +99,15 @@ class IcingaObjectFieldForm extends DirectorObjectForm $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( 'label' => $this->translate('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')); } } + + 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(); + } }