[icinga-checkins] icinga.org: icingaweb2-module-director/bugfix/loop-templateresolver-11803: IcingaServiceSetForm: restructure the form

git at icinga.org git at icinga.org
Mon Oct 31 13:31:44 CET 2016


Module: icingaweb2-module-director
Branch: bugfix/loop-templateresolver-11803
Commit: 5de24c6ec439c3b49fd730519361e1e024c35eb9
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=5de24c6ec439c3b49fd730519361e1e024c35eb9

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Oct 20 07:22:56 2016 +0000

IcingaServiceSetForm: restructure the form

---

 application/forms/IcingaServiceSetForm.php |  124 ++++++++++++++++++++--------
 1 file changed, 88 insertions(+), 36 deletions(-)

diff --git a/application/forms/IcingaServiceSetForm.php b/application/forms/IcingaServiceSetForm.php
index efd9c1b..d21370c 100644
--- a/application/forms/IcingaServiceSetForm.php
+++ b/application/forms/IcingaServiceSetForm.php
@@ -12,8 +12,36 @@ class IcingaServiceSetForm extends DirectorObjectForm
 
     public function setup()
     {
-        $this->addImportsElement();
+        if ($this->host === null) {
+            $this->setupTemplate();
+        } else {
+            $this->setupHost();
+        }
+
+        $this->setupFields()
+             ->setButtons();
+    }
+
+    protected function setupFields()
+    {
+        $object = $this->object();
 
+        $this->assertResolvedImports();
+
+        if ($this->hasBeenSent() && $services = $this->getSentValue('service')) {
+            $object->service = $services;
+        }
+
+        if ($this->assertResolvedImports()) {
+            $this->fieldLoader($object)
+                ->loadFieldsForMultipleObjects($object->getServiceObjects());
+        }
+
+        return $this;
+    }
+
+    protected function setupTemplate()
+    {
         $this->addElement('text', 'object_name', array(
             'label'       => $this->translate('Service set name'),
             'description' => $this->translate(
@@ -21,50 +49,43 @@ class IcingaServiceSetForm extends DirectorObjectForm
             ),
             'required'    => true,
         ));
-        
-        $this->addElement('textarea', 'description', array(
-            'label'       => $this->translate('Description'),
-            'description' => $this->translate(
-                'A meaningful description explaining your users what to expect'
-                . ' when assigning this set of services'
+
+        $this->addHidden('object_type', 'template');
+        $this->addDescriptionElement();
+
+        $this->addElement('multiselect', 'service', array(
+            'label'        => $this->translate('Services'),
+            'description'  => $this->translate(
+                'Services in this set'
             ),
-            'rows'        => '3',
-            'required'    => ! $this->isTemplate(),
+            'rows'         => '5',
+            'multiOptions' => $this->enumServices(),
+            'required'     => true,
+            'class'        => 'autosubmit',
         ));
+    }
 
-
-        if ($this->host === null) {
-            $this->addHidden('object_type', 'object');
-
-            $this->addElement('multiselect', 'service', array(
-                'label'        => $this->translate('Services'),
-                'description'  => $this->translate(
-                    'Services in this set'
-                ),
-                'rows'         => '5',
-                'multiOptions' => $this->enumServices(),
-                'required'     => true,
-                'class'        => 'autosubmit',
-            ));
-        } else {
-            $this->addHidden('object_type', 'object');
-            $this->addHidden('host_id', $this->host->id);
+    protected function setupHost()
+    {
+       $object = $this->object();
+        if ($this->hasBeenSent()) {
+            $object->object_name = $object->imports = $this->getSentValue('imports');
         }
 
-        $services = array();
-        foreach ($this->getSentOrObjectValue('service') as $name) {
-            $services[] = IcingaService::load(array(
-                'object_name' => $name,
-                'object_type' => 'template'
-            ), $this->db);
+        if (! $object->hasBeenLoadedFromDb()) {
+            $this->addSingleImportsElement();
         }
 
-        if ($this->assertResolvedImports()) {
-            $loader = $this->fieldLoader($this->object);
-            $loader->loadFieldsForMultipleObjects($services);
+        if (count($object->imports)) {
+            $this->addHtmlHint(
+                $this->getView()->escape(
+                    $object->getResolvedProperty('description')
+                )
+            );
         }
 
-        $this->setButtons();
+        $this->addHidden('object_type', 'object');
+        $this->addHidden('host_id', $this->host->id);
     }
 
     public function setHost(IcingaHost $host)
@@ -72,6 +93,37 @@ class IcingaServiceSetForm extends DirectorObjectForm
         $this->host = $host;
         return $this;
     }
+    protected function addSingleImportsElement()
+    {
+        $enum = $this->enumAllowedTemplates();
+
+        $this->addElement('select', 'imports', array(
+            'label'        => $this->translate('Service set'),
+            'description'  => $this->translate(
+                'The service set that should be assigned to this host'
+            ),
+            'required'     => true,
+            'multiOptions' => $this->optionallyAddFromEnum($enum),
+            'class'        => 'autosubmit'
+        ));
+
+        return $this;
+    }
+
+    protected function addDescriptionElement()
+    {
+        $this->addElement('textarea', 'description', array(
+            'label'       => $this->translate('Description'),
+            'description' => $this->translate(
+                'A meaningful description explaining your users what to expect'
+                . ' when assigning this set of services'
+            ),
+            'rows'        => '3',
+            'required'    => ! $this->isTemplate(),
+        ));
+
+        return $this;
+    }
 
     protected function enumServices()
     {



More information about the icinga-checkins mailing list