From b777ce23c1121f469f377ef09345e1914dbf1aef Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 14 Jan 2020 10:29:38 +0100
Subject: [PATCH] FEAT #10633 TIME 1:30 Pre process visa + fixes

---
 rest/index.php                                |  1 +
 .../PreProcessActionController.php            | 50 +++++++++++++++++++
 .../controllers/OnlyOfficeController.php      |  5 +-
 .../resource/controllers/ResController.php    |  2 +-
 4 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index ebcd558ba15..d3511c4f825 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -384,6 +384,7 @@ $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/ch
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/checkExternalNoteBook', \Action\controllers\PreProcessActionController::class . ':checkExternalNoteBook');
 $app->get('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/getRedirect', \Action\controllers\PreProcessActionController::class . ':getRedirectInformations');
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkShippings', \Action\controllers\PreProcessActionController::class . ':checkShippings');
+$app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}/checkSignatureBook', \Action\controllers\PreProcessActionController::class . ':checkSignatureBook');
 
 //Search
 $app->get('/search', \Search\controllers\SearchController::class . ':get');
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index cb182aa8207..ad1f880e552 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -798,6 +798,56 @@ class PreProcessActionController
         ]);
     }
 
+    public function checkSignatureBook(Request $request, Response $response, array $args)
+    {
+        $body = $request->getParsedBody();
+
+        if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Body resources is empty or not an array']);
+        }
+
+        $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
+        if (!empty($errors['errors'])) {
+            return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
+        }
+
+        $body['resources'] = array_slice($body['resources'], 0, 500);
+        if (!ResController::hasRightByResId(['resId' => $body['resources'], 'userId' => $GLOBALS['id']])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
+        }
+
+        $signableAttachmentsTypes = [];
+        $attachmentsTypes = AttachmentModel::getAttachmentsTypesByXML();
+        foreach ($attachmentsTypes as $key => $type) {
+            if ($type['sign']) {
+                $signableAttachmentsTypes[] = $key;
+            }
+        }
+
+        $resourcesInformations = [];
+        foreach ($body['resources'] as $resId) {
+            $resource = ResModel::getById(['resId' => $resId, 'select' => ['alt_identifier']]);
+            if (empty($resource['alt_identifier'])) {
+                $resource['alt_identifier'] = _UNDEFINED;
+            }
+
+            $attachments = AttachmentModel::get([
+                'select'    => [1],
+                'where'     => ['res_id_master = ?', 'attachment_type in (?)', 'in_signature_book = ?', 'status not in (?)'],
+                'data'      => [$resId, $signableAttachmentsTypes, true, ['OBS', 'DEL', 'FRZ']],
+                'groupBy'   => ['res_id_master']
+            ]);
+
+            if (empty($attachments)) {
+                $resourcesInformations['noAttachment'][] = ['alt_identifier' => $resource['alt_identifier'], 'res_id' => $resId, 'reason' => 'noAttachmentInSignatoryBook'];
+            } else {
+                $resourcesInformations['attachments'][] = ['res_id' => $resId];
+            }
+        }
+
+        return $response->withJson(['resourcesInformations' => $resourcesInformations]);
+    }
+
     public function isDestinationChanging(Request $request, Response $response, array $args)
     {
         if (!ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
diff --git a/src/app/contentManagement/controllers/OnlyOfficeController.php b/src/app/contentManagement/controllers/OnlyOfficeController.php
index 741073fee36..4b2e81fae89 100644
--- a/src/app/contentManagement/controllers/OnlyOfficeController.php
+++ b/src/app/contentManagement/controllers/OnlyOfficeController.php
@@ -161,7 +161,10 @@ class OnlyOfficeController
 
         $checkUrl = str_replace('http://', '', $queryParams['url']);
         $checkUrl = str_replace('https://', '', $checkUrl);
-        if (strpos($checkUrl, (string)$loadedXml->onlyoffice->server_uri .'/cache/files/') !== 0) {
+        $uri = (string)$loadedXml->onlyoffice->server_uri;
+        $port = (string)$loadedXml->onlyoffice->server_port;
+
+        if (strpos($checkUrl, "{$uri}:{$port}/cache/files/") !== 0) {
             return $response->withStatus(400)->withJson(['errors' => 'Query params url is not allowed']);
         }
 
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index 309838f9bcd..eafd381c378 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -238,7 +238,7 @@ class ResController
             $formattedData['opinionCircuit'] = 0;
             $listInstanceItems = ListInstanceModel::get(['select' => ['count(1)', 'difflist_type'], 'where' => ['res_id = ?'], 'data' => [$args['resId']], 'groupBy' => ['difflist_type']]);
             foreach ($listInstanceItems as $item) {
-                $type = $item['difflist_type'] == 'entity_id' ? 'diffusionList' : ($item['difflist_type'] == 'visaCircuit' ? 'visaCircuitItems' : 'opinionCircuit');
+                $type = $item['difflist_type'] == 'entity_id' ? 'diffusionList' : ($item['difflist_type'] == 'VISA_CIRCUIT' ? 'visaCircuit' : 'opinionCircuit');
                 $formattedData[$type] = $item['count'];
             }
 
-- 
GitLab