From 6de19330de7beb57b68f61455a7ba3ac9d0a4e63 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 27 Apr 2020 22:58:39 +0100
Subject: [PATCH] FEAT #13126 TIME 1:30  linkedResources in printedFolder

---
 .../controllers/FolderPrintController.php     | 171 ++++++++++--------
 .../printed-folder-modal.component.ts         |  31 +---
 .../resource/FolderPrintControllerTest.php    |  14 +-
 3 files changed, 106 insertions(+), 110 deletions(-)

diff --git a/src/app/resource/controllers/FolderPrintController.php b/src/app/resource/controllers/FolderPrintController.php
index abb2456d25a..6a357e8aa05 100755
--- a/src/app/resource/controllers/FolderPrintController.php
+++ b/src/app/resource/controllers/FolderPrintController.php
@@ -364,106 +364,133 @@ class FolderPrintController
                     }
                 }
             }
-        }
-
-        if (!empty($resource['linkedResources']) && is_array($resource['linkedResources'])) {
-            $controlResource = ResModel::getById(['resId' => $resource['resId'], 'select' => ['linked_resources']]);
-            $controlResource['linked_resources'] = json_decode($controlResource['linked_resources'], true);
-            foreach ($resource['linkedResources'] as $linkedResource) {
-                if (!Validator::intVal()->validate($linkedResource['resId'])) {
-                    return $response->withStatus(400)->withJson(['errors' => 'LinkedResources resId is not an integer']);
-                }
-                if (!in_array($linkedResource['resId'], $controlResource['linked_resources'])) {
-                    return $response->withStatus(400)->withJson(['errors' => 'LinkedResources resId is not linked to resource']);
-                }
-                if (!ResController::hasRightByResId(['resId' => [$linkedResource['resId']], 'userId' => $GLOBALS['id']])) {
-                    return $response->withStatus(403)->withJson(['errors' => 'LinkedResources out of perimeter']);
-                }
-
-                if (!empty($linkedResource['document'])) {
-                    $document = ResModel::getById([
-                        'select' => ['res_id', 'docserver_id', 'path', 'filename', 'fingerprint', 'category_id', 'alt_identifier'],
-                        'resId'  => $linkedResource['resId']
-                    ]);
-                    if (empty($document)) {
-                        return $response->withStatus(400)->withJson(['errors' => 'LinkedResources Document does not exist']);
-                    }
-
-                    if (empty($document['filename'])) {
-                        return $response->withStatus(400)->withJson(['errors' => 'LinkedResources document has no file']);
-                    }
-
-                    $path = FolderPrintController::getDocumentFilePath(['document' => $document, 'collId' => 'letterbox_coll']);
-                    if (!empty($path['errors'])) {
-                        return $response->withStatus($path['code'])->withJson(['errors' => $path['errors']]);
-                    }
-
-                    $documentPaths[] = $path;
-                }
 
-                if (!empty($linkedResource['attachments']) && is_array($linkedResource['attachments'])) {
-                    foreach ($linkedResource['attachments'] as $attachment) {
+            $linkedAttachmentsPath = [];
+            if (!empty($resource['linkedResourcesAttachments'])) {
+                if (is_array($resource['linkedResourcesAttachments'])) {
+                    foreach ($resource['linkedResourcesAttachments'] as $attachment) {
                         if (!Validator::intVal()->validate($attachment)) {
                             return $response->withStatus(400)->withJson(['errors' => 'LinkedResources attachment id is not an integer']);
                         }
                     }
-
                     $attachments = AttachmentModel::get([
                         'select'  => ['res_id', 'res_id_master', 'recipient_type', 'recipient_id', 'typist', 'status', 'attachment_type',
                             'creation_date', 'identifier', 'title', 'format', 'docserver_id', 'origin'],
                         'where'   => ['res_id in (?)', 'status not in (?)'],
-                        'data'    => [$linkedResource['attachments'], ['DEL', 'OBS']],
+                        'data'    => [$resource['linkedResourcesAttachments'], ['DEL', 'OBS']],
                         'orderBy' => ['creation_date desc']
                     ]);
 
-                    if (count($attachments) < count($linkedResource['attachments'])) {
+                    if (count($attachments) < count($resource['linkedResourcesAttachments'])) {
                         return $response->withStatus(400)->withJson(['errors' => 'LinkedResources attachments not found']);
                     }
 
-                    $chronoResource = ResModel::getById(['select' => ['alt_identifier'], 'resId' => $resource['resId']]);
-                    $chronoResource = $chronoResource['alt_identifier'];
+                    $linkedResources = array_column($attachments, 'res_id_master');
+                    if (!ResController::hasRightByResId(['resId' => $linkedResources, 'userId' => $GLOBALS['id']])) {
+                        return $response->withStatus(403)->withJson(['errors' => 'LinkedResources out of perimeter']);
+                    }
+                } else {
+                    $oLinkedResources = ResModel::getById(['resId' => $resource['resId'], 'select' => ['linked_resources']]);
+                    $linkedResources = json_decode($oLinkedResources['linked_resources'], true);
+                    $attachments = AttachmentModel::get([
+                        'select'  => ['res_id', 'res_id_master', 'recipient_type', 'recipient_id', 'typist', 'status', 'attachment_type',
+                            'creation_date', 'identifier', 'title', 'format', 'docserver_id', 'origin'],
+                        'where'   => ['res_id_master in (?)', 'status not in (?)'],
+                        'data'    => [$linkedResources, ['DEL', 'OBS']],
+                        'orderBy' => ['creation_date desc']
+                    ]);
+                }
 
-                    $attachmentsIds = array_column($attachments, 'res_id');
+                $attachmentsIds = array_column($attachments, 'res_id');
 
-                    foreach ($attachments as $attachment) {
-                        if ($attachment['res_id_master'] != $linkedResource['resId']) {
-                            return $response->withStatus(400)->withJson(['errors' => 'LinkedResources attachment is not linked to resource']);
+                foreach ($attachments as $attachment) {
+                    $resourceInfo = ResModel::getById(['resId' => $attachment['res_id_master'], 'select' => ['alt_identifier']]);
+                    $chronoResource = $resourceInfo['alt_identifier'];
+
+                    $originAttachment = AttachmentModel::get([
+                        'select' => [
+                            'res_id', 'res_id_master', 'recipient_type', 'recipient_id', 'typist', 'status', 'attachment_type',
+                            'creation_date', 'identifier', 'title', 'format', 'docserver_id', 'origin'
+                        ],
+                        'where'  => ['origin = ?'],
+                        'data'   => [$attachment['res_id'] . ',res_attachments']
+                    ]);
+
+                    if (!empty($originAttachment[0])) {
+                        $originAttachment = $originAttachment[0];
+                        if (in_array($originAttachment['res_id'], $attachmentsIds)) {
+                            continue;
                         }
 
-                        $originAttachment = AttachmentModel::get([
-                            'select' => [
-                                'res_id', 'res_id_master', 'recipient_type', 'recipient_id', 'typist', 'status', 'attachment_type',
-                                'creation_date', 'identifier', 'title', 'format', 'docserver_id', 'origin'
-                            ],
-                            'where'  => ['origin = ?'],
-                            'data'   => [$attachment['res_id'] . ',res_attachments']
+                        $attachment = $originAttachment;
+                    }
+
+                    if ($withSeparators) {
+                        $linkedAttachmentsPath[$attachment['res_id_master']][] = FolderPrintController::getAttachmentSeparator([
+                            'attachment'     => $attachment,
+                            'chronoResource' => $chronoResource
                         ]);
+                    }
 
-                        if (!empty($originAttachment[0])) {
-                            $originAttachment = $originAttachment[0];
-                            if (in_array($originAttachment['res_id'], $attachmentsIds)) {
-                                continue;
-                            }
+                    $path = FolderPrintController::getDocumentFilePath(['document' => $attachment, 'collId' => 'attachments_coll']);
+                    if (!empty($path['errors'])) {
+                        return $response->withStatus($path['code'])->withJson(['errors' => $path['errors']]);
+                    }
 
-                            $attachment = $originAttachment;
-                        }
+                    $linkedAttachmentsPath[$attachment['res_id_master']][] = $path;
+                }
+            }
 
-                        if ($withSeparators) {
-                            $documentPaths[] = FolderPrintController::getAttachmentSeparator([
-                                'attachment'     => $attachment,
-                                'chronoResource' => $chronoResource
-                            ]);
-                        }
+            if (!empty($resource['linkedResources'])) {
+                $controlResource = ResModel::getById(['resId' => $resource['resId'], 'select' => ['linked_resources']]);
+                $controlResource['linked_resources'] = json_decode($controlResource['linked_resources'], true);
+                if (!is_array($resource['linkedResources'])) {
+                    $resource['linkedResources'] = $controlResource['linked_resources'];
+                }
+                if (!ResController::hasRightByResId(['resId' => $resource['linkedResources'], 'userId' => $GLOBALS['id']])) {
+                    return $response->withStatus(403)->withJson(['errors' => 'LinkedResources out of perimeter']);
+                }
+                foreach ($resource['linkedResources'] as $linkedResource) {
+                    if (!Validator::intVal()->validate($linkedResource)) {
+                        return $response->withStatus(400)->withJson(['errors' => 'LinkedResources resId is not an integer']);
+                    }
+                    if (!in_array($linkedResource, $controlResource['linked_resources'])) {
+                        return $response->withStatus(400)->withJson(['errors' => 'LinkedResources resId is not linked to resource']);
+                    }
 
-                        $path = FolderPrintController::getDocumentFilePath(['document' => $attachment, 'collId' => 'attachments_coll']);
-                        if (!empty($path['errors'])) {
-                            return $response->withStatus($path['code'])->withJson(['errors' => $path['errors']]);
-                        }
+                    $document = ResModel::getById([
+                        'select' => ['res_id', 'docserver_id', 'path', 'filename', 'fingerprint', 'category_id', 'alt_identifier'],
+                        'resId'  => $linkedResource
+                    ]);
+                    if (empty($document)) {
+                        return $response->withStatus(400)->withJson(['errors' => 'LinkedResources Document does not exist']);
+                    }
 
-                        $documentPaths[] = $path;
+                    if (empty($document['filename'])) {
+                        return $response->withStatus(400)->withJson(['errors' => 'LinkedResources document has no file']);
+                    }
+
+                    $path = FolderPrintController::getDocumentFilePath(['document' => $document, 'collId' => 'letterbox_coll']);
+                    if (!empty($path['errors'])) {
+                        return $response->withStatus($path['code'])->withJson(['errors' => $path['errors']]);
+                    }
+
+                    if ($withSummarySheet) {
+                        $documentPaths[] = FolderPrintController::getSummarySheet(['units' => $units, 'resId' => $linkedResource]);
+                    }
+
+                    $documentPaths[] = $path;
+
+                    if (!empty($linkedAttachmentsPath[$linkedResource])) {
+                        $documentPaths = array_merge($documentPaths, $linkedAttachmentsPath[$linkedResource]);
+                        unset($linkedAttachmentsPath[$linkedResource]);
                     }
                 }
             }
+
+            foreach ($linkedAttachmentsPath as $linkedAttachmentPath) {
+                $documentPaths = array_merge($documentPaths, $linkedAttachmentPath);
+            }
         }
 
         if (!empty($documentPaths)) {
diff --git a/src/frontend/app/printedFolder/printed-folder-modal.component.ts b/src/frontend/app/printedFolder/printed-folder-modal.component.ts
index d18d84f48e5..f0c1c954e4f 100644
--- a/src/frontend/app/printedFolder/printed-folder-modal.component.ts
+++ b/src/frontend/app/printedFolder/printed-folder-modal.component.ts
@@ -56,11 +56,11 @@ export class PrintedFolderModalComponent implements OnInit {
             this.selectedPrintedFolderElement[element] = new FormControl({ value: [], disabled: false });
         });
 
-        await this.getMainDocInfo();
-        await this.getAttachments();
-        await this.getEmails();
-        await this.getAcknowledgementReceips();
-        await this.getNotes();
+        this.getMainDocInfo();
+        this.getAttachments();
+        this.getEmails();
+        this.getAcknowledgementReceips();
+        this.getNotes();
         await this.getLinkedResources();
 
         this.loading = false;
@@ -337,29 +337,8 @@ export class PrintedFolderModalComponent implements OnInit {
             resource[element] = this.selectedPrintedFolderElement[element].value.length === this.printedFolderElement[element].length ? 'ALL' : this.selectedPrintedFolderElement[element].value;
         });
 
-        // for Linked ressource (complex array)
-        if (!this.functions.empty(resource['linkedResourcesAttachments'])) {
-            resource['linkedResourcesAttachments'] = [];
-            this.selectedPrintedFolderElement['linkedResourcesAttachments'].value.forEach((item: any) => {
-                const resIdMaster = this.printedFolderElement.linkedResources.filter((res: any) => res.id === item)[0].resIdMaster;
-                if (resource['linkedResourcesAttachments'].filter((res: any) => res.resId === resIdMaster).length > 0) {
-                    resource['linkedResourcesAttachments'].filter((res: any) => res.resId === resIdMaster)[0].attachments.push(item);
-                } else {
-                    resource['linkedResourcesAttachments'].push(
-                        {
-                            resId : resIdMaster,
-                            attachments : [
-                                item
-                            ]
-                        }
-                    );
-                }
-            });
-        }
-
         printedFolder.resources.push(resource);
 
-
         return printedFolder;
     }
 
diff --git a/test/unitTests/app/resource/FolderPrintControllerTest.php b/test/unitTests/app/resource/FolderPrintControllerTest.php
index c03516ade53..18047920ef3 100755
--- a/test/unitTests/app/resource/FolderPrintControllerTest.php
+++ b/test/unitTests/app/resource/FolderPrintControllerTest.php
@@ -134,12 +134,7 @@ class FolderPrintControllerTest extends TestCase
                     "notes"                   => [self::$noteId],
                     "acknowledgementReceipts" => [],
                     "emails"                  => [],
-                    "linkedResources"         => [
-                        [
-                            'resId'    => $GLOBALS['resources'][1],
-                            'document' => true
-                        ]
-                    ],
+                    "linkedResources"         => [$GLOBALS['resources'][1]],
                 ], [
                     "resId"                   => $GLOBALS['resources'][0] * 1000,
                     "document"                => true,
@@ -147,12 +142,7 @@ class FolderPrintControllerTest extends TestCase
                     "notes"                   => [self::$noteId],
                     "acknowledgementReceipts" => [],
                     "emails"                  => [],
-                    "linkedResources"         => [
-                        [
-                            'resId'    => $GLOBALS['resources'][1],
-                            'document' => true
-                        ]
-                    ],
+                    "linkedResources"         => [$GLOBALS['resources'][1]],
                 ]
             ]
         ];
-- 
GitLab