2013-06-27 10:14:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Web\Widget;
|
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
2013-08-06 11:53:42 +02:00
|
|
|
use Icinga\Config\Config as IcingaConfig;;
|
|
|
|
use Icinga\Application\Logger;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
2013-08-06 18:00:33 +02:00
|
|
|
use Icinga\Web\Widget\Widget;
|
2013-06-27 10:14:15 +02:00
|
|
|
use Icinga\Web\Widget\Dashboard\Pane;
|
2013-08-06 11:53:42 +02:00
|
|
|
use Icinga\Web\Widget\Dashboard\Component as DashboardComponent;
|
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
use Icinga\Web\Url;
|
|
|
|
|
2013-08-06 18:00:33 +02:00
|
|
|
class Dashboard implements Widget
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 18:00:33 +02:00
|
|
|
/**
|
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;
|
|
|
|
private $configfile;
|
|
|
|
private $panes = array();
|
|
|
|
private $tabs;
|
|
|
|
|
|
|
|
private $url = null;
|
|
|
|
private $tabParam = 'pane';
|
2013-06-27 10:14:15 +02:00
|
|
|
|
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
public function __construct()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
|
|
|
if ($this->url === null) {
|
2013-08-06 18:00:33 +02:00
|
|
|
$this->url = Url::fromRequest()->getUrlWithout($this->tabParam);
|
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-06 11:53:42 +02:00
|
|
|
public function getTabs()
|
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-06-27 10:14:15 +02:00
|
|
|
foreach ($this->panes as $key => $pane) {
|
2013-08-06 11:53:42 +02:00
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
$this->tabs->add($key, array(
|
|
|
|
'title' => $pane->getTitle(),
|
|
|
|
'url' => clone($this->url),
|
|
|
|
'urlParams' => array($this->tabParam => $key)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isWritable()
|
|
|
|
{
|
|
|
|
return is_writable($this->configfile);
|
|
|
|
}
|
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
public function store($file = null)
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
if ($file === null) {
|
|
|
|
$file = IcingaConfig::app('dashboard/dashboard')->getConfigFile();
|
|
|
|
}
|
|
|
|
$this->configfile = $file;
|
|
|
|
if (!$this->isWritable()) {
|
|
|
|
Logger::error("Tried to persist dashboard to %s, but path is not writeable", $this->configfile);
|
|
|
|
throw new ConfigurationError('Can\'t persist dashboard');
|
|
|
|
}
|
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
if (! @file_put_contents($this->configfile, $this->toIni())) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$error = error_get_last();
|
|
|
|
if ($error == NULL) {
|
|
|
|
$error = "Unknown error";
|
|
|
|
} else {
|
|
|
|
$error = $error["message"];
|
|
|
|
}
|
|
|
|
Logger::error("Tried to persist dashboard to %s, but got error: %s", $this->configfile, $error);
|
|
|
|
throw new ConfigurationError('Can\'t persist dashboard');
|
2013-06-27 10:14:15 +02:00
|
|
|
} else {
|
|
|
|
return $this;
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
|
2013-06-27 10:14:15 +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-06 11:53:42 +02:00
|
|
|
public function createPane($title)
|
|
|
|
{
|
|
|
|
$pane = new Pane($title);
|
|
|
|
$pane->setTitle($title);
|
|
|
|
$this->addPane($pane);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-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-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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getComponentEnum()
|
|
|
|
{
|
|
|
|
$list = array();
|
|
|
|
foreach ($this->panes as $name => $pane) {
|
|
|
|
foreach ($pane->getComponents() as $component) {
|
|
|
|
$list[$name . '.' . $component->getTitle()] =
|
|
|
|
$pane->getTitle() . ': ' . $component->getTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addPane(Pane $pane)
|
|
|
|
{
|
|
|
|
$this->panes[$pane->getName()] = $pane;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPane($name)
|
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
if (!isset($this->panes[$name]))
|
|
|
|
return null;
|
2013-06-27 10:14:15 +02:00
|
|
|
return $this->panes[$name];
|
|
|
|
}
|
|
|
|
|
2013-08-06 18:00:33 +02:00
|
|
|
public function render(\Zend_View_Abstract $view)
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
|
|
|
if (empty($this->panes)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
return $this->getActivePane()->render($view);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setDefaultPane()
|
|
|
|
{
|
|
|
|
reset($this->panes);
|
|
|
|
$active = key($this->panes);
|
|
|
|
$this->activate($active);
|
|
|
|
return $active;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getActivePane()
|
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$active = $this->getTabs()->getActiveName();
|
2013-06-27 10:14:15 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->panes[$active];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toIni()
|
|
|
|
{
|
|
|
|
$ini = '';
|
|
|
|
foreach ($this->panes as $pane) {
|
|
|
|
$ini .= $pane->toIni();
|
|
|
|
}
|
|
|
|
return $ini;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadConfigPanes()
|
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$items = $this->config;
|
2013-06-27 10:14:15 +02:00
|
|
|
$app = Icinga::app();
|
2013-08-06 11:53:42 +02:00
|
|
|
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));
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
} else {
|
|
|
|
list($paneName, $title) = explode('.', $key , 2);
|
|
|
|
$pane = $this->getPane($paneName);
|
|
|
|
$pane->addComponent(DashboardComponent::fromIni($title, $item, $pane));
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
|
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|