From da8e0b860aa94aa5828e4b8e921b916649d6b2ee Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 29 Nov 2018 10:39:37 +0100
Subject: [PATCH] FEAT #8917 Send resource to external software

---
 apps/maarch_entreprise/actions/close_mail.php | 11 ++-
 apps/maarch_entreprise/actions/index_mlb.php  |  2 +-
 .../actions/validate_mail.php                 | 95 ++++++++++++++++++-
 .../class/class_contacts_v2_Abstract.php      |  2 +-
 apps/maarch_entreprise/xml/curlCall.xml       | 38 ++++++--
 modules/attachments/attachments_content.php   | 19 ++--
 src/core/models/CurlModel.php                 | 10 +-
 7 files changed, 153 insertions(+), 24 deletions(-)

diff --git a/apps/maarch_entreprise/actions/close_mail.php b/apps/maarch_entreprise/actions/close_mail.php
index 165cc689ae4..78144b0a636 100755
--- a/apps/maarch_entreprise/actions/close_mail.php
+++ b/apps/maarch_entreprise/actions/close_mail.php
@@ -138,10 +138,11 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
         if (\SrcCore\models\CurlModel::isEnabled(['curlCallId' => 'closeResource'])) {
             $bodyData = [];
             $config = \SrcCore\models\CurlModel::getConfigByCallId(['curlCallId' => 'closeResource']);
+            $configResource = \SrcCore\models\CurlModel::getConfigByCallId(['curlCallId' => 'sendResourceToExternalApplication']);
 
-            $resource = \Resource\models\ResModel::getOnView(['select' => ['external_id'], 'where' => ['res_id = ?'], 'data' => [$res_id]]);
+            $resource = \Resource\models\ResModel::getOnView(['select' => ['doc_' . $configResource['return']['value']], 'where' => ['res_id = ?'], 'data' => [$res_id]]);
 
-            if (!empty($resource[0]['external_id'])) {
+            if (!empty($resource[0]['doc_' . $configResource['return']['value']])) {
                 if (!empty($config['inObject'])) {
                     $multipleObject = true;
 
@@ -149,7 +150,9 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
                         $select = [];
                         $tmpBodyData = [];
                         foreach ($object['rawData'] as $value) {
-                            if ($value != 'note') {
+                            if ($value == $configResource['return']['value']) {
+                                $select[] = 'doc_' . $configResource['return']['value'];
+                            } elseif ($value != 'note') {
                                 $select[] = $value;
                             }
                         }
@@ -159,6 +162,8 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
                             foreach ($object['rawData'] as $key => $value) {
                                 if ($value == 'note') {
                                     $tmpBodyData[$key] = $formValues['note_content_to_users'];
+                                } elseif ($value == $configResource['return']['value']) {
+                                    $tmpBodyData[$key] = $document[0]['doc_' . $value];
                                 } else {
                                     $tmpBodyData[$key] = $document[0][$value];
                                 }
diff --git a/apps/maarch_entreprise/actions/index_mlb.php b/apps/maarch_entreprise/actions/index_mlb.php
index d936b95a98f..d913b8e6d9e 100755
--- a/apps/maarch_entreprise/actions/index_mlb.php
+++ b/apps/maarch_entreprise/actions/index_mlb.php
@@ -2150,7 +2150,7 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI
 
             $response = \SrcCore\models\CurlModel::exec(['curlCallId' => 'sendResourceToExternalApplication', 'bodyData' => $bodyData, 'multipleObject' => $multipleObject, 'noAuth' => true]);
 
-            \Resource\models\ResModel::update(['set' => ['external_id' => $response[$config['return']]], 'where' => ['res_id = ?'], 'data' => [$resId]]);
+            \Resource\models\ResModel::update(['set' => [$config['return']['value'] => $response[$config['return']['key']]], 'where' => ['res_id = ?'], 'data' => [$resId]]);
         }
     }
 
diff --git a/apps/maarch_entreprise/actions/validate_mail.php b/apps/maarch_entreprise/actions/validate_mail.php
index 569d16592ae..3708c536659 100755
--- a/apps/maarch_entreprise/actions/validate_mail.php
+++ b/apps/maarch_entreprise/actions/validate_mail.php
@@ -2072,7 +2072,100 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
         require 'modules/attachments/remove_letterbox.php';
     }
 
-    //$_SESSION['indexing'] = array();
+    if ($cat_id != 'outgoing' && $cat_id != 'attachment') {
+        if (\SrcCore\models\CurlModel::isEnabled(['curlCallId' => 'sendResourceToExternalApplication'])) {
+            $bodyData = [];
+            $config = \SrcCore\models\CurlModel::getConfigByCallId(['curlCallId' => 'sendResourceToExternalApplication']);
+
+            $columnsInContact = ['external_contact_id'];
+            $resource = \Resource\models\ResModel::getById(['select' => [$config['return']['value'], 'docserver_id', 'path', 'filename'], 'resId' => $res_id]);
+
+            if (empty($resource[$config['return']['value']])) {
+                if (!empty($config['inObject'])) {
+                    $multipleObject = true;
+
+                    foreach ($config['objects'] as $object) {
+                        $select = [];
+                        $tmpBodyData = [];
+                        $getContact = false;
+                        foreach ($object['rawData'] as $value) {
+                            if (in_array($value, $columnsInContact)) {
+                                $getContact = true;
+                            } else {
+                                $select[] = $value;
+                            }
+                        }
+
+                        $select[] = 'address_id';
+                        $document = \Resource\models\ResModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$res_id]]);
+                        if (!empty($document[0])) {
+                            if ($getContact && !empty($document[0]['address_id'])) {
+                                $contact = \Contact\models\ContactModel::getOnView(['select' => $columnsInContact, 'where' => ['ca_id = ?'], 'data' => [$document[0]['address_id']]]);
+                            }
+                            foreach ($object['rawData'] as $key => $value) {
+                                if (in_array($value, $columnsInContact)) {
+                                    $tmpBodyData[$key] = '';
+                                    if (!empty($contact[0][$value])) {
+                                        $tmpBodyData[$key] = $contact[0][$value];
+                                    }
+                                } else {
+                                    $tmpBodyData[$key] = $document[0][$value];
+                                }
+                            }
+                        }
+
+                        if (!empty($object['data'])) {
+                            $tmpBodyData = array_merge($tmpBodyData, $object['data']);
+                        }
+
+                        $bodyData[$object['name']] = $tmpBodyData;
+                    }
+
+                    if (!empty($config['file'])) {
+                        $docserver = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $resource['docserver_id'], 'select' => ['path_template']]);
+                        $bodyData[$config['file']] = \SrcCore\models\CurlModel::makeCurlFile(['path' => $docserver['path_template'] . str_replace('#', '/', $resource['path']) . $resource['filename']]);
+                    }
+                } else {
+                    $multipleObject = false;
+                    $getContact = false;
+
+                    $select = [];
+                    foreach ($config['rawData'] as $value) {
+                        if (in_array($value, $columnsInContact)) {
+                            $getContact = true;
+                        } else {
+                            $select[] = $value;
+                        }
+                    }
+
+                    $select[] = 'address_id';
+                    $document = \Resource\models\ResModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$res_id]]);
+                    if (!empty($document[0])) {
+                        if ($getContact) {
+                            $contact = \Contact\models\ContactModel::getOnView(['select' => $columnsInContact, 'where' => ['ca_id = ?'], 'data' => [$document[0]['address_id']]]);
+                        }
+                        foreach ($config['rawData'] as $key => $value) {
+                            if (in_array($value, $columnsInContact)) {
+                                $bodyData[$key] = $contact[0][$value];
+                            } else {
+                                $bodyData[$key] = $document[0][$value];
+                            }
+                        }
+
+                    }
+
+                    if (!empty($config['data'])) {
+                        $bodyData = array_merge($bodyData, $config['data']);
+                    }
+                }
+
+                $response = \SrcCore\models\CurlModel::exec(['curlCallId' => 'sendResourceToExternalApplication', 'bodyData' => $bodyData, 'multipleObject' => $multipleObject, 'noAuth' => true]);
+
+                \Resource\models\ResModel::update(['set' => [$config['return']['value'] => $response[$config['return']['key']]], 'where' => ['res_id = ?'], 'data' => [$res_id]]);
+            }
+        }
+    }
+
     unset($_SESSION['upfile']);
 
     //$_SESSION['indexation'] = true;
diff --git a/apps/maarch_entreprise/class/class_contacts_v2_Abstract.php b/apps/maarch_entreprise/class/class_contacts_v2_Abstract.php
index 68592fd751a..28b90626d4a 100755
--- a/apps/maarch_entreprise/class/class_contacts_v2_Abstract.php
+++ b/apps/maarch_entreprise/class/class_contacts_v2_Abstract.php
@@ -2308,7 +2308,7 @@ abstract class contacts_v2_Abstract extends Database
 
                     $response = \SrcCore\models\CurlModel::exec(['curlCallId' => 'sendContactToExternalApplication', 'bodyData' => $bodyData, 'multipleObject' => $multipleObject, 'noAuth' => true]);
 
-                    \Contact\models\ContactModel::updateAddress(['set' => ['external_contact_id' => $response[$config['return']]], 'where' => ['id = ?'], 'data' => [$document[0]['ca_id']]]);
+                    \Contact\models\ContactModel::updateAddress(['set' => [$config['return']['value'] => $response[$config['return']['key']]], 'where' => ['id = ?'], 'data' => [$document[0]['ca_id']]]);
                 }
 
                 if ($iframe) {
diff --git a/apps/maarch_entreprise/xml/curlCall.xml b/apps/maarch_entreprise/xml/curlCall.xml
index db79d4f55ba..8569defe456 100644
--- a/apps/maarch_entreprise/xml/curlCall.xml
+++ b/apps/maarch_entreprise/xml/curlCall.xml
@@ -66,7 +66,10 @@
                 <value>address_id</value>
             </rawData>
         </sendInObject>
-        <return>id</return>
+        <return>
+            <key>id</key>
+            <value>custom_t1</value>
+        </return>
     </call>
     <call>
         <id>sendAttachmentToExternalApplication</id>
@@ -113,6 +116,10 @@
                 <key>city</key>
                 <value>address_town</value>
             </rawData>
+            <rawData>
+                <key>email</key>
+                <value>email</value>
+            </rawData>
             <rawData>
                 <key>usualName</key>
                 <value>contact_lastname</value>
@@ -126,20 +133,31 @@
                 <value>73</value>
             </data>
         </sendInObject>
-        <return>id</return>
+        <return>
+            <key>id</key>
+            <value>external_contact_id</value>
+        </return>
     </call>
     <call>
         <id>closeResource</id>
         <enabled>false</enabled>
         <url></url>
         <method>POST</method>
-        <rawData>
-            <key>status</key>
-            <value>status</value>
-        </rawData>
-        <data>
-            <key>message</key>
-            <value>Close</value>
-        </data>
+        <header></header>
+        <sendInObject>
+            <objectName>requete</objectName>
+            <rawData>
+                <key>id</key>
+                <value>custom_t1</value>
+            </rawData>
+            <rawData>
+                <key>note</key>
+                <value>note</value>
+            </rawData>
+            <data>
+                <key>statusId</key>
+                <value>7</value>
+            </data>
+        </sendInObject>
     </call>
 </ROOT>
diff --git a/modules/attachments/attachments_content.php b/modules/attachments/attachments_content.php
index ccb153e2836..95fa9e2d1c2 100755
--- a/modules/attachments/attachments_content.php
+++ b/modules/attachments/attachments_content.php
@@ -378,11 +378,12 @@ if (isset($_POST['add']) && $_POST['add']) {
                                     if (\SrcCore\models\CurlModel::isEnabled(['curlCallId' => 'sendAttachmentToExternalApplication'])) {
                                         $bodyData = [];
                                         $config = \SrcCore\models\CurlModel::getConfigByCallId(['curlCallId' => 'sendAttachmentToExternalApplication']);
+                                        $configResource = \SrcCore\models\CurlModel::getConfigByCallId(['curlCallId' => 'sendResourceToExternalApplication']);
 
                                         $columnsInContact = ['external_contact_id'];
-                                        $resource = \Resource\models\ResModel::getOnView(['select' => ['external_id', 'address_id'], 'where' => ['res_id = ?'], 'data' => [$_SESSION['doc_id']]]);
+                                        $resource = \Resource\models\ResModel::getOnView(['select' => ['doc_' . $configResource['return']['value'], 'address_id'], 'where' => ['res_id = ?'], 'data' => [$_SESSION['doc_id']]]);
 
-                                        if (!empty($resource[0]['external_id']) && !empty($resource[0]['address_id'])) {
+                                        if (!empty($resource[0]['doc_' . $configResource['return']['value']]) && !empty($resource[0]['address_id'])) {
                                             if (!empty($config['inObject'])) {
                                                 $multipleObject = true;
 
@@ -393,7 +394,7 @@ if (isset($_POST['add']) && $_POST['add']) {
                                                     foreach ($object['rawData'] as $value) {
                                                         if (in_array($value, $columnsInContact)) {
                                                             $getContact = true;
-                                                        } elseif (!in_array($value, ['external_id', 'address_id'])) {
+                                                        } elseif (!in_array($value, [$configResource['return']['value'], 'address_id'])) {
                                                             $select[] = $value;
                                                         }
                                                     }
@@ -407,8 +408,12 @@ if (isset($_POST['add']) && $_POST['add']) {
                                                     foreach ($object['rawData'] as $key => $value) {
                                                         if (in_array($value, $columnsInContact)) {
                                                             $tmpBodyData[$key] = $contact[0][$value];
-                                                        } elseif (in_array($value, ['external_id', 'address_id'])) {
-                                                            $tmpBodyData[$key] = $resource[0][$value];
+                                                        } elseif (in_array($value, [$configResource['return']['value'], 'address_id'])) {
+                                                            if ($value == $configResource['return']['value']) {
+                                                                $tmpBodyData[$key] = $resource[0]['doc_' . $value];
+                                                            } else {
+                                                                $tmpBodyData[$key] = $resource[0][$value];
+                                                            }
                                                         } else {
                                                             $tmpBodyData[$key] = $document[0][$value];
                                                         }
@@ -433,7 +438,7 @@ if (isset($_POST['add']) && $_POST['add']) {
                                                 foreach ($config['rawData'] as $value) {
                                                     if (in_array($value, $columnsInContact)) {
                                                         $getContact = true;
-                                                    } elseif (!in_array($value, ['external_id', 'address_id'])) {
+                                                    } elseif (!in_array($value, [$configResource['return']['value'], 'address_id'])) {
                                                         $select[] = $value;
                                                     }
                                                 }
@@ -446,7 +451,7 @@ if (isset($_POST['add']) && $_POST['add']) {
                                                     foreach ($config['rawData'] as $key => $value) {
                                                         if (in_array($value, $columnsInContact)) {
                                                             $bodyData[$key] = $contact[0][$value];
-                                                        } elseif (in_array($value, ['external_id', 'address_id'])) {
+                                                        } elseif (in_array($value, [$configResource['return']['value'], 'address_id'])) {
                                                             $bodyData[$key] = $resource[0][$value];
                                                         } else {
                                                             $bodyData[$key] = $document[0][$value];
diff --git a/src/core/models/CurlModel.php b/src/core/models/CurlModel.php
index 04a9cabfaa5..9a98d76ee2e 100644
--- a/src/core/models/CurlModel.php
+++ b/src/core/models/CurlModel.php
@@ -124,6 +124,13 @@ class CurlModel
                 $cookies = array_merge($cookies, [$cookie[0] => $cookie[1]]);
             }
             $rawResponse = substr($rawResponse, $infos['header_size']);
+        } elseif (!empty($aArgs['delete_header'])) { // Delete header for iparapheur
+            $body = explode(PHP_EOL . PHP_EOL, $rawResponse)[1]; // put the header ahead
+            if (empty($body)) {
+                $body = explode(PHP_EOL, $rawResponse)[5];
+            }
+            $pattern = '/--uuid:[0-9a-f-]+--/';                  // And also the footer
+            $rawResponse = preg_replace($pattern, '', $body);
         }
 
         return ['response' => simplexml_load_string($rawResponse), 'infos' => $infos, 'cookies' => $cookies, 'raw' => $rawResponse, 'error' => $error];
@@ -190,7 +197,8 @@ class CurlModel
                         }
                     }
                     if (!empty($call->return)) {
-                        $curlConfig['return'] = (string)$call->return;
+                        $curlConfig['return']['key'] = (string)$call->return->key;
+                        $curlConfig['return']['value'] = (string)$call->return->value;
                     }
                 }
             }
-- 
GitLab