TemplateTree: allow lookup for single objects too

This commit is contained in:
Thomas Gelf 2017-08-12 11:07:34 +02:00
parent 1356733332
commit 241bad7d81

View File

@ -39,13 +39,17 @@ class TemplateTree
return $this->type; return $this->type;
} }
public function listParentNamesForObject(IcingaObject $object) public function listParentNamesFor(IcingaObject $object)
{ {
if ($this->objectMaps === null) { $id = (int) $object->getProperty('id');
$this->loadObjectMaps(); $this->requireTree();
if (array_key_exists($id, $this->parents)) {
return array_values($this->parents[$id]);
} }
$id = $object->getProperty('id'); $this->requireObjectMaps();
$parents = []; $parents = [];
if (array_key_exists($id, $this->objectMaps)) { if (array_key_exists($id, $this->objectMaps)) {
foreach ($this->objectMaps[$id] as $pid) { foreach ($this->objectMaps[$id] as $pid) {
@ -74,11 +78,11 @@ class TemplateTree
)->order('i.weight'); )->order('i.weight');
foreach ($db->fetchAll($query) as $row) { foreach ($db->fetchAll($query) as $row) {
$id = $row->object; $id = (int) $row->object;
if (! array_key_exists($id, $map)) { if (! array_key_exists($id, $map)) {
$map[$id] = []; $map[$id] = [];
} }
$map[$id][] = $row->parent; $map[$id][] = (int) $row->parent;
} }
$this->objectMaps = $map; $this->objectMaps = $map;
@ -124,12 +128,29 @@ class TemplateTree
} }
} }
protected function requireObjectMaps()
{
if ($this->objectMaps === null) {
$this->loadObjectMaps();
}
}
public function getParentsById($id) public function getParentsById($id)
{ {
$this->requireTree(); $this->requireTree();
if (array_key_exists($id, $this->parents)) { if (array_key_exists($id, $this->parents)) {
return $this->parents[$id]; return $this->parents[$id];
}
$this->requireObjectMaps();
if (array_key_exists($id, $this->objectMaps)) {
$parents = [];
foreach ($this->objectMaps[$id] as $pid) {
$parents[$pid] = $this->names[$pid];
}
return $parents;
} else { } else {
return []; return [];
} }
@ -144,7 +165,6 @@ class TemplateTree
if (false !== ($key = array_search($name, $ancestors))) { if (false !== ($key = array_search($name, $ancestors))) {
unset($ancestors[$key]); unset($ancestors[$key]);
} }
$ancestors[$pid] = $name; $ancestors[$pid] = $name;
} }