2013-08-06 11:53:42 +02:00
|
|
|
<?php
|
2013-08-08 17:42:34 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-08-06 11:53:42 +02:00
|
|
|
|
2014-04-29 11:30:34 +02:00
|
|
|
use \Zend_Config;
|
2014-03-03 18:16:07 +01:00
|
|
|
use Icinga\Web\Url;
|
2014-04-29 11:30:34 +02:00
|
|
|
use Icinga\Logger\Logger;
|
|
|
|
use Icinga\Config\PreservingIniWriter;
|
2014-03-03 18:16:07 +01:00
|
|
|
use Icinga\Application\Config as IcingaConfig;
|
2014-04-29 11:30:34 +02:00
|
|
|
use Icinga\Web\Widget\Dashboard;
|
2014-03-03 18:16:07 +01:00
|
|
|
use Icinga\Form\Dashboard\AddUrlForm;
|
|
|
|
use Icinga\Exception\NotReadableError;
|
2014-04-29 11:30:34 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
use Icinga\Web\Controller\ActionController;
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle creation, removal and displaying of dashboards, panes and components
|
|
|
|
*
|
|
|
|
* @see Icinga\Web\Widget\Dashboard for more information about dashboards
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
class DashboardController extends ActionController
|
|
|
|
{
|
2014-06-05 15:20:54 +02:00
|
|
|
/**
|
|
|
|
* Default configuration
|
|
|
|
*/
|
|
|
|
const DEFAULT_CONFIG = 'dashboard/dashboard';
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Retrieve a dashboard from the provided config
|
|
|
|
*
|
2013-08-16 14:56:23 +02:00
|
|
|
* @param string $config The config to read the dashboard from, or 'dashboard/dashboard' if none is given
|
2013-08-07 17:44:18 +02:00
|
|
|
*
|
2013-08-16 14:56:23 +02:00
|
|
|
* @return \Icinga\Web\Widget\Dashboard
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2014-06-05 15:20:54 +02:00
|
|
|
private function getDashboard($config = self::DEFAULT_CONFIG)
|
2013-08-06 11:53:42 +02:00
|
|
|
{
|
|
|
|
$dashboard = new Dashboard();
|
2014-03-03 18:16:07 +01:00
|
|
|
try {
|
|
|
|
$dashboardConfig = IcingaConfig::app($config);
|
2014-06-05 15:20:54 +02:00
|
|
|
if (count($dashboardConfig) === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-03 18:16:07 +01:00
|
|
|
$dashboard->readConfig($dashboardConfig);
|
|
|
|
} catch (NotReadableError $e) {
|
2014-03-03 19:03:39 +01:00
|
|
|
Logger::error(new Exception('Cannot load dashboard configuration. An exception was thrown:', 0, $e));
|
2014-06-05 15:20:54 +02:00
|
|
|
return null;
|
2014-03-03 18:16:07 +01:00
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
return $dashboard;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Remove a component from the pane identified by the 'pane' parameter
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function removecomponentAction()
|
|
|
|
{
|
|
|
|
$pane = $this->_getParam('pane');
|
|
|
|
$dashboard = $this->getDashboard();
|
2013-08-07 17:40:18 +02:00
|
|
|
try {
|
|
|
|
$dashboard->removeComponent(
|
|
|
|
$pane,
|
|
|
|
$this->_getParam('component')
|
|
|
|
)->store();
|
|
|
|
$this->redirectNow(Url::fromPath('dashboard', array('pane' => $pane)));
|
2013-08-07 18:10:39 +02:00
|
|
|
} catch (ConfigurationError $exc ) {
|
2013-08-07 17:40:18 +02:00
|
|
|
$this->_helper->viewRenderer('show_configuration');
|
|
|
|
$this->view->exceptionMessage = $exc->getMessage();
|
|
|
|
$this->view->iniConfigurationString = $dashboard->toIni();
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the form for adding new components or add the new component if submitted
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function addurlAction()
|
|
|
|
{
|
2014-04-29 11:30:34 +02:00
|
|
|
$this->getTabs()->add(
|
|
|
|
'addurl',
|
|
|
|
array(
|
|
|
|
'title' => 'Add Dashboard URL',
|
|
|
|
'url' => Url::fromRequest()
|
|
|
|
)
|
|
|
|
)->activate('addurl');
|
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
$form = new AddUrlForm();
|
2014-04-29 11:30:34 +02:00
|
|
|
$form->setRequest($this->getRequest());
|
|
|
|
$form->setAction(Url::fromRequest()->setParams(array())->getAbsoluteUrl());
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->view->form = $form;
|
2014-04-29 11:30:34 +02:00
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
if ($form->isSubmittedAndValid()) {
|
|
|
|
$dashboard = $this->getDashboard();
|
|
|
|
$dashboard->setComponentUrl(
|
|
|
|
$form->getValue('pane'),
|
|
|
|
$form->getValue('component'),
|
|
|
|
ltrim($form->getValue('url'), '/')
|
|
|
|
);
|
2014-04-29 11:30:34 +02:00
|
|
|
|
|
|
|
$configFile = IcingaConfig::app('dashboard/dashboard')->getConfigFile();
|
|
|
|
if ($this->writeConfiguration(new Zend_Config($dashboard->toArray()), $configFile)) {
|
|
|
|
$this->redirectNow(Url::fromPath('dashboard', array('pane' => $form->getValue('pane'))));
|
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
2013-08-07 17:44:18 +02:00
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Display the dashboard with the pane set in the 'pane' request parameter
|
|
|
|
*
|
|
|
|
* If no pane is submitted or the submitted one doesn't exist, the default pane is
|
|
|
|
* displayed (normally the first one)
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function indexAction()
|
|
|
|
{
|
|
|
|
$dashboard = $this->getDashboard();
|
2013-08-08 17:42:34 +02:00
|
|
|
if ($this->_getParam('pane')) {
|
|
|
|
$pane = $this->_getParam('pane');
|
|
|
|
$dashboard->activate($pane);
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2014-06-05 15:20:54 +02:00
|
|
|
|
2014-06-05 16:07:40 +02:00
|
|
|
$this->view->configPath = IcingaConfig::resolvePath(self::DEFAULT_CONFIG);
|
2014-06-05 15:20:54 +02:00
|
|
|
|
|
|
|
if ($dashboard === null) {
|
|
|
|
$this->view->title = 'Dashboard';
|
|
|
|
} else {
|
|
|
|
$this->view->title = $dashboard->getActivePane()->getTitle() . ' :: Dashboard';
|
|
|
|
$this->view->tabs = $dashboard->getTabs();
|
|
|
|
|
|
|
|
/* Temporarily removed
|
|
|
|
$this->view->tabs->add(
|
|
|
|
'Add',
|
|
|
|
array(
|
|
|
|
'title' => '+',
|
|
|
|
'url' => Url::fromPath('dashboard/addurl')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
|
|
|
$this->view->dashboard = $dashboard;
|
|
|
|
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2014-04-29 11:30:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the given configuration as INI file
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config The configuration to store
|
|
|
|
* @param string $target The path where to store the configuration
|
|
|
|
*
|
|
|
|
* @return bool Whether the configuartion has been successfully stored
|
|
|
|
*/
|
|
|
|
protected function writeConfiguration(Zend_Config $config, $target)
|
|
|
|
{
|
|
|
|
$writer = new PreservingIniWriter(array('config' => $config, 'filename' => $target));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$writer->write();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Logger::error(new ConfiguationError("Cannot write dashboard to $target", 0, $e));
|
|
|
|
$this->view->iniConfigurationString = $writer->render();
|
|
|
|
$this->view->exceptionMessage = $e->getMessage();
|
|
|
|
$this->view->file = $target;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|