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-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
2014-11-18 12:51:28 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
use Icinga\Forms\ConfirmRemovalForm;
|
|
|
|
use Icinga\Forms\Dashboard\ComponentForm;
|
|
|
|
use Icinga\Web\Form;
|
2014-11-18 09:50:10 +01:00
|
|
|
use Icinga\Web\Notification;
|
2014-10-31 10:54:53 +01:00
|
|
|
use Icinga\Web\Controller\ActionController;
|
2014-11-18 12:51:28 +01:00
|
|
|
use Icinga\Web\Request;
|
2014-10-31 10:54:53 +01:00
|
|
|
use Icinga\Web\Url;
|
|
|
|
use Icinga\Web\Widget\Dashboard;
|
2014-11-18 09:50:10 +01:00
|
|
|
use Icinga\Web\Widget\Tabextension\DashboardSettings;
|
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-11-18 09:50:10 +01:00
|
|
|
/**
|
|
|
|
* @var Dashboard;
|
|
|
|
*/
|
|
|
|
private $dashboard;
|
|
|
|
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->dashboard = new Dashboard();
|
|
|
|
$this->dashboard->setUser($this->getRequest()->getUser());
|
|
|
|
$this->dashboard->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function newComponentAction()
|
|
|
|
{
|
|
|
|
$form = new ComponentForm();
|
|
|
|
$this->createTabs();
|
2014-11-18 12:51:28 +01:00
|
|
|
$dashboard = $this->dashboard;
|
2014-11-18 09:50:10 +01:00
|
|
|
$form->setDashboard($dashboard);
|
2014-11-18 12:51:28 +01:00
|
|
|
if ($this->_request->getParam('url')) {
|
|
|
|
$params = $this->_request->getParams();
|
|
|
|
$params['url'] = rawurldecode($this->_request->getParam('url'));
|
|
|
|
$form->populate($params);
|
|
|
|
}
|
|
|
|
$form->setOnSuccess(function (Request $request, Form $form) use ($dashboard) {
|
|
|
|
try {
|
|
|
|
$pane = $dashboard->getPane($form->getValue('pane'));
|
|
|
|
} catch (ProgrammingError $e) {
|
|
|
|
$pane = new Dashboard\Pane($form->getValue('pane'));
|
|
|
|
$pane->setUserWidget();
|
|
|
|
$dashboard->addPane($pane);
|
|
|
|
}
|
|
|
|
$component = new Dashboard\Component($form->getValue('component'), $form->getValue('url'), $pane);
|
|
|
|
$component->setUserWidget();
|
|
|
|
$pane->addComponent($component);
|
|
|
|
$dashboard->write();
|
|
|
|
Notification::success(t('Component created'));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
$form->setRedirectUrl('dashboard');
|
2014-11-18 09:50:10 +01:00
|
|
|
$form->handleRequest();
|
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateComponentAction()
|
|
|
|
{
|
|
|
|
$this->createTabs();
|
|
|
|
$dashboard = $this->dashboard;
|
|
|
|
$form = new ComponentForm();
|
|
|
|
$form->setDashboard($dashboard);
|
|
|
|
$form->setSubmitLabel(t('Update Component'));
|
|
|
|
if (! $this->_request->getParam('pane')) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
'Missing parameter "pane"',
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (! $this->_request->getParam('component')) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
'Missing parameter "component"',
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
2014-11-18 12:51:28 +01:00
|
|
|
$form->setOnSuccess(function (Request $request, Form $form) use ($dashboard) {
|
2014-11-18 09:50:10 +01:00
|
|
|
$pane = $dashboard->getPane($form->getValue('pane'));
|
|
|
|
try {
|
|
|
|
$component = $pane->getComponent($form->getValue('component'));
|
|
|
|
$component->setUrl($form->getValue('url'));
|
2014-11-18 12:51:28 +01:00
|
|
|
} catch (ProgrammingError $e) {
|
2014-11-18 09:50:10 +01:00
|
|
|
$component = new Dashboard\Component($form->getValue('component'), $form->getValue('url'), $pane);
|
|
|
|
$pane->addComponent($component);
|
|
|
|
}
|
|
|
|
$component->setUserWidget();
|
|
|
|
// Rename component
|
|
|
|
if ($form->getValue('org_component') && $form->getValue('org_component') !== $component->getTitle()) {
|
|
|
|
$pane->removeComponent($form->getValue('org_component'));
|
|
|
|
}
|
|
|
|
$dashboard->write();
|
2014-11-18 12:51:28 +01:00
|
|
|
$dashboard->write();
|
2014-11-18 09:50:10 +01:00
|
|
|
Notification::success(t('Component updated'));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
$form->setRedirectUrl('dashboard/settings');
|
|
|
|
$form->handleRequest();
|
|
|
|
$pane = $dashboard->getPane($this->getParam('pane'));
|
|
|
|
$component = $pane->getComponent($this->getParam('component'));
|
|
|
|
$form->load($component);
|
|
|
|
|
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
2014-11-18 12:51:28 +01:00
|
|
|
public function removeComponentAction()
|
2014-11-18 09:50:10 +01:00
|
|
|
{
|
2014-11-18 12:51:28 +01:00
|
|
|
$form = new ConfirmRemovalForm();
|
2014-11-18 09:50:10 +01:00
|
|
|
$this->createTabs();
|
2014-11-18 12:51:28 +01:00
|
|
|
$dashboard = $this->dashboard;
|
|
|
|
if (! $this->_request->getParam('pane')) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
'Missing parameter "pane"',
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (! $this->_request->getParam('component')) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
'Missing parameter "component"',
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$pane = $this->_request->getParam('pane');
|
|
|
|
$component = $this->_request->getParam('component');
|
|
|
|
$form->setOnSuccess(function (Request $request, Form $form) use ($dashboard, $component, $pane) {
|
|
|
|
try {
|
|
|
|
$pane = $dashboard->getPane($pane);
|
|
|
|
$pane->removeComponent($component);
|
|
|
|
$dashboard->write();
|
|
|
|
Notification::success(t('Component has been removed from') . ' ' . $pane->getTitle());
|
|
|
|
return true;
|
|
|
|
} catch (ProgrammingError $e) {
|
|
|
|
Notification::error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$form->setRedirectUrl('dashboard/settings');
|
2014-11-11 11:51:18 +01:00
|
|
|
$form->handleRequest();
|
2014-11-18 12:51:28 +01:00
|
|
|
$this->view->pane = $pane;
|
|
|
|
$this->view->component = $component;
|
2014-08-12 09:49:27 +02:00
|
|
|
$this->view->form = $form;
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 16:28:04 +01:00
|
|
|
public function removePaneAction()
|
|
|
|
{
|
|
|
|
$form = new ConfirmRemovalForm();
|
|
|
|
$this->createTabs();
|
|
|
|
$dashboard = $this->dashboard;
|
|
|
|
if (! $this->_request->getParam('pane')) {
|
|
|
|
throw new Zend_Controller_Action_Exception(
|
|
|
|
'Missing parameter "pane"',
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$pane = $this->_request->getParam('pane');
|
|
|
|
$form->setOnSuccess(function (Request $request, Form $form) use ($dashboard, $pane) {
|
|
|
|
try {
|
|
|
|
$pane = $dashboard->getPane($pane);
|
|
|
|
$dashboard->removePane($pane->getTitle());
|
|
|
|
$dashboard->write();
|
|
|
|
Notification::success(t('Pane has been removed') . ': ' . $pane->getTitle());
|
|
|
|
return true;
|
|
|
|
} catch (ProgrammingError $e) {
|
|
|
|
Notification::error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$form->setRedirectUrl('dashboard/settings');
|
|
|
|
$form->handleRequest();
|
|
|
|
$this->view->pane = $pane;
|
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2014-11-18 09:50:10 +01:00
|
|
|
$this->createTabs();
|
|
|
|
if (! $this->dashboard->hasPanes()) {
|
2014-06-05 15:20:54 +02:00
|
|
|
$this->view->title = 'Dashboard';
|
|
|
|
} else {
|
2014-08-26 10:08:33 +02:00
|
|
|
if ($this->_getParam('pane')) {
|
|
|
|
$pane = $this->_getParam('pane');
|
2014-11-18 09:50:10 +01:00
|
|
|
$this->dashboard->activate($pane);
|
2014-08-26 10:08:33 +02:00
|
|
|
}
|
2014-11-18 09:50:10 +01:00
|
|
|
if ($this->dashboard === null) {
|
2014-08-26 10:08:33 +02:00
|
|
|
$this->view->title = 'Dashboard';
|
|
|
|
} else {
|
2014-11-18 09:50:10 +01:00
|
|
|
$this->view->title = $this->dashboard->getActivePane()->getTitle() . ' :: Dashboard';
|
2014-11-11 16:35:35 +01:00
|
|
|
if ($this->hasParam('remove')) {
|
2014-11-18 09:50:10 +01:00
|
|
|
$this->dashboard->getActivePane()->removeComponent($this->getParam('remove'));
|
|
|
|
$this->dashboard->write();
|
2014-11-11 16:35:35 +01:00
|
|
|
$this->redirectNow(URL::fromRequest()->remove('remove'));
|
|
|
|
}
|
2014-11-18 12:51:28 +01:00
|
|
|
$this->view->tabs->add(
|
2014-06-05 15:20:54 +02:00
|
|
|
'Add',
|
|
|
|
array(
|
|
|
|
'title' => '+',
|
2014-11-18 12:51:28 +01:00
|
|
|
'url' => Url::fromPath('dashboard/new-component')
|
2014-06-05 15:20:54 +02:00
|
|
|
)
|
2014-11-18 12:51:28 +01:00
|
|
|
);
|
2014-11-18 09:50:10 +01:00
|
|
|
$this->view->dashboard = $this->dashboard;
|
2014-08-26 10:08:33 +02:00
|
|
|
}
|
2014-06-05 15:20:54 +02:00
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2014-11-18 09:50:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setting dialog
|
|
|
|
*/
|
|
|
|
public function settingsAction()
|
|
|
|
{
|
|
|
|
$this->createTabs();
|
|
|
|
$this->view->dashboard = $this->dashboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create tab aggregation
|
|
|
|
*/
|
|
|
|
private function createTabs()
|
|
|
|
{
|
|
|
|
$this->view->tabs = $this->dashboard->getTabs()->extend(new DashboardSettings());
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|