[icinga-checkins] icinga.org: icingaweb2-module-director/feature/scheduled-downtimes-12138: ObjectForm: do not allow to choose type...

git at icinga.org git at icinga.org
Thu Oct 13 14:01:50 CEST 2016


Module: icingaweb2-module-director
Branch: feature/scheduled-downtimes-12138
Commit: 0e45429196024c24daf096b293eb8ee52482594b
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=0e45429196024c24daf096b293eb8ee52482594b

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Oct 13 11:53:01 2016 +0000

ObjectForm: do not allow to choose type...

...when one is already given

---

 .../Director/Web/Controller/ObjectController.php    |   19 +++++++++++++++----
 .../Director/Web/Controller/ObjectsController.php   |    4 +---
 library/Director/Web/Form/DirectorObjectForm.php    |   17 +++++++++++++++++
 3 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/library/Director/Web/Controller/ObjectController.php b/library/Director/Web/Controller/ObjectController.php
index eb600cf..e97bdda 100644
--- a/library/Director/Web/Controller/ObjectController.php
+++ b/library/Director/Web/Controller/ObjectController.php
@@ -184,10 +184,21 @@ abstract class ObjectController extends ActionController
             ->setApi($this->getApiIfAvailable())
             ->setSuccessUrl($url);
 
-        $this->view->title = sprintf(
-            $this->translate('Add new Icinga %s'),
-            ucfirst($ltype)
-        );
+        if ($type = $this->params->shift('type')) {
+            $form->setPreferredObjectType($type);
+        }
+
+        if ($type === 'template') {
+            $this->view->title = sprintf(
+                $this->translate('Add new Icinga %s template'),
+                ucfirst($ltype)
+            );
+        } else {
+            $this->view->title = sprintf(
+                $this->translate('Add new Icinga %s'),
+                ucfirst($ltype)
+            );
+        }
 
         $this->beforeHandlingAddRequest($form);
 
diff --git a/library/Director/Web/Controller/ObjectsController.php b/library/Director/Web/Controller/ObjectsController.php
index 432d52d..1941c57 100644
--- a/library/Director/Web/Controller/ObjectsController.php
+++ b/library/Director/Web/Controller/ObjectsController.php
@@ -104,12 +104,10 @@ abstract class ObjectsController extends ActionController
             $addParams = array('type' => 'template');
             $this->getTabs()->activate('templates');
             $title = $this->translate('Icinga ' . ucfirst($ltype) . ' Templates');
-            $addTitle = $this->translate('Add %s template');
             $table->enforceFilter(Filter::expression('object_type', '=', 'template'));
         } else {
             $addParams = array('type' => 'object');
             $title = $this->translate('Icinga ' . ucfirst($ltype) . 's');
-            $addTitle = $this->translate('Add %s');
             if ($dummy->supportsImports()
                 && array_key_exists('object_type', $table->getColumns())
                 && ! in_array(ucfirst($type), $this->globalTypes)
@@ -121,7 +119,7 @@ abstract class ObjectsController extends ActionController
         $this->view->title = $title;
 
         $this->view->addLink = $this->view->qlink(
-            sprintf($addTitle, $this->translate(ucfirst($ltype))),
+            $this->translate('Add'),
             'director/' . $ltype .'/add',
             $addParams,
             array('class' => 'icon-plus')
diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php
index 0844cad..d6478d6 100644
--- a/library/Director/Web/Form/DirectorObjectForm.php
+++ b/library/Director/Web/Form/DirectorObjectForm.php
@@ -30,14 +30,26 @@ abstract class DirectorObjectForm extends QuickForm
 
     protected $listUrl;
 
+    protected $preferredObjectType;
+
     private $allowsExperimental;
 
     private $api;
 
+    public function setPreferredObjectType($type)
+    {
+        $this->preferredObjectType = $type;
+        return $this;
+    }
+
     protected function object($values = array())
     {
         if ($this->object === null) {
             $class = $this->getObjectClassname();
+            if ($this->preferredObjectType && ! array_key_exists('object_type', $values)) {
+                $values['object_type'] = $this->preferredObjectType;
+            }
+
             $this->object = $class::create($values, $this->db);
             foreach ($this->getValues() as $key => $value) {
                 if ($this->object->hasProperty($key)) {
@@ -1056,6 +1068,11 @@ abstract class DirectorObjectForm extends QuickForm
             return;
         }
 
+        if ($this->preferredObjectType) {
+            $this->addHidden('object_type', $this->preferredObjectType);
+            return $this;
+        }
+
         $object = $this->object();
 
         if ($object->supportsImports()) {



More information about the icinga-checkins mailing list