From 3418d5bee48808dd89bdabecdc7a9d86d46a3981 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 25 Nov 2020 18:21:22 +0100
Subject: [PATCH] FEAT #10118 TIME 2:25 WIP Attachment types refactoring

---
 rest/index.php                                |  3 +--
 .../controllers/AttachmentController.php      |  7 -------
 .../controllers/AttachmentTypeController.php  | 20 ++++++++++++++++---
 .../attachment-create.component.ts            |  6 +++---
 .../attachment-page.component.ts              |  4 ++--
 5 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 3dd87752ec2..99b4f7b43c1 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -96,8 +96,7 @@ $app->get('/attachmentsInformations', \Attachment\controllers\AttachmentControll
 
 
 //AttachmentsTypes
-$app->get('/attachmentsTypes', \Attachment\controllers\AttachmentController::class . ':getAttachmentsTypes');
-//$app->get('/attachmentsTypes', \Attachment\controllers\AttachmentTypeController::class . ':get');
+$app->get('/attachmentsTypes', \Attachment\controllers\AttachmentTypeController::class . ':get');
 $app->post('/attachmentsTypes', \Attachment\controllers\AttachmentTypeController::class . ':create');
 $app->put('/attachmentsTypes/{id}', \Attachment\controllers\AttachmentTypeController::class . ':update');
 $app->delete('/attachmentsTypes/{id}', \Attachment\controllers\AttachmentTypeController::class . ':delete');
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index 8ea0be72723..4a1cc4a3686 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -693,13 +693,6 @@ class AttachmentController
         return $response->withJson($attachment);
     }
 
-    public function getAttachmentsTypes(Request $request, Response $response)
-    {
-        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
-
-        return $response->withJson(['attachmentsTypes' => $attachmentsTypes]);
-    }
-
     public static function getEncodedDocument(array $args)
     {
         ValidatorModel::notEmpty($args, ['id']);
diff --git a/src/app/attachment/controllers/AttachmentTypeController.php b/src/app/attachment/controllers/AttachmentTypeController.php
index 35b7b0b3723..c0617a30f3d 100644
--- a/src/app/attachment/controllers/AttachmentTypeController.php
+++ b/src/app/attachment/controllers/AttachmentTypeController.php
@@ -14,6 +14,7 @@
 
 namespace Attachment\controllers;
 
+use Attachment\models\AttachmentModel;
 use Attachment\models\AttachmentTypeModel;
 use Group\controllers\PrivilegeController;
 use Respect\Validation\Validator;
@@ -24,7 +25,12 @@ class AttachmentTypeController
 {
     public function get(Request $request, Response $response)
     {
-        $attachmentsTypes = AttachmentTypeModel::get(['select' => ['*']]);
+        $rawAttachmentsTypes = AttachmentTypeModel::get(['select' => ['*']]);
+
+        $attachmentsTypes = [];
+        foreach ($rawAttachmentsTypes as $rawAttachmentsType) {
+            $attachmentsTypes[$rawAttachmentsType['type_id']] = $rawAttachmentsType;
+        }
 
         return $response->withJson(['attachmentsTypes' => $attachmentsTypes]);
     }
@@ -51,6 +57,7 @@ class AttachmentTypeController
             'visible'               => empty($body['visible']) ? 'false' : 'true',
             'email_link'            => empty($body['emailLink']) ? 'false' : 'true',
             'signable'              => empty($body['signable']) ? 'false' : 'true',
+            'chrono'                => empty($body['chrono']) ? 'false' : 'true',
             'icon'                  => $body['icon'] ?? null,
             'version_enabled'       => empty($body['versionEnabled']) ? 'false' : 'true',
             'new_version_default'   => empty($body['newVersionDefault']) ? 'false' : 'true'
@@ -88,6 +95,9 @@ class AttachmentTypeController
         if (isset($body['signable'])) {
             $set['signable'] = empty($body['signable']) ? 'false' : 'true';
         }
+        if (isset($body['chrono'])) {
+            $set['chrono'] = empty($body['chrono']) ? 'false' : 'true';
+        }
         if (isset($body['versionEnabled'])) {
             $set['version_enabled'] = empty($body['versionEnabled']) ? 'false' : 'true';
         }
@@ -113,12 +123,16 @@ class AttachmentTypeController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        //TODO que faire quand on supprime
-        $attachmentType = AttachmentTypeModel::getById(['select' => 1, 'id' => $args['id']]);
+        $attachmentType = AttachmentTypeModel::getById(['select' => ['type_id'], 'id' => $args['id']]);
         if (empty($attachmentType)) {
             return $response->withStatus(400)->withJson(['errors' => 'Attachment type does not exist']);
         }
 
+        $attachments = AttachmentModel::get(['select' => 1, 'where' => ['attachment_type = ?', 'status != ?'], 'data' => [$attachmentType['type_id'], 'DEL']]);
+        if (!empty($attachments)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Type is used in attachments']);
+        }
+
         AttachmentTypeModel::delete([
             'where'     => ['id = ?'],
             'data'      => [$args['id']],
diff --git a/src/frontend/app/attachments/attachment-create/attachment-create.component.ts b/src/frontend/app/attachments/attachment-create/attachment-create.component.ts
index bcaffd82c01..dc0d8b2f70a 100644
--- a/src/frontend/app/attachments/attachment-create/attachment-create.component.ts
+++ b/src/frontend/app/attachments/attachment-create/attachment-create.component.ts
@@ -88,10 +88,10 @@ export class AttachmentCreateComponent implements OnInit {
             this.http.get('../rest/attachmentsTypes').pipe(
                 tap((data: any) => {
                     Object.keys(data.attachmentsTypes).forEach(templateType => {
-                        if (data.attachmentsTypes[templateType].show) {
+                        if (data.attachmentsTypes[templateType].visible) {
                             this.attachmentsTypes.push({
-                                id: templateType,
-                                ...data.attachmentsTypes[templateType]
+                                ...data.attachmentsTypes[templateType],
+                                id: templateType
                             });
                         }
                     });
diff --git a/src/frontend/app/attachments/attachments-page/attachment-page.component.ts b/src/frontend/app/attachments/attachments-page/attachment-page.component.ts
index f287cc4f506..95c7d4c356e 100644
--- a/src/frontend/app/attachments/attachments-page/attachment-page.component.ts
+++ b/src/frontend/app/attachments/attachments-page/attachment-page.component.ts
@@ -74,8 +74,8 @@ export class AttachmentPageComponent implements OnInit {
                 tap((data: any) => {
                     Object.keys(data.attachmentsTypes).forEach(templateType => {
                         this.attachmentsTypes.push({
-                            id: templateType,
-                            ...data.attachmentsTypes[templateType]
+                            ...data.attachmentsTypes[templateType],
+                            id: templateType
                         });
                     });
                     this.attachmentsTypes = this.sortPipe.transform(this.attachmentsTypes, 'label');
-- 
GitLab