From 4be1a9daac3c41ebbe836b664701fff535c48c74 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Thu, 7 Nov 2019 18:01:20 +0100
Subject: [PATCH] FIX #11691 TIME 3:00 added delete attachment

---
 rest/index.php                                |  1 +
 .../controllers/AttachmentController.php      | 37 +++++++++++++++++++
 .../models/AttachmentModelAbstract.php        | 17 +++++++++
 src/core/lang/lang-en.php                     |  1 +
 src/core/lang/lang-fr.php                     |  1 +
 src/core/lang/lang-nl.php                     |  1 +
 6 files changed, 58 insertions(+)

diff --git a/rest/index.php b/rest/index.php
index 0d4874fa0d4..94f16e0d7f3 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -71,6 +71,7 @@ $app->get('/attachments/{id}/originalContent', \Attachment\controllers\Attachmen
 $app->get('/attachments/{id}/thumbnail', \Attachment\controllers\AttachmentController::class . ':getThumbnailContent');
 $app->put('/attachments/{id}/inSendAttachment', \Attachment\controllers\AttachmentController::class . ':setInSendAttachment');
 $app->get('/attachmentsTypes', \Attachment\controllers\AttachmentController::class . ':getAttachmentsTypes');
+$app->delete('/attachments/{id}', \Attachment\controllers\AttachmentController::class . ':delete');
 
 //AutoComplete
 $app->get('/autocomplete/contacts', \SrcCore\controllers\AutoCompleteController::class . ':getContacts');
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index e6fbc9fbd61..868b3028954 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -447,6 +447,43 @@ class AttachmentController
         return $response->withJson(['attachmentsTypes' => $attachmentsTypes]);
     }
 
+    public function delete(Request $request, Response $response, array $args)
+    {
+        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Route id must be an integer val']);
+        }
+
+        $attachment = AttachmentModel::getById(['id' => $args['id'], 'select' => ['origin_id', 'res_id_master', 'attachment_type', 'res_id', 'title']]);
+        if (empty($attachment)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Attachment not found']);
+        }
+
+        if (!ResController::hasRightByResId(['resId' => [$attachment['res_id_master']], 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
+        }
+
+        if ($attachment['attachment_type'] == 'signed_response') {
+            AttachmentModel::delete(['id' => $attachment['res_id']]);
+        } else {
+            if (empty($attachment['origin_id'])) {
+                $idToDelete = $attachment['res_id'];
+            } else {
+                $idToDelete = $attachment['origin_id'];
+            }
+
+            AttachmentModel::delete(['id' => $idToDelete]);
+        }
+        HistoryController::add([
+            'tableName' => 'res_attachments',
+            'recordId'  => $args['id'],
+            'eventType' => 'DEL',
+            'info'      =>  _DOC_DELETED . " : {$attachment['title']}",
+            'eventId'   => 'attachmentSuppression',
+        ]);
+
+        return $response->withStatus(204);
+    }
+
     public static function getEncodedDocument(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['id']);
diff --git a/src/app/attachment/models/AttachmentModelAbstract.php b/src/app/attachment/models/AttachmentModelAbstract.php
index 728dbb5a753..316259a7aa0 100755
--- a/src/app/attachment/models/AttachmentModelAbstract.php
+++ b/src/app/attachment/models/AttachmentModelAbstract.php
@@ -284,4 +284,21 @@ abstract class AttachmentModelAbstract
 
         return true;
     }
+
+    public static function delete(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['id']);
+        ValidatorModel::intVal($args, ['id']);
+
+        DatabaseModel::update([
+            'table' => 'res_attachments',
+            'set'   => [
+                'status'    => 'DEL'
+            ],
+            'where' => ['res_id = ? or origin_id = ?'],
+            'data'  => [$args['id'], $args['id']]
+        ]);
+
+        return true;
+    }
 }
diff --git a/src/core/lang/lang-en.php b/src/core/lang/lang-en.php
index 1344a511bc7..d3124ff683e 100755
--- a/src/core/lang/lang-en.php
+++ b/src/core/lang/lang-en.php
@@ -141,6 +141,7 @@ define('_DOC_DISPLAYING', 'Displaying document');
 define('_AR_DISPLAYING', 'Displaying acknowledgement receipt');
 define('_DOC_ADDED', 'Document added');
 define('_ATTACH_DISPLAYING', 'Displaying attachment');
+define('_DOC_DELETED', 'Document deleted');
 define('_NOTE_ADDED', 'Note added');
 define('_NOTE_UPDATED', 'Note updated');
 define('_NOTE_DELETED', 'Note deleted');
diff --git a/src/core/lang/lang-fr.php b/src/core/lang/lang-fr.php
index f12a708090d..6ccffc0ed43 100755
--- a/src/core/lang/lang-fr.php
+++ b/src/core/lang/lang-fr.php
@@ -141,6 +141,7 @@ define('_DOC_DISPLAYING', 'Visualisation du document');
 define('_AR_DISPLAYING', 'Visualisation de l\'accusé de réception');
 define('_DOC_ADDED', 'Document ajouté');
 define('_ATTACH_DISPLAYING', 'Visualisation de la pièce jointe');
+define('_DOC_DELETED', 'Document supprimé');
 define('_NOTE_ADDED', 'Annotation ajoutée');
 define('_NOTE_UPDATED', 'Annotation modifiée');
 define('_NOTE_DELETED', 'Annotation supprimée');
diff --git a/src/core/lang/lang-nl.php b/src/core/lang/lang-nl.php
index 035537e54e1..30f486a288f 100755
--- a/src/core/lang/lang-nl.php
+++ b/src/core/lang/lang-nl.php
@@ -140,6 +140,7 @@ define('_BACK_FROM_VACATION', 'bij terugkeer na afwezigheid');
 define('_DOC_DISPLAYING', 'Weergave van het document');
 define('_DOC_ADDED', 'Document toegevoegd');
 define('_ATTACH_DISPLAYING', 'Weergave van de bijlage');
+define('_DOC_DELETED', 'Document deleted');//TRANSLATE
 define('_NOTE_ADDED', 'note added _TO_TRANSLATE');
 define('_NOTE_UPDATED', 'Note updated');//TRANSLATE
 define('_NOTE_DELETED', 'Note deleted');//TRANSLATE
-- 
GitLab