Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IcingaMultiEditForm: Fix editing custom vars with space in their name #2900

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions application/forms/IcingaMultiEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class IcingaMultiEditForm extends DirectorObjectForm

private $propertiesToPick;

/** @var array<string, string> Custom variable name map to its element's name in the form */
private $varNameMap = [];

public function setObjects($objects)
{
$this->objects = $objects;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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) {
Expand Down