From bd8208e20c7327d43a8180298d767082b244ea50 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Thu, 30 Jan 2020 11:37:43 +0100
Subject: [PATCH] FEAT #12280 TIME 0:10 refactor attachments

---
 .../ResourceDataExportController.php          | 25 +++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/app/resource/controllers/ResourceDataExportController.php b/src/app/resource/controllers/ResourceDataExportController.php
index 945d8b17886..a3d5c90ffc5 100644
--- a/src/app/resource/controllers/ResourceDataExportController.php
+++ b/src/app/resource/controllers/ResourceDataExportController.php
@@ -131,24 +131,35 @@ class ResourceDataExportController
             }
 
             if (!empty($resource['attachments'])) {
-                $attachmentsIds = [];
-                if (!empty($resource['attachments']['resIds'])) {
-                    $attachmentsIds = $resource['attachments']['resIds'];
-                }
+                if (is_array($resource['attachments'])) {
+                    foreach ($resource['attachments'] as $note) {
+                        if (!Validator::intVal()->validate($note)) {
+                            return $response->withStatus(400)->withJson(['errors' => 'Attachment id is not an integer']);
+                        }
+                    }
 
-                if (!empty($attachmentsIds)) {
                     $attachments = AttachmentModel::get([
                         'select'  => ['res_id', 'res_id_master', 'recipient_type', 'recipient_id', 'typist', 'status', 'attachment_type',
                                       'creation_date', 'identifier', 'title', 'format', 'docserver_id'],
                         'where'   => ['res_id in (?)', 'status not in (?)'],
-                        'data'    => [$attachmentsIds, ['DEL', 'OBS']],
+                        'data'    => [$resource['attachments'], ['DEL', 'OBS']],
                         'orderBy' => ['creation_date desc']
                     ]);
 
-                    if (count($attachments) < count($attachmentsIds)) {
+                    if (count($attachments) < count($resource['attachments'])) {
                         return $response->withStatus(400)->withJson(['errors' => 'Attachment(s) not found']);
                     }
+                } else {
+                    $attachments = AttachmentModel::get([
+                        'select'  => ['res_id', 'res_id_master', 'recipient_type', 'recipient_id', 'typist', 'status', 'attachment_type',
+                                      'creation_date', 'identifier', 'title', 'format', 'docserver_id'],
+                        'where'   => ['res_id_master = ?', 'status not in (?)'],
+                        'data'    => [$resource['resId'], ['DEL', 'OBS']],
+                        'orderBy' => ['creation_date desc']
+                    ]);
+                }
 
+                if (!empty($attachments)) {
                     $chronoResource = ResModel::getById(['select' => ['alt_identifier'], 'resId' => $resource['resId']]);
                     $chronoResource = $chronoResource['alt_identifier'];
 
-- 
GitLab