From d0b3d4f83e05ced535b7e6b434c109ebf2fcecca Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 21 Jan 2021 18:43:39 +0100
Subject: [PATCH] FEAT #15155 TIME 2:30 script to archive mails in mass mode

---
 .../controllers/ExportSEDATrait.php           |  26 ++--
 .../controllers/PreProcessActionSEDATrait.php |   7 +
 .../exportSeda/scripts/ExportSedaScript.php   | 145 ++++++++++++++++++
 .../controllers/IndexingController.php        |   2 +-
 src/lang/lang-fr.json                         |   1 +
 5 files changed, 167 insertions(+), 14 deletions(-)
 create mode 100644 src/app/external/exportSeda/scripts/ExportSedaScript.php

diff --git a/src/app/external/exportSeda/controllers/ExportSEDATrait.php b/src/app/external/exportSeda/controllers/ExportSEDATrait.php
index 2946830fcf1..730aa077a39 100644
--- a/src/app/external/exportSeda/controllers/ExportSEDATrait.php
+++ b/src/app/external/exportSeda/controllers/ExportSEDATrait.php
@@ -193,19 +193,19 @@ trait ExportSEDATrait
             unlink($sedaPackage['encodedFilePath']);
             return ['data' => ['encodedFile' => $encodedContent]];
         } else {
-            $elementSend  = ExportSEDATrait::sendSedaPackage([
-                'messageId'       => $messageSaved['messageId'],
-                'config'          => $config,
-                'encodedFilePath' => $sedaPackage['encodedFilePath'],
-                'messageFilename' => $sedaPackage['messageFilename'],
-                'resId'           => $resource['res_id'],
-                'reference'       => $data['messageObject']['messageIdentifier']
-            ]);
-            unlink($sedaPackage['encodedFilePath']);
-            if (!empty($elementSend['errors'])) {
-                return ['errors' => [$elementSend['errors']]];
-            }
-    
+            $resId           = '--resId ' . $resource['res_id'];
+            $userId          = '--userId ' . $GLOBALS['id'];
+            $messageId       = '--messageId ' . $messageSaved['messageId'];
+            $encodedFilePath = '--encodedFilePath ' . $sedaPackage['encodedFilePath'];
+            $messageFileName = '--messageFilename ' . $sedaPackage['messageFilename'];
+            $reference       = '--reference ' . $data['messageObject']['messageIdentifier'];
+            $actionId        = '--actionId ' . $args['actionId'];
+
+            $customId = CoreConfigModel::getCustomId();
+            $customId = empty($customId) ? 'null' : $customId;
+            $customId = '--customId ' . $customId;
+
+            exec("php src/app/external/exportSeda/scripts/ExportSedaScript.php {$customId} {$resId} {$userId} {$messageId} {$encodedFilePath} {$messageFileName} {$reference} {$actionId} > /dev/null &");
             return true;
         }
     }
diff --git a/src/app/external/exportSeda/controllers/PreProcessActionSEDATrait.php b/src/app/external/exportSeda/controllers/PreProcessActionSEDATrait.php
index 8edb82b5fc5..5a8defd8a01 100644
--- a/src/app/external/exportSeda/controllers/PreProcessActionSEDATrait.php
+++ b/src/app/external/exportSeda/controllers/PreProcessActionSEDATrait.php
@@ -13,6 +13,7 @@
 namespace ExportSeda\controllers;
 
 use Action\controllers\PreProcessActionController;
+use Action\models\ActionModel;
 use Attachment\models\AttachmentModel;
 use Docserver\models\DocserverModel;
 use Docserver\models\DocserverTypeModel;
@@ -49,6 +50,12 @@ trait PreProcessActionSEDATrait
             return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
         }
 
+        $action = ActionModel::getById(['id' => $aArgs['actionId'], 'select' => ['parameters']]);
+        $actionParams = json_decode($action['parameters'], true);
+        if (empty($actionParams['errorStatus']) || empty($actionParams['successStatus'])) {
+            return $response->withStatus(403)->withJson(['errors' => 'errorStatus or successStatus is not set for this action', 'lang' => 'actionStatusNotSet']);
+        }
+
         $resourcesInformations = ['success' => [], 'errors' => []];
         $body['resources'] = PreProcessActionController::getNonLockedResources(['resources' => $body['resources'], 'userId' => $GLOBALS['id']]);
 
diff --git a/src/app/external/exportSeda/scripts/ExportSedaScript.php b/src/app/external/exportSeda/scripts/ExportSedaScript.php
new file mode 100644
index 00000000000..9b3a7133401
--- /dev/null
+++ b/src/app/external/exportSeda/scripts/ExportSedaScript.php
@@ -0,0 +1,145 @@
+<?php
+
+/**
+ * Copyright Maarch since 2008 under licence GPLv3.
+ * See LICENCE.txt file at the root folder for more details.
+ * This file is part of Maarch software.
+ *
+ */
+
+/**
+ * @brief Export Seda Script
+ * @author dev@maarch.org
+ */
+
+namespace ExportSeda\controllers;
+
+require 'vendor/autoload.php';
+
+use Action\models\ActionModel;
+use ExportSeda\controllers\ExportSEDATrait;
+use Resource\models\ResModel;
+use SrcCore\controllers\LogsController;
+use SrcCore\models\CoreConfigModel;
+use SrcCore\models\DatabasePDO;
+use User\models\UserModel;
+
+// ARGS
+// --customId    : instance id;
+// --resId      : res_id of the mail to archive;
+// --userId      : technical identifer user (for saving log);
+// --actionId   : identifier of the action made to archive mail
+// --messageId : message_id in message_exchange table
+// --encodedFilePath : Path of the encoded archive file
+// --messageFilename : Name of the archive file
+// --reference : reference of the archive
+
+ExportSedaScript::initalize($argv);
+
+class ExportSedaScript
+{
+    public static function initalize($args)
+    {
+        $customId        = '';
+        $resId           = '';
+        $actionId        = '';
+        $messageId       = '';
+        $encodedFilePath = '';
+        $messageFilename = '';
+        $reference       = '';
+
+        if (array_search('--customId', $args) > 0) {
+            $cmd = array_search('--customId', $args);
+            $customId = $args[$cmd+1];
+        }
+        
+        if (array_search('--resId', $args) > 0) {
+            $cmd = array_search('--resId', $args);
+            $resId = $args[$cmd+1];
+        }
+
+        if (array_search('--userId', $args) > 0) {
+            $cmd = array_search('--userId', $args);
+            $userId = $args[$cmd+1];
+        }
+        
+        if (array_search('--actionId', $args) > 0) {
+            $cmd = array_search('--actionId', $args);
+            $actionId = $args[$cmd+1];
+        }
+
+        if (array_search('--messageId', $args) > 0) {
+            $cmd = array_search('--messageId', $args);
+            $messageId = $args[$cmd+1];
+        }
+
+        if (array_search('--encodedFilePath', $args) > 0) {
+            $cmd = array_search('--encodedFilePath', $args);
+            $encodedFilePath = $args[$cmd+1];
+        }
+
+        if (array_search('--messageFilename', $args) > 0) {
+            $cmd = array_search('--messageFilename', $args);
+            $messageFilename = $args[$cmd+1];
+        }
+
+        if (array_search('--reference', $args) > 0) {
+            $cmd = array_search('--reference', $args);
+            $reference = $args[$cmd+1];
+        }
+
+        if (!empty($userId)) {
+            ExportSedaScript::send([
+                'customId' => $customId, 'resId' => $resId, 'userId' => $userId, 'actionId' => $actionId,
+                'messageId' => $messageId, 'encodedFilePath' => $encodedFilePath, 'messageFilename' => $messageFilename, 'reference' => $reference]);
+        }
+    }
+
+    public static function send(array $args)
+    {
+        DatabasePDO::reset();
+        new DatabasePDO(['customId' => $args['customId']]);
+
+        $currentUser = UserModel::getById(['id' => $args['userId'], 'select' => ['user_id']]);
+        $GLOBALS['login'] = $currentUser['user_id'];
+
+        $config = CoreConfigModel::getJsonLoaded(['path' => 'apps/maarch_entreprise/xml/config.json']);
+        $action = ActionModel::getById(['id' => $args['actionId'], 'select' => ['parameters']]);
+        $actionParams = json_decode($action['parameters'], true);
+
+        $elementSend  = ExportSEDATrait::sendSedaPackage([
+            'messageId'       => $args['messageId'],
+            'config'          => $config,
+            'encodedFilePath' => $args['encodedFilePath'],
+            'messageFilename' => $args['messageFilename'],
+            'resId'           => $args['resId'],
+            'reference'       => $args['reference']
+        ]);
+        unlink($args['encodedFilePath']);
+        if (!empty($elementSend['errors'])) {
+            ResModel::update(['set' => ['status' => $actionParams['errorStatus']], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]);
+            LogsController::add([
+                'isTech'    => true,
+                'moduleId'  => 'exportSeda',
+                'level'     => 'ERROR',
+                'tableName' => 'letterbox_coll',
+                'recordId'  => $args['resId'],
+                'eventType' => "Export Seda failed : {$elementSend['errors']}",
+                'eventId'   => "resId : {$args['resId']}"
+            ]);
+        } else {
+            ResModel::update(['set' => ['status' => $actionParams['successStatus']], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]);
+            LogsController::add([
+                'isTech'    => true,
+                'moduleId'  => 'exportSeda',
+                'level'     => 'INFO',
+                'tableName' => 'letterbox_coll',
+                'recordId'  => $args['resId'],
+                'eventType' => "Export Seda success",
+                'eventId'   => "resId : {$args['resId']}"
+            ]);
+        }
+
+        return true;
+    }
+}
diff --git a/src/app/resource/controllers/IndexingController.php b/src/app/resource/controllers/IndexingController.php
index 5279ca7c0a8..221f9843e85 100755
--- a/src/app/resource/controllers/IndexingController.php
+++ b/src/app/resource/controllers/IndexingController.php
@@ -108,7 +108,7 @@ class IndexingController
 
         $method = ActionMethodController::COMPONENTS_ACTIONS[$action['component']];
         if (!empty($method)) {
-            $methodResponse = ActionMethodController::$method(['resId' => $body['resource'], 'data' => $body['data'], 'note' => $body['note'], 'parameters' => $parameters]);
+            $methodResponse = ActionMethodController::$method(['resId' => $body['resource'], 'data' => $body['data'], 'note' => $body['note'], 'parameters' => $parameters, 'actionId' => $args['actionId']]);
         }
         if (!empty($methodResponse['errors'])) {
             $return = ['errors' => $methodResponse['errors'][0]];
diff --git a/src/lang/lang-fr.json b/src/lang/lang-fr.json
index c3058c87b66..87977bd095e 100644
--- a/src/lang/lang-fr.json
+++ b/src/lang/lang-fr.json
@@ -2101,6 +2101,7 @@
     "noProducerService": "L'entité traitante n'a pas d'identifiant de service producteur",
     "differentProducerServices": "Tous les courriers n'ont pas le même service producteur",
     "noSenderOrgRegNumber": "Aucun service versant défini (senderOrgRegNumber dans config.json)",
+    "actionStatusNotSet": "L'action n'a pas de statut d'erreur ou de succès défini",
     "producerServiceDoesNotExists": "Le service producteur n'existe pas dans MaarchRM",
     "nextPage": "Page suivante",
     "prevPage": "Page précédente",
-- 
GitLab