diff --git a/library/Icinga/Web/Menu/MonitoringMenuItemRenderer.php b/library/Icinga/Web/Menu/MonitoringMenuItemRenderer.php new file mode 100644 index 000000000..072e9eaeb --- /dev/null +++ b/library/Icinga/Web/Menu/MonitoringMenuItemRenderer.php @@ -0,0 +1,90 @@ +select()->from( + 'statusSummary', + array( + 'hosts_down_unhandled', + 'services_critical_unhandled' + ) + )->getQuery()->fetchRow(); + } + + if ($column === null) { + return self::$summary; + } elseif (isset(self::$summary->$column)) { + return self::$summary->$column; + } else { + return null; + } + } + + protected function getBadgeTitle() + { + $translations = array( + 'hosts_down_unhandled' => mt('monitoring', '%d unhandled hosts down'), + 'services_critical_unhandled' => mt('monitoring', '%d unhandled services critical') + ); + + $titles = array(); + $sum = $this->summary(); + + foreach ($this->columns as $col) { + if (isset($sum->$col) && $sum->$col > 0) { + $titles[] = sprintf($translations[$col], $sum->$col); + } + } + + return implode(', ', $titles); + } + + protected function countItems() + { + $sum = self::summary(); + $count = 0; + + foreach ($this->columns as $col) { + if (isset($sum->$col)) { + $count += $sum->$col; + } + } + + return $count; + } + + public function render(Menu $menu) + { + $count = $this->countItems(); + $badge = ''; + if ($count) { + $badge = sprintf( + '
%s
', + $this->getBadgeTitle(), + $count + ); + } + return sprintf( + '%s%s%s', + $menu->getUrl() ?: '#', + $menu->getIcon() ? ' ' : '', + htmlspecialchars($menu->getTitle()), + $badge + ); + } +} diff --git a/library/Icinga/Web/Menu/ProblemMenuItemRenderer.php b/library/Icinga/Web/Menu/ProblemMenuItemRenderer.php index 1254033ae..58689547f 100644 --- a/library/Icinga/Web/Menu/ProblemMenuItemRenderer.php +++ b/library/Icinga/Web/Menu/ProblemMenuItemRenderer.php @@ -1,39 +1,11 @@ select()->from( - 'statusSummary', - array( - 'hosts_down_unhandled', - 'services_critical_unhandled' - ) - )->getQuery()->fetchRow(); - $unhandled = $statusSummary->hosts_down_unhandled + $statusSummary->services_critical_unhandled; - $badge = ''; - if ($unhandled) { - $badge = sprintf( - '
%s
', - $unhandled - ); - } - return sprintf( - '%s%s %s', - $menu->getUrl() ?: '#', - $menu->getIcon() ? ' ' : '', - htmlspecialchars($menu->getTitle()), - $badge - ); - } +class ProblemMenuItemRenderer extends MonitoringMenuItemRenderer +{ + protected $columns = array( + 'hosts_down_unhandled', + 'services_critical_unhandled' + ); } diff --git a/library/Icinga/Web/Menu/UnhandledHostMenuItemRenderer.php b/library/Icinga/Web/Menu/UnhandledHostMenuItemRenderer.php index c7e6036fb..10a75c00f 100644 --- a/library/Icinga/Web/Menu/UnhandledHostMenuItemRenderer.php +++ b/library/Icinga/Web/Menu/UnhandledHostMenuItemRenderer.php @@ -1,38 +1,12 @@ select()->from( - 'statusSummary', - array( - 'hosts_down_unhandled' - ) - )->getQuery()->fetchRow(); - $badge = ''; - if ($statusSummary->hosts_down_unhandled) { - $badge = sprintf( - '
%s
', - t(sprintf('%d unhandled host problems', $statusSummary->hosts_down_unhandled)), - $statusSummary->hosts_down_unhandled - ); - } - return sprintf( - '%s%s %s', - $menu->getUrl() ?: '#', - $menu->getIcon() ? ' ' : '', - htmlspecialchars($menu->getTitle()), - $badge - ); - } +class UnhandledHostMenuItemRenderer extends MonitoringMenuItemRenderer +{ + protected $columns = array( + 'hosts_down_unhandled', + ); } diff --git a/library/Icinga/Web/Menu/UnhandledServiceMenuItemRenderer.php b/library/Icinga/Web/Menu/UnhandledServiceMenuItemRenderer.php index b677a4935..8a59e9baa 100644 --- a/library/Icinga/Web/Menu/UnhandledServiceMenuItemRenderer.php +++ b/library/Icinga/Web/Menu/UnhandledServiceMenuItemRenderer.php @@ -1,38 +1,12 @@ select()->from( - 'statusSummary', - array( - 'services_critical_unhandled' - ) - )->getQuery()->fetchRow(); - $badge = ''; - if ($statusSummary->services_critical_unhandled) { - $badge = sprintf( - '
%s
', - t(sprintf('%d unhandled service problems', $statusSummary->services_critical_unhandled)), - $statusSummary->services_critical_unhandled - ); - } - return sprintf( - '%s%s %s', - $menu->getUrl() ?: '#', - $menu->getIcon() ? ' ' : '', - htmlspecialchars($menu->getTitle()), - $badge - ); - } +class UnhandledServiceMenuItemRenderer extends MonitoringMenuItemRenderer +{ + protected $columns = array( + 'services_critical_unhandled' + ); }