diff --git a/application/forms/IcingaHostForm.php b/application/forms/IcingaHostForm.php index e19945ee..b611195c 100644 --- a/application/forms/IcingaHostForm.php +++ b/application/forms/IcingaHostForm.php @@ -283,24 +283,7 @@ class IcingaHostForm extends DirectorObjectForm return []; } - $db = $this->getDb()->getDbAdapter(); - $query = $db->select()->from( - ['hghr' => 'icinga_hostgroup_host_resolved'], - ['hg.object_name'] - )->join( - ['hg' => 'icinga_hostgroup'], - 'hg.id = hghr.hostgroup_id', - [] - )->joinLeft( - ['hgh' => 'icinga_hostgroup_host'], - 'hgh.hostgroup_id = hghr.hostgroup_id', - [] - )->where( - 'hghr.host_id = ?', - $this->object()->get('id') - )->where('hgh.host_id IS NULL')->order('hg.object_name'); - - return $db->fetchCol($query); + return $this->object()->getAppliedGroups(); } protected function hasHostGroupRestriction() diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index b47944b1..441907b9 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -853,6 +853,34 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return $this->groups->hasBeenModified(); } + + public function getAppliedGroups() + { + $this->assertGroupsSupport(); + if (! $this instanceof IcingaHost) { + throw new ProgrammingError('getAppliedGroups is only available for hosts currently!'); + } + + $type = strtolower($this->type); + $query = $this->db->select()->from( + ['gr' => "icinga_${type}group_${type}_resolved"], + ['g.object_name'] + )->join( + ['g' => "icinga_${type}group"], + "g.id = gr.${type}group_id", + [] + )->joinLeft( + ['go' => "icinga_${type}group_${type}"], + "go.${type}group_id = gr.${type}group_id", + [] + )->where( + "gr.${type}_id = ?", + $this->id + )->where("go.${type}_id IS NULL")->order('g.object_name'); + + return $this->db->fetchCol($query); + } + /** * @return IcingaTimePeriodRanges */