diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php index 968d5607e..4169bc1b1 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HoststatusQuery.php @@ -242,62 +242,65 @@ class HoststatusQuery extends IdoQuery */ public function getGroup() { - $group = array(); - if ($this->hasJoinedVirtualTable('hostgroups') || $this->hasJoinedVirtualTable('services')) { - $group = array('h.host_id', 'ho.object_id'); - if ($this->hasJoinedVirtualTable('hoststatus')) { - $group[] = 'hs.hoststatus_id'; - } - - if ($this->hasJoinedVirtualTable('serviceproblemsummary')) { - $group[] = 'sps.unhandled_services_count'; - } - - if ($this->hasJoinedVirtualTable('hostgroups')) { - $selected = false; - foreach ($this->columns as $alias => $column) { - if ($column instanceof Zend_Db_Expr) { - continue; - } - - $table = $this->aliasToTableName( - $this->hasAliasName($alias) ? $alias : $this->customAliasToAlias($alias) - ); - if ($table === 'hostgroups') { - $selected = true; - break; - } - } - - if ($selected) { - $group[] = 'hg.hostgroup_id'; - $group[] = 'hgo.object_id'; - } - } - - if ($this->hasJoinedVirtualTable('servicegroups')) { - $selected = false; - foreach ($this->columns as $alias => $column) { - if ($column instanceof Zend_Db_Expr) { - continue; - } - - $table = $this->aliasToTableName( - $this->hasAliasName($alias) ? $alias : $this->customAliasToAlias($alias) - ); - if ($table === 'servicegroups') { - $selected = true; - break; - } - } - - if ($selected) { - $group[] = 'sg.servicegroup_id'; - $group[] = 'sgo.object_id'; - } + $group = parent::getGroup() ?: array(); + if (! is_array($group)) { + $group = array($group); + } + $groupedTables = array(); + if ($this->hasJoinedVirtualTable('servicegroups')) { + $serviceGroupColumns = array_keys($this->columnMap['servicegroups']); + $selectedServiceGroupColumns = array_intersect($serviceGroupColumns, array_keys($this->columns)); + if (! empty($selectedServiceGroupColumns)) { + $group[] = 'ho.object_id'; + $group[] = 'h.host_id'; + $group[] = 'sgo.object_id'; + $group[] = 'sg.servicegroup_id'; + $groupedTables['hosts'] = true; + $groupedTables['servicegroups'] = true; + } + } + if ($this->hasJoinedVirtualTable('hostgroups')) { + $hostGroupColumns = array_keys($this->columnMap['hostgroups']); + $selectedHostGroupColumns = array_intersect($hostGroupColumns, array_keys($this->columns)); + if (! empty($selectedHostGroupColumns)) { + if (! isset($groupedTables['hosts'])) { + $group[] = 'ho.object_id'; + $group[] = 'h.host_id'; + $groupedTables['hosts'] = true; + } + $group[] = 'hgo.object_id'; + $group[] = 'hg.hostgroup_id'; + $groupedTables['hostgroups'] = true; + } + } + if (! empty($groupedTables)) { + foreach ($this->columns as $alias => $column) { + if ($column instanceof Zend_Db_Expr || $column === '(NULL)') { + continue; + } + $tableName = $this->aliasToTableName( + $this->hasAliasName($alias) ? $alias : $this->customAliasToAlias($alias) + ); + if (isset($groupedTables[$tableName])) { + continue; + } + switch ($tableName) { + case 'hoststatus': + $group[] = 'hs.hoststatus_id'; + break; + case 'serviceproblemsummary': + $group[] = 'sps.unhandled_services_count'; + break; + case 'services': + $group[] = 'so.object_id'; + $group[] = 's.service_id'; + break; + default: + continue 2; + } + $groupedTables[$tableName] = true; } } - return $group; }