[icinga-checkins] icinga.org: icingaweb2-module-director/master: DirectorObjectForm: some more steps to simplify...

git at icinga.org git at icinga.org
Wed Dec 14 21:11:08 CET 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Dec 14 21:07:07 2016 +0100

DirectorObjectForm: some more steps to simplify...

...this old monster

fixes #13583

---

 library/Director/Web/Form/DirectorObjectForm.php |   85 +++++++++++-----------
 1 file changed, 42 insertions(+), 43 deletions(-)

diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php
index f514fb5..4f25158 100644
--- a/library/Director/Web/Form/DirectorObjectForm.php
+++ b/library/Director/Web/Form/DirectorObjectForm.php
@@ -261,42 +261,27 @@ abstract class DirectorObjectForm extends QuickForm
 
     protected function handleProperties(DbObject $object, & $values)
     {
-        $resolve = $this->assertResolvedImports();
         if ($this->hasBeenSent()) {
             foreach ($values as $key => $value) {
-                if (in_array($key, $this->earlyProperties) || substr($key, 0, 4) === 'var_') {
-                    continue;
-                }
-
                 try {
-                    $object->set($key, $value);
-                    if ($object instanceof IcingaObject) {
-                        $object->resolveUnresolvedRelatedProperties();
+                    if ($object->hasProperty($key)) {
+                        $object->set($key, $value);
+                        if ($object instanceof IcingaObject) {
+                            $object->resolveUnresolvedRelatedProperties();
+                        }
                     }
-
                 } catch (Exception $e) {
                     $this->addException($e, $key);
                 }
             }
         }
+    }
 
-        if ($object instanceof IcingaObject) {
-            $props = (array) $object->toPlainObject(
-                false,
-                false,
-                null,
-                true // is default//false // Do not resolve IDs
-            );
-        } else {
-            $props = $object->getProperties();
-            unset($props['vars']);
-        }
-
-        $this->setDefaults($this->removeEmptyProperties($props));
-
-        if ($resolve) {
+    protected function loadInheritedProperties()
+    {
+        if ($this->assertResolvedImports()) {
             try {
-                $this->showInheritedProperties($object);
+                $this->showInheritedProperties($this->object());
             } catch (Exception $e) {
                 $this->addException($e);
             }
@@ -639,37 +624,51 @@ abstract class DirectorObjectForm extends QuickForm
 
     protected function onRequest()
     {
-        $values = array();
-
         $object = $this->object();
+        $this->setDefaultsFromObject($object);
         $this->prepareFields($object);
         if ($this->hasBeenSent()) {
+            $this->handlePost();
+        }
+        $this->loadInheritedProperties();
+        $this->addFields();
+    }
 
-            if ($this->shouldBeDeleted()) {
-                $this->deleteObject($object);
-            }
+    protected function handlePost()
+    {
+        $object = $this->object();
+        if ($this->shouldBeDeleted()) {
+            $this->deleteObject($object);
+        }
 
-            $post = $this->getRequest()->getPost();
-            $this->populate($post);
-            $values = $this->getValues();
+        $post = $this->getRequest()->getPost();
+        $this->populate($post);
+        $values = $this->getValues();
 
-            if ($object instanceof IcingaObject) {
-                $this->setCustomVarValues($object, $post);
-            }
+        if ($object instanceof IcingaObject) {
+            $this->setCustomVarValues($object, $post);
         }
-        $this->addFields();
 
+        $this->handleProperties($object, $values);
+
+        // TODO: get rid of this
         if ($object instanceof IcingaObject) {
             $this->handleRanges($object, $values);
         }
-        $this->handleProperties($object, $values);
+    }
 
-        /*
-        // TODO: something like this could be used to remember unstored changes
-        if ($object->hasBeenModified()) {
-            $this->addHtmlHint($this->translate('Object has been modified'));
+    protected function setDefaultsFromObject(DbObject $object)
+    {
+        /** @var ZfElement $element */
+        foreach ($this->getElements() as $element) {
+            $key = $element->getName();
+            if ($object->hasProperty($key)) {
+                $value = $object->get($key);
+                if ($value !== null) {
+                    $element->setValue($value);
+                }
+            }
         }
-        */
     }
 
     protected function deleteObject($object)



More information about the icinga-checkins mailing list