FormFieldSuggestion: refactor, dedicated class

This commit is contained in:
Thomas Gelf 2023-08-19 13:17:29 +02:00
parent 62f59d4c5a
commit d97bbc0526
2 changed files with 165 additions and 98 deletions

View File

@ -2,19 +2,20 @@
namespace Icinga\Module\Director\Forms; namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Field\FormFieldSuggestion;
use Icinga\Module\Director\Objects\IcingaCommand; use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\DirectorDatafield; use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader;
class IcingaObjectFieldForm extends DirectorObjectForm class IcingaObjectFieldForm extends DirectorObjectForm
{ {
/** @var IcingaObject Please note that $object would conflict with logic in parent class */ /** @var IcingaObject Please note that $object would conflict with logic in parent class */
protected $icingaObject; protected $icingaObject;
/** @var FormFieldSuggestion */
protected $formFieldSuggestion;
public function setIcingaObject($object) public function setIcingaObject($object)
{ {
$this->icingaObject = $object; $this->icingaObject = $object;
@ -24,6 +25,7 @@ class IcingaObjectFieldForm extends DirectorObjectForm
public function setup() public function setup()
{ {
$this->formFieldSuggestion = new FormFieldSuggestion();
$object = $this->icingaObject; $object = $this->icingaObject;
$type = $object->getShortTableName(); $type = $object->getShortTableName();
$this->addHidden($type . '_id', $object->get('id')); $this->addHidden($type . '_id', $object->get('id'));
@ -36,22 +38,9 @@ class IcingaObjectFieldForm extends DirectorObjectForm
. ' a specific set, shown as a dropdown.' . ' 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: think about imported existing vars without fields
// TODO: extract vars from command line (-> dummy) // TODO: extract vars from command line (-> dummy)
// TODO: do not suggest chosen ones // TODO: do not suggest chosen ones
$argumentVars = array();
$argumentVarDescriptions = array();
if ($object instanceof IcingaCommand) { if ($object instanceof IcingaCommand) {
$command = $object; $command = $object;
} elseif ($object->hasProperty('check_command_id')) { } elseif ($object->hasProperty('check_command_id')) {
@ -60,57 +49,20 @@ class IcingaObjectFieldForm extends DirectorObjectForm
$command = null; $command = null;
} }
if ($command) { $descriptions = [];
foreach ($command->arguments() as $arg) { $fields = $command ? $this->formFieldSuggestion->getCommandFields(
if ($arg->argument_format === 'string') { $command,
$val = $arg->argument_value; $this->db->enumDatafields(),
// TODO: create var::extractMacros or so $descriptions
) : [];
if (preg_match_all('/(\$[a-z0-9_]+\$)/i', $val, $m, PREG_PATTERN_ORDER)) { $this->addElement('select', 'datafield_id', [
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($suggestedFields, SORT_NATURAL | SORT_FLAG_CASE);
$fields[$this->translate('Suggested fields')] = $suggestedFields;
}
if (! empty($argumentVars)) {
ksort($argumentVars);
$fields[$this->translate('Argument macros')] = $argumentVars;
}
if (! empty($existingFields)) {
asort($existingFields, SORT_NATURAL | SORT_FLAG_CASE);
$fields[$this->translate('Other available fields')] = $existingFields;
}
$this->addElement('select', 'datafield_id', array(
'label' => 'Field', 'label' => 'Field',
'required' => true, 'required' => true,
'description' => 'Field to assign', 'description' => 'Field to assign',
'class' => 'autosubmit', 'class' => 'autosubmit',
'multiOptions' => $this->optionalEnum($fields) 'multiOptions' => $this->optionalEnum($fields)
)); ]);
if (empty($fields)) { if (empty($fields)) {
// TODO: show message depending on permissions // TODO: show message depending on permissions
@ -122,67 +74,58 @@ class IcingaObjectFieldForm extends DirectorObjectForm
} }
if (($id = $this->getSentValue('datafield_id')) && ! ctype_digit($id)) { if (($id = $this->getSentValue('datafield_id')) && ! ctype_digit($id)) {
$this->addElement('text', 'caption', array( $this->addElement('text', 'caption', [
'label' => $this->translate('Caption'), 'label' => $this->translate('Caption'),
'required' => true, 'required' => true,
'ignore' => true, 'ignore' => true,
'value' => trim($id, '$'), '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'), '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, 'ignore' => true,
'value' => array_key_exists($id, $argumentVarDescriptions) ? $argumentVarDescriptions[$id] : null, 'value' => array_key_exists($id, $descriptions) ? $descriptions[$id] : null,
'rows' => '3', 'rows' => '3',
)); ]);
} }
$this->addElement('select', 'is_required', array( $this->addElement('select', 'is_required', [
'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'),
'required' => true, 'required' => true,
'multiOptions' => array( 'multiOptions' => [
'n' => $this->translate('Optional'), 'n' => $this->translate('Optional'),
'y' => $this->translate('Mandatory'), 'y' => $this->translate('Mandatory'),
) ]
)); ]);
$filterFields = array(); if ($filterFields = $this->formFieldSuggestion->getFilterFields($object)) {
$prefix = null; $this->addFilterElement('var_filter', [
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(
'description' => $this->translate( 'description' => $this->translate(
'You might want to show this field only when certain conditions are met.' 'You might want to show this field only when certain conditions are met.'
. ' Otherwise it will not be available and values eventually set before' . ' Otherwise it will not be available and values eventually set before'
. ' will be cleared once stored' . ' will be cleared once stored'
), ),
'columns' => $filterFields, 'columns' => $filterFields,
)); ]);
$this->addDisplayGroup(array($this->getElement('var_filter')), 'field_filter', array( $this->addDisplayGroup([$this->getElement('var_filter')], 'field_filter', [
'decorators' => array( 'decorators' => [
'FormElements', 'FormElements',
array('HtmlTag', array('tag' => 'dl')), ['HtmlTag', ['tag' => 'dl']],
'Fieldset', 'Fieldset',
), ],
'order' => 30, 'order' => 30,
'legend' => $this->translate('Show based on filter') 'legend' => $this->translate('Show based on filter')
)); ]);
} }
$this->setButtons(); $this->setButtons();
@ -203,18 +146,18 @@ class IcingaObjectFieldForm extends DirectorObjectForm
$fieldId = $this->getValue('datafield_id'); $fieldId = $this->getValue('datafield_id');
if (! ctype_digit($fieldId)) { if (! ctype_digit($fieldId)) {
$field = DirectorDatafield::create(array( $field = DirectorDatafield::create([
'varname' => trim($fieldId, '$'), 'varname' => trim($fieldId, '$'),
'caption' => $this->getValue('caption'), 'caption' => $this->getValue('caption'),
'description' => $this->getValue('description'), 'description' => $this->getValue('description'),
'datatype' => 'Icinga\Module\Director\DataType\DataTypeString', 'datatype' => 'Icinga\Module\Director\DataType\DataTypeString',
)); ]);
$field->store($this->getDb()); $field->store($this->getDb());
$this->setElementValue('datafield_id', $field->get('id')); $this->setElementValue('datafield_id', $field->get('id'));
$this->object()->set('datafield_id', $field->get('id')); $this->object()->set('datafield_id', $field->get('id'));
} }
$this->object()->set('var_filter', $this->getValue('var_filter')); $this->object()->set('var_filter', $this->getValue('var_filter'));
return parent::onSuccess(); parent::onSuccess();
} }
} }

View File

@ -0,0 +1,124 @@
<?php
namespace Icinga\Module\Director\Field;
use gipfl\Translation\TranslationHelper;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader;
class FormFieldSuggestion
{
use TranslationHelper;
public function getCommandFields(
IcingaCommand $command,
array $existingFields,
array &$descriptions
): array
{
// TODO: remove assigned ones!
$argumentVars = [];
$blacklistedVars = [];
$suggestedFields = [];
$booleans = [];
foreach ($existingFields as $id => $field) {
if (preg_match('/ \(([^)]+)\)$/', $field, $m)) {
$blacklistedVars['$' . $m[1] . '$'] = $id;
}
}
foreach ($command->arguments() as $arg) {
if ($arg->argument_format === 'string') {
foreach (self::extractMacroNamesFromString($arg->argument_value) as $val) {
self::addSuggestion(
$val,
$arg->description,
$blacklistedVars,
$existingFields,
$suggestedFields,
$argumentVars,
$descriptions
);
}
}
}
// Prepare combined fields array
$fields = [];
if (! empty($suggestedFields)) {
asort($suggestedFields, SORT_NATURAL | SORT_FLAG_CASE);
$fields[$this->translate('Suggested fields')] = $suggestedFields;
}
if (! empty($argumentVars)) {
ksort($argumentVars);
$fields[$this->translate('Argument macros')] = $argumentVars;
}
if (! empty($existingFields)) {
asort($existingFields, SORT_NATURAL | SORT_FLAG_CASE);
$fields[$this->translate('Other available fields')] = $existingFields;
}
return $fields;
}
protected static function addSuggestion(
string $val,
?string $description,
array $blacklistedVars,
array &$existingFields,
array &$suggestedFields,
array &$targetList,
array &$descriptions
) {
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 {
$targetList[$val] = $val;
$descriptions[$val] = $description;
}
}
public 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;
}
protected static function extractMacroNamesFromString(?string $string): array
{
if ($string !== null && preg_match_all('/(\$[a-z0-9_]+\$)/i', $string, $matches, PREG_PATTERN_ORDER)) {
return $matches[1];
}
return [];
}
}