From 4e3b6bbe0ae7df2607bd84bff496861ef3ffd84e Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 4 Apr 2019 16:45:34 +0100
Subject: [PATCH] FEAT #8391 maarchParapheur V1

---
 .../actions/sendToExternalSignatureBook.php   |   5 +-
 apps/maarch_entreprise/lang/en.php            |   6 +
 apps/maarch_entreprise/lang/fr.php            |   6 +
 apps/maarch_entreprise/lang/nl.php            |   6 +
 .../visa/class/MaarchParapheurController.php  | 188 +-----------------
 5 files changed, 21 insertions(+), 190 deletions(-)

diff --git a/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php b/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php
index 19fc833aa1f..24d28b009ad 100755
--- a/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php
+++ b/apps/maarch_entreprise/actions/sendToExternalSignatureBook.php
@@ -177,7 +177,6 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
                 include_once 'modules/visa/class/FastParapheurController.php';
                 $attachmentToFreeze = FastParapheurController::sendDatas(['config' => $config, 'resIdMaster' => $res_id]);
             } elseif ($config['id'] == 'maarchParapheur') {
-                include_once 'modules/visa/class/MaarchParapheurController.php';
 
                 $processingUser  = get_value_fields($values_form, 'processingUser');
                 $objectSentNote  = get_value_fields($values_form, 'objectSentNote');
@@ -188,8 +187,8 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
                 } else {
                     $objectSent = $objectSentSign;
                 }
-                $processingUserInfo = MaarchParapheurController::getUserById(['config' => $config, 'id' => $processingUser]);
-                $attachmentToFreeze = MaarchParapheurController::sendDatas([
+                $processingUserInfo = \ExternalSignatoryBook\controllers\MaarchParapheurController::getUserById(['config' => $config, 'id' => $processingUser]);
+                $attachmentToFreeze = \ExternalSignatoryBook\controllers\MaarchParapheurController::sendDatas([
                     'config'             => $config,
                     'resIdMaster'        => $res_id,
                     'processingUser'     => $processingUserInfo['login'],
diff --git a/apps/maarch_entreprise/lang/en.php b/apps/maarch_entreprise/lang/en.php
index 3111d160a14..9b07d7200da 100755
--- a/apps/maarch_entreprise/lang/en.php
+++ b/apps/maarch_entreprise/lang/en.php
@@ -4904,3 +4904,9 @@ if (!defined('_ACKNOWLEDGEMENT_RECEIPTS')) {
 if (!defined('_PUT_IN_SEND_ATTACH')) {
     define('_PUT_IN_SEND_ATTACH', 'Put in send attachement');
 }
+if (!defined('_INITIATOR_ENTITY')) {
+    define('_INITIATOR_ENTITY', 'Initiator entity');
+}
+if (!defined('_RECIPIENTS')) {
+    define('_RECIPIENTS', 'Recipient(s)');
+}
diff --git a/apps/maarch_entreprise/lang/fr.php b/apps/maarch_entreprise/lang/fr.php
index 8e08911bc55..e0188fc04c8 100755
--- a/apps/maarch_entreprise/lang/fr.php
+++ b/apps/maarch_entreprise/lang/fr.php
@@ -5041,3 +5041,9 @@ if (!defined('_ACKNOWLEDGEMENT_RECEIPTS')) {
 if (!defined('_PUT_IN_SEND_ATTACH')) {
     define('_PUT_IN_SEND_ATTACH', 'Intégrer aux envois');
 }
+if (!defined('_INITIATOR_ENTITY')) {
+    define('_INITIATOR_ENTITY', 'Entité initiatrice');
+}
+if (!defined('_RECIPIENTS')) {
+    define('_RECIPIENTS', 'Destinataire(s)');
+}
diff --git a/apps/maarch_entreprise/lang/nl.php b/apps/maarch_entreprise/lang/nl.php
index b3966b8019c..65c83dff630 100755
--- a/apps/maarch_entreprise/lang/nl.php
+++ b/apps/maarch_entreprise/lang/nl.php
@@ -1661,4 +1661,10 @@ if (!defined('_ACKNOWLEDGEMENT_RECEIPTS')) {
 }
 if (!defined('_PUT_IN_SEND_ATTACH')) {
     define('_PUT_IN_SEND_ATTACH', 'Put in send attachement');
+}
+if (!defined('_INITIATOR_ENTITY')) {
+    define('_INITIATOR_ENTITY', 'Initiator entity');
+}
+if (!defined('_RECIPIENTS')) {
+    define('_RECIPIENTS', 'Recipient(s)');
 }
\ No newline at end of file
diff --git a/modules/visa/class/MaarchParapheurController.php b/modules/visa/class/MaarchParapheurController.php
index 84d21209b26..5a51c216f11 100755
--- a/modules/visa/class/MaarchParapheurController.php
+++ b/modules/visa/class/MaarchParapheurController.php
@@ -40,196 +40,10 @@ class MaarchParapheurController
 
     public static function getInitializeDatas($config)
     {
-        $rawResponse['users'] = MaarchParapheurController::getUsers(['config' => $config]);
+        $rawResponse['users'] = \ExternalSignatoryBook\controllers\MaarchParapheurController::getUsers(['config' => $config]);
         if (!empty($rawResponse['users']['error'])) {
             return ['error' => $rawResponse['users']['error']];
         }
         return $rawResponse;
     }
-
-    public static function getUsers($aArgs)
-    {
-        $response = \SrcCore\models\CurlModel::exec([
-            'url'      => $aArgs['config']['data']['url'] . '/rest/users',
-            'user'     => $aArgs['config']['data']['userId'],
-            'password' => $aArgs['config']['data']['password'],
-            'method'   => 'GET'
-        ]);
-
-        if (!empty($response['error'])) {
-            return ["error" => $response['error']];
-        }
-
-        return $response['users'];
-    }
-
-    public static function sendDatas($aArgs)
-    {
-        $attachmentToFreeze = [];
-
-        $adrMainInfo              = \Convert\controllers\ConvertPdfController::getConvertedPdfById(['resId' => $aArgs['resIdMaster'], 'collId' => 'letterbox_coll']);
-        $docserverMainInfo        = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $adrMainInfo['docserver_id']]);
-        $arrivedMailMainfilePath  = $docserverMainInfo['path_template'] . str_replace('#', '/', $adrMainInfo['path']) . $adrMainInfo['filename'];
-        $encodedMainZipFile       = MaarchParapheurController::createZip(['filepath' => $arrivedMailMainfilePath, 'filename' => 'courrier_arrivee.pdf']);
-
-        $mainResource = \Resource\models\ResModel::getOnView([
-            'select' => ['process_limit_date', 'status', 'category_id', 'alt_identifier', 'subject', 'priority', 'contact_firstname', 'contact_lastname', 'contact_society', 'category_id'],
-            'where'  => ['res_id = ?'],
-            'data'   => [$aArgs['resIdMaster']]
-        ]);
-        if (empty($mainResource[0]['process_limit_date'])) {
-            $processLimitDate = date('Y-m-d H:i:s', strtotime(date("Y-m-d H:i:s"). ' + 14 days'));
-        } else {
-            $processLimitDate = $mainResource[0]['process_limit_date'];
-        }
-
-        $processingUser      = $aArgs['processingUser'];
-        $priority            = \Priority\models\PriorityModel::getById(['select' => ['label'], 'id' => $mainResource[0]['priority']]);
-        $sender              = \User\models\UserModel::getByLogin(['select' => ['firstname', 'lastname'], 'login' => $aArgs['userId']]);
-        $senderPrimaryEntity = \User\models\UserModel::getPrimaryEntityByUserId(['userId' => $aArgs['userId']]);
-
-        if ($aArgs['objectSent'] == 'attachment') {
-            $excludeAttachmentTypes = ['converted_pdf', 'print_folder', 'incoming_mail_attachment', 'signed_response'];
-
-            $attachments = \Attachment\models\AttachmentModel::getOnView([
-                'select'    => [
-                    'res_id', 'res_id_version', 'title', 'identifier', 'attachment_type',
-                    'status', 'typist', 'docserver_id', 'path', 'filename', 'creation_date',
-                    'validation_date', 'relation', 'attachment_id_master'
-                ],
-                'where'     => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP')", "in_signature_book = 'true'"],
-                'data'      => [$aArgs['resIdMaster'], $excludeAttachmentTypes]
-            ]);
-
-            foreach ($attachments as $value) {
-                if (!empty($value['res_id'])) {
-                    $resId  = $value['res_id'];
-                    $collId = 'attachments_coll';
-                    $is_version = false;
-                } else {
-                    $resId  = $value['res_id_version'];
-                    $collId = 'attachments_version_coll';
-                    $is_version = true;
-                }
-                
-                $adrInfo       = \Convert\controllers\ConvertPdfController::getConvertedPdfById(['resId' => $resId, 'collId' => $collId, 'isVersion' => $is_version]);
-                $docserverInfo = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $adrInfo['docserver_id']]);
-                $filePath      = $docserverInfo['path_template'] . str_replace('#', '/', $adrInfo['path']) . $adrInfo['filename'];
-    
-                $encodedZipDocument = MaarchParapheurController::createZip(['filepath' => $filePath, 'filename' => $adrInfo['filename']]);
-    
-                if ($mainResource[0]['category_id'] != 'outgoing') {
-                    $attachmentsData = [[
-                        'encodedDocument' => $encodedMainZipFile,
-                        'subject'            => $mainResource[0]['subject'],
-                        'reference'          => $mainResource[0]['alt_identifier']
-                    ]];
-                } else {
-                    $attachmentsData = [];
-                }
-
-                $metadata = [];
-                if (!empty($priority['label'])) {
-                    $metadata[] = [_PRIORITY => $priority['label']];
-                }
-                if (!empty($senderPrimaryEntity['entity_label'])) {
-                    $metadata[] = [_INITIATOR_ENTITY => $senderPrimaryEntity['entity_label']];
-                }
-                $contact = trim($mainResource[0]['contact_firstname'] . ' ' . $mainResource[0]['contact_lastname'] . ' ' . $mainResource[0]['contact_society']);
-                if (!empty($contact)) {
-                    $metadata[] = [_RECIPIENTS => $contact];
-                }
-    
-                $bodyData = [
-                    'title'           => $value['title'],
-                    'reference'       => $value['identifier'],
-                    'mode'            => $aArgs['config']['data']['signature'],
-                    'encodedDocument' => $encodedZipDocument,
-                    'processingUser'  => $processingUser,
-                    'sender'          => trim($sender['firstname'] . ' ' .$sender['lastname']),
-                    'deadline'        => $processLimitDate,
-                    'attachments'     => $attachmentsData,
-                    'metadata'        => json_encode($metadata)
-                ];
-    
-                $response = \SrcCore\models\CurlModel::exec([
-                    'url'      => $aArgs['config']['data']['url'] . '/rest/documents',
-                    'user'     => $aArgs['config']['data']['userId'],
-                    'password' => $aArgs['config']['data']['password'],
-                    'method'   => 'POST',
-                    'bodyData' => $bodyData
-                ]);
-    
-                $attachmentToFreeze[$collId][$resId] = $response['id'];
-            }
-        } elseif ($aArgs['objectSent'] == 'mail') {
-            $metadata = [];
-            if (!empty($priority['label'])) {
-                $metadata[] = [_PRIORITY => $priority['label']];
-            }
-            if (!empty($senderPrimaryEntity['entity_label'])) {
-                $metadata[] = [_INITIATOR_ENTITY => $senderPrimaryEntity['entity_label']];
-            }
-            $contact = trim($mainResource[0]['contact_firstname'] . ' ' . $mainResource[0]['contact_lastname'] . ' ' . $mainResource[0]['contact_society']);
-            if (!empty($contact)) {
-                $metadata[] = [_RECIPIENTS => $contact];
-            }
-
-            $bodyData = [
-                'title'              => $mainResource[0]['subject'],
-                'reference'          => $mainResource[0]['alt_identifier'],
-                'mode'               => $aArgs['config']['data']['annotation'],
-                'encodedDocument'    => $encodedMainZipFile,
-                'processingUser'     => $processingUser,
-                'sender'             => trim($sender['firstname'] . ' ' .$sender['lastname']),
-                'deadline'           => $processLimitDate,
-                'metadata'           => json_encode($metadata)
-            ];
-
-            $response = \SrcCore\models\CurlModel::exec([
-                'url'      => $aArgs['config']['data']['url'] . '/rest/documents',
-                'user'     => $aArgs['config']['data']['userId'],
-                'password' => $aArgs['config']['data']['password'],
-                'method'   => 'POST',
-                'bodyData' => $bodyData
-            ]);
-
-            $attachmentToFreeze['letterbox_coll'][$aArgs['resIdMaster']] = $response['id'];
-        }
-
-        return $attachmentToFreeze;
-    }
-
-    public static function createZip($aArgs)
-    {
-        $zip = new ZipArchive();
-
-        $pathInfo    = pathinfo($aArgs['filepath'], PATHINFO_FILENAME);
-        $tmpPath     = \SrcCore\models\CoreConfigModel::getTmpPath();
-        $zipFilename = $tmpPath . $pathInfo."_".rand().".zip";
-
-        if ($zip->open($zipFilename, ZipArchive::CREATE) === true) {
-            $zip->addFile($aArgs['filepath'], $aArgs['filename']);
-
-            $zip->close();
-
-            $fileContent = file_get_contents($zipFilename);
-            $base64 =  base64_encode($fileContent);
-            return $base64;
-        } else {
-            echo 'Impossible de créer l\'archive;';
-        }
-    }
-
-    public static function getUserById($aArgs)
-    {
-        $response = \SrcCore\models\CurlModel::exec([
-            'url'      => $aArgs['config']['data']['url'] . '/rest/users/'.$aArgs['id'],
-            'user'     => $aArgs['config']['data']['userId'],
-            'password' => $aArgs['config']['data']['password'],
-            'method'   => 'GET'
-        ]);
-
-        return $response['user'];
-    }
 }
-- 
GitLab