[icinga-checkins] icinga.org: icingaweb2-module-director/master: YesNo: new boolean-like form fields

git at icinga.org git at icinga.org
Fri Oct 14 15:40:09 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 6f46e6b496f3d2ce70b765d53e8ff003ea4de450
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=6f46e6b496f3d2ce70b765d53e8ff003ea4de450

Author: Thomas Gelf <thomas at gelf.net>
Date:   Fri Oct 14 13:39:43 2016 +0000

YesNo: new boolean-like form fields

fixes #12927

---

 application/forms/SyncPropertyForm.php             |    4 +--
 library/Director/Web/Form/DirectorObjectForm.php   |   35 +++-----------------
 .../Director/Web/Form/Element/OptionalYesNo.php    |   22 ++++++++++++
 library/Director/Web/Form/Element/YesNo.php        |   14 ++++++++
 4 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/application/forms/SyncPropertyForm.php b/application/forms/SyncPropertyForm.php
index 7e92f69..55320df 100644
--- a/application/forms/SyncPropertyForm.php
+++ b/application/forms/SyncPropertyForm.php
@@ -124,14 +124,12 @@ class SyncPropertyForm extends DirectorObjectForm
         }
         */
 
-
         // TODO: we need modifier
-        $this->addElement('select', 'use_filter', array(
+        $this->addElement('YesNo', 'use_filter', array(
             'label'        => $this->translate('Set based on filter'),
             'ignore'       => true,
             'class'        => 'autosubmit',
             'required'     => true,
-            'multiOptions' => $this->enumBoolean()
         ));
 
         if ($this->hasBeenSent()) {
diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php
index 402ad85..69b505b 100644
--- a/library/Director/Web/Form/DirectorObjectForm.php
+++ b/library/Director/Web/Form/DirectorObjectForm.php
@@ -229,12 +229,6 @@ abstract class DirectorObjectForm extends QuickForm
             unset($props['vars']);
         }
 
-        foreach ($props as $k => $v) {
-            if (is_bool($v)) {
-                $props[$k] = $v ? 'y' : 'n';
-            }
-        }
-
         $this->setDefaults($props);
 
         if (! $object instanceof IcingaObject) {
@@ -424,25 +418,12 @@ abstract class DirectorObjectForm extends QuickForm
 
     protected function addBoolean($key, $options, $default = null)
     {
-        $map = array(
-            false => 'n',
-            true  => 'y',
-            'n'   => 'n',
-            'y'   => 'y',
-        );
-        if ($default !== null) {
-            $options['multiOptions'] = $this->enumBoolean();
+        if ($default === null) {
+            return $this->addElement('OptionalYesNo', $key, $options);
         } else {
-            $options['multiOptions'] = $this->optionalEnum($this->enumBoolean());
-        }
-
-        $res = $this->addElement('select', $key, $options);
-
-        if ($default !== null) {
-            $this->getElement($key)->setValue($map[$default]);
+            $this->addElement('YesNo', $key, $options);
+            return $this->getElement($key)->setValue($default);
         }
-
-        return $res;
     }
 
     protected function optionalBoolean($key, $label, $description)
@@ -453,14 +434,6 @@ abstract class DirectorObjectForm extends QuickForm
         ));
     }
 
-    protected function enumBoolean()
-    {
-        return array(
-            'y'  => $this->translate('Yes'),
-            'n'  => $this->translate('No'),
-        );
-    }
-
     public function hasElement($name)
     {
         return $this->getElement($name) !== null;
diff --git a/library/Director/Web/Form/Element/OptionalYesNo.php b/library/Director/Web/Form/Element/OptionalYesNo.php
new file mode 100644
index 0000000..7ef6d7f
--- /dev/null
+++ b/library/Director/Web/Form/Element/OptionalYesNo.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace Icinga\Module\Director\Web\Form\Element;
+
+/**
+ * Input control for booleans, gives y/n
+ */
+class OptionalYesNo extends Boolean
+{
+    public function getValue()
+    {
+        $value = $this->getUnfilteredValue();
+
+        if ($value === 'y' || $value === true) {
+            return 'y';
+        } elseif ($value === 'n' || $value === false) {
+            return 'n';
+        }
+
+        return null;
+    }
+}
diff --git a/library/Director/Web/Form/Element/YesNo.php b/library/Director/Web/Form/Element/YesNo.php
new file mode 100644
index 0000000..3e8aaa7
--- /dev/null
+++ b/library/Director/Web/Form/Element/YesNo.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace Icinga\Module\Director\Web\Form\Element;
+
+/**
+ * Input control for booleans, gives y/n
+ */
+class YesNo extends OptionalYesNo
+{
+    public $options = array(
+        'y'  => 'Yes',
+        'n'  => 'No',
+    );
+}



More information about the icinga-checkins mailing list