From 1a08b90c7a43a190c15e544f1602aa6e7cc821dd Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 25 Aug 2017 14:22:49 +0200 Subject: [PATCH] IcingaTemplateRepository: fetch parents, not... ...ancestors per default fixes #1114 --- application/controllers/HostController.php | 2 +- .../Repository/IcingaTemplateRepository.php | 29 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 600525ce..b08baeb8 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -111,7 +111,7 @@ class HostController extends ObjectController /** @var IcingaHost[] $parents */ $parents = IcingaTemplateRepository::instanceByObject($this->object) - ->getTemplatesFor($this->object); + ->getTemplatesFor($this->object, true); foreach ($parents as $parent) { $table = IcingaHostServiceTable::load($parent)->setInheritedBy($host); if (count($table)) { diff --git a/library/Director/Repository/IcingaTemplateRepository.php b/library/Director/Repository/IcingaTemplateRepository.php index f0a4cc04..b3f6b92f 100644 --- a/library/Director/Repository/IcingaTemplateRepository.php +++ b/library/Director/Repository/IcingaTemplateRepository.php @@ -29,11 +29,27 @@ class IcingaTemplateRepository /** * @param IcingaObject $object + * @param bool $recursive * @return IcingaObject[] */ - public function getTemplatesFor(IcingaObject $object) + public function getTemplatesFor(IcingaObject $object, $recursive = false) + { + if ($recursive) { + $ids = $this->tree()->listAncestorIdsFor($object); + } else { + $ids = $this->tree()->listParentIdsFor($object); + } + + return $this->getTemplatesForIds($ids, $object); + } + + /** + * @param array $ids + * @param IcingaObject $object + * @return IcingaObject[] + */ + public function getTemplatesForIds(array $ids, IcingaObject $object) { - $ids = $this->tree()->listAncestorIdsFor($object); $templates = []; foreach ($ids as $id) { if (! array_key_exists($id, $this->loadedById)) { @@ -52,12 +68,15 @@ class IcingaTemplateRepository /** * @param IcingaObject $object + * @param bool $recursive * @return IcingaObject[] */ - public function getTemplatesIndexedByNameFor(IcingaObject $object) - { + public function getTemplatesIndexedByNameFor( + IcingaObject $object, + $recursive = false + ) { $templates = []; - foreach ($this->getTemplatesFor($object) as $template) { + foreach ($this->getTemplatesFor($object, $recursive) as $template) { $templates[$template->getObjectName()] = $template; }