2013-06-27 10:14:15 +02:00
|
|
|
<?php
|
2013-08-08 17:42:34 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-27 10:14:15 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web\Widget;
|
|
|
|
|
2014-02-18 19:11:44 +01:00
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
use Icinga\Application\Config as IcingaConfig;
|
2014-06-05 16:07:40 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-03-04 12:18:28 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2014-02-18 19:11:44 +01:00
|
|
|
use Icinga\Web\Widget\AbstractWidget;
|
|
|
|
use Icinga\Web\Widget\Dashboard\Pane;
|
|
|
|
use Icinga\Web\Widget\Dashboard\Component as DashboardComponent;
|
|
|
|
use Icinga\Web\Url;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Dashboards display multiple views on a single page
|
|
|
|
*
|
|
|
|
* The terminology is as follows:
|
|
|
|
* - Component: A single view showing a specific url
|
|
|
|
* - Pane: Aggregates one or more components on one page, displays it's title as a tab
|
|
|
|
* - Dashboard: Shows all panes
|
|
|
|
*
|
|
|
|
*/
|
2014-02-18 19:53:56 +01:00
|
|
|
class Dashboard extends AbstractWidget
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 18:00:33 +02:00
|
|
|
/**
|
2013-08-07 17:44:18 +02:00
|
|
|
* The configuration containing information about this dashboard
|
|
|
|
*
|
2013-08-06 11:53:42 +02:00
|
|
|
* @var IcingaConfig;
|
2013-08-06 18:00:33 +02:00
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $config;
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array containing all panes of this dashboard
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $panes = array();
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The @see Icinga\Web\Widget\Tabs object for displaying displayable panes
|
|
|
|
*
|
|
|
|
* @var Tabs
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $tabs;
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* The parameter that will be added to identify panes
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $tabParam = 'pane';
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Set the given tab name as active.
|
|
|
|
*
|
|
|
|
* @param string $name The tab name to activate
|
|
|
|
*
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function activate($name)
|
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->getTabs()->activate($name);
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the tab object used to navigate through this dashboard
|
|
|
|
*
|
|
|
|
* @return Tabs
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function getTabs()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-07 17:44:18 +02:00
|
|
|
$url = Url::fromRequest()->getUrlWithout($this->tabParam);
|
2013-06-27 10:14:15 +02:00
|
|
|
if ($this->tabs === null) {
|
2013-08-06 18:00:33 +02:00
|
|
|
$this->tabs = new Tabs();
|
2013-08-06 11:53:42 +02:00
|
|
|
|
2013-08-07 17:40:18 +02:00
|
|
|
foreach ($this->panes as $key => $pane) {
|
2013-08-07 18:10:39 +02:00
|
|
|
$this->tabs->add(
|
|
|
|
$key,
|
|
|
|
array(
|
|
|
|
'title' => $pane->getTitle(),
|
|
|
|
'url' => clone($url),
|
|
|
|
'urlParams' => array($this->tabParam => $key)
|
|
|
|
)
|
|
|
|
);
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->tabs;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Populate this dashboard via the given configuration file
|
|
|
|
*
|
|
|
|
* @param IcingaConfig $config The configuration file to populate this dashboard with
|
|
|
|
*
|
2013-08-09 10:32:57 +02:00
|
|
|
* @return self
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function readConfig(IcingaConfig $config)
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->panes = array();
|
|
|
|
$this->loadConfigPanes();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Creates a new empty pane with the given title
|
|
|
|
*
|
2013-08-08 17:42:34 +02:00
|
|
|
* @param string $title
|
|
|
|
*
|
2013-08-09 10:32:57 +02:00
|
|
|
* @return self
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function createPane($title)
|
|
|
|
{
|
|
|
|
$pane = new Pane($title);
|
|
|
|
$pane->setTitle($title);
|
|
|
|
$this->addPane($pane);
|
2013-08-08 17:42:34 +02:00
|
|
|
|
|
|
|
return $this;
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Update or adds a new component with the given url to a pane
|
|
|
|
*
|
|
|
|
* @TODO: Should only allow component objects to be added directly as soon as we store more information
|
|
|
|
*
|
|
|
|
* @param string $pane The pane to add the component to
|
|
|
|
* @param Component|string $component The component to add or the title of the newly created component
|
|
|
|
* @param $url The url to use for the component
|
|
|
|
*
|
2013-08-09 10:32:57 +02:00
|
|
|
* @return self
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function setComponentUrl($pane, $component, $url)
|
|
|
|
{
|
|
|
|
if ($component === null && strpos($pane, '.')) {
|
|
|
|
list($pane, $component) = preg_split('~\.~', $pane, 2);
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
if (!isset($this->panes[$pane])) {
|
|
|
|
$this->createPane($pane);
|
|
|
|
}
|
2013-06-27 10:14:15 +02:00
|
|
|
$pane = $this->getPane($pane);
|
|
|
|
if ($pane->hasComponent($component)) {
|
|
|
|
$pane->getComponent($component)->setUrl($url);
|
|
|
|
} else {
|
|
|
|
$pane->addComponent($component, $url);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return true if a pane doesn't exist or doesn't have any components in it
|
|
|
|
*
|
|
|
|
* @param string $pane The name of the pane to check for emptyness
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function isEmptyPane($pane)
|
|
|
|
{
|
|
|
|
$paneObj = $this->getPane($pane);
|
|
|
|
if ($paneObj === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$cmps = $paneObj->getComponents();
|
|
|
|
return !empty($cmps);
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Remove a component $component from the given pane
|
|
|
|
*
|
|
|
|
* @param string $pane The pane to remove the component from
|
|
|
|
* @param Component|string $component The component to remove or it's name
|
|
|
|
*
|
2013-08-09 10:32:57 +02:00
|
|
|
* @return self
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function removeComponent($pane, $component)
|
|
|
|
{
|
|
|
|
if ($component === null && strpos($pane, '.')) {
|
|
|
|
list($pane, $component) = preg_split('~\.~', $pane, 2);
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
$pane = $this->getPane($pane);
|
|
|
|
if ($pane !== null) {
|
|
|
|
$pane->removeComponent($component);
|
|
|
|
}
|
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return an array with pane name=>title format used for comboboxes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function getPaneKeyTitleArray()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
|
|
|
$list = array();
|
|
|
|
foreach ($this->panes as $name => $pane) {
|
|
|
|
$list[$name] = $pane->getTitle();
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Add a pane object to this dashboard
|
|
|
|
*
|
|
|
|
* @param Pane $pane The pane to add
|
|
|
|
*
|
2013-08-09 10:32:57 +02:00
|
|
|
* @return self
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function addPane(Pane $pane)
|
|
|
|
{
|
|
|
|
$this->panes[$pane->getName()] = $pane;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
2014-03-04 12:18:28 +01:00
|
|
|
* Return the pane with the provided name
|
2013-08-07 17:44:18 +02:00
|
|
|
*
|
|
|
|
* @param string $name The name of the pane to return
|
|
|
|
*
|
2014-03-04 12:18:28 +01:00
|
|
|
* @return Pane The pane or null if no pane with the given name exists
|
|
|
|
* @throws ProgrammingError
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function getPane($name)
|
|
|
|
{
|
2014-03-04 12:18:28 +01:00
|
|
|
if (! array_key_exists($name, $this->panes)) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
sprintf('Trying to retrieve invalid dashboard pane "%s"', $name)
|
|
|
|
);
|
2013-08-07 17:40:18 +02:00
|
|
|
}
|
2013-06-27 10:14:15 +02:00
|
|
|
return $this->panes[$name];
|
|
|
|
}
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Icinga\Web\Widget::render
|
|
|
|
*/
|
2014-03-17 17:04:09 +01:00
|
|
|
public function render()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
|
|
|
if (empty($this->panes)) {
|
|
|
|
return '';
|
|
|
|
}
|
2014-03-17 17:04:09 +01:00
|
|
|
return $this->determineActivePane()->render();
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Activates the default pane of this dashboard and returns it's name
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private function setDefaultPane()
|
|
|
|
{
|
|
|
|
reset($this->panes);
|
|
|
|
$active = key($this->panes);
|
|
|
|
$this->activate($active);
|
|
|
|
return $active;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
|
2014-03-04 12:18:28 +01:00
|
|
|
public function getActivePane()
|
|
|
|
{
|
|
|
|
if ($active = $this->getTabs()->getActiveName()) {
|
|
|
|
return $this->getPane($active);
|
|
|
|
}
|
|
|
|
return $this->determineActivePane();
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Determine the active pane either by the selected tab or the current request
|
|
|
|
*
|
|
|
|
* @return Pane The currently active pane
|
|
|
|
*/
|
|
|
|
public function determineActivePane()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$active = $this->getTabs()->getActiveName();
|
2014-02-18 19:11:44 +01:00
|
|
|
if (! $active) {
|
2013-07-31 10:59:34 +02:00
|
|
|
if ($active = Url::fromRequest()->getParam($this->tabParam)) {
|
2013-08-06 11:53:42 +02:00
|
|
|
if ($this->isEmptyPane($active)) {
|
|
|
|
$active = $this->setDefaultPane();
|
|
|
|
} else {
|
|
|
|
$this->activate($active);
|
|
|
|
}
|
2013-06-27 10:14:15 +02:00
|
|
|
} else {
|
2013-08-06 11:53:42 +02:00
|
|
|
$active = $this->setDefaultPane();
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 16:07:40 +02:00
|
|
|
|
|
|
|
if (isset($this->panes[$active])) {
|
|
|
|
return $this->panes[$active];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ConfigurationError('Could not determine active pane');
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
2014-04-29 11:30:34 +02:00
|
|
|
* Return this dashboard's structure as array
|
2013-08-07 17:44:18 +02:00
|
|
|
*
|
2014-04-29 11:30:34 +02:00
|
|
|
* @return array
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2014-04-29 11:30:34 +02:00
|
|
|
public function toArray()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2014-04-29 11:30:34 +02:00
|
|
|
$array = array();
|
2013-06-27 10:14:15 +02:00
|
|
|
foreach ($this->panes as $pane) {
|
2014-04-29 11:30:34 +02:00
|
|
|
$array += $pane->toArray();
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
2014-04-29 11:30:34 +02:00
|
|
|
|
|
|
|
return $array;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all config panes from @see Dashboard::$config
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function loadConfigPanes()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$items = $this->config;
|
|
|
|
foreach ($items->keys() as $key) {
|
|
|
|
$item = $this->config->get($key, false);
|
2013-06-27 10:14:15 +02:00
|
|
|
if (false === strstr($key, '.')) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->addPane(Pane::fromIni($key, $item));
|
|
|
|
} else {
|
2013-08-07 17:44:18 +02:00
|
|
|
list($paneName, $title) = explode('.', $key, 2);
|
2013-08-06 11:53:42 +02:00
|
|
|
$pane = $this->getPane($paneName);
|
|
|
|
$pane->addComponent(DashboardComponent::fromIni($title, $item, $pane));
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|