From eea43e9a60e6fee6c3c18670b579da0008e2e287 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 12 Aug 2014 09:49:27 +0200 Subject: [PATCH] Adjust Icinga\Form\Dashboard\AddUrlForm to suit the new form interface refs #5525 --- .../controllers/DashboardController.php | 36 ++-- application/forms/Dashboard/AddUrlForm.php | 203 ++++++++---------- 2 files changed, 113 insertions(+), 126 deletions(-) diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php index 1f45eacde..b2c84c675 100644 --- a/application/controllers/DashboardController.php +++ b/application/controllers/DashboardController.php @@ -82,25 +82,29 @@ class DashboardController extends ActionController )->activate('addurl'); $form = new AddUrlForm(); - $form->setRequest($this->getRequest()); - $form->setAction(Url::fromRequest()->setParams(array())->getAbsoluteUrl()); - $this->view->form = $form; + $request = $this->getRequest(); + if ($request->isPost()) { + if ($form->isValid($request->getPost()) && $form->getElement('btn_submit')->isChecked()) { + $dashboard = $this->getDashboard(); + $dashboard->setComponentUrl( + $form->getValue('pane'), + $form->getValue('component'), + ltrim($form->getValue('url'), '/') + ); - if ($form->isSubmittedAndValid()) { - $dashboard = $this->getDashboard(); - $dashboard->setComponentUrl( - $form->getValue('pane'), - $form->getValue('component'), - ltrim($form->getValue('url'), '/') - ); - - $configFile = IcingaConfig::app('dashboard/dashboard')->getConfigFile(); - if ($this->writeConfiguration(new Zend_Config($dashboard->toArray()), $configFile)) { - $this->redirectNow(Url::fromPath('dashboard', array('pane' => $form->getValue('pane')))); - } else { - $this->render('show-configuration'); + $configFile = IcingaConfig::app('dashboard/dashboard')->getConfigFile(); + if ($this->writeConfiguration(new Zend_Config($dashboard->toArray()), $configFile)) { + $this->redirectNow(Url::fromPath('dashboard', array('pane' => $form->getValue('pane')))); + } else { + $this->render('show-configuration'); + return; + } } + } else { + $form->create()->setDefault('url', htmlspecialchars_decode($request->getParam('url', ''))); } + + $this->view->form = $form; } /** diff --git a/application/forms/Dashboard/AddUrlForm.php b/application/forms/Dashboard/AddUrlForm.php index 09759a265..e73fd5067 100644 --- a/application/forms/Dashboard/AddUrlForm.php +++ b/application/forms/Dashboard/AddUrlForm.php @@ -5,12 +5,8 @@ namespace Icinga\Form\Dashboard; use Icinga\Application\Config as IcingaConfig; -use Icinga\Web\Form; use Icinga\Web\Widget\Dashboard; -use Zend_Form_Element_Text; -use Zend_Form_Element_Submit; -use Zend_Form_Element_Hidden; -use Zend_Form_Element_Select; +use Icinga\Web\Form; /** * Form to add an url a dashboard pane @@ -18,124 +14,111 @@ use Zend_Form_Element_Select; class AddUrlForm extends Form { /** - * Add a selection box for different panes to the form - * - * @param Dashboard $dashboard The dashboard to retrieve the panes from + * @see Form::createElements() */ - private function addPaneSelectionBox(Dashboard $dashboard) + public function createElements(array $formData) { - $selectPane = new Zend_Form_Element_Select( - 'pane', - array( - 'label' => 'Dashboard', - 'required' => true, - 'style' => 'display:inline-block;', - - 'multiOptions' => $dashboard->getPaneKeyTitleArray() + $elements = array( + $this->createElement( + 'text', + 'url', + array( + 'required' => true, + 'label' => t('Url'), + 'helptext' => t('The url being loaded in the dashlet') + ) ) ); - $newDashboardBtn = new Zend_Form_Element_Submit( - 'create_new_pane', - array( - 'label' => 'Create A New Pane', - 'required' => false, - 'class' => 'btn btn-default', - 'style' => 'display:inline-block' - ) - ); - - $newDashboardBtn->removeDecorator('DtDdWrapper'); - $selectPane->removeDecorator('DtDdWrapper'); - $selectPane->removeDecorator('htmlTag'); - - $this->addElement($selectPane); - $this->addElement($newDashboardBtn); - $this->enableAutoSubmit(array('create_new_pane')); - } - - /** - * Add a textfield for creating a new pane to this form - */ - private function addNewPaneTextField($showExistingButton = true) - { - $txtCreatePane = new Zend_Form_Element_Text( - 'pane', - array( - 'label' => 'New Dashboard Title', - 'required' => true, - 'style' => 'display:inline-block' - ) - ); - - // Marks this field as a new pane (and prevents the checkbox being displayed when validation errors occur) - $markAsNewPane = new Zend_Form_Element_Hidden( - 'create_new_pane', - array( - 'required' => true, - 'value' => 1 - ) - ); - - $cancelDashboardBtn = new Zend_Form_Element_Submit( - 'use_existing_dashboard', - array( - 'class' => 'btn', - 'escape' => false, - 'label' => 'Use An Existing Dashboard', - 'required' => false - ) - ); - - - $cancelDashboardBtn->removeDecorator('DtDdWrapper'); - $txtCreatePane->removeDecorator('DtDdWrapper'); - $txtCreatePane->removeDecorator('htmlTag'); - - $this->addElement($txtCreatePane); - if ($showExistingButton) { - $this->addElement($cancelDashboardBtn); - } - $this->addElement($markAsNewPane); - } - - /** - * Add elements to this form (used by extending classes) - */ - protected function create() - { - $dashboard = new Dashboard(); - $this->setName('form_dashboard_add'); - $dashboard->readConfig(IcingaConfig::app('dashboard/dashboard')); - $this->addElement( - 'text', - 'url', - array( - 'label' => 'Url', - 'required' => true, - 'value' => htmlspecialchars_decode($this->getRequest()->getParam('url', '')) - ) - ); - $elems = $dashboard->getPaneKeyTitleArray(); - - if (empty($elems) || // show textfield instead of combobox when no pane is available - ($this->getRequest()->getPost('create_new_pane', '0') && // or when a new pane should be created (+ button) - !$this->getRequest()->getPost('use_existing_dashboard', '0')) // and the user didn't click the 'use - // existing' button + $paneSelectionValues = $this->getDashboardPaneSelectionValues(); + if (empty($paneSelectionValues) || + ((isset($formData['create_new_pane']) && $formData['create_new_pane'] != false) && + (false === isset($formData['use_existing_dashboard']) || $formData['use_existing_dashboard'] != true)) ) { - $this->addNewPaneTextField(!empty($elems)); + $elements[] = $this->createElement( + 'text', + 'pane', + array( + 'required' => true, + 'label' => t("The New Pane's Title"), + 'style' => 'display: inline-block' + ) + ); + $elements[] = $this->createElement( // Prevent the button from being displayed again on validation errors + 'hidden', + 'create_new_pane', + array( + 'value' => 1 + ) + ); + if (false === empty($paneSelectionValues)) { + $elements[] = $this->createElement( + 'submit', + 'use_existing_dashboard', + array( + 'label' => t('Use An Existing Pane'), + 'style' => 'display: inline-block' + ) + ); + } } else { - $this->addPaneSelectionBox($dashboard); + $elements[] = $this->createElement( + 'select', + 'pane', + array( + 'required' => true, + 'label' => t('Pane'), + 'style' => 'display: inline-block;', + 'multiOptions' => $paneSelectionValues + ) + ); + $elements[] = $this->createElement( + 'submit', + 'create_new_pane', + array( + 'label' => t('Create A New Pane'), + 'style' => 'display: inline-block' + ) + ); } - $this->addElement( + $elements[] = $this->createElement( 'text', 'component', array( - 'label' => 'Title', - 'required' => true, + 'required' => true, + 'label' => t('Title'), + 'helptext' => t('The title for the dashlet') ) ); - $this->setSubmitLabel("Add To Dashboard"); + return $elements; + } + + /** + * @see Form::addSubmitButton() + */ + public function addSubmitButton() + { + $this->addElement( + 'submit', + 'btn_submit', + array( + 'label' => t('Add To Dashboard') + ) + ); + + return $this; + } + + /** + * Return the names and titles of the available dashboard panes as key-value array + * + * @return array + */ + protected function getDashboardPaneSelectionValues() + { + $dashboard = new Dashboard(); + $dashboard->readConfig(IcingaConfig::app('dashboard/dashboard')); + return $dashboard->getPaneKeyTitleArray(); } }