From b6a54b868d2d9bae484af4df70a83138cc1ccb00 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 29 Mar 2019 17:22:50 +0100
Subject: [PATCH] FEAT #9906 Attachments list

---
 core/class/class_resource.php                 | 10 +++++-
 rest/index.php                                |  2 +-
 .../controllers/AttachmentController.php      | 32 +++++++++++++++++--
 .../models/AttachmentModelAbstract.php        | 22 ++++++++-----
 .../attachments/attachments-list.component.ts |  4 +--
 5 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/core/class/class_resource.php b/core/class/class_resource.php
index 644237788b8..6f1e37f55ff 100755
--- a/core/class/class_resource.php
+++ b/core/class/class_resource.php
@@ -143,7 +143,15 @@
         $docserverControler = new docservers_controler();
         $docserverTypeControler = new docserver_types_controler();
         $docserver = $docserverControler->get($docserver_id);
+        $arrayTmp = (array)$docserver;
+        foreach ($arrayTmp as $key => $value) {
+            $docserver = (object)$value;
+        }
         $docserverTypeObject = $docserverTypeControler->get($docserver->docserver_type_id);
+        $arrayTmp = (array)$docserver;
+        foreach ($arrayTmp as $key => $value) {
+            $docserverTypeObject = (object)$value;
+        }
         $fingerprint = Ds_doFingerprint($filetmp, $docserverTypeObject->fingerprint_mode);
         $filesize = filesize($filetmp);
         array_push($data, array('column' => "fingerprint", 'value' => $fingerprint, 'type' => "string"));
@@ -242,7 +250,7 @@
                 }
             } elseif ($value['column'] == 'filename') {
                 $find_filename = true;
-                if (!preg_match("/^[\w-.]+.([a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9]?|maarch)$/", $value['value'])) {
+                if (!preg_match("/^[\w\-.]+.([a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9][a-zA-Z-0-9]?|maarch)$/", $value['value'])) {
                     $error .= _FILENAME_ERROR . ' ' . $value['value'] . '<br/>';
                 }
             } elseif ($value['column'] == 'fingerprint') {
diff --git a/rest/index.php b/rest/index.php
index d14d23f8229..1b15e0ac56e 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -69,7 +69,7 @@ $app->get('/administration', \SrcCore\controllers\CoreController::class . ':getA
 
 //Attachments
 $app->post('/attachments', \Attachment\controllers\AttachmentController::class . ':create');
-$app->get('/res/{resId}/attachments', \Attachment\controllers\AttachmentController::class . ':getAttachmentsListById');
+$app->get('/resources/{resId}/attachments', \Attachment\controllers\AttachmentController::class . ':getByResId');
 $app->get('/res/{resIdMaster}/attachments/{resId}/content', \Attachment\controllers\AttachmentController::class . ':getFileContent');
 $app->get('/res/{resIdMaster}/attachments/{resId}/thumbnail', \Attachment\controllers\AttachmentController::class . ':getThumbnailContent');
 
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index 1673bec8e86..a0ee537f2bd 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 Contact\models\ContactModel;
 use Convert\controllers\ConvertPdfController;
 use Convert\controllers\ConvertThumbnailController;
 use Convert\models\AdrModel;
@@ -29,6 +30,7 @@ use Respect\Validation\Validator;
 use setasign\Fpdi\Tcpdf\Fpdi;
 use Slim\Http\Request;
 use Slim\Http\Response;
+use SrcCore\controllers\AutoCompleteController;
 use SrcCore\models\CoreConfigModel;
 use SrcCore\models\DatabaseModel;
 use SrcCore\models\ValidatorModel;
@@ -68,7 +70,7 @@ class AttachmentController
         return $response->withJson(['resId' => $resId]);
     }
 
-    public function getAttachmentsListById(Request $request, Response $response, array $aArgs)
+    public function getByResId(Request $request, Response $response, array $aArgs)
     {
         if (!Validator::intVal()->validate($aArgs['resId']) || !ResController::hasRightByResId(['resId' => [$aArgs['resId']], 'userId' => $GLOBALS['userId']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
@@ -79,10 +81,34 @@ class AttachmentController
             $excludeAttachmentTypes[] = 'document_with_notes';
         }
 
-        $attachments = AttachmentModel::getListByResIdMaster(['id' => $aArgs['resId'], 'excludeAttachmentTypes' => $excludeAttachmentTypes]);
+        $attachments = AttachmentModel::getListByResIdMaster([
+            'resId'                     => $aArgs['resId'],
+            'excludeAttachmentTypes'    => $excludeAttachmentTypes,
+            'orderBy'                   => ['res_id DESC']
+        ]);
+        foreach ($attachments as $key => $attachment) {
+            if (!empty($attachment['res_id_version'])) {
+                $attachments[$key]['res_id'] = $attachment['res_id_version'];
+            }
+            $attachments[$key]['contact'] = '';
+            if (!empty($attachment['dest_address_id'])) {
+                $contact = ContactModel::getOnView([
+                    'select' => [
+                        'is_corporate_person', 'lastname', 'firstname',
+                        'ca_id', 'society', 'contact_firstname', 'contact_lastname'
+                    ],
+                    'where' => ['ca_id = ?'],
+                    'data'  => [$attachment['dest_address_id']]
+                ]);
+                if (!empty($contact[0])) {
+                    $contact = AutoCompleteController::getFormattedContact(['contact' => $contact[0]]);
+                    $attachments[$key]['contact'] = $contact['contact']['contact'];
+                }
+            }
+        }
         $attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
 
-        return $response->withJson(['attachments'  => $attachments, 'attachment_types'  => $attachmentTypes]);
+        return $response->withJson(['attachments' => $attachments, 'attachmentTypes'  => $attachmentTypes]);
     }
 
     public function setInSignatureBook(Request $request, Response $response, array $aArgs)
diff --git a/src/app/attachment/models/AttachmentModelAbstract.php b/src/app/attachment/models/AttachmentModelAbstract.php
index 8fe64e0d307..416dff6f723 100755
--- a/src/app/attachment/models/AttachmentModelAbstract.php
+++ b/src/app/attachment/models/AttachmentModelAbstract.php
@@ -85,17 +85,23 @@ abstract class AttachmentModelAbstract
 
     public static function getListByResIdMaster(array $aArgs)
     {
-        ValidatorModel::notEmpty($aArgs, ['id']);
-        ValidatorModel::intVal($aArgs, ['id']);
+        ValidatorModel::notEmpty($aArgs, ['resId']);
+        ValidatorModel::intVal($aArgs, ['resId']);
         ValidatorModel::arrayType($aArgs, ['excludeAttachmentTypes']);
 
         $aAttachments = DatabaseModel::select([
-            'select'    => empty($aArgs['select']) ? ['res_id', 'res_attachments.identifier', 'title', 'format', 'creation_date', 'doc_date as update_date', 'validation_date as return_date', 'effective_date as real_return_date', 'u.firstname as firstname_updated', 'u.lastname as lastname_updated', 'relation', 'docserver_id', 'path', 'filename', 'fingerprint', 'filesize', 'label_status as status', 'attachment_type', 'dest_contact_id', 'dest_address_id', 'ut.firstname as firstname_typist', 'ut.lastname as lastname_typist'] : $aArgs['select'],
-            'table'     => ['res_attachments','users ut', 'status', 'users u'],
-            'left_join' => ['res_attachments.typist = ut.user_id', 'res_attachments.status = status.id', 'res_attachments.updated_by = u.user_id'],
-            'where'     => ['res_id_master = ?', 'res_attachments.status not in (?,?)', 'attachment_type not in (?)'],
-            'data'      => [$aArgs['id'], 'OBS', 'DEL', $aArgs['excludeAttachmentTypes']],
-            'order_by'  => empty($aArgs['orderBy']) ? ['res_id DESC'] : $aArgs['orderBy'],
+            'select'    => [
+                'res_id', 'res_id_version', 'res_view_attachments.identifier', 'title', 'format', 'creation_date',
+                'doc_date as update_date', 'validation_date as return_date', 'effective_date as real_return_date',
+                'u.firstname as firstname_updated', 'u.lastname as lastname_updated', 'relation', 'docserver_id', 'path',
+                'filename', 'fingerprint', 'filesize', 'label_status as status', 'attachment_type', 'dest_contact_id',
+                'dest_address_id', 'ut.firstname as firstname_typist', 'ut.lastname as lastname_typist'
+            ],
+            'table'     => ['res_view_attachments', 'users ut', 'status', 'users u'],
+            'left_join' => ['res_view_attachments.typist = ut.user_id', 'res_view_attachments.status = status.id', 'res_view_attachments.updated_by = u.user_id'],
+            'where'     => ['res_id_master = ?', 'res_view_attachments.status not in (?)', 'attachment_type not in (?)'],
+            'data'      => [$aArgs['resId'], ['OBS', 'DEL'], $aArgs['excludeAttachmentTypes']],
+            'order_by'  => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'],
         ]);
 
         return $aAttachments;
diff --git a/src/frontend/app/attachments/attachments-list.component.ts b/src/frontend/app/attachments/attachments-list.component.ts
index 8e9ba7814b5..793b119de23 100644
--- a/src/frontend/app/attachments/attachments-list.component.ts
+++ b/src/frontend/app/attachments/attachments-list.component.ts
@@ -26,13 +26,13 @@ export class AttachmentsListComponent implements OnInit {
     loadAttachments(resId: number) {
         this.resIds[0] = resId;
         this.loading = true;
-        this.http.get("../../rest/res/" + this.resIds[0] + "/attachments")
+        this.http.get("../../rest/resources/" + this.resIds[0] + "/attachments")
             .subscribe((data: any) => {
                 this.attachments = data.attachments;
                 this.attachments.forEach((element: any) => {
                     element.thumbnailUrl = '../../rest/res/' + this.resIds[0] + '/attachments/' + element.res_id + '/thumbnail';
                 });
-                this.attachmentTypes = data.attachment_types;
+                this.attachmentTypes = data.attachmentTypes;
                 this.reloadBadgeNotes.emit(`${this.attachments.length}`);
                 this.loading = false;
             });
-- 
GitLab