Module: Use the new dashbaord widget for providing dashbaords and dashlets

This commit is contained in:
Yonas Habteab 2022-03-11 16:36:42 +01:00
parent dab899e1be
commit b42fcd96f5

View File

@ -13,11 +13,14 @@ use Icinga\Exception\IcingaException;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Module\Setup\SetupWizard; use Icinga\Module\Setup\SetupWizard;
use Icinga\Util\File; use Icinga\Util\File;
use Icinga\Web\Dashboard;
use Icinga\Web\Navigation\Navigation; use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Widget;
use ipl\I18n\GettextTranslator; use ipl\I18n\GettextTranslator;
use ipl\I18n\StaticTranslator; use ipl\I18n\StaticTranslator;
use ipl\I18n\Translation; use ipl\I18n\Translation;
use ipl\Web\Url;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Zend_Controller_Router_Route; use Zend_Controller_Router_Route;
use Zend_Controller_Router_Route_Abstract; use Zend_Controller_Router_Route_Abstract;
use Zend_Controller_Router_Route_Regex; use Zend_Controller_Router_Route_Regex;
@ -214,9 +217,16 @@ class Module
/** /**
* A set of Pane elements * A set of Pane elements
* *
* @var array * @var Dashboard\Pane[]
*/ */
protected $paneItems = array(); protected $paneItems = [];
/**
* A set of Dashlet elements
*
* @var Dashboard\Dashlet[]
*/
protected $dashletItems = [];
/** /**
* A set of objects representing a searchUrl configuration * A set of objects representing a searchUrl configuration
@ -307,49 +317,38 @@ class Module
/** /**
* Return this module's dashboard * Return this module's dashboard
* *
* @return Navigation * @return Dashboard\Pane[]
*/ */
public function getDashboard() public function getDashboard()
{ {
$this->launchConfigScript(); $this->launchConfigScript();
return $this->createDashboard($this->paneItems); return $this->paneItems;
} }
/** /**
* Create and return a new navigation for the given dashboard panes * Create and return a new navigation for the given dashboard panes
* *
* @param DashboardContainer[] $panes * @param DashboardContainer[] $paneItems
* *
* @return Navigation * @return DashboardContainer[]
*/ */
public function createDashboard(array $panes) public function createDashboard(array $paneItems)
{ {
$navigation = new Navigation(); $panes = [];
foreach ($panes as $pane) { foreach ($paneItems as $pane) {
/** @var DashboardContainer $pane */
$dashlets = []; $dashlets = [];
foreach ($pane->getDashlets() as $dashletName => $dashletConfig) { foreach ($pane->getDashlets() as $dashletName => $dashletConfig) {
$dashlets[$dashletName] = [ $dashlet = new Dashboard\Dashlet($dashletName, $dashletConfig['url']);
'label' => $this->translate($dashletName), $dashlet->fromArray($dashletConfig);
'url' => $dashletConfig['url'],
'priority' => $dashletConfig['priority'] $dashlets[$dashletName] = $dashlet;
];
} }
$navigation->addItem( $pane->setDashlets($dashlets);
$pane->getName(), $panes[$pane->getName()] = $pane;
array_merge(
$pane->getProperties(),
array(
'label' => $this->translate($pane->getName()),
'type' => 'dashboard-pane',
'children' => $dashlets
)
)
);
} }
return $navigation; return $panes;
} }
/** /**
@ -358,19 +357,50 @@ class Module
* @param string $name * @param string $name
* @param array $properties * @param array $properties
* *
* @return DashboardContainer * @return Dashboard\Pane
*/ */
protected function dashboard($name, array $properties = array()) protected function dashboard($name, array $properties = array())
{ {
if (array_key_exists($name, $this->paneItems)) { if (array_key_exists($name, $this->paneItems)) {
$this->paneItems[$name]->setProperties($properties); $this->paneItems[$name]->fromArray($properties);
} else { } else {
$this->paneItems[$name] = new DashboardContainer($name, $properties); $this->paneItems[$name] = new Dashboard\Pane($name, $properties);
} }
return $this->paneItems[$name]; return $this->paneItems[$name];
} }
/**
* Get this module's provided dashlets
*
* @return Dashboard\Dashlet[]
*/
public function getDashlet()
{
$this->launchConfigScript();
return $this->dashletItems;
}
/**
* Add or get a dashlet
*
* @param string $name
* @param Url|string $url
* @param string $description
*
* @return Dashboard\Dashlet
*/
protected function provideDashlet($name, $url, $description)
{
if (! array_key_exists($name, $this->dashletItems)) {
$this->dashletItems[$name] = new Dashboard\Dashlet($name, $url);
}
$this->dashletItems[$name]->setDescription($description);
return $this->dashletItems[$name];
}
/** /**
* Return this module's menu * Return this module's menu
* *