From c0e87bc707fe9f04a4f95bf0883e34068a016ead Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Tue, 17 Dec 2019 17:59:16 +0100
Subject: [PATCH] FIX #12510 TIME 5:00 migrate notes merge field

---
 migration/19.12/migrateTemplates.php          | 301 +++++++++---------
 .../datasources/mlb_notes_content.php         |  52 ++-
 2 files changed, 204 insertions(+), 149 deletions(-)

diff --git a/migration/19.12/migrateTemplates.php b/migration/19.12/migrateTemplates.php
index e6dfbdc5502..ea51ce865fd 100644
--- a/migration/19.12/migrateTemplates.php
+++ b/migration/19.12/migrateTemplates.php
@@ -11,177 +11,192 @@ include_once('../../vendor/tinybutstrong/opentbs/tbs_plugin_opentbs.php');
 const OFFICE_EXTENSIONS = ['odt', 'ods', 'odp', 'xlsx', 'pptx', 'docx', 'odf'];
 
 $DATA_TO_REPLACE = [
-    'res_letterbox.destination' => '[destination.entity_id]',
-    'res_letterbox.entity_label' => '[destination.entity_label]',
+    'res_letterbox.destination'   => '[destination.entity_id]',
+    'res_letterbox.entity_label'  => '[destination.entity_label]',
     'res_letterbox.process_notes' => '[notes]',
 
-    'res_letterbox.nature_id' => '[res_letterbox.custom_1]',
+    'res_letterbox.nature_id'                     => '[res_letterbox.custom_1]',
 
     // Initiator
-    'res_letterbox.initiator_entity_id' => '[initiator.entity_id]',
-    'res_letterbox.initiator_entity_label' => '[initiator.entity_label]',
-    'res_letterbox.initiator_short_label' => '[initiator.short_label]',
-    'res_letterbox.initiator_email' => '[initiator.email]',
-    'res_letterbox.initiator_parent_entity_id' => '[initiator.parent_entity_id]',
+    'res_letterbox.initiator_entity_id'           => '[initiator.entity_id]',
+    'res_letterbox.initiator_entity_label'        => '[initiator.entity_label]',
+    'res_letterbox.initiator_short_label'         => '[initiator.short_label]',
+    'res_letterbox.initiator_email'               => '[initiator.email]',
+    'res_letterbox.initiator_parent_entity_id'    => '[initiator.parent_entity_id]',
     'res_letterbox.initiator_parent_entity_label' => '[initiator.parent_entity_label]',
-    'res_letterbox.initiator_entity_type' => '[initiator.entity_type]',
-    'res_letterbox.initiator_entity_path' => '[initiator.entity_path]',
-    'res_letterbox.initiator_entity_fullname' => '[initiator.entity_fullname]',
-    'res_letterbox.initiator_zipcode' => '[initiator.zipcode]',
-    'res_letterbox.initiator_city' => '[initiator.city]',
-    'res_letterbox.initiator_country' => '[initiator.country]',
-    'res_letterbox.initiator_ldap_id' => '[initiator.ldap_id]',
-    'res_letterbox.initiator_archival_agence' => '[initiator.archival_agence]',
-    'res_letterbox.initiator_archival_agreement' => '[initiator.archival_agreement]',
-    'res_letterbox.initiator_business_id' => '[initiator.business_id]',
+    'res_letterbox.initiator_entity_type'         => '[initiator.entity_type]',
+    'res_letterbox.initiator_entity_path'         => '[initiator.entity_path]',
+    'res_letterbox.initiator_entity_fullname'     => '[initiator.entity_fullname]',
+    'res_letterbox.initiator_zipcode'             => '[initiator.zipcode]',
+    'res_letterbox.initiator_city'                => '[initiator.city]',
+    'res_letterbox.initiator_country'             => '[initiator.country]',
+    'res_letterbox.initiator_ldap_id'             => '[initiator.ldap_id]',
+    'res_letterbox.initiator_archival_agence'     => '[initiator.archival_agence]',
+    'res_letterbox.initiator_archival_agreement'  => '[initiator.archival_agreement]',
+    'res_letterbox.initiator_business_id'         => '[initiator.business_id]',
 
     'attachments.chrono' => '[attachment.chrono]',
 
     'visa.firstnameSign' => '',
-    'visa.lastnameSign' => '[visas]',
-    'visa.entitySign' => '',
-    'visa.firstname1' => '',
-    'visa.lastname1' => '[visas]',
-    'visa.firstname2' => '',
-    'visa.lastname2' => '',
-    'visa.firstname3' => '',
-    'visa.lastname3' => '',
-    'visa.firstname4' => '',
-    'visa.lastname4' => '',
-    'visa.firstname5' => '',
-    'visa.lastname5' => '',
-    'visa.firstname6' => '',
-    'visa.lastname6' => '',
-    'visa.firstname7' => '',
-    'visa.lastname7' => '',
-    'visa.firstname8' => '',
-    'visa.lastname8' => '',
-    'visa.firstname9' => '',
-    'visa.lastname9' => '',
-    'visa.entity1' => '',
-    'visa.entity2' => '',
-    'visa.entity3' => '',
-    'visa.entity4' => '',
-    'visa.entity5' => '',
-    'visa.entity6' => '',
-    'visa.entity7' => '',
-    'visa.entity8' => '',
-    'visa.entity9' => '',
+    'visa.lastnameSign'  => '[visas]',
+    'visa.entitySign'    => '',
+    'visa.firstname1'    => '',
+    'visa.lastname1'     => '[visas]',
+    'visa.firstname2'    => '',
+    'visa.lastname2'     => '',
+    'visa.firstname3'    => '',
+    'visa.lastname3'     => '',
+    'visa.firstname4'    => '',
+    'visa.lastname4'     => '',
+    'visa.firstname5'    => '',
+    'visa.lastname5'     => '',
+    'visa.firstname6'    => '',
+    'visa.lastname6'     => '',
+    'visa.firstname7'    => '',
+    'visa.lastname7'     => '',
+    'visa.firstname8'    => '',
+    'visa.lastname8'     => '',
+    'visa.firstname9'    => '',
+    'visa.lastname9'     => '',
+    'visa.entity1'       => '',
+    'visa.entity2'       => '',
+    'visa.entity3'       => '',
+    'visa.entity4'       => '',
+    'visa.entity5'       => '',
+    'visa.entity6'       => '',
+    'visa.entity7'       => '',
+    'visa.entity8'       => '',
+    'visa.entity9'       => '',
 
     'avis.firstname1' => '',
-    'avis.lastname1' => '[opinions]',
+    'avis.lastname1'  => '[opinions]',
     'avis.firstname2' => '',
-    'avis.lastname2' => '',
+    'avis.lastname2'  => '',
     'avis.firstname3' => '',
-    'avis.lastname3' => '',
+    'avis.lastname3'  => '',
     'avis.firstname4' => '',
-    'avis.lastname4' => '',
+    'avis.lastname4'  => '',
     'avis.firstname5' => '',
-    'avis.lastname5' => '',
+    'avis.lastname5'  => '',
     'avis.firstname6' => '',
-    'avis.lastname6' => '',
+    'avis.lastname6'  => '',
     'avis.firstname7' => '',
-    'avis.lastname7' => '',
+    'avis.lastname7'  => '',
     'avis.firstname8' => '',
-    'avis.lastname8' => '',
+    'avis.lastname8'  => '',
     'avis.firstname9' => '',
-    'avis.lastname9' => '',
-    'avis.role1' => '',
-    'avis.entity1' => '',
-    'avis.note1' => '',
-    'avis.role2' => '',
-    'avis.entity2' => '',
-    'avis.note2' => '',
-    'avis.role3' => '',
-    'avis.entity3' => '',
-    'avis.note3' => '',
-    'avis.role4' => '',
-    'avis.entity4' => '',
-    'avis.note4' => '',
-    'avis.role5' => '',
-    'avis.entity5' => '',
-    'avis.note5' => '',
-    'avis.role6' => '',
-    'avis.entity6' => '',
-    'avis.note6' => '',
-    'avis.role7' => '',
-    'avis.entity7' => '',
-    'avis.note7' => '',
-    'avis.role8' => '',
-    'avis.entity8' => '',
-    'avis.note8' => '',
-    'avis.role9' => '',
-    'avis.entity9' => '',
-    'avis.note9' => '',
+    'avis.lastname9'  => '',
+    'avis.role1'      => '',
+    'avis.entity1'    => '',
+    'avis.note1'      => '',
+    'avis.role2'      => '',
+    'avis.entity2'    => '',
+    'avis.note2'      => '',
+    'avis.role3'      => '',
+    'avis.entity3'    => '',
+    'avis.note3'      => '',
+    'avis.role4'      => '',
+    'avis.entity4'    => '',
+    'avis.note4'      => '',
+    'avis.role5'      => '',
+    'avis.entity5'    => '',
+    'avis.note5'      => '',
+    'avis.role6'      => '',
+    'avis.entity6'    => '',
+    'avis.note6'      => '',
+    'avis.role7'      => '',
+    'avis.entity7'    => '',
+    'avis.note7'      => '',
+    'avis.role8'      => '',
+    'avis.entity8'    => '',
+    'avis.note8'      => '',
+    'avis.role9'      => '',
+    'avis.entity9'    => '',
+    'avis.note9'      => '',
 
     'copies.firstname1' => '',
-    'copies.lastname1' => '[copies]',
+    'copies.lastname1'  => '[copies]',
     'copies.firstname2' => '',
-    'copies.lastname2' => '',
+    'copies.lastname2'  => '',
     'copies.firstname3' => '',
-    'copies.lastname3' => '',
+    'copies.lastname3'  => '',
     'copies.firstname4' => '',
-    'copies.lastname4' => '',
+    'copies.lastname4'  => '',
     'copies.firstname5' => '',
-    'copies.lastname5' => '',
+    'copies.lastname5'  => '',
     'copies.firstname6' => '',
-    'copies.lastname6' => '',
+    'copies.lastname6'  => '',
     'copies.firstname7' => '',
-    'copies.lastname7' => '',
+    'copies.lastname7'  => '',
     'copies.firstname8' => '',
-    'copies.lastname8' => '',
+    'copies.lastname8'  => '',
     'copies.firstname9' => '',
-    'copies.lastname9' => '',
-    'copies.entity1' => '',
-    'copies.entity2' => '',
-    'copies.entity3' => '',
-    'copies.entity4' => '',
-    'copies.entity5' => '',
-    'copies.entity6' => '',
-    'copies.entity7' => '',
-    'copies.entity8' => '',
-    'copies.entity9' => '',
-
-    'user.role' => '[userPrimaryEntity.role]',
-    'user.entity_id' => '[userPrimaryEntity.entity_id]',
-    'user.entity_label' => '[userPrimaryEntity.entity_label]',
-    'user.short_label' => '[userPrimaryEntity.short_label]',
-    'user.adrs_1' => '[userPrimaryEntity.adrs_1]',
-    'user.adrs_2' => '[userPrimaryEntity.adrs_2]',
-    'user.adrs_3' => '[userPrimaryEntity.adrs_3]',
-    'user.zipcode' => '[userPrimaryEntity.zipcode]',
-    'user.city' => '[userPrimaryEntity.city]',
-    'user.email' => '[userPrimaryEntity.email]',
+    'copies.lastname9'  => '',
+    'copies.entity1'    => '',
+    'copies.entity2'    => '',
+    'copies.entity3'    => '',
+    'copies.entity4'    => '',
+    'copies.entity5'    => '',
+    'copies.entity6'    => '',
+    'copies.entity7'    => '',
+    'copies.entity8'    => '',
+    'copies.entity9'    => '',
+
+    'user.role'             => '[userPrimaryEntity.role]',
+    'user.entity_id'        => '[userPrimaryEntity.entity_id]',
+    'user.entity_label'     => '[userPrimaryEntity.entity_label]',
+    'user.short_label'      => '[userPrimaryEntity.short_label]',
+    'user.adrs_1'           => '[userPrimaryEntity.adrs_1]',
+    'user.adrs_2'           => '[userPrimaryEntity.adrs_2]',
+    'user.adrs_3'           => '[userPrimaryEntity.adrs_3]',
+    'user.zipcode'          => '[userPrimaryEntity.zipcode]',
+    'user.city'             => '[userPrimaryEntity.city]',
+    'user.email'            => '[userPrimaryEntity.email]',
     'user.parent_entity_id' => '[userPrimaryEntity.parent_entity_id]',
-    'user.entity_type' => '[userPrimaryEntity.entity_type]',
-    'user.entity_path' => '[userPrimaryEntity.path]',
-
-    'contact.contact_type_label' => '',
-    'contact.society_short' => '',
-    'contact.contact_purpose_label' => '',
-    'contact.website' => '',
-    'contact.salutation_header' => '',
-    'contact.salutation_footer' => '',
-    'contact.society' => '[recipient.company]',
-    'contact.departement' => '[recipient.department]',
-    'contact.title' => '[recipient.civility]',
-    'contact.contact_title' => '[recipient.civility]',
-    'contact.contact_lastname' => '[recipient.lastname]',
-    'contact.contact_firstname' => '[recipient.firstname]',
-    'contact.lastname' => '[recipient.lastname]',
-    'contact.firstname' => '[recipient.firstname]',
-    'contact.function' => '[recipient.function]',
+    'user.entity_type'      => '[userPrimaryEntity.entity_type]',
+    'user.entity_path'      => '[userPrimaryEntity.path]',
+
+    'contact.contact_type_label'        => '',
+    'contact.society_short'             => '',
+    'contact.contact_purpose_label'     => '',
+    'contact.website'                   => '',
+    'contact.salutation_header'         => '',
+    'contact.salutation_footer'         => '',
+    'contact.society'                   => '[recipient.company]',
+    'contact.departement'               => '[recipient.department]',
+    'contact.title'                     => '[recipient.civility]',
+    'contact.contact_title'             => '[recipient.civility]',
+    'contact.contact_lastname'          => '[recipient.lastname]',
+    'contact.contact_firstname'         => '[recipient.firstname]',
+    'contact.lastname'                  => '[recipient.lastname]',
+    'contact.firstname'                 => '[recipient.firstname]',
+    'contact.function'                  => '[recipient.function]',
     'contact.postal_address;strconv=no' => '[recipient.postal_address;strconv=no]',
-    'contact.postal_address' => '[recipient.postal_address]',
-    'contact.address_num' => '[recipient.address_number]',
-    'contact.address_street' => '[recipient.address_street]',
-    'contact.occupancy' => '[recipient.address_additional1]',
-    'contact.address_complement' => '[recipient.address_additional2]',
-    'contact.address_town' => '[recipient.address_town]',
-    'contact.address_postal_code' => '[recipient.address_postcode]',
-    'contact.address_country' => '[recipient.address_country]',
-    'contact.phone' => '[recipient.phone]',
-    'contact.email' => '[recipient.email]',
+    'contact.postal_address'            => '[recipient.postal_address]',
+    'contact.address_num'               => '[recipient.address_number]',
+    'contact.address_street'            => '[recipient.address_street]',
+    'contact.occupancy'                 => '[recipient.address_additional1]',
+    'contact.address_complement'        => '[recipient.address_additional2]',
+    'contact.address_town'              => '[recipient.address_town]',
+    'contact.address_postal_code'       => '[recipient.address_postcode]',
+    'contact.address_country'           => '[recipient.address_country]',
+    'contact.phone'                     => '[recipient.phone]',
+    'contact.email'                     => '[recipient.email]',
+
+    'notes.identifier'                       => '[res_letterbox.res_id]',
+    'notes.subject'                          => '[res_letterbox.subject]',
+    'notes.note_text'                        => '[notes]',
+    'notes.user_id'                          => '',
+    'notes.# ;frm=0000'                      => '[res_letterbox.# ;frm=0000]',
+    'notes.doc_date;block=tr;frm=dd/mm/yyyy' => '[res_letterbox.doc_date;block=tr;frm=dd/mm/yyyy]',
+    'notes.doc_date;block=tr'                => '[res_letterbox.doc_date;block=tr]',
+    'notes.doc_date;frm=dd/mm/yyyy'          => '[res_letterbox.doc_date;frm=dd/mm/yyyy]',
+    'notes.doc_date'                         => '[res_letterbox.doc_date]',
+    'notes.contact_society'                  => '[contact.company]',
+    'notes.contact_firstname'                => '[contact.firstname]',
+    'notes.contact_lastname'                 => '[contact.lastname]',
+    'notes.linktodetail'                     => '[res_letterbox.linktodetail]',
+    'notes.linktodoc'                        => '[res_letterbox.linktodoc]',
 ];
 
 $customFields = [
@@ -195,7 +210,7 @@ $customFields = [
 
 chdir('../..');
 
-$customs =  scandir('custom');
+$customs = scandir('custom');
 
 foreach ($customs as $custom) {
     if ($custom == 'custom.xml' || $custom == '.' || $custom == '..') {
@@ -236,11 +251,11 @@ foreach ($customs as $custom) {
 
             if ($content != $newContent) {
                 TemplateModel::update([
-                    'set' => [
+                    'set'   => [
                         'template_content' => $newContent
                     ],
                     'where' => ['template_id = ?'],
-                    'data' => [$template['template_id']]
+                    'data'  => [$template['template_id']]
                 ]);
                 $migrated++;
             } else {
diff --git a/modules/templates/datasources/mlb_notes_content.php b/modules/templates/datasources/mlb_notes_content.php
index 9bd61ac938d..f32a372ffd4 100755
--- a/modules/templates/datasources/mlb_notes_content.php
+++ b/modules/templates/datasources/mlb_notes_content.php
@@ -11,6 +11,13 @@
     [notes] = detail of notes added
 */
 
+use Contact\models\ContactModel;
+use Note\models\NoteModel;
+use Resource\models\ResModel;
+use Resource\models\ResourceContactModel;
+use SrcCore\models\TextFormatModel;
+use User\models\UserModel;
+
 $dbDatasource = new Database();
 
 $datasources['recipient'][0] = (array)$recipient;
@@ -52,7 +59,19 @@ foreach ($events as $event) {
     }
     
     $stmt = $dbDatasource->query($query, $arrayPDO);
-    $note = $stmt->fetch(PDO::FETCH_ASSOC);
+
+    if ($event->table_name != 'notes') {
+        $note = $stmt->fetch(PDO::FETCH_ASSOC);
+        $resId = $resId;
+    } else {
+        $note = NoteModel::getById(['id' => $event->record_id]);
+        $resId = $note['identifier'];
+        $resLetterbox = ResModel::getById([
+            'select' => ['*'],
+            'resId'  => $resId
+        ]);
+        $datasources['res_letterbox'][] = $resLetterbox;
+    }
     
     // Lien vers la page détail
     $urlToApp = trim($maarchUrl, '/').'/apps/'.trim($maarchApps, '/').'/index.php?';
@@ -65,14 +84,35 @@ foreach ($events as $event) {
         'data'    => [$user['id'], 'MyBasket']
     ]);
 
-    $note['linktodoc']     = $urlToApp . 'linkToDoc='.$note['res_id'];
-    $note['linktodetail']  = $urlToApp . 'linkToDetail='.$note['res_id'];
-    if (!empty($note['res_id']) && !empty($preferenceBasket[0]['group_serial_id']) && !empty($basket['id']) && !empty($user['id'])) {
-        $note['linktoprocess'] = $urlToApp . 'linkToProcess='.$note['res_id'].'&groupId='.$preferenceBasket[0]['group_serial_id'].'&basketId='.$basket['id'].'&userId='.$user['id'];
+    $note['linktodoc']     = $urlToApp . 'linkToDoc='.$resId;
+    $note['linktodetail']  = $urlToApp . 'linkToDetail='.$resId;
+
+    if (!empty($resId) && !empty($preferenceBasket[0]['group_serial_id']) && !empty($basket['id']) && !empty($user['id'])) {
+        $note['linktoprocess'] = $urlToApp . 'linkToProcess='.$resId.'&groupId='.$preferenceBasket[0]['group_serial_id'].'&basketId='.$basket['id'].'&userId='.$user['id'];
+    }
+
+    $resourceContacts = ResourceContactModel::get([
+        'where' => ['res_id = ?', "type = 'contact'"],
+        'data'  => [$resId]
+    ]);
+
+    if ($event->table_name == 'notes') {
+        $datasources['res_letterbox'][0]['linktodoc'] = $note['linktodoc'];
+        $datasources['res_letterbox'][0]['linktodetail'] = $note['linktodetail'];
+        $datasources['res_letterbox'][0]['linktoprocess'] = $note['linktodoc'];
+
+        $labelledUser = UserModel::getLabelledUserById(['id' => $note['user_id']]);
+        $creationDate = TextFormatModel::formatDate($note['creation_date'], 'd/m/Y');
+        $note = "{$labelledUser} : {$creationDate} : {$note['note_text']}\n";
+    }
+
+    foreach ($resourceContacts as $resourceContact) {
+        $contact = ContactModel::getById(['id' => $resourceContact['item_id'], 'select' => ['*']]);
+        $datasources['contact'][] = $contact;
     }
     
     // Insertion
-    $datasources['notes'][] = $note;
+    $datasources['notes'] = $note;
 }
 
 $datasources['images'][0]['imgdetail'] = str_replace('//', '/', $maarchUrl . '/apps/' . $maarchApps . '/img/object.gif');
-- 
GitLab