Fix form field suggestions

Other available fields should be suggested when no check command has been
chosen for service template.
This commit is contained in:
raviks789 2023-12-05 11:18:07 +01:00
parent 6851778863
commit c0e870d543
2 changed files with 19 additions and 20 deletions

View File

@ -53,13 +53,8 @@ class IcingaObjectFieldForm extends DirectorObjectForm
$command = null; $command = null;
} }
if ($command) { $suggestions = $this->fieldSuggestion = new FormFieldSuggestion($command, $this->db->enumDatafields());
$suggestions = $this->fieldSuggestion = new FormFieldSuggestion($command, $this->db->enumDatafields()); $fields = $suggestions->getCommandFields();
$fields = $suggestions->getCommandFields();
} else {
$suggestions = null;
$fields = [];
}
$this->addElement('select', 'datafield_id', [ $this->addElement('select', 'datafield_id', [
'label' => 'Field', 'label' => 'Field',
@ -97,7 +92,7 @@ class IcingaObjectFieldForm extends DirectorObjectForm
. ' user puts the focus on this field' . ' user puts the focus on this field'
), ),
'ignore' => true, 'ignore' => true,
'value' => $suggestions ? $suggestions->getDescription($id) : null, 'value' => $command ? $suggestions->getDescription($id) : null,
'rows' => '3', 'rows' => '3',
]); ]);
} }

View File

@ -20,7 +20,7 @@ class FormFieldSuggestion
protected $descriptions = []; protected $descriptions = [];
protected $booleans = []; protected $booleans = [];
/** @var IcingaCommand */ /** @var ?IcingaCommand */
protected $command; protected $command;
/** @var array */ /** @var array */
@ -29,7 +29,7 @@ class FormFieldSuggestion
protected $fields = null; protected $fields = null;
public function __construct( public function __construct(
IcingaCommand $command, ?IcingaCommand $command,
array $existingFields array $existingFields
) { ) {
$this->command = $command; $this->command = $command;
@ -54,19 +54,23 @@ class FormFieldSuggestion
$this->blacklistedVars['$' . $m[1] . '$'] = $id; $this->blacklistedVars['$' . $m[1] . '$'] = $id;
} }
} }
foreach ($this->command->arguments() as $arg) {
if ($arg->argument_format === 'string') { if ($this->command) {
foreach (self::extractMacroNamesFromString($arg->argument_value) as $val) { foreach ($this->command->arguments() as $arg) {
$this->addSuggestion($val, $arg->description, $this->argumentVars); if ($arg->argument_format === 'string') {
foreach (self::extractMacroNamesFromString($arg->argument_value) as $val) {
$this->addSuggestion($val, $arg->description, $this->argumentVars);
}
}
if (($arg->set_if_format === 'string' || $arg->set_if_format === null)
&& $val = self::getMacroIfStringIsSingleMacro($arg->set_if)
) {
$this->addSuggestion($val, $arg->description, $this->booleans);
} }
} }
if (($arg->set_if_format === 'string' || $arg->set_if_format === null)
&& $val = self::getMacroIfStringIsSingleMacro($arg->set_if)
) {
$this->addSuggestion($val, $arg->description, $this->booleans);
}
} }
asort($this->suggestedFields, SORT_NATURAL | SORT_FLAG_CASE); asort($this->suggestedFields, SORT_NATURAL | SORT_FLAG_CASE);
ksort($this->argumentVars); ksort($this->argumentVars);
ksort($this->booleans); ksort($this->booleans);