Skip to content

Commit

Permalink
Dashboard: Use new controller actions
Browse files Browse the repository at this point in the history
refs #4537
  • Loading branch information
mxhash committed Nov 18, 2014
1 parent cbcd276 commit 3e58ec2
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 53 deletions.
98 changes: 66 additions & 32 deletions application/controllers/DashboardController.php
Expand Up @@ -3,9 +3,13 @@
// {{{ICINGA_LICENSE_HEADER}}}

use Icinga\Application\Logger;
use Icinga\Form\Dashboard\ComponentForm;
use Icinga\Exception\ProgrammingError;
use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Forms\Dashboard\ComponentForm;
use Icinga\Web\Form;
use Icinga\Web\Notification;
use Icinga\Web\Controller\ActionController;
use Icinga\Web\Request;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Widget\Tabextension\DashboardSettings;
Expand Down Expand Up @@ -33,10 +37,29 @@ public function newComponentAction()
{
$form = new ComponentForm();
$this->createTabs();
$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
$dashboard = $this->dashboard;
$form->setDashboard($dashboard);
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');
$form->handleRequest();
$this->view->form = $form;
}
Expand All @@ -60,12 +83,12 @@ public function updateComponentAction()
400
);
}
$form->setOnSuccess(function (\Icinga\Web\Request $request, \Icinga\Web\Form $form) use ($dashboard) {
$form->setOnSuccess(function (Request $request, 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) {
} catch (ProgrammingError $e) {
$component = new Dashboard\Component($form->getValue('component'), $form->getValue('url'), $pane);
$pane->addComponent($component);
}
Expand All @@ -75,6 +98,7 @@ public function updateComponentAction()
$pane->removeComponent($form->getValue('org_component'));
}
$dashboard->write();
$dashboard->write();
Notification::success(t('Component updated'));
return true;
});
Expand All @@ -87,31 +111,42 @@ public function updateComponentAction()
$this->view->form = $form;
}

public function deleteComponentAction()
public function removeComponentAction()
{
$form = new ComponentForm();

$form = new ConfirmRemovalForm();
$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()
{
$form = new AddUrlForm();

$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
$form->setDashboard($dashboard);
$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');
$form->handleRequest();
$this->view->pane = $pane;
$this->view->component = $component;
$this->view->form = $form;
}

Expand Down Expand Up @@ -140,14 +175,13 @@ public function indexAction()
$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')
'url' => Url::fromPath('dashboard/new-component')
)
); */
);
$this->view->dashboard = $this->dashboard;
}
}
Expand Down
4 changes: 0 additions & 4 deletions application/forms/Dashboard/ComponentForm.php
Expand Up @@ -4,10 +4,6 @@

namespace Icinga\Forms\Dashboard;

use Icinga\Application\Config;
use Icinga\Exception\ProgrammingError;
use Icinga\File\Ini\IniWriter;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form;
use Icinga\Web\Request;
Expand Down
6 changes: 6 additions & 0 deletions application/views/scripts/dashboard/remove-component.phtml
Expand Up @@ -4,5 +4,11 @@

<div class="content">
<h1><?= t('Remove Component From Dashboard'); ?></h1>

<p>
<?= $this->translate('Please confirm the removal'); ?>:
<?= $this->pane; ?>/<?= $this->component; ?>
</p>

<?= $this->form; ?>
</div>
37 changes: 23 additions & 14 deletions application/views/scripts/dashboard/settings.phtml
Expand Up @@ -24,23 +24,32 @@
<?= $pane->getName(); ?>
</th>
</tr>
<?php foreach ($pane->getComponents() as $component): ?>
<?php $components = $pane->getComponents(); ?>
<?php if(empty($components)): ?>
<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 colspan="3">
<?= $this->translate('No compoments added to dashboard') ?>.
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<?php foreach ($components 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-component', array('pane' => $pane->getName(), 'component' => $component->getTitle())); ?>">
<?= $this->icon('remove.png'); ?>
</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
Expand Down
5 changes: 4 additions & 1 deletion library/Icinga/Web/Widget/Dashboard/Component.php
Expand Up @@ -211,7 +211,10 @@ protected function getRemoveLink()
{
return sprintf(
'<a data-base-target="main" href="%s">%s</a>',
Url::fromRequest(array('remove' => $this->getTitle())),
Url::fromPath('dashboard/remove-component', array(
'component' => $this->getTitle(),
'pane' => $this->pane->getTitle()
)),
t('Remove')
);
}
Expand Down
2 changes: 1 addition & 1 deletion library/Icinga/Web/Widget/Tabextension/DashboardAction.php
Expand Up @@ -26,7 +26,7 @@ public function apply(Tabs $tabs)
array(
'icon' => 'dashboard',
'title' => 'Add To Dashboard',
'url' => Url::fromPath('dashboard/addurl'),
'url' => Url::fromPath('dashboard/new-component'),
'urlParams' => array(
'url' => rawurlencode(Url::fromRequest()->getRelativeUrl())
)
Expand Down
Expand Up @@ -24,7 +24,7 @@ public function apply(Tabs $tabs)
array(
'icon' => 'img/icons/dashboard.png',
'title' => t('Add To Dashboard'),
'url' => Url::fromPath('dashboard/addurl')
'url' => Url::fromPath('dashboard/new-component')
)
);

Expand Down

0 comments on commit 3e58ec2

Please sign in to comment.