Correctly manage fields categories whe loading forms

This commit is contained in:
Valentina Da Rold 2019-10-02 09:48:30 +02:00
parent 2d73aad9d1
commit 633f4802f0
1 changed files with 18 additions and 15 deletions

View File

@ -231,29 +231,32 @@ class IcingaObjectFieldLoader
protected function attachGroupElements(array $elements, DirectorObjectForm $form) protected function attachGroupElements(array $elements, DirectorObjectForm $form)
{ {
$categories = []; $categories = [];
$categoriesFetchedById = [];
foreach ($this->fields as $key => $field) { foreach ($this->fields as $key => $field) {
if ($category = $field->category_id) { if ($id = $field->get('category_id')) {
$categories[$key] = $category; if (isset($categoriesFetchedById[$id])) {
} elseif ($field) { $category = $categoriesFetchedById[$id];
} else {
$category = DirectorDatafieldCategory::loadWithAutoIncId($id, $form->getDb());
$categoriesFetchedById[$id] = $category;
}
} elseif ($field->hasCategory()) {
$category = $field->getCategory();
} else {
continue;
} }
$categories[$key] = $category;
} }
$cObjects = []; $prioIdx = \array_flip(\array_keys($categories));
foreach ($categories as $id) {
if (! isset($cObjects[$id])) {
$cObjects[$id] = DirectorDatafieldCategory::loadWithAutoIncId($id, $form->getDb());
}
}
$idx = array_flip(array_keys($cObjects));
foreach ($elements as $key => $element) { foreach ($elements as $key => $element) {
if (isset($categories[$key])) { if (isset($categories[$key])) {
$category = $categories[$key]; $category = $categories[$key];
$form->addElementsToGroup( $form->addElementsToGroup(
[$element], [$element],
'custom_fields_' . $category, 'custom_fields:' . $category->get('category_name'),
51 + $idx[$category], 51 + $prioIdx[$key],
$cObjects[$category]->get('category_name') $category->get('category_name')
); );
} else { } else {
$form->addElementsToGroup( $form->addElementsToGroup(
@ -575,7 +578,7 @@ class IcingaObjectFieldLoader
} }
foreach ($this->loadHookedDataFieldForObject($object) as $id => $fields) { foreach ($this->loadHookedDataFieldForObject($object) as $id => $fields) {
if (array_key_exists($id, $result)) { if (array_key_exists($id, $result)) {
foreach ($fields as $varName => $field) { foreach ($fields as $varName => $field) {
$result[$id]->$varName = $field; $result[$id]->$varName = $field;
} }