[icinga-checkins] icinga.org: icingaweb2/feature/add-to-dashboard-4537: Introduce Form:: setOnSuccess() in favor of overriding the constructor

git at icinga.org git at icinga.org
Tue Nov 18 17:03:11 CET 2014


Module: icingaweb2
Branch: feature/add-to-dashboard-4537
Commit: f2717b6d2661d262b0db78123a1fb8f479925922
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=f2717b6d2661d262b0db78123a1fb8f479925922

Author: Eric Lippmann <eric.lippmann at netways.de>
Date:   Tue Nov  4 16:15:06 2014 +0100

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

---

 application/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 896cb6c..105b407 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 c62bf82..54aa4c3 100644
--- a/application/forms/Dashboard/ComponentForm.php
+++ b/application/forms/Dashboard/ComponentForm.php
@@ -162,26 +162,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
      */
     public function setDashboard(Dashboard $dashboard)
diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php
index 8d75a9d..863d4f1 100644
--- a/library/Icinga/Web/Form.php
+++ b/library/Icinga/Web/Form.php
@@ -164,6 +164,26 @@ class Form extends Zend_Form
     }
 
     /**
+     * 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
      *
      * @param   string  $label  The label to use for the submit button
diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php
index 1dc7cf4..ccc8cf2 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();
     }



More information about the icinga-checkins mailing list