From f2717b6d2661d262b0db78123a1fb8f479925922 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 4 Nov 2014 16:15:06 +0100 Subject: [PATCH] Introduce Form::setOnSuccess() in favor of overriding the constructor Zend_Form uses setters for options if a respective setter method exists. It is not necessary to override the constructor for introducing new options. Conflicts: library/Icinga/Web/Form.php --- .../controllers/DashboardController.php | 22 ++++++++++++++----- application/forms/Dashboard/ComponentForm.php | 20 ----------------- library/Icinga/Web/Form.php | 20 +++++++++++++++++ library/Icinga/Web/Widget/Dashboard.php | 4 +++- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php index 896cb6c19..105b40762 100644 --- a/application/controllers/DashboardController.php +++ b/application/controllers/DashboardController.php @@ -44,7 +44,7 @@ class DashboardController extends ActionController $params['url'] = rawurldecode($this->_request->getParam('url')); $form->populate($params); } - $form->setOnSuccess(function (Request $request, Form $form) use ($dashboard) { + $form->setOnSuccess(function (Form $form) use ($dashboard) { try { $pane = $dashboard->getPane($form->getValue('pane')); } catch (ProgrammingError $e) { @@ -83,8 +83,14 @@ class DashboardController extends ActionController 400 ); } - $form->setOnSuccess(function (Request $request, Form $form) use ($dashboard) { - $pane = $dashboard->getPane($form->getValue('pane')); + $form->setOnSuccess(function (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); + } try { $component = $pane->getComponent($form->getValue('component')); $component->setUrl($form->getValue('url')); @@ -97,7 +103,11 @@ class DashboardController extends ActionController if ($form->getValue('org_component') && $form->getValue('org_component') !== $component->getTitle()) { $pane->removeComponent($form->getValue('org_component')); } - $dashboard->write(); + // Move + if ($form->getValue('org_pane') && $form->getValue('org_pane') !== $pane->getTitle()) { + $oldPane = $dashboard->getPane($form->getValue('org_pane')); + $oldPane->removeComponent($component->getTitle()); + } $dashboard->write(); Notification::success(t('Component updated')); return true; @@ -130,7 +140,7 @@ class DashboardController extends ActionController } $pane = $this->_request->getParam('pane'); $component = $this->_request->getParam('component'); - $form->setOnSuccess(function (Request $request, Form $form) use ($dashboard, $component, $pane) { + $form->setOnSuccess(function (Form $form) use ($dashboard, $component, $pane) { try { $pane = $dashboard->getPane($pane); $pane->removeComponent($component); @@ -162,7 +172,7 @@ class DashboardController extends ActionController ); } $pane = $this->_request->getParam('pane'); - $form->setOnSuccess(function (Request $request, Form $form) use ($dashboard, $pane) { + $form->setOnSuccess(function (Form $form) use ($dashboard, $pane) { try { $pane = $dashboard->getPane($pane); $dashboard->removePane($pane->getTitle()); diff --git a/application/forms/Dashboard/ComponentForm.php b/application/forms/Dashboard/ComponentForm.php index c62bf82fe..54aa4c33d 100644 --- a/application/forms/Dashboard/ComponentForm.php +++ b/application/forms/Dashboard/ComponentForm.php @@ -161,26 +161,6 @@ class ComponentForm extends Form ); } - /** - * Adjust preferences and persist them - * - * @see Form::onSuccess() - */ - public function onSuccess(Request $request) - { - return true; - } - - /** - * Populate data if any - * - * @see Form::onRequest() - */ - public function onRequest(Request $request) - { - return true; - } - /** * @param \Icinga\Web\Widget\Dashboard $dashboard */ diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 8d75a9df3..863d4f1c1 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -163,6 +163,26 @@ class Form extends Zend_Form parent::__construct($options); } + /** + * Set a callback that is called instead of this form's onSuccess method + * + * It is called using the following signature: (Request $request, Form $form). + * + * @param callable $onSuccess Callback + * + * @return $this + * + * @throws LogicException If the callback is not callable + */ + public function setOnSuccess($onSuccess) + { + if (! is_callable($onSuccess)) { + throw new LogicException('The option `onSuccess\' is not callable'); + } + $this->onSuccess = $onSuccess; + return $this; + } + /** * Set the label to use for the standard submit button * diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php index 1dc7cf4fd..ccc8cf279 100644 --- a/library/Icinga/Web/Widget/Dashboard.php +++ b/library/Icinga/Web/Widget/Dashboard.php @@ -6,6 +6,7 @@ namespace Icinga\Web\Widget; use Icinga\Application\Icinga; use Icinga\Application\Config; +use Icinga\Data\ConfigObject; use Icinga\Exception\ConfigurationError; use Icinga\Exception\NotReadableError; use Icinga\Exception\ProgrammingError; @@ -102,7 +103,8 @@ class Dashboard extends AbstractWidget } } - $config = new Config($output); + $co = new ConfigObject($output); + $config = new Config($co); $writer = new IniWriter(array('config' => $config, 'filename' => $configFile)); $writer->write(); }