Dashboard: Rewrite forms and controller [WIP]

refs #4537
This commit is contained in:
Marius Hein 2014-11-18 09:50:10 +01:00
parent 47414f3528
commit cbcd276b44
9 changed files with 294 additions and 91 deletions

View File

@ -2,16 +2,13 @@
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\IcingaException;
use Icinga\Exception\NotReadableError;
use Icinga\File\Ini\IniWriter;
use Icinga\Forms\Dashboard\AddUrlForm;
use Icinga\Form\Dashboard\ComponentForm;
use Icinga\Web\Notification;
use Icinga\Web\Controller\ActionController;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Widget\Tabextension\DashboardSettings;
/**
* Handle creation, removal and displaying of dashboards, panes and components
@ -20,26 +17,100 @@ use Icinga\Web\Widget\Dashboard;
*/
class DashboardController extends ActionController
{
/**
* @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();
$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
$form->setDashboard($dashboard);
$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
);
}
$form->setOnSuccess(function (\Icinga\Web\Request $request, \Icinga\Web\Form $form) use ($dashboard) {
$pane = $dashboard->getPane($form->getValue('pane'));
try {
$component = $pane->getComponent($form->getValue('component'));
$component->setUrl($form->getValue('url'));
} catch (\Icinga\Exception\ProgrammingError $e) {
$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();
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;
}
public function deleteComponentAction()
{
$form = new ComponentForm();
$this->createTabs();
$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
$form->setDashboard($dashboard);
$form->handleRequest();
$this->view->form = $form;
}
/**
* Display the form for adding new components or add the new component if submitted
*/
public function addurlAction()
{
$this->getTabs()->add(
'addurl',
array(
'title' => 'Add Dashboard URL',
'url' => Url::fromRequest()
)
)->activate('addurl');
$form = new AddUrlForm();
$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
$form->setDashboard($dashboard);
$form->setRedirectUrl($this->getParam('url'));
$form->handleRequest();
$this->view->form = $form;
}
@ -52,36 +123,50 @@ class DashboardController extends ActionController
*/
public function indexAction()
{
$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
if (! $dashboard->hasPanes()) {
$this->createTabs();
if (! $this->dashboard->hasPanes()) {
$this->view->title = 'Dashboard';
} else {
if ($this->_getParam('pane')) {
$pane = $this->_getParam('pane');
$dashboard->activate($pane);
$this->dashboard->activate($pane);
}
if ($dashboard === null) {
if ($this->dashboard === null) {
$this->view->title = 'Dashboard';
} else {
$this->view->title = $dashboard->getActivePane()->getTitle() . ' :: Dashboard';
$this->view->tabs = $dashboard->getTabs();
$this->view->title = $this->dashboard->getActivePane()->getTitle() . ' :: Dashboard';
if ($this->hasParam('remove')) {
$dashboard->getActivePane()->removeComponent($this->getParam('remove'));
$dashboard->write();
$this->dashboard->getActivePane()->removeComponent($this->getParam('remove'));
$this->dashboard->write();
$this->redirectNow(URL::fromRequest()->remove('remove'));
}
$this->view->tabs->add(
/* $this->view->tabs->add(
'Add',
array(
'title' => '+',
'url' => Url::fromPath('dashboard/addurl')
)
);
$this->view->dashboard = $dashboard;
); */
$this->view->dashboard = $this->dashboard;
}
}
}
/**
* 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());
}
}

View File

@ -11,19 +11,13 @@ use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form;
use Icinga\Web\Request;
use Icinga\Web\Widget\Dashboard\Component;
/**
* Form to add an url a dashboard pane
*/
class AddUrlForm extends Form
class ComponentForm extends Form
{
/**
* Config file name
*
* @var string
*/
private $configFile = 'dashboard/dashboard';
/**
* @var Dashboard
*/
@ -35,7 +29,9 @@ class AddUrlForm extends Form
public function init()
{
$this->setName('form_dashboard_addurl');
$this->setSubmitLabel(t('Add To Dashboard'));
if (! $this->getSubmitLabel()) {
$this->setSubmitLabel(t('Add To Dashboard'));
}
}
/**
@ -45,13 +41,28 @@ class AddUrlForm extends Form
*/
public function createElements(array $formData)
{
$paneSelectionValues = array();
$groupElements = array();
$panes = array();
if ($this->dashboard !== null) {
$paneSelectionValues = $this->dashboard->getPaneKeyTitleArray();
if ($this->dashboard) {
$panes = $this->dashboard->getPaneKeyTitleArray();
}
$groupElements = array();
$this->addElement(
'hidden',
'org_pane',
array(
'required' => false
)
);
$this->addElement(
'hidden',
'org_component',
array(
'required' => false
)
);
$this->addElement(
'text',
@ -72,7 +83,7 @@ class AddUrlForm extends Form
'description' => t('Enter a title for the dashlet.')
)
);
if (empty($paneSelectionValues) ||
if (empty($panes) ||
((isset($formData['create_new_pane']) && $formData['create_new_pane'] != false) &&
(false === isset($formData['use_existing_dashboard']) || $formData['use_existing_dashboard'] != true))
) {
@ -93,7 +104,7 @@ class AddUrlForm extends Form
'value' => 1
)
);
if (false === empty($paneSelectionValues)) {
if (false === empty($panes)) {
$buttonExistingPane = $this->createElement(
'submit',
'use_existing_dashboard',
@ -114,7 +125,7 @@ class AddUrlForm extends Form
array(
'required' => true,
'label' => t('Pane'),
'multiOptions' => $paneSelectionValues,
'multiOptions' => $panes,
'description' =>
t('Select a pane you want to add the dashlet.')
)
@ -161,33 +172,6 @@ class AddUrlForm extends Form
*/
public function onSuccess(Request $request)
{
$pane = null;
if ($this->dashboard === null) {
throw new ProgrammingError('Dashboard is not set, can not write values');
}
try {
$pane = $this->dashboard->getPane($this->getValue('pane'));
} catch (ProgrammingError $e) {
$pane = new Dashboard\Pane($this->getValue('pane'));
$pane->setUserWidget();
$this->dashboard->addPane($pane);
}
$component = new Dashboard\Component(
$this->getValue('component'),
$this->getValue('url'),
$pane
);
$component->setUserWidget();
$pane->addComponent($component);
$this->dashboard->write();
return true;
}
@ -198,25 +182,11 @@ class AddUrlForm extends Form
*/
public function onRequest(Request $request)
{
// TODO(mh): Im not sure if this is the right place for that
$url = $this->getValue('url');
if (! $url) {
$url = $request->getParam('url');
}
if (! $url) {
return;
}
$data = array(
'url' => urldecode(Url::fromPath($url)->getPath())
);
$this->populate($data);
return true;
}
/**
* @param Dashboard $dashboard
* @param \Icinga\Web\Widget\Dashboard $dashboard
*/
public function setDashboard(Dashboard $dashboard)
{
@ -224,10 +194,24 @@ class AddUrlForm extends Form
}
/**
* @return Dashboard
* @return \Icinga\Web\Widget\Dashboard
*/
public function getDashboard()
{
return $this->dashboard;
}
/**
* @param Component $component
*/
public function load(Component $component)
{
$this->populate(array(
'pane' => $component->getPane()->getName(),
'org_pane' => $component->getPane()->getName(),
'component' => $component->getTitle(),
'org_component' => $component->getTitle(),
'url' => $component->getUrl()
));
}
}

View File

@ -0,0 +1,8 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<h1><?= t('Add Component To Dashboard'); ?></h1>
<?= $this->form; ?>
</div>

View File

@ -0,0 +1,8 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<h1><?= t('Remove Component From Dashboard'); ?></h1>
<?= $this->form; ?>
</div>

View File

@ -0,0 +1,47 @@
<?php
?>
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<h1><?= t('Dashboard Settings'); ?></h1>
<table class="avp" data-base-target="_next">
<thead>
<tr>
<th>
<strong><?= t('Component Name') ?></strong>
</th>
<th>
<strong><?= t('Url') ?></strong>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->dashboard->getPanes() as $pane): ?>
<tr style="background-color: #f1f1f1;">
<th colspan="2" style="text-align: left; padding: 0.5em;">
<?= $pane->getName(); ?>
</th>
</tr>
<?php foreach ($pane->getComponents() as $component): ?>
<tr>
<td>
<a href="<?= $this->href('dashboard/update-component', array('pane' => $pane->getName(), 'component' => $component->getTitle())); ?>">
<?= $component->getTitle(); ?>
</a>
</td>
<td>
<a href="<?= $this->href($component->getUrl()); ?>"><?= $component->getUrl(); ?></a>
</td>
<td>
<a href="<?= $this->href('dashboard/remove', array('pane' => $pane->getName(), 'component' => $component->getTitle())); ?>">
<?= $this->icon('remove.png'); ?>
</a>
</td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>

View File

@ -0,0 +1,8 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<h1><?= t('Edit Component'); ?></h1>
<?= $this->form; ?>
</div>

View File

@ -187,7 +187,7 @@ class Dashboard extends AbstractWidget
*/
public function getTabs()
{
$url = Url::fromRequest()->getUrlWithout($this->tabParam);
$url = Url::fromPath('dashboard')->getUrlWithout($this->tabParam);
if ($this->tabs === null) {
$this->tabs = new Tabs();
@ -212,7 +212,6 @@ class Dashboard extends AbstractWidget
*/
public function getPanes()
{
return '';
return $this->panes;
}

View File

@ -93,6 +93,14 @@ EOD;
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Retrieve the components url
*
@ -228,4 +236,20 @@ EOD;
$cmp = new Component($title, Url::fromPath($url, $parameters), $pane);
return $cmp;
}
/**
* @param \Icinga\Web\Widget\Dashboard\Pane $pane
*/
public function setPane(Panel $pane)
{
$this->pane = $pane;
}
/**
* @return \Icinga\Web\Widget\Dashboard\Pane
*/
public function getPane()
{
return $this->pane;
}
}

View File

@ -0,0 +1,40 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Widget\Tabextension;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tabs;
/**
* Dashboard settings
*/
class DashboardSettings implements Tabextension
{
/**
* Apply this tabextension to the provided tabs
*
* @param Tabs $tabs The tabbar to modify
*/
public function apply(Tabs $tabs)
{
$tabs->addAsDropdown(
'dashboard_add',
array(
'icon' => 'img/icons/dashboard.png',
'title' => t('Add To Dashboard'),
'url' => Url::fromPath('dashboard/addurl')
)
);
$tabs->addAsDropdown(
'dashboard_settings',
array(
'icon' => 'img/icons/dashboard.png',
'title' => t('Settings'),
'url' => Url::fromPath('dashboard/settings')
)
);
}
}