From cbcd276b44f437c24df69c36298fcea46743cf9f Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Tue, 18 Nov 2014 09:50:10 +0100 Subject: [PATCH] Dashboard: Rewrite forms and controller [WIP] refs #4537 --- .../controllers/DashboardController.php | 143 ++++++++++++++---- .../{AddUrlForm.php => ComponentForm.php} | 104 ++++++------- .../scripts/dashboard/new-component.phtml | 8 + .../scripts/dashboard/remove-component.phtml | 8 + .../views/scripts/dashboard/settings.phtml | 47 ++++++ .../scripts/dashboard/update-component.phtml | 8 + library/Icinga/Web/Widget/Dashboard.php | 3 +- .../Icinga/Web/Widget/Dashboard/Component.php | 24 +++ .../Widget/Tabextension/DashboardSettings.php | 40 +++++ 9 files changed, 294 insertions(+), 91 deletions(-) rename application/forms/Dashboard/{AddUrlForm.php => ComponentForm.php} (75%) create mode 100644 application/views/scripts/dashboard/new-component.phtml create mode 100644 application/views/scripts/dashboard/remove-component.phtml create mode 100644 application/views/scripts/dashboard/settings.phtml create mode 100644 application/views/scripts/dashboard/update-component.phtml create mode 100644 library/Icinga/Web/Widget/Tabextension/DashboardSettings.php diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php index 2c2b7d2fd..14252ce22 100644 --- a/application/controllers/DashboardController.php +++ b/application/controllers/DashboardController.php @@ -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()); + } } diff --git a/application/forms/Dashboard/AddUrlForm.php b/application/forms/Dashboard/ComponentForm.php similarity index 75% rename from application/forms/Dashboard/AddUrlForm.php rename to application/forms/Dashboard/ComponentForm.php index f67c92a42..d3bcb22f2 100644 --- a/application/forms/Dashboard/AddUrlForm.php +++ b/application/forms/Dashboard/ComponentForm.php @@ -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() + )); + } } diff --git a/application/views/scripts/dashboard/new-component.phtml b/application/views/scripts/dashboard/new-component.phtml new file mode 100644 index 000000000..456d14a65 --- /dev/null +++ b/application/views/scripts/dashboard/new-component.phtml @@ -0,0 +1,8 @@ +
+ tabs ?> +
+ +
+

+ form; ?> +
\ No newline at end of file diff --git a/application/views/scripts/dashboard/remove-component.phtml b/application/views/scripts/dashboard/remove-component.phtml new file mode 100644 index 000000000..9138b95b1 --- /dev/null +++ b/application/views/scripts/dashboard/remove-component.phtml @@ -0,0 +1,8 @@ +
+ tabs ?> +
+ +
+

+ form; ?> +
\ No newline at end of file diff --git a/application/views/scripts/dashboard/settings.phtml b/application/views/scripts/dashboard/settings.phtml new file mode 100644 index 000000000..7a2e8f390 --- /dev/null +++ b/application/views/scripts/dashboard/settings.phtml @@ -0,0 +1,47 @@ + +
+ tabs ?> +
+
+

+ + + + + + + + + + dashboard->getPanes() as $pane): ?> + + + + getComponents() as $component): ?> + + + + + + + + +
+ + + +
+ getName(); ?> +
+ + getTitle(); ?> + + + getUrl(); ?> + + + icon('remove.png'); ?> + +
+
\ No newline at end of file diff --git a/application/views/scripts/dashboard/update-component.phtml b/application/views/scripts/dashboard/update-component.phtml new file mode 100644 index 000000000..0493f5613 --- /dev/null +++ b/application/views/scripts/dashboard/update-component.phtml @@ -0,0 +1,8 @@ +
+ tabs ?> +
+ +
+

+ form; ?> +
\ No newline at end of file diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php index 1a049ef59..1764e5cea 100644 --- a/library/Icinga/Web/Widget/Dashboard.php +++ b/library/Icinga/Web/Widget/Dashboard.php @@ -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; } diff --git a/library/Icinga/Web/Widget/Dashboard/Component.php b/library/Icinga/Web/Widget/Dashboard/Component.php index 693f60bfe..0ab210057 100644 --- a/library/Icinga/Web/Widget/Dashboard/Component.php +++ b/library/Icinga/Web/Widget/Dashboard/Component.php @@ -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; + } } diff --git a/library/Icinga/Web/Widget/Tabextension/DashboardSettings.php b/library/Icinga/Web/Widget/Tabextension/DashboardSettings.php new file mode 100644 index 000000000..4242f864d --- /dev/null +++ b/library/Icinga/Web/Widget/Tabextension/DashboardSettings.php @@ -0,0 +1,40 @@ +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') + ) + ); + } +} \ No newline at end of file