From 92de9435a67d4e57ceb5fd4a045143926f3800bd Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 27 Jan 2020 10:54:19 +0100
Subject: [PATCH] FEAT #12167 TIME 0:45 Delete doctypes templates

---
 migration/20.03/2003-postScript.sql           |  2 +
 migration/20.03/2003.sql                      |  2 +-
 .../class/class_modules_tools_Abstract.php    | 72 ++++++++-----------
 .../class/templates_controler_Abstract.php    | 29 --------
 .../templates/templates_tables_definition.php |  4 --
 modules/templates/xml/config.xml              |  1 -
 sql/data_fr.sql                               | 14 +---
 sql/structure.sql                             |  9 ---
 .../controllers/FirstLevelController.php      |  1 -
 .../doctypes-administration.component.ts      |  3 -
 .../template-administration.component.html    |  1 -
 .../app/doctype/DoctypeControllerTest.php     |  4 --
 12 files changed, 36 insertions(+), 106 deletions(-)

diff --git a/migration/20.03/2003-postScript.sql b/migration/20.03/2003-postScript.sql
index 0a70245bfd1..14ef07fe8d3 100644
--- a/migration/20.03/2003-postScript.sql
+++ b/migration/20.03/2003-postScript.sql
@@ -71,6 +71,8 @@ ALTER TABLE shippings DROP COLUMN IF EXISTS is_version;
 
 ALTER TABLE priorities DROP COLUMN IF EXISTS default_priority;
 
+UPDATE templates SET template_target = 'indexingFile', template_attachment_type = 'all' WHERE template_target = 'doctypes';
+
 DROP TABLE IF EXISTS doctypes_indexes;
 
 DROP TABLE IF EXISTS listmodels;
diff --git a/migration/20.03/2003.sql b/migration/20.03/2003.sql
index 99efcccfe4f..c0364a43b41 100644
--- a/migration/20.03/2003.sql
+++ b/migration/20.03/2003.sql
@@ -704,7 +704,7 @@ ALTER TABLE res_attachments DROP COLUMN IF EXISTS tnl_filename;
 ALTER TABLE users DROP COLUMN IF EXISTS custom_t1;
 ALTER TABLE users DROP COLUMN IF EXISTS custom_t2;
 ALTER TABLE users DROP COLUMN IF EXISTS custom_t3;
-
+DROP TABLE IF EXISTS templates_doctype_ext;
 
 /* M2M */
 DO $$ BEGIN
diff --git a/modules/templates/class/class_modules_tools_Abstract.php b/modules/templates/class/class_modules_tools_Abstract.php
index 2b7eeb19eea..05159a8edfe 100755
--- a/modules/templates/class/class_modules_tools_Abstract.php
+++ b/modules/templates/class/class_modules_tools_Abstract.php
@@ -34,7 +34,7 @@
 
 abstract class templates_Abstract extends Database
 {
-    function __construct()
+    public function __construct()
     {
         parent::__construct();
     }
@@ -44,19 +44,14 @@ abstract class templates_Abstract extends Database
     */
     public function build_modules_tables()
     {
-        if(file_exists($_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR."modules".DIRECTORY_SEPARATOR."templates".DIRECTORY_SEPARATOR."xml".DIRECTORY_SEPARATOR."config.xml"))
-        {
+        if (file_exists($_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR."modules".DIRECTORY_SEPARATOR."templates".DIRECTORY_SEPARATOR."xml".DIRECTORY_SEPARATOR."config.xml")) {
             $path_config = $_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR."modules".DIRECTORY_SEPARATOR."templates".DIRECTORY_SEPARATOR."xml".DIRECTORY_SEPARATOR."config.xml";
-        }
-        else
-        {
+        } else {
             $path_config = "modules".DIRECTORY_SEPARATOR."templates".DIRECTORY_SEPARATOR."xml".DIRECTORY_SEPARATOR."config.xml";
         }
         $xmlconfig = simplexml_load_file($path_config);
-        foreach($xmlconfig->TABLENAME as $TABLENAME)
-        {
+        foreach ($xmlconfig->TABLENAME as $TABLENAME) {
             $_SESSION['tablename']['temp_templates'] = (string) $TABLENAME->temp_templates;
-            $_SESSION['tablename']['temp_templates_doctype_ext'] = (string) $TABLENAME->temp_templates_doctype_ext;
             $_SESSION['tablename']['temp_templates_association'] = (string) $TABLENAME->temp_templates_association;
         }
         $HISTORY = $xmlconfig->HISTORY;
@@ -68,60 +63,53 @@ abstract class templates_Abstract extends Database
 
     public function getAllTemplates()
     {
-			$db = new Database();
-            $return = array();
+        $db = new Database();
+        $return = array();
               
-            $stmt = $db->query("select * from ".$_SESSION['tablename']['temp_templates']);
+        $stmt = $db->query("select * from ".$_SESSION['tablename']['temp_templates']);
             
-            while ($result = $stmt->fetchObject())
-            {
-                $this_template = array();
-                $this_template['id'] = $result->id;
-                $this_template['label'] = $result->label;
-                $this_template['template_comment'] = $result->template_comment;
+        while ($result = $stmt->fetchObject()) {
+            $this_template = array();
+            $this_template['id'] = $result->id;
+            $this_template['label'] = $result->label;
+            $this_template['template_comment'] = $result->template_comment;
                 
-                array_push($return, $this_template);
-            }
+            array_push($return, $this_template);
+        }
             
-            return $return;
+        return $return;
     }
     
     
     public function getAllItemsLinkedToModel($template_id, $field ='')
     {
-		$db = new Database();
+        $db = new Database();
         $items = array();
-        if(empty($template_id))
-        {
+        if (empty($template_id)) {
             return $items;
         }
 
-        if(empty($field))
-        {
+        if (empty($field)) {
             $items['destination'] = [];
-            foreach(array_keys($items) as $key)
-            {
-                $stmt2 = $db->query("select value_field from ".$_SESSION['tablename']['temp_templates_association']." where template_id = ? ",
-									array($template_id)
-						);
-                while($res = $stmt2->fetchOject())
-                {
+            foreach (array_keys($items) as $key) {
+                $stmt2 = $db->query(
+                    "select value_field from ".$_SESSION['tablename']['temp_templates_association']." where template_id = ? ",
+                                    array($template_id)
+                        );
+                while ($res = $stmt2->fetchOject()) {
                     array_push($items[$key], $res->value_field);
                 }
             }
-        }
-        else
-        {
+        } else {
             $items[$field] = [];
-            $stmt = $db->query("select value_field from ".$_SESSION['tablename']['temp_templates_association']." where template_id = ? ",
-							array($template_id)
-					);
-            while($res = $stmt->fetchObject())
-            {
+            $stmt = $db->query(
+                "select value_field from ".$_SESSION['tablename']['temp_templates_association']." where template_id = ? ",
+                            array($template_id)
+                    );
+            while ($res = $stmt->fetchObject()) {
                 array_push($items[$field], $res->value_field);
             }
         }
         return $items;
     }
-
 }
diff --git a/modules/templates/class/templates_controler_Abstract.php b/modules/templates/class/templates_controler_Abstract.php
index d8285272e22..7b6e5994808 100755
--- a/modules/templates/class/templates_controler_Abstract.php
+++ b/modules/templates/class/templates_controler_Abstract.php
@@ -322,14 +322,6 @@ abstract class templates_controler_Abstract extends ObjectControler implements O
             );
             return $control;
         }
-        if ($this->linkExists($template->template_id)) {
-            $control = array(
-                'status' => 'ko',
-                'value' => '',
-                'error' => _LINK_EXISTS,
-            );
-            return $control;
-        }
         $db = new Database();
         $query = "delete from "._TEMPLATES_TABLE_NAME." where template_id = ? " ;
             
@@ -433,27 +425,6 @@ abstract class templates_controler_Abstract extends ObjectControler implements O
         }
         return false;
     }
-
-    /**
-    * Checks if the template is linked
-    *
-    * @param $template_id templates identifier
-    * @return bool true if the template is linked
-    */
-    public function linkExists($template_id)
-    {
-        if (!isset($template_id) || empty($template_id)) {
-            return false;
-        }
-        $db = new Database();
-
-        $query = "select template_id from " . _TEMPLATES_DOCTYPES_EXT_TABLE_NAME
-            . " where template_id = ? ";
-        $stmt = $db->query($query, array($template_id));
-        if ($stmt->rowCount() > 0) {
-            return true;
-        }
-    }
     
     /**
     * Return the last templateId
diff --git a/modules/templates/templates_tables_definition.php b/modules/templates/templates_tables_definition.php
index 9ba0b7a2258..c79265f01fe 100755
--- a/modules/templates/templates_tables_definition.php
+++ b/modules/templates/templates_tables_definition.php
@@ -26,7 +26,3 @@ if (!defined('_TEMPLATES_TABLE_NAME')) {
 if (!defined('_TEMPLATES_ASSOCIATION_TABLE_NAME')) {
     define('_TEMPLATES_ASSOCIATION_TABLE_NAME', 'templates_association');
 }
-
-if (!defined('_TEMPLATES_DOCTYPES_EXT_TABLE_NAME')) {
-    define('_TEMPLATES_DOCTYPES_EXT_TABLE_NAME', 'templates_doctype_ext');
-}
diff --git a/modules/templates/xml/config.xml b/modules/templates/xml/config.xml
index d22a5eaa13b..554027b9bac 100755
--- a/modules/templates/xml/config.xml
+++ b/modules/templates/xml/config.xml
@@ -8,7 +8,6 @@
     </CONFIG>
     <TABLENAME>
         <temp_templates>templates</temp_templates>
-        <temp_templates_doctype_ext>templates_doctype_ext</temp_templates_doctype_ext>
         <temp_templates_association>templates_association</temp_templates_association>
     </TABLENAME>
     <HISTORY>
diff --git a/sql/data_fr.sql b/sql/data_fr.sql
index 32a89d5c980..fbe83282d34 100755
--- a/sql/data_fr.sql
+++ b/sql/data_fr.sql
@@ -223,7 +223,6 @@ INSERT INTO usergroups_services (group_id, service_id) VALUES ('MAARCHTOGEC', 's
 TRUNCATE TABLE DOCTYPES_FIRST_LEVEL;
 TRUNCATE TABLE DOCTYPES_SECOND_LEVEL;
 TRUNCATE TABLE DOCTYPES;
-TRUNCATE TABLE TEMPLATES_DOCTYPE_EXT;
 
 INSERT INTO doctypes_first_level (doctypes_first_level_id, doctypes_first_level_label, css_style, enabled) VALUES (1, 'COURRIERS', '#000000', 'Y');
 INSERT INTO doctypes_second_level (doctypes_second_level_id, doctypes_second_level_label, doctypes_first_level_id, css_style, enabled) VALUES (1, '01. Correspondances', 1, '#000000', 'Y');
@@ -1176,13 +1175,6 @@ INSERT INTO groupbasket_redirect (group_id, basket_id, action_id, entity_id, key
 INSERT INTO groupbasket_redirect (group_id, basket_id, action_id, entity_id, keyword, redirect_mode) VALUES ('RESPONSABLE', 'DepartmentBasket', 1, '', 'ALL_ENTITIES', 'ENTITY');
 Select setval('groupbasket_redirect_system_id_seq', (select max(system_id)+1 from groupbasket_redirect), false);
 
-------------
---TEMPLATES_DOCTYPE_EXT--
-------------
-TRUNCATE TABLE templates_doctype_ext;
-INSERT INTO templates_doctype_ext (template_id, type_id, is_generated) VALUES (3, 1201, 'Y');
-INSERT INTO templates_doctype_ext (template_id, type_id, is_generated) VALUES (9, 1202, 'Y');
-
 ------------
 --KEYWORDS / TAGS
 ------------
@@ -1229,7 +1221,7 @@ INSERT INTO templates (template_id, template_label, template_comment, template_c
 </table>
 <p>&nbsp;</p>
 <h4>Notes :</h4>
-<p>&nbsp;</p>', 'HTML', NULL, NULL, '', '', 'doctypes', 'all');
+<p>&nbsp;</p>', 'HTML', NULL, NULL, '', '', 'indexingFile', 'all');
 INSERT INTO templates (template_id, template_label, template_comment, template_content, template_type, template_path, template_file_name, template_style, template_datasource, template_target)
 VALUES (2, '[notification] Notifications événement', 'Notifications des événements système',
 '<p><font face="verdana,geneva" size="1">Bonjour [recipient.firstname] [recipient.lastname],</font></p>
@@ -1392,7 +1384,7 @@ INSERT INTO templates (template_id, template_label, template_comment, template_c
 <td>&nbsp;</td>
 </tr>
 </tbody>
-</table>', 'HTML', NULL, NULL, 'DOCX: demo_document_msoffice', '', 'doctypes', 'all');
+</table>', 'HTML', NULL, NULL, 'DOCX: demo_document_msoffice', '', 'indexingFile', 'all');
 INSERT INTO templates VALUES (10, '[maarch mairie] Clôture de demande', '[maarch mairie] Clôture de demande', '<p style="text-align: left;"><span style="font-size: small;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="text-decoration: underline;"><span style="font-size: small;">CLOTURE DEMANDE Maarch Mairie - [res_letterbox.type_label] - [res_letterbox.res_id] </span></span></p>
 <p style="text-align: center;">&nbsp;</p>
 <table style="background-color: #a8c33c; width: 800px; border: #000000 1pt solid;" border="1" cellspacing="1" cellpadding="5">
@@ -1435,7 +1427,7 @@ INSERT INTO templates VALUES (10, '[maarch mairie] Clôture de demande', '[maarc
 <td style="background-color: #e1f787;">&nbsp;</td>
 </tr>
 </tbody>
-</table>', 'HTML', NULL, NULL, 'DOCX: demo_document_msoffice', '', 'doctypes');
+</table>', 'HTML', NULL, NULL, 'DOCX: demo_document_msoffice', '', 'indexingFile');
 INSERT INTO templates (template_id, template_label, template_comment, template_content, template_type, template_path, template_file_name, template_style, template_datasource, template_target, template_attachment_type) VALUES (20, 'Accompagnement courriel', 'Modèle de courriel d''''accompagnement', '<p>Bonjour,</p>
 <p>En r&eacute;ponse &agrave; votre courrier en date du [res_letterbox.doc_date], veuillez trouver notre r&eacute;ponse en pi&egrave;ce-jointe.</p>
 <p>Cordialement,</p>
diff --git a/sql/structure.sql b/sql/structure.sql
index 89026a71828..44fd89269a7 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -777,15 +777,6 @@ WITH (
   OIDS=FALSE
 );
 
-
-CREATE TABLE templates_doctype_ext
-(
-  template_id bigint DEFAULT NULL,
-  type_id integer NOT NULL,
-  is_generated character(1) NOT NULL DEFAULT 'N'::bpchar
-)
-WITH (OIDS=FALSE);
-
 CREATE TABLE contacts
 (
     id SERIAL NOT NULL,
diff --git a/src/app/doctype/controllers/FirstLevelController.php b/src/app/doctype/controllers/FirstLevelController.php
index f096c837e22..c52a243b401 100755
--- a/src/app/doctype/controllers/FirstLevelController.php
+++ b/src/app/doctype/controllers/FirstLevelController.php
@@ -114,7 +114,6 @@ class FirstLevelController
             'data'      => ['Y'],
             'order_by'  => ['doctypes_second_level_label asc']
         ]);
-        $obj['models'] = TemplateModel::getByTarget(['select' => ['template_id', 'template_label', 'template_comment'], 'template_target' => 'doctypes']);
         $obj['indexes'] = [];
 
         return $response->withJson($obj);
diff --git a/src/frontend/app/administration/doctype/doctypes-administration.component.ts b/src/frontend/app/administration/doctype/doctypes-administration.component.ts
index 989481370b6..97abb14467a 100755
--- a/src/frontend/app/administration/doctype/doctypes-administration.component.ts
+++ b/src/frontend/app/administration/doctype/doctypes-administration.component.ts
@@ -35,7 +35,6 @@ export class DoctypesAdministrationComponent implements OnInit {
     types: any = false;
     secondLevels: any = false;
     processModes: any = false;
-    models: any = false;
 
     loading: boolean = false;
     creationMode: any = false;
@@ -160,7 +159,6 @@ export class DoctypesAdministrationComponent implements OnInit {
                     this.currentType  = dataValue['doctype'];
                     this.secondLevels = dataValue['secondLevel'];
                     this.processModes = ['NORMAL', 'SVA', 'SVR'];
-                    this.models       = dataValue['models'];
 
                     if(move){
                         if(this.currentType){
@@ -419,7 +417,6 @@ export class DoctypesAdministrationComponent implements OnInit {
                 this.firstLevels  = data['firstLevel'];
                 this.secondLevels = data['secondLevel'];
                 this.processModes = ['NORMAL', 'SVA', 'SVR'];
-                this.models       = data['models'];
             }, (err) => {
                 this.notify.error(err.error.errors);
             });
diff --git a/src/frontend/app/administration/template/template-administration.component.html b/src/frontend/app/administration/template/template-administration.component.html
index 7abe2b327c4..674cce9d3f1 100755
--- a/src/frontend/app/administration/template/template-administration.component.html
+++ b/src/frontend/app/administration/template/template-administration.component.html
@@ -51,7 +51,6 @@
                                         <mat-option value="indexingFile">{{lang.indexingFile}}</mat-option>
                                         <mat-option value="notifications">{{lang.notifications}}</mat-option>
                                         <mat-option value="attachments">{{lang.attachments}}</mat-option>
-                                        <mat-option value="doctypes">{{lang.doctypes}}</mat-option>
                                     </mat-select>
                                 </mat-form-field>
                             </div>
diff --git a/test/unitTests/app/doctype/DoctypeControllerTest.php b/test/unitTests/app/doctype/DoctypeControllerTest.php
index c28ac68adfd..a81acdeaf1e 100755
--- a/test/unitTests/app/doctype/DoctypeControllerTest.php
+++ b/test/unitTests/app/doctype/DoctypeControllerTest.php
@@ -53,10 +53,6 @@ class DoctypeControllerTest extends TestCase
         $this->assertNotNull($responseBody->firstLevel[0]->doctypes_first_level_label);
 
         $this->assertNotNull($responseBody->secondLevel);
-        $this->assertNotNull($responseBody->models);
-        $this->assertNotNull($responseBody->models[0]->template_id);
-        $this->assertNotNull($responseBody->models[0]->template_label);
-        $this->assertNotNull($responseBody->models[0]->template_comment);
     }
 
     public function testCreateFirstLevel()
-- 
GitLab