Skip to content

Commit dd916d4

Browse files
committed
WIP: Add items to instantiable dictionary
1 parent a13de3e commit dd916d4

3 files changed

Lines changed: 87 additions & 19 deletions

File tree

configuration.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,8 @@
175175
->setUrl('director/config/deployments')
176176
->setPriority(902)
177177
->setPermission(Permission::DEPLOYMENTS);
178-
$section->add(N_('Dictionaries'))
179-
->setUrl('director/dictionaries')
180-
->setPriority(903);
181178
$section->add(N_('Properties'))
182179
->setUrl('director/properties')
183-
->setPriority(904);
180+
->setPriority(903);
184181

185182
$this->provideCssFile('property-table.less');

library/Director/Web/Form/DirectorObjectForm.php

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
use Icinga\Module\Director\Util;
2222
use Icinga\Module\Director\Web\Form\Element\ExtensibleSet;
2323
use Icinga\Module\Director\Web\Form\Validate\NamePattern;
24-
use ipl\Html\HtmlElement;
25-
use ipl\Html\Text;
26-
use ipl\Web\Widget\Link;
2724
use Ramsey\Uuid\Uuid;
2825
use Ramsey\Uuid\UuidInterface;
2926
use Zend_Form_Element as ZfElement;
@@ -512,6 +509,37 @@ protected function setCustomVarValues($values)
512509
}
513510
}
514511

512+
$value = array_filter($value) === [] ? null : $value;
513+
} elseif ($type === 'dict' && $property['instantiable'] === 'y') {
514+
$items = $this->fetchPropertyItems(Uuid::fromBytes($property['uuid']));
515+
$value = [];
516+
$i = 0;
517+
while ($this->hasElement($varName . "_property_label_$i")) {
518+
$propertyLabel = $this->getValue($varName . "_property_label_$i");
519+
520+
if (! empty($propertyLabel)) {
521+
foreach ($items as $item) {
522+
$idx = $item['key_name'];
523+
$itemKey = $varName . "_property$i" . "_$idx";
524+
$el = $this->getElement($itemKey);
525+
if ($el === null) {
526+
// throw new IcingaException('No such element %s', $key);
527+
// Same here.
528+
continue;
529+
}
530+
531+
$el->setValue($values[$itemKey]);
532+
$value[$propertyLabel][$idx] = $el->getValue();
533+
534+
if (! is_int($idx) && empty($values[$itemKey])) {
535+
unset($value[$propertyLabel][$idx]);
536+
}
537+
}
538+
}
539+
540+
$i++;
541+
}
542+
515543
$value = array_filter($value) === [] ? null : $value;
516544
} else {
517545
$el = $this->getElement($varName);
@@ -1004,25 +1032,27 @@ protected function addProperties(): self
10041032
$originalItems = $value ? count($value) : 0;
10051033

10061034
// var_dump($this->getSentValue('add-item'));
1007-
$addedItems = 0;
1035+
// $addedItems = (int) $this->getSentValue('added_items') ?? 0;
10081036

10091037
$addItem = $this->createElement(
10101038
'button',
1011-
'add_item',
1039+
'var_' . $property['key_name'] . '_add_item',
10121040
[
10131041
'class' => 'control-button',
10141042
'type' => 'submit',
10151043
'value' => 'y',
1016-
'label' => $this->translate('Add Item')
1044+
'label' => $this->translate('Add Item'),
1045+
'formnovalidate' => 'formnovalidate'
10171046
]
10181047
);
10191048

1049+
// $this->addElement('hidden', 'added_items', $addedItems);
1050+
$propertyDescendants = $this->fetchPropertyItems($propertyUuid);
1051+
10201052
$propertyGroupElements = [];
10211053
if ($originalItems > 0) {
1022-
$propertyDescendants = $this->fetchPropertyItems($propertyUuid);
10231054
$i = 0;
10241055
foreach ($value as $key => $nestedItems) {
1025-
10261056
// $nestedProperty->addDecorator()
10271057

10281058
// $nestedProperty = (new FieldsetElement(
@@ -1044,13 +1074,13 @@ protected function addProperties(): self
10441074
// );
10451075

10461076
// $this->addHtmlHint($nestedProperty->render());
1047-
10481077
$label = $this->createElement(
10491078
'text',
1050-
$property['key_name'] . "_key_" . $i,
1079+
'var_' . $property['key_name'] . "_property_label_" . $i,
10511080
[
10521081
'label' => $this->translate('Label'),
1053-
'value' => $key
1082+
'value' => $key,
1083+
'required' => true
10541084
]
10551085
);
10561086

@@ -1060,7 +1090,7 @@ protected function addProperties(): self
10601090
foreach ($propertyDescendants as $descendant) {
10611091
$nestedElement = $this->createElement(
10621092
$this->fetchFieldType($descendant['value_type'], $descendant['instantiable'] === 'y'),
1063-
$key . '_' . $descendant['key_name'],
1093+
'var_' . $property['key_name'] . "_property$i" . '_' . $descendant['key_name'],
10641094
[
10651095
'label' => $descendant['label'],
10661096
'value' => $nestedItems[$descendant['key_name']] ?? null
@@ -1073,7 +1103,7 @@ protected function addProperties(): self
10731103

10741104
$nestedProperty = $this->createElement(
10751105
'formFieldset',
1076-
$property['key_name'] . "$key",
1106+
'var_' . $property['key_name'] . "_$key",
10771107
[
10781108
'decorators' => ['Fieldset'],
10791109
'legend' => $key,
@@ -1088,9 +1118,49 @@ protected function addProperties(): self
10881118
}
10891119
}
10901120

1091-
$this->addElement($addItem);
1121+
if ($this->getSentValue('var_' . $property['key_name'] . '_add_item') === 'y') {
1122+
$label = $this->createElement(
1123+
'text',
1124+
'var_' . $property['key_name'] . "_property_label_" . $i,
1125+
[
1126+
'label' => $this->translate('Label')
1127+
]
1128+
);
1129+
1130+
$this->addElement($label);
1131+
foreach ($propertyDescendants as $descendant) {
1132+
$nestedElement = $this->createElement(
1133+
$this->fetchFieldType(
1134+
$descendant['value_type'],
1135+
$descendant['instantiable'] === 'y'
1136+
),
1137+
'var_' . $property['key_name'] . "_property$i" . '_' . $descendant['key_name'],
1138+
[
1139+
'label' => $descendant['label']
1140+
]
1141+
);
1142+
1143+
$nestedElements[] = $nestedElement;
1144+
$this->addElement($nestedElement);
1145+
}
10921146

1093-
$propertyGroupElements[] = $addItem;
1147+
$nestedProperty = $this->createElement(
1148+
'formFieldset',
1149+
'var_' . $property['key_name'] . "_added_property_$i",
1150+
[
1151+
'decorators' => ['Fieldset'],
1152+
'legend' => "New Property",
1153+
'label' => "New Property"
1154+
]
1155+
);
1156+
1157+
$nestedProperty->addElements($nestedElements);
1158+
// $this->addDisplayGroups([$nestedProperty]);
1159+
$propertyGroupElements[] = $nestedProperty;
1160+
} else {
1161+
$this->addElement($addItem);
1162+
$propertyGroupElements[] = $addItem;
1163+
}
10941164
}
10951165

10961166
if (! empty($propertyGroupElements)) {

public/js/module.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
this.module.on('rendered', this.rendered);
2121
this.module.on('beforerender', this.beforeRender);
2222
this.module.on('click', 'fieldset > legend', this.toggleFieldset);
23+
// this.module.on('click', 'fieldset > legend', this.toggleFieldset);
2324
// Disabled
2425
// this.module.on('click', 'div.controls ul.tabs a', this.detailTabClick);
2526
this.module.on('click', 'input.related-action', this.extensibleSetAction);

0 commit comments

Comments
 (0)