From 42e2b3435699e7f6452304a0679b4954bdc7a6b0 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Tue, 26 Aug 2014 10:13:49 +0200 Subject: [PATCH] Implement self provided configuration for dashboard/dashlets in modules refs #6639 --- library/Icinga/Application/Modules/Module.php | 31 ++++++++++++ library/Icinga/Web/Widget/Dashboard.php | 50 +++++++++++++++++++ library/Icinga/Web/Widget/Dashboard/Pane.php | 41 +++++++++++++++ 3 files changed, 122 insertions(+) diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php index a4b87be08..a325dfef0 100644 --- a/library/Icinga/Application/Modules/Module.php +++ b/library/Icinga/Application/Modules/Module.php @@ -16,6 +16,7 @@ use Icinga\Util\Translator; use Icinga\Web\Hook; use Icinga\Web\Menu; use Icinga\Web\Widget; +use Icinga\Web\Widget\Dashboard\Pane; use Icinga\Util\File; use Icinga\Exception\ProgrammingError; @@ -154,6 +155,36 @@ class Module */ protected $menuItems = array(); + /** + * A set of Pane elements + * + * @var array + */ + protected $paneItems = array(); + + /** + * Get all Menu Items + * + * @return array + */ + public function getPaneItems() + { + $this->launchConfigScript(); + return $this->paneItems; + } + + /** + * Add a pane to dashboard + * + * @param $name + * @return Pane + */ + protected function dashboard($name) + { + $this->paneItems[$name] = new Pane($name); + return $this->paneItems[$name]; + } + /** * Get all Menu Items * diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php index c87afa1ab..f565c0fca 100644 --- a/library/Icinga/Web/Widget/Dashboard.php +++ b/library/Icinga/Web/Widget/Dashboard.php @@ -63,6 +63,46 @@ class Dashboard extends AbstractWidget $this->getTabs()->activate($name); } + /** + * Load Pane items provided by all enabled modules + * + * @return self + */ + public static function load() + { + /** @var $dashboard Dashboard */ + $dashboard = new static('dashboard'); + $manager = Icinga::app()->getModuleManager(); + foreach ($manager->getLoadedModules() as $module) { + /** @var $module \Icinga\Application\Modules\Module */ + $dashboard->mergePanes($module->getPaneItems()); + + } + return $dashboard; + } + + /** + * Merge panes with existing panes + * + * @param array $panes + * @return $this + */ + public function mergePanes(array $panes) + { + /** @var $pane Pane */ + foreach ($panes as $pane) { + if (array_key_exists($pane->getName(), $this->panes)) { + /** @var $current Pane */ + $current = $this->panes[$pane->getName()]; + $current->addComponents($pane->getComponents()); + } else { + $this->panes = array_filter(array_merge($this->panes, $panes)); + } + } + + return $this; + } + /** * Return the tab object used to navigate through this dashboard * @@ -147,6 +187,16 @@ class Dashboard extends AbstractWidget return $this; } + /** + * Checks if the current dashboard has any panes + * + * @return bool + */ + public function hasPanes() + { + return ! empty($this->panes); + } + /** * Return true if a pane doesn't exist or doesn't have any components in it * diff --git a/library/Icinga/Web/Widget/Dashboard/Pane.php b/library/Icinga/Web/Widget/Dashboard/Pane.php index 5e3e8ffea..15cf56524 100644 --- a/library/Icinga/Web/Widget/Dashboard/Pane.php +++ b/library/Icinga/Web/Widget/Dashboard/Pane.php @@ -162,6 +162,47 @@ class Pane extends AbstractWidget return $this; } + /** + * Add new components to existing components + * + * @param array $components + * @return $this + */ + public function addComponents(array $components) + { + /* @var $component Component */ + foreach ($components as $component) { + if (array_key_exists($component->getTitle(), $this->components)) { + if (preg_match('/_(\d+)$/', $component->getTitle(), $m)) { + $name = preg_replace('/_\d+$/', $m[1]++, $component->getTitle()); + } else { + $name = $component->getTitle() . '_2'; + } + $this->components[$name] = $component; + } else { + $this->components[$component->getTitle()] = $component; + } + } + + return $this; + } + + /** + * Add a component to the current pane + * + * @param $title + * @param $url + * @return Component + * + * @see addComponent() + */ + public function add($title, $url = null) + { + $this->addComponent($title, $url); + + return $this->components[$title]; + } + /** * Return the this pane's structure as array *