IcingaMultiEditForm: Fix editing custom vars with space in their name

This commit is contained in:
raviks789 2024-08-02 12:42:45 +02:00 committed by Eric Lippmann
parent 39c823c655
commit 5298c8ee5d

View File

@ -21,6 +21,9 @@ class IcingaMultiEditForm extends DirectorObjectForm
private $propertiesToPick; private $propertiesToPick;
/** @var array<string, string> Custom variable name map to its element's name in the form */
private $varNameMap = [];
public function setObjects($objects) public function setObjects($objects)
{ {
$this->objects = $objects; $this->objects = $objects;
@ -68,8 +71,7 @@ class IcingaMultiEditForm extends DirectorObjectForm
/** @var \Zend_Form_Element $el */ /** @var \Zend_Form_Element $el */
foreach ($this->getElements() as $el) { foreach ($this->getElements() as $el) {
$name = $el->getName(); if ($this->isCustomVar($el->getName())) {
if (substr($name, 0, 4) === 'var_') {
$this->makeVariants($el); $this->makeVariants($el);
} }
} }
@ -137,8 +139,8 @@ class IcingaMultiEditForm extends DirectorObjectForm
continue; continue;
} }
if (substr($property, 0, 4) === 'var_') { if ($this->isCustomVar($property)) {
$property = 'vars.' . substr($property, 4); $property = 'vars.' . $this->varNameMap[$property];
} }
foreach ($this->getObjects($objects) as $object) { foreach ($this->getObjects($objects) as $object) {
@ -147,6 +149,18 @@ class IcingaMultiEditForm extends DirectorObjectForm
} }
} }
/**
* 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() protected function storeModifiedObjects()
{ {
$modified = 0; $modified = 0;
@ -222,6 +236,11 @@ class IcingaMultiEditForm extends DirectorObjectForm
$key = $element->getName(); $key = $element->getName();
$this->removeElement($key); $this->removeElement($key);
$label = $element->getLabel(); $label = $element->getLabel();
if ($this->isCustomVar($key)) {
$this->varNameMap[$key] = $label;
}
$group = $this->getDisplayGroupForElement($element); $group = $this->getDisplayGroupForElement($element);
$description = $element->getDescription(); $description = $element->getDescription();
@ -241,11 +260,13 @@ class IcingaMultiEditForm extends DirectorObjectForm
} }
} }
protected function getVariants($key) protected function getVariants($key)
{ {
$variants = array(); $variants = array();
if (substr($key, 0, 4) === 'var_') { if ($this->isCustomVar($key)) {
$key = 'vars.' . substr($key, 4); $key = 'vars.' . $this->varNameMap[$key];
} }
foreach ($this->objects as $name => $object) { foreach ($this->objects as $name => $object) {