Implement self provided configuration for dashboard/dashlets in modules

refs #6639
This commit is contained in:
Alexander Fuhr 2014-08-26 10:13:49 +02:00
parent bae5ba2352
commit 42e2b34356
3 changed files with 122 additions and 0 deletions

View File

@ -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
*

View File

@ -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
*

View File

@ -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
*