From 13bca05025f3e0c8071365612ef7debd0131de98 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Tue, 14 Jan 2020 15:18:40 +0100
Subject: [PATCH] FEAT #10633 TIME 1:45 back action for redirect to initiator
 entity

---
 core/xml/actions_pages.xml                    |  2 +-
 migration/19.12/1912.sql                      |  1 +
 .../controllers/ActionMethodController.php    | 51 +++++++++++++++++++
 .../PreProcessActionController.php            | 37 ++++++++++++++
 4 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/core/xml/actions_pages.xml b/core/xml/actions_pages.xml
index f147da094ad..695d06fa77a 100755
--- a/core/xml/actions_pages.xml
+++ b/core/xml/actions_pages.xml
@@ -307,7 +307,7 @@ An action page is described in a ACTIONPAGE tag :
         <LABEL>_REDIRECT_WORKFLOW_ENTITY</LABEL>
         <NAME>redirect_visa_entity</NAME>
         <DESC>_REDIRECT_WORKFLOW_ENTITY_DESC</DESC>
-        <component>v1Action</component>
+        <component>redirectInitiatorEntityAction</component>
         <ORIGIN>module</ORIGIN>
         <MODULE>visa</MODULE>
         <FLAG_CREATE>false</FLAG_CREATE>
diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql
index c71e998918f..973b39accf7 100644
--- a/migration/19.12/1912.sql
+++ b/migration/19.12/1912.sql
@@ -87,6 +87,7 @@ UPDATE actions SET component = 'confirmAction', action_page = 'confirm_status' W
 DELETE FROM actions WHERE action_page = 'view' OR component = 'viewDoc';
 
 UPDATE actions SET component = 'rejectVisaBackToPrevious' WHERE action_page = 'rejection_visa_previous';
+UPDATE actions SET component = 'redirectInitiatorEntityAction' WHERE action_page = 'redirect_visa_entity';
 
 
 /* FOLDERS */
diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index e153a703b68..3f314ad918f 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -18,7 +18,9 @@ use Action\models\BasketPersistenceModel;
 use Action\models\ResMarkAsReadModel;
 use Attachment\models\AttachmentModel;
 use Entity\controllers\ListInstanceController;
+use Entity\models\EntityModel;
 use Entity\models\ListInstanceModel;
+use Entity\models\ListTemplateModel;
 use ExternalSignatoryBook\controllers\MaarchParapheurController;
 use History\controllers\HistoryController;
 use MessageExchange\controllers\MessageExchangeReviewController;
@@ -51,6 +53,7 @@ class ActionMethodController
         'sendShippingAction'                    => 'createMailevaShippings',
         'sendSignatureBookAction'               => 'sendSignatureBook',
         'rejectVisaBackToPrevious'              => 'rejectVisaBackToPrevious',
+        'redirectInitiatorEntityAction'         => 'redirectInitiatorEntityAction',
         'noConfirmAction'                       => null
     ];
 
@@ -272,6 +275,54 @@ class ActionMethodController
         return true;
     }
 
+    public static function redirectInitiatorEntityAction(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['resId']);
+        ValidatorModel::intVal($args, ['resId']);
+        ValidatorModel::arrayType($args, ['data']);
+
+        $resource = ResModel::getById(['select' => ['initiator'], 'resId' => $args['resId']]);
+        if (!empty($resource)) {
+            $entityInfo = EntityModel::getByEntityId(['entityId' => $resource['initiator'], 'select' => ['id']]);
+            if (!empty($entityInfo)) {
+                $destUser = ListTemplateModel::getWithItems(['data' => ['entity_id = ?', 'item_mode = ?', 'type = ?'], 'data' => [$entityInfo['id'], 'dest', 'diffusionList']]);
+                if (!empty($destUser)) {
+                    $listInstances = ListInstanceModel::get(['select' => ['*'], 'where' => ['res_id = ?', 'difflist_type = ?', 'item_mode = ?'], 'data' => [$args['resId'], 'entity_id', 'dest']]);
+                    if (!empty($listInstances)) {
+                        ListInstanceModel::create([
+                            'res_id'              => $listInstances[0]['res_id'],
+                            'sequence'            => $listInstances[0]['sequence'],
+                            'item_id'             => $listInstances[0]['item_id'],
+                            'item_type'           => $listInstances[0]['item_type'],
+                            'item_mode'           => 'cc',
+                            'added_by_user'       => $listInstances[0]['added_by_user'],
+                            'viewed'              => $listInstances[0]['viewed'],
+                            'difflist_type'       => $listInstances[0]['difflist_type'],
+                            'process_date'        => $listInstances[0]['process_date'],
+                            'process_comment'     => $listInstances[0]['process_comment'],
+                            'requested_signature' => $listInstances[0]['requested_signature']
+                        ]);
+                    }
+                    $userInfo = UserModel::getById(['select' => ['user_id'], 'id' => $destUser[0]['item_id']]);
+                    ListInstanceModel::update([
+                        'set' => [
+                            'item_id' => $userInfo['user_id']
+                        ],
+                        'where' => ['listinstance_id = ?'],
+                        'data' => [$listInstances[0]['listinstance_id']]
+                    ]);
+                    ResModel::update([
+                        'set'   => ['destination' => $resource['initiator']], 
+                        'where' => ['res_id = ?'],
+                        'data'  => [$args['resId']]
+                    ]);
+                }
+            }
+        }
+
+        return true;
+    }
+
     public function sendSignatureBook(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId']);
diff --git a/src/app/action/controllers/PreProcessActionController.php b/src/app/action/controllers/PreProcessActionController.php
index 2c9e7828190..da5a75827cf 100755
--- a/src/app/action/controllers/PreProcessActionController.php
+++ b/src/app/action/controllers/PreProcessActionController.php
@@ -798,6 +798,43 @@ class PreProcessActionController
         ]);
     }
 
+    public function checkInitiatorEntity(Request $request, Response $response, array $args)
+    {
+        $currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
+
+        $errors = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $currentUser['id']]);
+        if (!empty($errors['errors'])) {
+            return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
+        }
+
+        $data = $request->getParsedBody();
+
+        if (!Validator::arrayType()->notEmpty()->validate($data['resources'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Data resources is empty or not an array']);
+        }
+
+        $withEntity = [];
+        $withoutEntity = [];
+
+        $resources = ResModel::get([
+            'select' => ['initiator', 'res_id'],
+            'where'  => ['res_id in (?)'],
+            'data'   => [$data['resources']]
+        ]);
+
+        $resourcesInfo = array_column($resources, 'initiator', 'res_id');
+
+        foreach ($data['resources'] as $valueResId) {
+            if (!empty($resourcesInfo[$valueResId])) {
+                $withEntity[] = $valueResId;
+            } else {
+                $withoutEntity[] = $valueResId;
+            }
+        }
+
+        return $response->withJson(['withEntity' => $withEntity, 'withoutEntity' => $withoutEntity]);
+    }
+
     public function checkSignatureBook(Request $request, Response $response, array $args)
     {
         $body = $request->getParsedBody();
-- 
GitLab