From 77e4a8c2f48759c6b04bc9c208a487b68d062a64 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Wed, 16 Oct 2019 14:47:40 +0200
Subject: [PATCH] :ambulance: fix(templates) Make sure %askme% is available is
 selects at template creation

Reviewed the template creation so that is uses a new objects::createTemplate method,
 and the simplePlugin tabs are aware that they are part of template in
 the constructor (no need to call setTemplateMode later anymore).

issue #6039
---
 include/class_objects.inc               | 16 ++++++++++++++++
 include/management/class_management.inc | 12 +++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/include/class_objects.inc b/include/class_objects.inc
index aaf4945b0..9692f425c 100644
--- a/include/class_objects.inc
+++ b/include/class_objects.inc
@@ -391,6 +391,22 @@ class objects
     return static::open('new', $type);
   }
 
+  static function createTemplate ($type)
+  {
+    $infos    = static::infos($type);
+    $tabClass = $infos['tabClass'];
+
+    /* Pass fake attrs object to force template mode */
+    $attrsObject  = new stdClass();
+    $attrsObject->attrs = [];
+    $attrsObject->is_template = TRUE;
+
+    $tabObject = new $tabClass($type, 'new', $attrsObject);
+    @DEBUG(DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, "Create template of $type");
+
+    return $tabObject;
+  }
+
   static function &infos ($type)
   {
     global $config;
diff --git a/include/management/class_management.inc b/include/management/class_management.inc
index 289fc914d..0bb9b1111 100644
--- a/include/management/class_management.inc
+++ b/include/management/class_management.inc
@@ -722,9 +722,15 @@ class management
     if (static::$skipTemplates) {
       return;
     }
-    $action['subaction'] = preg_replace('/^template_/', '', $action['subaction']);
-    $this->newEntry($action);
-    $this->tabObject->setTemplateMode('template_cn');
+    $type = preg_replace('/^template_/', '', $action['subaction']);
+
+    $this->currentDn = 'new';
+
+    set_object_info($this->currentDn);
+
+    // Open object
+    $this->openTabObject(objects::createTemplate($type));
+    @DEBUG(DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $this->currentDn, 'Create template entry initiated');
   }
 
   function newEntryFromTemplate (array $action)
-- 
GitLab