From baf8c5bbaff01f9a10412550f5d6bfc2bb8ddfcc Mon Sep 17 00:00:00 2001 From: Florian Strohmaier Date: Wed, 9 Feb 2022 12:14:38 +0100 Subject: [PATCH] Create SecondaryNav refs #4651 --- .../Icinga/Web/Navigation/SecondaryNav.php | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 library/Icinga/Web/Navigation/SecondaryNav.php diff --git a/library/Icinga/Web/Navigation/SecondaryNav.php b/library/Icinga/Web/Navigation/SecondaryNav.php new file mode 100644 index 000000000..e33db5e84 --- /dev/null +++ b/library/Icinga/Web/Navigation/SecondaryNav.php @@ -0,0 +1,187 @@ + 'nav nav-level-1' + ]; + + protected $tag = 'ul'; + + protected $children = [ + 'system' => [ + 'title' => 'System', + 'items' => [ + 'about' => [ + 'label' => 'About', + 'url' => 'about' + ], + 'health' => [ + 'label' => 'Health', + 'url' => 'health', + ], + 'announcements' => [ + 'label' => 'Announcements', + 'url' => 'announcements' + ], + 'sessions' => [ + 'label' => 'User Sessions', + 'permission' => 'application/sessions', + 'url' => 'manage-user-devices' + ] + ] + ], + 'configuration' => [ + 'title' => 'Configuration', + 'items' => [ + 'application' => [ + 'label' => 'Application', + 'url' => 'config' + ], + 'authentication' => [ + 'label' => 'Access Control', + 'permission' => 'config/access-control/*', + 'url' => 'role' + ], + 'navigation' => [ + 'label' => 'Shared Navigation', + 'permission' => 'config/navigation', + 'url' => 'authentication' + ], + 'modules' => [ + 'label' => 'Modules', + 'permission' => 'config/modules', + 'url' => 'config/modules' + ] + ] + ], + 'logout' => [ + 'items' => [ + 'logout' => [ + 'label' => 'Logout', + 'atts' => [ + 'target' => '_self', + 'class' => 'nav-item-logout' + ], + 'url' => 'authentication/logout' + ] + ] + ] + ]; + + public function getHealthCount() + { + $count = 0; + $title = null; + $worstState = null; + foreach (HealthHook::collectHealthData()->select() as $result) { + if ($worstState === null || $result->state > $worstState) { + $worstState = $result->state; + $title = $result->message; + $count = 1; + } elseif ($worstState === $result->state) { + $count++; + } + } + + switch ($worstState) { + case HealthHook::STATE_OK: + $count = 0; + break; + case HealthHook::STATE_WARNING: + $this->state = self::STATE_WARNING; + break; + case HealthHook::STATE_CRITICAL: + $this->state = self::STATE_CRITICAL; + break; + case HealthHook::STATE_UNKNOWN: + $this->state = self::STATE_UNKNOWN; + break; + } + + $this->title = $title; + + return $count; + } + + protected function createHealthBadge() + { + return $this->getHealthCount() == 0 ? '' : new StateBadge($this->getHealthCount(), 'critical'); + } + + protected function createLevel2Menu() + { + $level2Nav = HtmlElement::create('div ', Attributes::create(['class' => 'nav nav-level-2'])); + + foreach($this->children as $c) { + + if (isset($c['title'])) { + $level2Nav->add(HtmlElement::create('h3', null, + t($c['title'])) + ); + } + + $ul = HtmlElement::create('ul'); + foreach ($c['items'] as $i) { + $li = HtmlElement::create('li', + Attributes::create(isset($i['atts']) ? $i['atts'] : []), [ + HtmlElement::create('a', ['href' => $i['url']], t($i['label'])) + ]); + + $li->addAttributes(['class' => 'nav-item']); + + $ul->add($li); + } + $level2Nav->add($ul); + } + + return $level2Nav; + } + + protected function assemble() + { + $username = Auth::getInstance()->getUser()->getUsername(); + + $htmlString = '' + . new HtmlElement( + 'i', + Attributes::create(['class' => 'user-ball']), + Text::create($username[0]) + ) + . $username + . '' + + . '' + . new Icon('cog') + . $this->createHealthBadge() + . '' + . $this->createLevel2Menu(); + + $this->add( + HtmlElement::create('li', ['class' => 'nav-item segmented-nav-item'], + new HtmlString($htmlString) + ) + ); + } +}