Dashboard: Remove actions

refs #4537
This commit is contained in:
Marius Hein 2014-11-18 16:28:04 +01:00
parent 8805e4af4b
commit 80871313e4
7 changed files with 161 additions and 11 deletions

View File

@ -150,6 +150,37 @@ class DashboardController extends ActionController
$this->view->form = $form; $this->view->form = $form;
} }
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;
}
/** /**
* Display the dashboard with the pane set in the 'pane' request parameter * Display the dashboard with the pane set in the 'pane' request parameter
* *

View File

@ -8,9 +8,11 @@
<?php else: ?> <?php else: ?>
<div class="content"> <div class="content">
<h1><?= $this->escape($this->translate('Welcome to Icinga Web!')) ?></h1> <h1><?= $this->escape($this->translate('Welcome to Icinga Web!')) ?></h1>
<p><?= sprintf( <p>
<?= sprintf(
$this->escape($this->translate('Currently there is no dashlet available. This might change once you enabled some of the available %s.')), $this->escape($this->translate('Currently there is no dashlet available. This might change once you enabled some of the available %s.')),
$this->qlink($this->translate('modules'), 'config/modules') $this->qlink($this->translate('modules'), 'config/modules')
) ?></p> ) ?>
</p>
</div> </div>
<?php endif ?> <?php endif ?>

View File

@ -0,0 +1,14 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<h1><?= t('Remove Pane'); ?></h1>
<p>
<?= $this->translate('Please confirm the removal of'); ?>:
<?= $this->pane; ?>
</p>
<?= $this->form; ?>
</div>

View File

@ -23,6 +23,11 @@
<th colspan="2" style="text-align: left; padding: 0.5em;"> <th colspan="2" style="text-align: left; padding: 0.5em;">
<?= $pane->getName(); ?> <?= $pane->getName(); ?>
</th> </th>
<th>
<a href="<?= $this->href('dashboard/remove-pane', array('pane' => $pane->getName())); ?>">
<?= $this->icon('remove.png'); ?>
</a>
</th>
</tr> </tr>
<?php $components = $pane->getComponents(); ?> <?php $components = $pane->getComponents(); ?>
<?php if(empty($components)): ?> <?php if(empty($components)): ?>

View File

@ -7,7 +7,9 @@ namespace Icinga\Web\Widget;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotReadableError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Exception\SystemPermissionException;
use Icinga\File\Ini\IniWriter; use Icinga\File\Ini\IniWriter;
use Icinga\User; use Icinga\User;
use Icinga\Web\Widget\Dashboard\Pane; use Icinga\Web\Widget\Dashboard\Pane;
@ -78,6 +80,7 @@ class Dashboard extends AbstractWidget
if ($this->user !== null) { if ($this->user !== null) {
$this->loadUserDashboards(); $this->loadUserDashboards();
} }
return $this; return $this;
} }
@ -117,9 +120,16 @@ class Dashboard extends AbstractWidget
$components = array(); $components = array();
foreach ($config as $key => $part) { foreach ($config as $key => $part) {
if (strpos($key, '.') === false) { if (strpos($key, '.') === false) {
if ($this->hasPane($part->title)) {
$panes[$key] = $this->getPane($part->title);
} else {
$panes[$key] = new Pane($key); $panes[$key] = new Pane($key);
$panes[$key]->setTitle($part->title); $panes[$key]->setTitle($part->title);
}
$panes[$key]->setUserWidget(); $panes[$key]->setUserWidget();
if ((bool) $part->get('disabled', false) === true) {
$panes[$key]->setDisabled();
}
} else { } else {
list($paneName, $componentName) = explode('.', $key, 2); list($paneName, $componentName) = explode('.', $key, 2);
@ -168,7 +178,13 @@ class Dashboard extends AbstractWidget
{ {
/** @var $pane Pane */ /** @var $pane Pane */
foreach ($panes as $pane) { foreach ($panes as $pane) {
if (array_key_exists($pane->getName(), $this->panes)) { if ($pane->getDisabled()) {
if ($this->hasPane($pane->getTitle()) === true) {
$this->removePane($pane->getTitle());
}
continue;
}
if ($this->hasPane($pane->getTitle()) === true) {
/** @var $current Pane */ /** @var $current Pane */
$current = $this->panes[$pane->getName()]; $current = $this->panes[$pane->getName()];
$current->addComponents($pane->getComponents()); $current->addComponents($pane->getComponents());
@ -242,6 +258,17 @@ class Dashboard extends AbstractWidget
return ! empty($this->panes); return ! empty($this->panes);
} }
/**
* Check if a panel exist
*
* @param string $pane
* @return bool
*/
public function hasPane($pane)
{
return $pane && array_key_exists($pane, $this->panes);
}
/** /**
* Add a pane object to this dashboard * Add a pane object to this dashboard
* *
@ -255,6 +282,21 @@ class Dashboard extends AbstractWidget
return $this; return $this;
} }
public function removePane($title)
{
if ($this->hasPane($title) === true) {
$pane = $this->getPane($title);
if ($pane->isUserWidget() === true) {
unset($this->panes[$pane->getName()]);
} else {
$pane->setDisabled();
$pane->setUserWidget();
}
} else {
throw new ProgrammingError('Pane not found: ' . $title);
}
}
/** /**
* Return the pane with the provided name * Return the pane with the provided name
* *
@ -296,6 +338,7 @@ class Dashboard extends AbstractWidget
if (empty($this->panes)) { if (empty($this->panes)) {
return ''; return '';
} }
return $this->determineActivePane()->render(); return $this->determineActivePane()->render();
} }
@ -382,6 +425,26 @@ class Dashboard extends AbstractWidget
if ($this->user === null) { if ($this->user === null) {
return ''; return '';
} }
return '/var/lib/icingaweb/' . $this->user->getUsername() . '/dashboard.ini';
$baseDir = '/var/lib/icingaweb';
if (! file_exists($baseDir)) {
throw new NotReadableError('Could not read: ' . $baseDir);
}
$userDir = $baseDir . '/' . $this->user->getUsername();
if (! file_exists($userDir)) {
$success = @mkdir($userDir);
if (!$success) {
throw new SystemPermissionException('Could not create: ' . $userDir);
}
}
if (! file_exists($userDir)) {
throw new NotReadableError('Could not read: ' . $userDir);
}
return $userDir . '/dashboard.ini';
} }
} }

View File

@ -53,7 +53,7 @@ class Component extends UserWidget
private $template =<<<'EOD' private $template =<<<'EOD'
<div class="container" data-icinga-url="{URL}"> <div class="container" data-icinga-url="{URL}">
<h1><a href="{FULL_URL}" data-base-target="col1">{TITLE}</a> ({REMOVE})</h1> <h1><a href="{FULL_URL}" data-base-target="col1">{TITLE}</a></h1>
<noscript> <noscript>
<iframe src="{IFRAME_URL}" style="height:100%; width:99%" frameborder="no"></iframe> <iframe src="{IFRAME_URL}" style="height:100%; width:99%" frameborder="no"></iframe>
</noscript> </noscript>

View File

@ -36,6 +36,13 @@ class Pane extends UserWidget
*/ */
private $components = array(); private $components = array();
/**
* Disabled flag of a pane
*
* @var bool
*/
private $disabled;
/** /**
* Create a new pane * Create a new pane
* *
@ -253,9 +260,15 @@ class Pane extends UserWidget
*/ */
public function toArray() public function toArray()
{ {
return array( $pane = array(
'title' => $this->getTitle() 'title' => $this->getTitle(),
); );
if ($this->getDisabled() === true) {
$pane['disabled'] = 1;
}
return $pane;
} }
/** /**
@ -274,4 +287,26 @@ class Pane extends UserWidget
} }
return $pane; return $pane;
} }
/**
* Setter for disabled
*
* @param boolean $disabled
*/
public function setDisabled($disabled = true)
{
$this->disabled = (bool) $disabled;
}
/**
* Getter for disabled
*
* @return boolean
*/
public function getDisabled()
{
return $this->disabled;
}
} }