From 0e45429196024c24daf096b293eb8ee52482594b Mon Sep 17 00:00:00 2001
From: Thomas Gelf <thomas@gelf.net>
Date: Thu, 13 Oct 2016 11:53:01 +0000
Subject: [PATCH] ObjectForm: do not allow to choose type...

...when one is already given
---
 .../Web/Controller/ObjectController.php       | 19 +++++++++++++++----
 .../Web/Controller/ObjectsController.php      |  4 +---
 .../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 eb600cfc..e97bdda5 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 432d52d5..1941c576 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 0844cad3..d6478d64 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()) {