[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaServiceForm: restructure to allow more...

git at icinga.org git at icinga.org
Mon Jan 2 10:52:28 CET 2017


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Mon Jan  2 10:16:37 2017 +0100

IcingaServiceForm: restructure to allow more...

...variants with less code

refs #13849

---

 application/forms/IcingaServiceForm.php |  135 ++++++++++++++++---------------
 1 file changed, 71 insertions(+), 64 deletions(-)

diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php
index 8b7ca72..8b293b5 100644
--- a/application/forms/IcingaServiceForm.php
+++ b/application/forms/IcingaServiceForm.php
@@ -39,18 +39,7 @@ class IcingaServiceForm extends DirectorObjectForm
 
     public function setup()
     {
-        if ($this->object && $this->object->usesVarOverrides()) {
-            $this->setupForVarOverrides();
-            return;
-        }
-
-        if ($this->hostGenerated) {
-            $this->setupHostGenerated();
-            return;
-        }
-
-        if ($this->inheritedFrom) {
-            $this->setupInherited();
+        if ($this->providesOverrides()) {
             return;
         }
 
@@ -71,6 +60,46 @@ class IcingaServiceForm extends DirectorObjectForm
         }
     }
 
+    protected function providesOverrides()
+    {
+        return ($this->object && $this->object->usesVarOverrides())
+            || $this->hostGenerated
+            || $this->inheritedFrom;
+    }
+
+    protected function onAddedFields()
+    {
+        if (! $this->providesOverrides()) {
+            return;
+        }
+
+        $this->addHtmlHint(
+            $this->getOverrideHint(),
+            array('name' => 'inheritance_hint')
+        );
+
+        $group = $this->getDisplayGroup('custom_fields');
+
+        if ($group) {
+            $elements = $group->getElements();
+            $group->setElements(array($this->getElement('inheritance_hint')));
+            $group->addElements($elements);
+            $this->setSubmitLabel(
+                $this->translate('Override vars')
+            );
+
+        } else {
+            $this->addElementsToGroup(
+                array('inheritance_hint'),
+                'custom_fields',
+                20,
+                $this->translate('Hints regarding this service')
+            );
+
+            $this->setSubmitLabel(false);
+        }
+    }
+
     public function createApplyRuleFor(IcingaService $service)
     {
         $this->apply = $service;
@@ -104,63 +133,41 @@ class IcingaServiceForm extends DirectorObjectForm
              ->setButtons();
     }
 
-    protected function setupForVarOverrides()
+    protected function getOverrideHint()
     {
-        $msg = $this->translate(
-            'This service has been generated in an automated way, but still'
-            . ' allows you to override the following properties in a safe way.'
-        );
-
-        $this->addHtmlHint($msg);
-        $this->setButtons();
-        $this->setSubmitLabel(
-            $this->translate('Override vars')
-        );
-    }
-
-    protected function setupHostGenerated()
-    {
-        $msg = $this->translate(
-            'This service has been generated from host properties.'
-        );
-
-        $this->addHtmlHint($msg);
-
-        $this->setSubmitLabel(
-            $this->translate('Override vars')
-        );
-    }
+        if ($this->object && $this->object->usesVarOverrides()) {
+            return $this->translate(
+                'This service has been generated in an automated way, but still'
+                . ' allows you to override the following properties in a safe way.'
+            );
+        }
 
-    protected function setupInherited()
-    {
-        $view = $this->getView();
-        $msg = $view->escape($this->translate(
-            'This service has been inherited from %s. Still, you might want'
-            . ' to change the following properties for this host only.'
-        ));
+        if ($this->hostGenerated) {
+            return $this->translate(
+                'This service has been generated from host properties.'
+            );
+        }
 
-        $name = $this->inheritedFrom;
-        $link = $view->qlink(
-            $name,
-            'director/service',
-            array(
-                'host' => $name,
-                'name' => $this->object->object_name,
-            ),
-            array('data-base-target' => '_next')
-        );
+        if ($this->inheritedFrom) {
+            $view = $this->getView();
+            $msg = $view->escape($this->translate(
+                'This service has been inherited from %s. Still, you might want'
+                . ' to change the following properties for this host only.'
+            ));
 
-        $this->addHtmlHint(
-            sprintf($msg, $link),
-            array('name' => 'inheritance_hint')
-        );
+            $name = $this->inheritedFrom;
+            $link = $view->qlink(
+                $name,
+                'director/service',
+                array(
+                    'host' => $name,
+                    'name' => $this->object->object_name,
+                ),
+                array('data-base-target' => '_next')
+            );
 
-        $this->addElementsToGroup(
-            array('inheritance_hint'),
-            'custom_fields',
-            50,
-            $this->translate('Custom properties')
-        );
+            return sprintf($msg, $link);
+        }
 
         $this->setSubmitLabel(
             $this->translate('Override vars')



More information about the icinga-checkins mailing list