From 5298c8ee5d5d69b18b86c39cd39f04ba333673c9 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Fri, 2 Aug 2024 12:42:45 +0200 Subject: [PATCH] IcingaMultiEditForm: Fix editing custom vars with space in their name --- application/forms/IcingaMultiEditForm.php | 33 ++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/application/forms/IcingaMultiEditForm.php b/application/forms/IcingaMultiEditForm.php index 4149a708d..4849d4673 100644 --- a/application/forms/IcingaMultiEditForm.php +++ b/application/forms/IcingaMultiEditForm.php @@ -21,6 +21,9 @@ class IcingaMultiEditForm extends DirectorObjectForm private $propertiesToPick; + /** @var array Custom variable name map to its element's name in the form */ + private $varNameMap = []; + public function setObjects($objects) { $this->objects = $objects; @@ -68,8 +71,7 @@ public function setup() /** @var \Zend_Form_Element $el */ foreach ($this->getElements() as $el) { - $name = $el->getName(); - if (substr($name, 0, 4) === 'var_') { + if ($this->isCustomVar($el->getName())) { $this->makeVariants($el); } } @@ -137,8 +139,8 @@ protected function setSubmittedMultiValue($key, $value) continue; } - if (substr($property, 0, 4) === 'var_') { - $property = 'vars.' . substr($property, 4); + if ($this->isCustomVar($property)) { + $property = 'vars.' . $this->varNameMap[$property]; } foreach ($this->getObjects($objects) as $object) { @@ -147,6 +149,18 @@ protected function setSubmittedMultiValue($key, $value) } } + /** + * Check if the given property is a custom var + * + * @param string $property + * + * @return bool + */ + protected function isCustomVar(string $property): bool + { + return substr($property, 0, 4) === 'var_'; + } + protected function storeModifiedObjects() { $modified = 0; @@ -222,6 +236,11 @@ protected function makeVariants(ZfElement $element) $key = $element->getName(); $this->removeElement($key); $label = $element->getLabel(); + + if ($this->isCustomVar($key)) { + $this->varNameMap[$key] = $label; + } + $group = $this->getDisplayGroupForElement($element); $description = $element->getDescription(); @@ -241,11 +260,13 @@ protected function makeVariants(ZfElement $element) } } + + protected function getVariants($key) { $variants = array(); - if (substr($key, 0, 4) === 'var_') { - $key = 'vars.' . substr($key, 4); + if ($this->isCustomVar($key)) { + $key = 'vars.' . $this->varNameMap[$key]; } foreach ($this->objects as $name => $object) {