From 488ea4633d232dfc54acbadeb2fd8469a50257fa Mon Sep 17 00:00:00 2001
From: Johannes Meyer <johannes.meyer@netways.de>
Date: Fri, 25 Jul 2014 14:50:53 +0200
Subject: [PATCH] Merge EditBackendForm with CreateBackendForm and rename it

It is not necessary to distinct between whether a backend is created or
edited as a user perhaps likes to change the name of a backend directly
instead of the need to remove and recreate it.

refs #5525
---
 .../Config/Backend/CreateBackendForm.php      |  45 ------
 .../forms/Config/Backend/EditBackendForm.php  |  99 -------------
 .../application/forms/Config/BackendForm.php  | 134 ++++++++++++++++++
 3 files changed, 134 insertions(+), 144 deletions(-)
 delete mode 100644 modules/monitoring/application/forms/Config/Backend/CreateBackendForm.php
 delete mode 100644 modules/monitoring/application/forms/Config/Backend/EditBackendForm.php
 create mode 100644 modules/monitoring/application/forms/Config/BackendForm.php

diff --git a/modules/monitoring/application/forms/Config/Backend/CreateBackendForm.php b/modules/monitoring/application/forms/Config/Backend/CreateBackendForm.php
deleted file mode 100644
index 513dea238..000000000
--- a/modules/monitoring/application/forms/Config/Backend/CreateBackendForm.php
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php
-// {{{ICINGA_LICENSE_HEADER}}}
-// {{{ICINGA_LICENSE_HEADER}}}
-
-namespace Icinga\Module\Monitoring\Form\Config\Backend;
-
-use \Zend_Config;
-
-/**
- * Extended EditBackendForm for creating new Backends
- *
- * @see EditBackendForm
- */
-class CreateBackendForm extends EditBackendForm
-{
-    /**
-     * Create this form
-     *
-     * @see EditBackendForm::create()
-     */
-    public function create()
-    {
-        $this->setBackendConfiguration(new Zend_Config(array('type' => 'ido')));
-        $this->addElement(
-            'text',
-            'backend_name',
-            array(
-                'label'     =>  'Backend Name',
-                'required'  =>  true,
-                'helptext'  =>  'This will be the identifier of this backend'
-            )
-        );
-        parent::create();
-    }
-
-    /**
-     * Return the name of the backend that is to be created
-     *
-     * @return string The name of the backend as entered in the form
-     */
-    public function getBackendName()
-    {
-        return $this->getValue('backend_name');
-    }
-}
diff --git a/modules/monitoring/application/forms/Config/Backend/EditBackendForm.php b/modules/monitoring/application/forms/Config/Backend/EditBackendForm.php
deleted file mode 100644
index b66383f73..000000000
--- a/modules/monitoring/application/forms/Config/Backend/EditBackendForm.php
+++ /dev/null
@@ -1,99 +0,0 @@
-<?php
-// {{{ICINGA_LICENSE_HEADER}}}
-// {{{ICINGA_LICENSE_HEADER}}}
-
-namespace Icinga\Module\Monitoring\Form\Config\Backend;
-
-use Zend_Config;
-use Icinga\Web\Form;
-use Icinga\Application\Icinga;
-use Icinga\Data\ResourceFactory;
-
-/**
- * Form for modifying a monitoring backend
- */
-class EditBackendForm extends Form
-{
-    /**
-     * Create this form
-     *
-     * @see Icinga\Web\Form::create()
-     */
-    public function create()
-    {
-        $backendType = $this->getRequest()->getParam('backend_type', $this->backend->type);
-
-        $this->addElement(
-            'select',
-            'backend_type',
-            array(
-                'label'         =>  'Backend Type',
-                'value'         =>  $this->backend->type,
-                'required'      =>  true,
-                'helptext'      =>  'The data source used for retrieving monitoring information',
-                'multiOptions'  =>  array(
-                    'ido'           => 'IDO Backend',
-                    'statusdat'     => 'Status.dat',
-                    'livestatus'    => 'Livestatus'
-                )
-            )
-        );
-        $this->addElement(
-            'select',
-            'backend_resource',
-            array(
-                'label'         => 'Resource',
-                'value'         => $this->backend->resource,
-                'required'      => true,
-                'multiOptions'  => $this->getResourcesByType($backendType === 'ido' ? 'db' : $backendType),
-                'helptext'      => 'The resource to use'
-            )
-        );
-        $this->addElement(
-            'checkbox',
-            'backend_disable',
-            array(
-                'label'     => 'Disable This Backend',
-                'required'  =>  true,
-                'value'     =>  $this->backend->disabled
-            )
-        );
-
-        $this->enableAutoSubmit(array('backend_type'));
-        $this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
-    }
-
-    /**
-     * Return a configuration containing the backend settings entered in this form
-     *
-     * @return Zend_Config The updated configuration for this backend
-     */
-    public function getConfig()
-    {
-        $values = $this->getValues();
-        return new Zend_Config(
-            array(
-                'type'     => $values['backend_type'],
-                'disabled' => $values['backend_disable'],
-                'resource' => $values['backend_resource']
-            )
-        );
-    }
-
-    /**
-     * Return a list of all resources of the given type ready to be used as content for a select input
-     *
-     * @param   string  $type   The type of resources to return
-     *
-     * @return  array
-     */
-    protected function getResourcesByType($type)
-    {
-        $backends = array();
-        foreach (array_keys(ResourceFactory::getResourceConfigs($type)->toArray()) as $name) {
-            $backends[$name] = $name;
-        }
-
-        return $backends;
-    }
-}
diff --git a/modules/monitoring/application/forms/Config/BackendForm.php b/modules/monitoring/application/forms/Config/BackendForm.php
new file mode 100644
index 000000000..19f870778
--- /dev/null
+++ b/modules/monitoring/application/forms/Config/BackendForm.php
@@ -0,0 +1,134 @@
+<?php
+// {{{ICINGA_LICENSE_HEADER}}}
+// {{{ICINGA_LICENSE_HEADER}}}
+
+namespace Icinga\Module\Monitoring\Form\Config;
+
+use Icinga\Web\Form;
+use Icinga\Data\ResourceFactory;
+
+/**
+ * Form for modifying a monitoring backend
+ */
+class BackendForm extends Form
+{
+    /**
+     * @see Form::createElements()
+     */
+    public function createElements(array $formData)
+    {
+        return array(
+            $this->createElement(
+                'text',
+                'name',
+                array(
+                    'required'  => true,
+                    'label'     => t('Backend Name'),
+                    'helptext'  => t('The identifier of this backend')
+                )
+            ),
+            $this->createElement(
+                'select',
+                'type',
+                array(
+                    'required'      => true,
+                    'class'         => 'autosubmit',
+                    'label'         => t('Backend Type'),
+                    'helptext'      => t('The data source used for retrieving monitoring information'),
+                    'multiOptions'  => array(
+                        'ido'           => 'IDO Backend',
+                        'statusdat'     => 'Status.dat',
+                        'livestatus'    => 'Livestatus'
+                    )
+                )
+            ),
+            $this->createElement(
+                'select',
+                'resource',
+                array(
+                    'required'      => true,
+                    'label'         => t('Resource'),
+                    'helptext'      => t('The resource to use'),
+                    'multiOptions'  => $this->getResourcesByType(
+                        false === isset($formData['type']) || $formData['type'] === 'ido' ? 'db' : $formData['type']
+                    )
+                )
+            ),
+            $this->createElement(
+                'checkbox',
+                'disabled',
+                array(
+                    'required'  => true,
+                    'label'     => t('Disable This Backend')
+                )
+            )
+        );
+    }
+
+    /**
+     * @see Form::addSubmitButton()
+     */
+    public function addSubmitButton()
+    {
+        $this->addElement(
+            'submit',
+            'btn_submit',
+            array(
+                'label' => t('Save Changes')
+            )
+        );
+
+        return $this;
+    }
+
+    /**
+     * Return the backend configuration values and its name
+     *
+     * The first value is the name and the second one the values as array.
+     *
+     * @return  array
+     */
+    public function getBackendConfig()
+    {
+        $values = $this->getValues();
+        $name = $values['name'];
+
+        if ($values['disabled'] == '0') {
+            unset($values['disabled']);
+        }
+
+        unset($values['name']);
+        unset($values['btn_submit']);
+        unset($values[$this->getTokenElementName()]);
+        return array($name, $values);
+    }
+
+    /**
+     * Populate the form with the given configuration values
+     *
+     * @param   string  $name       The name of the backend
+     * @param   array   $config     The configuration values
+     */
+    public function setBackendConfig($name, array $config)
+    {
+        $config['name'] = $name;
+        $this->populate($config);
+    }
+
+    /**
+     * Return a list of all resources of the given type ready to be used as content for a select input
+     *
+     * @param   string  $type   The type of resources to return
+     *
+     * @return  array
+     */
+    protected function getResourcesByType($type)
+    {
+        $backends = array();
+        foreach (array_keys(ResourceFactory::getResourceConfigs($type)->toArray()) as $name) {
+            $backends[$name] = $name;
+        }
+
+        return $backends;
+    }
+}