[icinga-checkins] icinga.org: icingaweb2-module-director/master: ObjectForm: allow to preset imports

git at icinga.org git at icinga.org
Wed Nov 16 16:20:07 CET 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Nov 16 16:02:28 2016 +0100

ObjectForm: allow to preset imports

---

 .../Director/Web/Controller/ObjectController.php   |    3 +++
 library/Director/Web/Form/DirectorObjectForm.php   |   22 +++++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/library/Director/Web/Controller/ObjectController.php b/library/Director/Web/Controller/ObjectController.php
index b43be53..779ec34 100644
--- a/library/Director/Web/Controller/ObjectController.php
+++ b/library/Director/Web/Controller/ObjectController.php
@@ -9,6 +9,7 @@ use Icinga\Exception\NotFoundError;
 use Icinga\Module\Director\Exception\NestingError;
 use Icinga\Module\Director\IcingaConfig\IcingaConfig;
 use Icinga\Module\Director\Objects\IcingaObject;
+use Icinga\Module\Director\Web\Form\DirectorObjectForm;
 use Icinga\Web\Url;
 
 abstract class ObjectController extends ActionController
@@ -186,8 +187,10 @@ abstract class ObjectController extends ActionController
         $ltype = strtolower($type);
 
         $url = sprintf('director/%ss', $ltype);
+        /** @var DirectorObjectForm $form */
         $form = $this->view->form = $this->loadForm('icinga' . ucfirst($type))
             ->setDb($this->db())
+            ->presetImports($this->params->shift('imports'))
             ->setApi($this->getApiIfAvailable())
             ->setSuccessUrl($url);
 
diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php
index b50dbe2..868d567 100644
--- a/library/Director/Web/Form/DirectorObjectForm.php
+++ b/library/Director/Web/Form/DirectorObjectForm.php
@@ -45,12 +45,27 @@ abstract class DirectorObjectForm extends QuickForm
     /** @var  CoreApi */
     private $api;
 
+    private $presetImports;
+
     public function setPreferredObjectType($type)
     {
         $this->preferredObjectType = $type;
         return $this;
     }
 
+    public function presetImports($imports)
+    {
+        if (! empty($imports)) {
+            if (is_array($imports)) {
+                $this->presetImports = $imports;
+            } else {
+                $this->presetImports = array($imports);
+            }
+        }
+
+        return $this;
+    }
+
     /**
      * @param array $values
      *
@@ -248,7 +263,7 @@ abstract class DirectorObjectForm extends QuickForm
             unset($props['vars']);
         }
 
-        $this->setDefaults($this->removeNullProperties($props));
+        $this->setDefaults($this->removeEmptyProperties($props));
 
         if ($resolve) {
             $this->showInheritedProperties($object);
@@ -270,11 +285,11 @@ abstract class DirectorObjectForm extends QuickForm
         }
     }
 
-    protected function removeNullProperties($props)
+    protected function removeEmptyProperties($props)
     {
         $result = array();
         foreach ($props as $k => $v) {
-            if ($v !== null && $v !== '') {
+            if ($v !== null && $v !== '' && $v !== array()) {
                 $result[$k] = $v;
             }
         }
@@ -913,6 +928,7 @@ abstract class DirectorObjectForm extends QuickForm
             'required'     => ($required !== null ? $required : !$this->isTemplate()),
             'multiOptions' => $this->optionallyAddFromEnum($enum),
             'sorted'       => true,
+            'value'        => $this->presetImports,
             'class'        => 'autosubmit'
         ));
 



More information about the icinga-checkins mailing list