1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
<?php
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;
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\IcingaObjectFieldLoader;
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;
$this->className = get_class($object) . 'Field';
return $this;
}
public function setup()
{
$object = $this->icingaObject;
$type = $object->getShortTableName();
$this->addHidden($type . '_id', $object->get('id'));
$this->addHtmlHint(
'Custom data fields allow you to easily fill custom variables with'
. " meaningful data. It's perfectly legal to override inherited fields."
. ' You may for example want to allow "network devices" specifying any'
. ' string for vars.snmp_community, but restrict "customer routers" to'
. ' a specific set, shown as a dropdown.'
);
// TODO: think about imported existing vars without fields
// TODO: extract vars from command line (-> dummy)
// TODO: do not suggest chosen ones
if ($object instanceof IcingaCommand) {
$command = $object;
} elseif ($object->hasProperty('check_command_id')) {
$command = $object->getResolvedRelated('check_command');
} else {
$command = null;
}
$suggestions = $this->fieldSuggestion = new FormFieldSuggestion($command, $this->db->enumDatafields());
$fields = $suggestions->getCommandFields();
$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
$msg = $this->translate(
'There are no data fields available. Please ask an administrator to create such'
);
$this->getElement('datafield_id')->addError($msg);
}
if (($id = $this->getSentValue('datafield_id')) && ! ctype_digit($id)) {
$this->addElement('text', 'caption', [
'label' => $this->translate('Caption'),
'required' => true,
'ignore' => true,
'value' => trim($id, '$'),
'description' => $this->translate(
'The caption which should be displayed to your users when this field'
. ' is shown'
)
]);
$this->addElement('textarea', 'description', [
'label' => $this->translate('Description'),
'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' => $command ? $suggestions->getDescription($id) : null,
'rows' => '3',
]);
}
$this->addElement('select', 'is_required', [
'label' => $this->translate('Mandatory'),
'description' => $this->translate('Whether this field should be mandatory'),
'required' => true,
'multiOptions' => [
'n' => $this->translate('Optional'),
'y' => $this->translate('Mandatory'),
]
]);
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([$this->getElement('var_filter')], 'field_filter', [
'decorators' => [
'FormElements',
['HtmlTag', ['tag' => 'dl']],
'Fieldset',
],
'order' => 30,
'legend' => $this->translate('Show based on filter')
]);
}
$this->setButtons();
}
protected function onRequest()
{
parent::onRequest();
if ($this->getSentValue('delete') === $this->translate('Delete')) {
$this->object()->delete();
$this->setSuccessUrl($this->getSuccessUrl()->without('field_id'));
$this->redirectOnSuccess($this->translate('Field has been removed'));
}
}
public function onSuccess()
{
$fieldId = $this->getValue('datafield_id');
if (! ctype_digit($fieldId)) {
$field = DirectorDatafield::create([
'varname' => trim($fieldId, '$'),
'caption' => $this->getValue('caption'),
'description' => $this->getValue('description'),
'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'));
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;
}
}
|