From e3f517f2eeec4a2521f52a4cf7c6e90883d282be Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 26 Oct 2018 10:53:54 +0200
Subject: [PATCH] FEAT #8156 Syncho adjustments

---
 apps/maarch_entreprise/actions/close_mail.php |  71 +++++++++---
 apps/maarch_entreprise/actions/index_mlb.php  |  21 ++--
 modules/attachments/attachments_content.php   | 103 ++++++++++++++----
 src/core/models/CurlModel.php                 |   2 +-
 4 files changed, 151 insertions(+), 46 deletions(-)

diff --git a/apps/maarch_entreprise/actions/close_mail.php b/apps/maarch_entreprise/actions/close_mail.php
index 827fbc54ff5..165cc689ae4 100755
--- a/apps/maarch_entreprise/actions/close_mail.php
+++ b/apps/maarch_entreprise/actions/close_mail.php
@@ -134,27 +134,72 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
         if ($formValues['note_content_to_users'] != '') {
             \Note\models\NoteModel::create(['identifier' => $res_id, 'tablename' => 'res_letterbox', 'user_id' => $_SESSION['user']['UserId'], 'coll_id' => 'letterbox_coll', 'note_text' => $formValues['note_content_to_users']]);
         }
+
         if (\SrcCore\models\CurlModel::isEnabled(['curlCallId' => 'closeResource'])) {
             $bodyData = [];
             $config = \SrcCore\models\CurlModel::getConfigByCallId(['curlCallId' => 'closeResource']);
 
-            if (!empty($config['rawData'])) {
-                $select = [];
-                foreach ($config['rawData'] as $value) {
-                    $select[] = $value;
-                }
+            $resource = \Resource\models\ResModel::getOnView(['select' => ['external_id'], 'where' => ['res_id = ?'], 'data' => [$res_id]]);
+
+            if (!empty($resource[0]['external_id'])) {
+                if (!empty($config['inObject'])) {
+                    $multipleObject = true;
+
+                    foreach ($config['objects'] as $object) {
+                        $select = [];
+                        $tmpBodyData = [];
+                        foreach ($object['rawData'] as $value) {
+                            if ($value != 'note') {
+                                $select[] = $value;
+                            }
+                        }
+
+                        $document = \Resource\models\ResModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$res_id]]);
+                        if (!empty($document[0])) {
+                            foreach ($object['rawData'] as $key => $value) {
+                                if ($value == 'note') {
+                                    $tmpBodyData[$key] = $formValues['note_content_to_users'];
+                                } else {
+                                    $tmpBodyData[$key] = $document[0][$value];
+                                }
+                            }
+                        }
+
+                        if (!empty($object['data'])) {
+                            $tmpBodyData = array_merge($tmpBodyData, $object['data']);
+                        }
 
-                $document = \Resource\models\ResModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$res_id]]);
-                if (!empty($document[0])) {
-                    $bodyData = $document[0];
+                        $bodyData[$object['name']] = $tmpBodyData;
+                    }
+                } else {
+                    $multipleObject = false;
+
+                    $select = [];
+                    foreach ($config['rawData'] as $value) {
+                        if ($value != 'note') {
+                            $select[] = $value;
+                        }
+                    }
+
+                    $document = \Resource\models\ResModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$res_id]]);
+                    if (!empty($document[0])) {
+                        foreach ($config['rawData'] as $key => $value) {
+                            if ($value == 'note') {
+                                $bodyData[$key] = $formValues['note_content_to_users'];
+                            } else {
+                                $bodyData[$key] = $document[0][$value];
+                            }
+                        }
+
+                    }
+
+                    if (!empty($config['data'])) {
+                        $bodyData = array_merge($bodyData, $config['data']);
+                    }
                 }
-            }
 
-            if (!empty($config['data'])) {
-                $bodyData = array_merge($bodyData, $config['data']);
+                \SrcCore\models\CurlModel::exec(['curlCallId' => 'closeResource', 'bodyData' => $bodyData, 'multipleObject' => $multipleObject, 'noAuth' => true]);
             }
-
-            \SrcCore\models\CurlModel::exec(['curlCallId' => 'closeResource', 'bodyData' => $bodyData]);
         }
     }
 
diff --git a/apps/maarch_entreprise/actions/index_mlb.php b/apps/maarch_entreprise/actions/index_mlb.php
index 78d4b23f2d0..3e369997ef2 100755
--- a/apps/maarch_entreprise/actions/index_mlb.php
+++ b/apps/maarch_entreprise/actions/index_mlb.php
@@ -2088,12 +2088,15 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI
                     $select[] = 'address_id';
                     $document = \Resource\models\ResModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$resId]]);
                     if (!empty($document[0])) {
-                        if ($getContact) {
+                        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] = $contact[0][$value];
+                                $tmpBodyData[$key] = '';
+                                if (!empty($contact[0][$value])) {
+                                    $tmpBodyData[$key] = $contact[0][$value];
+                                }
                             } else {
                                 $tmpBodyData[$key] = $document[0][$value];
                             }
@@ -2107,9 +2110,9 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI
                     $bodyData[$object['name']] = $tmpBodyData;
                 }
 
-                if (!empty($config['file']) && $config['file'] == 'true') {
+                if (!empty($config['file'])) {
                     $docserver = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $_SESSION['indexing']['docserver_id'], 'select' => ['path_template']]);
-                    $bodyData['file'] = \SrcCore\models\CurlModel::makeCurlFile(['path' => $docserver['path_template'] . str_replace('#', '/', $_SESSION['indexing']['destination_dir']) . $_SESSION['indexing']['file_destination_name']]);
+                    $bodyData[$config['file']] = \SrcCore\models\CurlModel::makeCurlFile(['path' => $docserver['path_template'] . str_replace('#', '/', $_SESSION['indexing']['destination_dir']) . $_SESSION['indexing']['file_destination_name']]);
                 }
             } else {
                 $multipleObject = false;
@@ -2132,9 +2135,9 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI
                     }
                     foreach ($config['rawData'] as $key => $value) {
                         if (in_array($value, $columnsInContact)) {
-                            $tmpBodyData[$key] = $contact[0][$value];
+                            $bodyData[$key] = $contact[0][$value];
                         } else {
-                            $tmpBodyData[$key] = $document[0][$value];
+                            $bodyData[$key] = $document[0][$value];
                         }
                     }
 
@@ -2143,12 +2146,6 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI
                 if (!empty($config['data'])) {
                     $bodyData = array_merge($bodyData, $config['data']);
                 }
-
-//                if (!empty($config['file'])) {
-//                    $docserver = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $_SESSION['indexing']['docserver_id'], 'select' => ['path_template']]);
-//                    $file = file_get_contents($docserver['path_template'] . str_replace('#', '/', $_SESSION['indexing']['destination_dir']) . $_SESSION['indexing']['file_destination_name']);
-//                    $bodyData[$config['file']] = base64_encode($file);
-//                }
             }
 
             $response = \SrcCore\models\CurlModel::exec(['curlCallId' => 'sendResourceToExternalApplication', 'bodyData' => $bodyData, 'multipleObject' => $multipleObject, 'noAuth' => true]);
diff --git a/modules/attachments/attachments_content.php b/modules/attachments/attachments_content.php
index 70df11c6c05..cbe7268798b 100755
--- a/modules/attachments/attachments_content.php
+++ b/modules/attachments/attachments_content.php
@@ -374,31 +374,94 @@ if (isset($_POST['add']) && $_POST['add']) {
                                         $_SESSION['data'],
                                         $_SESSION['config']['databasetype']
                                     );
+
                                     if (\SrcCore\models\CurlModel::isEnabled(['curlCallId' => 'sendAttachmentToExternalApplication'])) {
                                         $bodyData = [];
                                         $config = \SrcCore\models\CurlModel::getConfigByCallId(['curlCallId' => 'sendAttachmentToExternalApplication']);
 
-                                        $select = [];
-                                        foreach ($config['rawData'] as $value) {
-                                            $select[] = $value;
-                                        }
-
-                                        $attachment = \Attachment\models\AttachmentModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$id]]);
-                                        if (!empty($attachment[0])) {
-                                            $bodyData = $attachment[0];
-                                        }
-
-                                        if (!empty($config['data'])) {
-                                            $bodyData = array_merge($bodyData, $config['data']);
-                                        }
-
-                                        if (!empty($config['file'])) {
-                                            $docserver = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $storeResult['docserver_id'], 'select' => ['path_template']]);
-                                            $file = file_get_contents($docserver['path_template'] . str_replace('#', '/', $storeResult['destination_dir']) . $storeResult['file_destination_name']);
-                                            $bodyData[$config['file']] = base64_encode($file);
+                                        $columnsInContact = ['external_contact_id'];
+                                        $resource = \Resource\models\ResModel::getOnView(['select' => ['external_id', 'address_id'], 'where' => ['res_id = ?'], 'data' => [$_SESSION['doc_id']]]);
+
+                                        if (!empty($resource[0]['external_id']) && !empty($resource[0]['address_id'])) {
+                                            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;
+                                                        } elseif (!in_array($value, ['external_id', 'address_id'])) {
+                                                            $select[] = $value;
+                                                        }
+                                                    }
+
+                                                    if (!empty($select)) {
+                                                        $document = \Attachment\models\AttachmentModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$id]]);
+                                                    }
+                                                    if ($getContact) {
+                                                        $contact = \Contact\models\ContactModel::getOnView(['select' => $columnsInContact, 'where' => ['ca_id = ?'], 'data' => [$resource[0]['address_id']]]);
+                                                    }
+                                                    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];
+                                                        } 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' => $storeResult['docserver_id'], 'select' => ['path_template']]);
+                                                    $bodyData[$config['file']] = \SrcCore\models\CurlModel::makeCurlFile(['path' => $docserver['path_template'] . str_replace('#', '/', $storeResult['destination_dir']) . $storeResult['file_destination_name']]);
+                                                }
+                                            } else {
+                                                $multipleObject = false;
+                                                $getContact = false;
+
+                                                $select = [];
+                                                foreach ($config['rawData'] as $value) {
+                                                    if (in_array($value, $columnsInContact)) {
+                                                        $getContact = true;
+                                                    } elseif (!in_array($value, ['external_id', 'address_id'])) {
+                                                        $select[] = $value;
+                                                    }
+                                                }
+
+                                                $document = \Attachment\models\AttachmentModel::getOnView(['select' => $select, 'where' => ['res_id = ?'], 'data' => [$id]]);
+                                                if (!empty($document[0])) {
+                                                    if ($getContact) {
+                                                        $contact = \Contact\models\ContactModel::getOnView(['select' => $columnsInContact, 'where' => ['ca_id = ?'], 'data' => [$resource[0]['address_id']]]);
+                                                    }
+                                                    foreach ($config['rawData'] as $key => $value) {
+                                                        if (in_array($value, $columnsInContact)) {
+                                                            $bodyData[$key] = $contact[0][$value];
+                                                        } elseif (in_array($value, ['external_id', 'address_id'])) {
+                                                            $bodyData[$key] = $resource[0][$value];
+                                                        } else {
+                                                            $bodyData[$key] = $document[0][$value];
+                                                        }
+                                                    }
+
+                                                }
+
+                                                if (!empty($config['data'])) {
+                                                    $bodyData = array_merge($bodyData, $config['data']);
+                                                }
+                                            }
+
+                                            $response = \SrcCore\models\CurlModel::exec(['curlCallId' => 'sendAttachmentToExternalApplication', 'bodyData' => $bodyData, 'multipleObject' => $multipleObject, 'noAuth' => true]);
                                         }
-
-                                        $response = \SrcCore\models\CurlModel::exec(['curlCallId' => 'sendAttachmentToExternalApplication', 'bodyData' => $bodyData]);
                                     }
                                 }
 
diff --git a/src/core/models/CurlModel.php b/src/core/models/CurlModel.php
index 72bdcac852b..42fc4802761 100644
--- a/src/core/models/CurlModel.php
+++ b/src/core/models/CurlModel.php
@@ -53,7 +53,7 @@ class CurlModel
             if (is_array($aArgs['bodyData']) && !empty($aArgs['bodyData']) && $aArgs['multipleObject']) {
                 $bodyData = [];
                 foreach ($aArgs['bodyData'] as $key => $value) {
-                    if ($key == 'file') {
+                    if (is_object($value)) {
                         $bodyData[$key] = $value;
                     } else {
                         $bodyData[$key] = json_encode($value);
-- 
GitLab