From 63497a1218ec6cd3487f7f8cdf533079bd0c742e Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Wed, 25 Nov 2020 17:22:38 +0100
Subject: [PATCH] FEAT #10118 TIME 6:45 attachment types migration + refactor
 call to xml file

---
 migration/21.04/2104.sql                      | 25 +++++++
 migration/21.04/migrate.sh                    |  2 +
 migration/21.04/migrateAttachmentTypes.php    | 65 +++++++++++++++++++
 sql/data_fr.sql                               | 10 +++
 sql/structure.sql                             |  1 +
 .../controllers/ActionMethodController.php    |  9 +--
 .../PreProcessActionController.php            | 40 ++++++------
 .../controllers/AttachmentController.php      | 21 +++---
 .../attachment/models/AttachmentTypeModel.php |  5 +-
 src/app/email/controllers/EmailController.php |  5 +-
 .../controllers/AlfrescoController.php        |  6 +-
 .../controllers/FastParapheurController.php   |  6 +-
 .../controllers/IParapheurController.php      |  6 +-
 .../controllers/IxbusController.php           |  6 +-
 .../controllers/MaarchParapheurController.php |  6 +-
 .../SendMessageExchangeController.php         |  6 +-
 .../controllers/FolderPrintController.php     |  6 +-
 .../resource/controllers/StoreController.php  | 11 ++--
 .../controllers/SignatureBookController.php   | 17 +++--
 .../controllers/TemplateController.php        | 17 ++---
 20 files changed, 202 insertions(+), 68 deletions(-)
 create mode 100644 migration/21.04/2104.sql
 create mode 100644 migration/21.04/migrate.sh
 create mode 100644 migration/21.04/migrateAttachmentTypes.php

diff --git a/migration/21.04/2104.sql b/migration/21.04/2104.sql
new file mode 100644
index 00000000000..e23016251dc
--- /dev/null
+++ b/migration/21.04/2104.sql
@@ -0,0 +1,25 @@
+-- *************************************************************************--
+--                                                                          --
+--                                                                          --
+-- Model migration script - 20.10 to 21.04                                  --
+--                                                                          --
+--                                                                          --
+-- *************************************************************************--
+UPDATE parameters SET param_value_string = '21.04.1' WHERE id = 'database_version';
+
+DROP TABLE IF EXISTS attachment_types;
+CREATE TABLE attachment_types
+(
+    id SERIAL NOT NULL,
+    type_id text NOT NULL,
+    label text NOT NULL,
+    visible BOOLEAN NOT NULL,
+    email_link BOOLEAN NOT NULL,
+    signable BOOLEAN NOT NULL,
+    icon text,
+    chrono BOOLEAN NOT NULL,
+    version_enabled BOOLEAN NOT NULL,
+    new_version_default BOOLEAN NOT NULL,
+    CONSTRAINT attachment_types_pkey PRIMARY KEY (id)
+)
+WITH (OIDS=FALSE);
diff --git a/migration/21.04/migrate.sh b/migration/21.04/migrate.sh
new file mode 100644
index 00000000000..1beecc30387
--- /dev/null
+++ b/migration/21.04/migrate.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+php ./migrateAttachmentTypes.php
diff --git a/migration/21.04/migrateAttachmentTypes.php b/migration/21.04/migrateAttachmentTypes.php
new file mode 100644
index 00000000000..99b3d3ef0f9
--- /dev/null
+++ b/migration/21.04/migrateAttachmentTypes.php
@@ -0,0 +1,65 @@
+<?php
+
+require '../../vendor/autoload.php';
+
+chdir('../..');
+
+$customs =  scandir('custom');
+
+
+foreach ($customs as $custom) {
+    if (in_array($custom, ['custom.json', 'custom.xml', '.', '..'])) {
+        continue;
+    }
+
+    \SrcCore\models\DatabasePDO::reset();
+    new \SrcCore\models\DatabasePDO(['customId' => $custom]);
+    $GLOBALS['customId'] = $custom;
+
+    $language = \SrcCore\models\CoreConfigModel::getLanguage();
+    if (file_exists("custom/{$custom}/src/core/lang/lang-{$language}.php")) {
+        require_once("custom/{$custom}/src/core/lang/lang-{$language}.php");
+    }
+    require_once("src/core/lang/lang-{$language}.php");
+
+    $migrated = 0;
+    $attachmentTypesXml = [];
+
+    $loadedXml = \SrcCore\models\CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/entreprise.xml']);
+    if ($loadedXml) {
+        $attachmentTypesXML = $loadedXml->attachment_types;
+        if (count($attachmentTypesXML) > 0) {
+            foreach ($attachmentTypesXML->type as $value) {
+                $label = defined((string)$value->label) ? constant((string)$value->label) : (string)$value->label;
+                $attachmentTypesXml[(string)$value->id] = [
+                    'label'         => $label,
+                    'icon'          => (string)$value['icon'],
+                    'sign'          => (empty($value['sign']) || (string)$value['sign'] == 'true') ? 'true' : 'false',
+                    'chrono'        => (empty($value['with_chrono']) || (string)$value['with_chrono'] == 'true') ? 'true' : 'false',
+                    'attachInMail'  => (!empty($value['attach_in_mail']) && (string)$value['attach_in_mail'] == 'true') ? 'true' : 'false',
+                    'show'          => (empty($value->attributes()->show) || (string)$value->attributes()->show == 'true') ? 'true' : 'false'
+                ];
+            }
+        }
+    }
+
+    if (!empty($attachmentTypesXml)) {
+        foreach ($attachmentTypesXml as $key => $typeXml) {
+            $type = [
+                'type_id'             => $key,
+                'label'               => $typeXml['label'],
+                'visible'             => $typeXml['show'],
+                'email_link'          => $typeXml['attachInMail'],
+                'signable'            => $typeXml['sign'],
+                'icon'                => $typeXml['icon'],
+                'chrono'              => $typeXml['chrono'],
+                'version_enabled'     => 'true',
+                'new_version_default' => 'true'
+            ];
+
+            \Attachment\models\AttachmentTypeModel::create($type);
+            $migrated++;
+        }
+    }
+    printf("Migration types de pièces jointes (CUSTOM {$custom}) : {$migrated} types migré(s).\n");
+}
diff --git a/sql/data_fr.sql b/sql/data_fr.sql
index 20909e4870e..5008fc927d0 100755
--- a/sql/data_fr.sql
+++ b/sql/data_fr.sql
@@ -1773,3 +1773,13 @@ INSERT INTO registered_mail_issuing_sites_entities (site_id, entity_id) VALUES (
 INSERT INTO registered_mail_number_range (type, tracking_account_number, range_start, range_end, creator, creation_date, status, current_number) VALUES ('2C', 'SuiviNumber', 1, 10, 23, '2020-09-14 14:38:09.008644', 'OK', 1);
 INSERT INTO registered_mail_number_range (type, tracking_account_number, range_start, range_end, creator, creation_date, status, current_number) VALUES ('RW', 'SuiviNumberInternational', 1, 10, 23, '2020-09-14 14:39:32.972626', 'OK', 1);
 INSERT INTO registered_mail_number_range (type, tracking_account_number, range_start, range_end, creator, creation_date, status, current_number) VALUES ('2D', 'suiviNumber', 1, 10, 23, '2020-09-14 14:39:16.779322', 'OK', 1);
+
+/* Attachment types */
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (2, 'response_project', 'Projet de réponse', true, true, true, 'R', true, true, true);
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (3, 'signed_response', 'Réponse signée', false, true, false, '', true, true, true);
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (4, 'simple_attachment', 'Pièce jointe', true, false, false, 'PJ', false, true, true);
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (5, 'incoming_mail_attachment', 'Pièce jointe capturée', true, false, false, '', false, true, true);
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (6, 'outgoing_mail', 'Courrier départ spontané', true, false, true, 'DS', true, true, true);
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (7, 'summary_sheet', 'Fiche de liaison', false, false, false, '', true, true, true);
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (8, 'acknowledgement_record_management', 'Accusé de réception (Archivage)', false, false, false, '', true, true, true);
+INSERT INTO attachment_types (id, type_id, label, visible, email_link, signable, icon, chrono, version_enabled, new_version_default) VALUES (9, 'reply_record_management', 'Réponse au transfert (Archivage)', false, false, false, '', true, true, true);
diff --git a/sql/structure.sql b/sql/structure.sql
index 6b4fd08b15c..ad8039d6a94 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -1502,6 +1502,7 @@ CREATE TABLE attachment_types
     email_link BOOLEAN NOT NULL,
     signable BOOLEAN NOT NULL,
     icon text,
+    chrono BOOLEAN NOT NULL,
     version_enabled BOOLEAN NOT NULL,
     new_version_default BOOLEAN NOT NULL,
     CONSTRAINT attachment_types_pkey PRIMARY KEY (id)
diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index c7268842139..0a5fadb1f03 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -19,6 +19,7 @@ use Action\models\ResMarkAsReadModel;
 use Alfresco\controllers\AlfrescoController;
 use Attachment\controllers\AttachmentController;
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Convert\controllers\ConvertPdfController;
 use Convert\models\AdrModel;
 use Docserver\controllers\DocserverController;
@@ -369,10 +370,10 @@ class ActionMethodController
         $resourceIn     = !empty($integrations['inSignatureBook']);
 
         $signableAttachmentsTypes = [];
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        foreach ($attachmentsTypes as $key => $type) {
-            if ($type['sign']) {
-                $signableAttachmentsTypes[] = $key;
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        foreach ($attachmentsTypes as $type) {
+            if ($type['signable']) {
+                $signableAttachmentsTypes[] = $type['type_id'];
             }
         }
 
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index cb5cf93c997..27c7bf3af87 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -15,6 +15,7 @@ namespace Action\controllers;
 use AcknowledgementReceipt\models\AcknowledgementReceiptModel;
 use Action\models\ActionModel;
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Basket\models\BasketModel;
 use Basket\models\GroupBasketRedirectModel;
 use Configuration\models\ConfigurationModel;
@@ -455,8 +456,9 @@ class PreProcessActionController
                         'data'   => [$resId]
                     ]);
 
-                    $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
-                    
+                    $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+                    $attachmentTypes = array_column($attachmentTypes, 'signable', 'type_id');
+
                     if (empty($attachments) && empty($integratedResource)) {
                         $additionalsInfos['noAttachment'][] = ['alt_identifier' => $noAttachmentsResource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noAttachmentInSignatoryBook'];
                     } else {
@@ -480,7 +482,7 @@ class PreProcessActionController
                                 $additionalsInfos['noAttachment'][] = ['alt_identifier' => $noAttachmentsResource['alt_identifier'], 'res_id' => $resIdAttachment, 'reason' => 'fileDoesNotExists'];
                                 break;
                             }
-                            if ($attachmentTypes[$value['attachment_type']]['sign']) {
+                            if ($attachmentTypes[$value['attachment_type']]) {
                                 $hasSignableAttachment = true;
                             }
                         }
@@ -987,10 +989,10 @@ class PreProcessActionController
         }
 
         $signableAttachmentsTypes = [];
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        foreach ($attachmentsTypes as $key => $type) {
-            if ($type['sign']) {
-                $signableAttachmentsTypes[] = $key;
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        foreach ($attachmentsTypes as $type) {
+            if ($type['signable']) {
+                $signableAttachmentsTypes[] = $type['type_id'];
             }
         }
 
@@ -1052,10 +1054,10 @@ class PreProcessActionController
         $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
 
         $signableAttachmentsTypes = [];
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        foreach ($attachmentsTypes as $key => $type) {
-            if ($type['sign']) {
-                $signableAttachmentsTypes[] = $key;
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        foreach ($attachmentsTypes as $type) {
+            if ($type['signable']) {
+                $signableAttachmentsTypes[] = $type['type_id'];
             }
         }
 
@@ -1115,10 +1117,10 @@ class PreProcessActionController
         $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
 
         $signableAttachmentsTypes = [];
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        foreach ($attachmentsTypes as $key => $type) {
-            if ($type['sign']) {
-                $signableAttachmentsTypes[] = $key;
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        foreach ($attachmentsTypes as $type) {
+            if ($type['signable']) {
+                $signableAttachmentsTypes[] = $type['type_id'];
             }
         }
 
@@ -1187,10 +1189,10 @@ class PreProcessActionController
         $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
 
         $signableAttachmentsTypes = [];
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        foreach ($attachmentsTypes as $key => $type) {
-            if ($type['sign']) {
-                $signableAttachmentsTypes[] = $key;
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        foreach ($attachmentsTypes as $type) {
+            if ($type['signable']) {
+                $signableAttachmentsTypes[] = $type['type_id'];
             }
         }
 
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index cb25ff59e16..8ea0be72723 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -15,6 +15,7 @@
 namespace Attachment\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Contact\models\ContactModel;
 use ContentManagement\controllers\MergeController;
 use Convert\controllers\ConvertPdfController;
@@ -110,9 +111,10 @@ class AttachmentController
         $attachment['typistLabel'] = $typist['firstname']. ' ' .$typist['lastname'];
         $attachment['modifiedBy'] = UserModel::getLabelledUserById(['id' => $attachment['modifiedBy']]);
 
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        if (!empty($attachmentsTypes[$attachment['type']]['label'])) {
-            $attachment['typeLabel'] = $attachmentsTypes[$attachment['type']]['label'];
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'label']]);
+        $attachmentsTypes = array_column($attachmentsTypes, 'label', 'type_id');
+        if (!empty($attachmentsTypes[$attachment['type']])) {
+            $attachment['typeLabel'] = $attachmentsTypes[$attachment['type']];
         }
 
         $oldVersions = [];
@@ -173,7 +175,8 @@ class AttachmentController
             return $response->withStatus(400)->withJson(['errors' => 'Can not update attachment use for record_management']);
         }
 
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id']]);
+        $attachmentsTypes = array_column($attachmentsTypes, null, 'type_id');
         if (empty($attachmentsTypes[$body['type']])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body type does not exist']);
         }
@@ -303,7 +306,8 @@ class AttachmentController
             'limit'     => (int)$queryParams['limit'] ?? 0
         ]);
 
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'label']]);
+        $attachmentsTypes = array_column($attachmentsTypes, 'label', 'type_id');
         foreach ($attachments as $key => $attachment) {
             if ($attachment['modificationDate'] == $attachment['creationDate']) {
                 $attachments[$key]['modificationDate'] = null;
@@ -315,8 +319,8 @@ class AttachmentController
             }
             $attachments[$key]['modifiedBy'] = UserModel::getLabelledUserById(['id' => $attachment['modifiedBy']]);
 
-            if (!empty($attachmentsTypes[$attachment['type']]['label'])) {
-                $attachments[$key]['typeLabel'] = $attachmentsTypes[$attachment['type']]['label'];
+            if (!empty($attachmentsTypes[$attachment['type']])) {
+                $attachments[$key]['typeLabel'] = $attachmentsTypes[$attachment['type']];
             }
 
             if ($attachment['status'] == 'SIGN') {
@@ -904,7 +908,8 @@ class AttachmentController
             return ['errors' => 'Body resIdMaster is out of perimeter'];
         }
 
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id']]);
+        $attachmentsTypes = array_column($attachmentsTypes, 'type_id', 'type_id');
         if (empty($attachmentsTypes[$body['type']])) {
             return ['errors' => 'Body type does not exist'];
         }
diff --git a/src/app/attachment/models/AttachmentTypeModel.php b/src/app/attachment/models/AttachmentTypeModel.php
index b2f6582be11..34bb8587dda 100644
--- a/src/app/attachment/models/AttachmentTypeModel.php
+++ b/src/app/attachment/models/AttachmentTypeModel.php
@@ -60,8 +60,8 @@ class AttachmentTypeModel
 
     public static function create(array $args)
     {
-        ValidatorModel::notEmpty($args, ['type_id', 'label', 'visible', 'email_link', 'signable', 'version_enabled', 'new_version_default']);
-        ValidatorModel::stringType($args, ['type_id', 'label', 'visible', 'email_link', 'signable', 'version_enabled', 'new_version_default', 'icon']);
+        ValidatorModel::notEmpty($args, ['type_id', 'label', 'visible', 'email_link', 'signable', 'version_enabled', 'new_version_default', 'chrono']);
+        ValidatorModel::stringType($args, ['type_id', 'label', 'visible', 'email_link', 'signable', 'version_enabled', 'new_version_default', 'chrono', 'icon']);
 
         $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'attachment_types_id_seq']);
 
@@ -75,6 +75,7 @@ class AttachmentTypeModel
                 'email_link'            => $args['email_link'],
                 'signable'              => $args['signable'],
                 'icon'                  => $args['icon'],
+                'chrono'                => $args['chrono'],
                 'version_enabled'       => $args['version_enabled'],
                 'new_version_default'   => $args['new_version_default'],
             ]
diff --git a/src/app/email/controllers/EmailController.php b/src/app/email/controllers/EmailController.php
index c546b01a4d2..31d4a820a7a 100644
--- a/src/app/email/controllers/EmailController.php
+++ b/src/app/email/controllers/EmailController.php
@@ -395,7 +395,8 @@ class EmailController
         }
 
         $attachments = [];
-        $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentTypes = AttachmentModel::get(['select' => ['type_id', 'label', 'email_link']]);
+        $attachmentTypes = array_column($attachmentTypes, null, 'type_id');
         $rawAttachments = AttachmentModel::get([
             'select'    => ['res_id', 'title', 'identifier', 'attachment_type', 'typist', 'format', 'filesize', 'status'],
             'where'     => ['res_id_master = ?', 'attachment_type not in (?)', 'status not in (?)'],
@@ -433,7 +434,7 @@ class EmailController
                 'chrono'            => $attachment['identifier'],
                 'label'             => $attachment['title'],
                 'typeLabel'         => $attachmentTypes[$attachment['attachment_type']]['label'],
-                'attachInMail'      => $attachmentTypes[$attachment['attachment_type']]['attachInMail'],
+                'attachInMail'      => $attachmentTypes[$attachment['attachment_type']]['email_link'],
                 'convertedDocument' => $convertedDocument,
                 'creator'           => UserModel::getLabelledUserById(['id' => $attachment['typist']]),
                 'format'            => $attachment['format'],
diff --git a/src/app/external/alfresco/controllers/AlfrescoController.php b/src/app/external/alfresco/controllers/AlfrescoController.php
index d6d0d29e4cc..e79ee9bd89e 100644
--- a/src/app/external/alfresco/controllers/AlfrescoController.php
+++ b/src/app/external/alfresco/controllers/AlfrescoController.php
@@ -15,6 +15,7 @@
 namespace Alfresco\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Configuration\models\ConfigurationModel;
 use Contact\controllers\ContactController;
 use Contact\models\ContactModel;
@@ -794,8 +795,9 @@ class AlfrescoController
             if (!empty($alfrescoParameters['mapping']['attachment'])) {
                 foreach ($alfrescoParameters['mapping']['attachment'] as $key => $alfrescoParameter) {
                     if ($alfrescoParameter == 'typeLabel') {
-                        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-                        $properties[$key] = $attachmentsTypes[$attachment['attachment_type']]['label'];
+                        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'label']]);
+                        $attachmentsTypes = array_column($attachmentsTypes, 'label', 'type_id');
+                        $properties[$key] = $attachmentsTypes[$attachment['attachment_type']];
                     } else {
                         $properties[$key] = $attachment[$alfrescoParameter];
                     }
diff --git a/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php b/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php
index 5d3b611a6d8..24cea7721ec 100755
--- a/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/FastParapheurController.php
@@ -16,6 +16,7 @@
 namespace ExternalSignatoryBook\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Convert\controllers\ConvertPdfController;
 use Convert\models\AdrModel;
 use Docserver\models\DocserverModel;
@@ -167,9 +168,10 @@ class FastParapheurController
             'data'      => [$aArgs['resIdMaster'], ['signed_response']]
         ]);
 
-        $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        $attachmentTypes = array_column($attachmentTypes, 'signable', 'type_id');
         foreach ($attachments as $key => $value) {
-            if (!$attachmentTypes[$value['attachment_type']]['sign']) {
+            if (!$attachmentTypes[$value['attachment_type']]) {
                 $annexeAttachmentPath = DocserverModel::getByDocserverId(['docserverId' => $value['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]);
                 $value['filePath']    = $annexeAttachmentPath['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $value['path']) . $value['filename'];
 
diff --git a/src/app/external/externalSignatoryBook/controllers/IParapheurController.php b/src/app/external/externalSignatoryBook/controllers/IParapheurController.php
index 4fc6ffaa95d..81065fccd75 100755
--- a/src/app/external/externalSignatoryBook/controllers/IParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/IParapheurController.php
@@ -16,6 +16,7 @@
 namespace ExternalSignatoryBook\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Convert\controllers\ConvertPdfController;
 use Convert\models\AdrModel;
 use Docserver\models\DocserverModel;
@@ -104,9 +105,10 @@ class IParapheurController
             'data'   => [$aArgs['resIdMaster'], ['signed_response']]
         ]);
 
-        $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        $attachmentTypes = array_column($attachmentTypes, 'signable', 'type_id');
         foreach ($attachments as $key => $value) {
-            if (!$attachmentTypes[$value['attachment_type']]['sign']) {
+            if (!$attachmentTypes[$value['attachment_type']]) {
                 $adrInfo              = AdrModel::getConvertedDocumentById(['resId' => $value['res_id'], 'collId' => 'attachments_coll', 'type' => 'PDF']);
                 $annexeAttachmentPath = DocserverModel::getByDocserverId(['docserverId' => $adrInfo['docserver_id'], 'select' => ['path_template']]);
                 $value['filePath']    = $annexeAttachmentPath['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $adrInfo['path']) . $adrInfo['filename'];
diff --git a/src/app/external/externalSignatoryBook/controllers/IxbusController.php b/src/app/external/externalSignatoryBook/controllers/IxbusController.php
index d6f56e168d0..d08fde2b0ff 100755
--- a/src/app/external/externalSignatoryBook/controllers/IxbusController.php
+++ b/src/app/external/externalSignatoryBook/controllers/IxbusController.php
@@ -15,6 +15,7 @@
 namespace ExternalSignatoryBook\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Convert\controllers\ConvertPdfController;
 use Docserver\models\DocserverModel;
 use Docserver\models\DocserverTypeModel;
@@ -197,9 +198,10 @@ class IxbusController
             'data'      => [$aArgs['resIdMaster'], ['incoming_mail_attachment', 'signed_response']]
         ]);
 
-        $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        $attachmentTypes = array_column($attachmentTypes, 'signable', 'type_id');
         foreach ($attachments as $key => $value) {
-            if (!$attachmentTypes[$value['attachment_type']]['sign']) {
+            if (!$attachmentTypes[$value['attachment_type']]) {
                 unset($attachments[$key]);
             }
         }
diff --git a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
index 5c07346b648..acfdb9718fb 100755
--- a/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
+++ b/src/app/external/externalSignatoryBook/controllers/MaarchParapheurController.php
@@ -15,6 +15,7 @@
 namespace ExternalSignatoryBook\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Contact\controllers\ContactController;
 use Convert\controllers\ConvertPdfController;
 use Convert\models\AdrModel;
@@ -242,9 +243,10 @@ class MaarchParapheurController
                 return ['error' => 'No attachment to send'];
             } else {
                 $nonSignableAttachments = [];
-                $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+                $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+                $attachmentTypes = array_column($attachmentTypes, 'signable', 'type_id');
                 foreach ($attachments as $key => $value) {
-                    if (!$attachmentTypes[$value['attachment_type']]['sign']) {
+                    if (!$attachmentTypes[$value['attachment_type']]) {
                         $adrInfo = ConvertPdfController::getConvertedPdfById(['resId' => $value['res_id'], 'collId' => 'attachments_coll']);
                         if (empty($adrInfo['docserver_id']) || strtolower(pathinfo($adrInfo['filename'], PATHINFO_EXTENSION)) != 'pdf') {
                             return ['error' => 'Attachment ' . $value['res_id'] . ' is not converted in pdf'];
diff --git a/src/app/external/messageExchange/controllers/SendMessageExchangeController.php b/src/app/external/messageExchange/controllers/SendMessageExchangeController.php
index bf70f5d0adf..9468c171fb0 100755
--- a/src/app/external/messageExchange/controllers/SendMessageExchangeController.php
+++ b/src/app/external/messageExchange/controllers/SendMessageExchangeController.php
@@ -15,6 +15,7 @@
 namespace MessageExchange\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Contact\models\ContactModel;
 use Docserver\models\DocserverModel;
 use Doctype\models\DoctypeModel;
@@ -175,11 +176,12 @@ class SendMessageExchangeController
         $AttachmentsInfo = [];
         if (!empty($body['joinAttachment'])) {
             $AttachmentsInfo = AttachmentModel::get(['select' => ['*'], 'where' => ['res_id in (?)'], 'data' => [$body['joinAttachment']]]);
-            $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+            $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'label']]);
+            $attachmentTypes = array_column($attachmentTypes, 'label', 'type_id');
             foreach ($AttachmentsInfo as $key => $value) {
                 $AttachmentsInfo[$key]['Title']                                  = $value['title'];
                 $AttachmentsInfo[$key]['OriginatingAgencyArchiveUnitIdentifier'] = $value['identifier'];
-                $AttachmentsInfo[$key]['DocumentType']                           = $attachmentTypes[$value['attachment_type']]['label'];
+                $AttachmentsInfo[$key]['DocumentType']                           = $attachmentTypes[$value['attachment_type']];
                 $AttachmentsInfo[$key]['tablenameExchangeMessage']               = 'res_attachments';
             }
         }
diff --git a/src/app/resource/controllers/FolderPrintController.php b/src/app/resource/controllers/FolderPrintController.php
index 46eb83bcf5b..7a0ac054c85 100755
--- a/src/app/resource/controllers/FolderPrintController.php
+++ b/src/app/resource/controllers/FolderPrintController.php
@@ -16,6 +16,7 @@ namespace Resource\controllers;
 
 use AcknowledgementReceipt\models\AcknowledgementReceiptModel;
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Contact\controllers\ContactController;
 use Contact\models\ContactModel;
 use Convert\controllers\ConvertPdfController;
@@ -748,8 +749,9 @@ class FolderPrintController
         $status = StatusModel::getById(['id' => $attachment['status'], 'select' => ['label_status']]);
         $status = $status['label_status'];
 
-        $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
-        $attachmentType = $attachmentTypes[$attachment['attachment_type']]['label'];
+        $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        $attachmentTypes = array_column($attachmentTypes, 'label', 'type_id');
+        $attachmentType = $attachmentTypes[$attachment['attachment_type']];
 
         $creationDate = new \DateTime($attachment['creation_date']);
         $creationDate = $creationDate->format('d-m-Y H:i');
diff --git a/src/app/resource/controllers/StoreController.php b/src/app/resource/controllers/StoreController.php
index 9ce2ac77e2a..be67493a6d6 100755
--- a/src/app/resource/controllers/StoreController.php
+++ b/src/app/resource/controllers/StoreController.php
@@ -16,6 +16,7 @@
 namespace Resource\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use ContentManagement\controllers\MergeController;
 use CustomField\models\CustomFieldModel;
 use Docserver\controllers\DocserverController;
@@ -378,8 +379,9 @@ class StoreController
 
     public static function prepareAttachmentStorage(array $args)
     {
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        if ($attachmentsTypes[$args['type']]['chrono'] && empty($args['chrono'])) {
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'chrono']]);
+        $attachmentsTypes = array_column($attachmentsTypes, 'chrono', 'type_id');
+        if ($attachmentsTypes[$args['type']] && empty($args['chrono'])) {
             $resource = ResModel::getById(['select' => ['destination', 'type_id'], 'resId' => $args['resIdMaster']]);
             $args['chrono'] = ChronoModel::getChrono(['id' => 'outgoing', 'entityId' => $resource['destination'], 'typeId' => $resource['type_id'], 'resId' => $args['resIdMaster']]);
         }
@@ -435,8 +437,9 @@ class StoreController
     public static function prepareUpdateAttachmentStorage(array $args)
     {
         $attachment = AttachmentModel::getById(['id' => $args['id'], 'select' => ['identifier', 'res_id_master']]);
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-        if ($attachmentsTypes[$args['type']]['chrono'] && empty($attachment['identifier'])) {
+        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'chrono']]);
+        $attachmentsTypes = array_column($attachmentsTypes, 'chrono', 'type_id');
+        if ($attachmentsTypes[$args['type']] && empty($attachment['identifier'])) {
             $resource = ResModel::getById(['select' => ['destination', 'type_id'], 'resId' => $attachment['res_id_master']]);
             $chrono = ChronoModel::getChrono(['id' => 'outgoing', 'entityId' => $resource['destination'], 'typeId' => $resource['type_id'], 'resId' => $attachment['res_id_master']]);
         }
diff --git a/src/app/signatureBook/controllers/SignatureBookController.php b/src/app/signatureBook/controllers/SignatureBookController.php
index 4bb34c98205..46ce954eb88 100755
--- a/src/app/signatureBook/controllers/SignatureBookController.php
+++ b/src/app/signatureBook/controllers/SignatureBookController.php
@@ -15,6 +15,7 @@
 namespace SignatureBook\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use Basket\models\BasketModel;
 use Basket\models\GroupBasketModel;
 use Basket\models\RedirectBasketModel;
@@ -169,13 +170,14 @@ class SignatureBookController
         ValidatorModel::notEmpty($args, ['resId', 'userId']);
         ValidatorModel::intVal($args, ['resId', 'userId']);
 
-        $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'label', 'icon', 'signable']]);
+        $attachmentTypes = array_column($attachmentTypes, null, 'type_id');
 
         $orderBy = "CASE attachment_type WHEN 'response_project' THEN 1";
         $c = 2;
-        foreach ($attachmentTypes as $key => $value) {
-            if ($value['sign'] && $key != 'response_project') {
-                $orderBy .= " WHEN '{$key}' THEN {$c}";
+        foreach ($attachmentTypes as $value) {
+            if ($value['signable'] && $value['type_id'] != 'response_project') {
+                $orderBy .= " WHEN '{$value['type_id']}' THEN {$c}";
                 ++$c;
             }
         }
@@ -249,7 +251,7 @@ class SignatureBookController
             $attachments[$key]['viewerNoSignId']  = $viewerNoSignId;
             $attachments[$key]['attachment_type'] = $attachmentTypes[$value['attachment_type']]['label'];
             $attachments[$key]['icon']            = $attachmentTypes[$value['attachment_type']]['icon'];
-            $attachments[$key]['sign']            = $attachmentTypes[$value['attachment_type']]['sign'];
+            $attachments[$key]['sign']            = $attachmentTypes[$value['attachment_type']]['signable'];
             $attachments[$key]['signed']          = $value['status'] == 'SIGN';
             $attachments[$key]['viewerId']        = $viewerId;
 
@@ -352,12 +354,13 @@ class SignatureBookController
             'data'      => [$resIds]
         ]);
 
-        $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentTypes = AttachmentTypeModel::get(['select' => ['type_id', 'signable']]);
+        $attachmentTypes = array_column($attachmentTypes, 'signable', 'type_id');
         foreach ($attachmentsInResList as $value) {
             if ($resListForAttachments[$value['res_id_master']] === null) {
                 $resListForAttachments[$value['res_id_master']] = true;
             }
-            if ($attachmentTypes[$value['attachment_type']]['sign'] && ($value['status'] == 'TRA' || $value['status'] == 'A_TRA')) {
+            if ($attachmentTypes[$value['attachment_type']] && ($value['status'] == 'TRA' || $value['status'] == 'A_TRA')) {
                 $resListForAttachments[$value['res_id_master']] = false;
             }
         }
diff --git a/src/app/template/controllers/TemplateController.php b/src/app/template/controllers/TemplateController.php
index a0adb772d1a..3b72445d0b4 100755
--- a/src/app/template/controllers/TemplateController.php
+++ b/src/app/template/controllers/TemplateController.php
@@ -15,6 +15,7 @@
 namespace Template\controllers;
 
 use Attachment\models\AttachmentModel;
+use Attachment\models\AttachmentTypeModel;
 use ContentManagement\controllers\MergeController;
 use Convert\controllers\ConvertPdfController;
 use Docserver\controllers\DocserverController;
@@ -84,13 +85,13 @@ class TemplateController
             }
         }
 
-        $attachmentModelsTmp = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentModelsTmp = AttachmentTypeModel::get(['select' => ['type_id', 'label', 'visible']]);
         $attachmentTypes = [];
-        foreach ($attachmentModelsTmp as $key => $value) {
-            if ($value['show']) {
+        foreach ($attachmentModelsTmp as $value) {
+            if ($value['visible']) {
                 $attachmentTypes[] = [
                     'label' => $value['label'],
-                    'id'    => $key
+                    'id'    => $value['type_id']
                 ];
             }
         }
@@ -380,13 +381,13 @@ class TemplateController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $attachmentModelsTmp = AttachmentModel::getAttachmentsTypesByXML();
+        $attachmentModelsTmp = AttachmentTypeModel::get(['select' => ['type_id', 'label', 'visible']]);
         $attachmentTypes = [];
-        foreach ($attachmentModelsTmp as $key => $value) {
-            if ($value['show']) {
+        foreach ($attachmentModelsTmp as $value) {
+            if ($value['visible']) {
                 $attachmentTypes[] = [
                     'label' => $value['label'],
-                    'id'    => $key
+                    'id'    => $value['type_id']
                 ];
             }
         }
-- 
GitLab