[icinga-checkins] icinga.org: icingaweb2-module-director/fiddle/mfrosch: DataTypeBoolean: provide a new data type

git at icinga.org git at icinga.org
Thu May 19 16:46:16 CEST 2016


Module: icingaweb2-module-director
Branch: fiddle/mfrosch
Commit: 41731241c51e6b0b00284d94a84ff98deb52f66e
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=41731241c51e6b0b00284d94a84ff98deb52f66e

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu May 19 14:59:25 2016 +0200

DataTypeBoolean: provide a new data type

fixes #11594

---

 library/Director/DataType/DataTypeBoolean.php    |   29 ++++++++++
 library/Director/Web/Form/DirectorObjectForm.php |   18 ++++++
 library/Director/Web/Form/Element/Boolean.php    |   64 ++++++++++++++++++++++
 run.php                                          |    1 +
 4 files changed, 112 insertions(+)

diff --git a/library/Director/DataType/DataTypeBoolean.php b/library/Director/DataType/DataTypeBoolean.php
new file mode 100644
index 0000000..aa2cd5d
--- /dev/null
+++ b/library/Director/DataType/DataTypeBoolean.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Icinga\Module\Director\DataType;
+
+use Icinga\Module\Director\Hook\DataTypeHook;
+use Icinga\Module\Director\Web\Form\Decorator\ViewHelperRaw;
+use Icinga\Module\Director\Web\Form\QuickForm;
+
+class DataTypeBoolean extends DataTypeHook
+{
+    public function getFormElement($name, QuickForm $form)
+    {
+        return $this->applyRawViewHelper(
+            $form->createElement('boolean', $name)
+        );
+    }
+
+    protected function applyRawViewHelper($element)
+    {
+        $vhClass = 'Zend_Form_Decorator_ViewHelper';
+        $decorators = $element->getDecorators();
+        if (array_key_exists($vhClass, $decorators)) {
+            $decorators[$vhClass] = new ViewHelperRaw;
+            $element->setDecorators($decorators);
+        }
+
+        return $element;
+    }
+}
diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php
index dd97750..6843b5c 100644
--- a/library/Director/Web/Form/DirectorObjectForm.php
+++ b/library/Director/Web/Form/DirectorObjectForm.php
@@ -298,6 +298,24 @@ abstract class DirectorObjectForm extends QuickForm
                     }
 
                     $mykey = substr($key, 4);
+
+                    // Get value through form element.
+                    // TODO: reorder the related code. Create elements once
+                    if (property_exists($fields, $mykey)) {
+                        $field = $fields->$mykey;
+                        $datafield = DirectorDatafield::load($field->datafield_id, $this->getDb());
+                        $name = 'var_' . $datafield->varname;
+                        $className = $datafield->datatype;
+
+                        if (class_exists($className)) {
+                            $datatype = new $className;
+                            $datatype->setSettings($datafield->getSettings());
+                            $el = $datatype->getFormElement($name, $this);
+                        }
+
+                        $value = $el->setValue($value)->getValue();
+                    }
+
                     if (property_exists($fields, $mykey) && $fields->$mykey->format === 'json') {
                         $value = json_decode($value);
                     }
diff --git a/library/Director/Web/Form/Element/Boolean.php b/library/Director/Web/Form/Element/Boolean.php
new file mode 100644
index 0000000..f043208
--- /dev/null
+++ b/library/Director/Web/Form/Element/Boolean.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Icinga\Module\Director\Web\Form\Element;
+
+use Zend_Form_Element_Select as ZfSelect;
+
+/**
+ * Input control for booleans
+ */
+class Boolean extends ZfSelect
+{
+    public $options = array(
+        null => '- please choose -',
+        'y'  => 'Yes',
+        'n'  => 'No',
+    );
+
+    public function getValue()
+    {
+        $value = $this->getUnfilteredValue();
+
+        if ($value === 'y' || $value === true) {
+            return true;
+        } elseif ($value === 'n' || $value === false) {
+            return false;
+        }
+
+        return null;
+    }
+
+    public function isValid($value, $context = null)
+    {
+        return $value === 'y'
+            || $value === 'n'
+            || $value === null
+            || $value === true
+            || $value === false;
+    }
+
+    public function setValue($value)
+    {
+        if ($value === true) {
+            $value = 'y';
+        } elseif ($value === false) {
+            $value = 'n';
+        }
+
+        return parent::setValue($value);
+    }
+
+    protected function _translateOption($option, $value)
+    {
+        if (!isset($this->_translated[$option]) && !empty($value)) {
+            $this->options[$option] = mt('director', $value);
+            if ($this->options[$option] === $value) {
+                return false;
+            }
+            $this->_translated[$option] = true;
+            return true;
+        }
+
+        return false;
+    }
+}
diff --git a/run.php b/run.php
index c3e770d..772d33a 100644
--- a/run.php
+++ b/run.php
@@ -15,6 +15,7 @@ $this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeString');
 $this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeArray');
 $this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeNumber');
 $this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeTime');
+$this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeBoolean');
 $this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeDatalist');
 $this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeSqlQuery');
 



More information about the icinga-checkins mailing list