From 0cb4e8316f7c4169d3e60e6777b779eb80d2eaa9 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Mon, 18 Oct 2021 10:39:02 +0200
Subject: [PATCH] FIX #18516 TIME 1:35 migrate custom migrations

---
 migration/20.03/migrateCustomNatures.php | 82 ++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
 create mode 100755 migration/20.03/migrateCustomNatures.php

diff --git a/migration/20.03/migrateCustomNatures.php b/migration/20.03/migrateCustomNatures.php
new file mode 100755
index 00000000000..d22bff49c9d
--- /dev/null
+++ b/migration/20.03/migrateCustomNatures.php
@@ -0,0 +1,82 @@
+<?php
+
+require '../../vendor/autoload.php';
+
+chdir('../..');
+
+// /!\ If nature label is set in a language file, only the 'fr' label will be migrated
+require 'apps/maarch_entreprise/lang/fr.php';
+
+const DEFAULT_NATURES = [
+    'simple_mail',
+    'email',
+    'fax',
+    'chronopost',
+    'fedex',
+    'registered_mail',
+    'courier',
+    'message_exchange',
+    'other'
+];
+
+$migrated = 0;
+$customs =  scandir('custom');
+foreach ($customs as $custom) {
+    if ($custom == 'custom.xml' || $custom == '.' || $custom == '..') {
+        continue;
+    }
+
+    \SrcCore\models\DatabasePDO::reset();
+    new \SrcCore\models\DatabasePDO(['customId' => $custom]);
+
+    if (file_exists("custom/$custom/apps/maarch_entreprise/lang/fr.php")) {
+        require "custom/$custom/apps/maarch_entreprise/lang/fr.php";
+    }
+
+    $xmlfile = null;
+    $path = "custom/{$custom}/apps/maarch_entreprise/xml/entreprise.xml";
+    if (!file_exists($path)) {
+        continue;
+    }
+    if (!is_readable($path)) {
+        printf("The file $path it is not readable or not writable.\n");
+        continue;
+    }
+
+    $xmlfile = simplexml_load_file($path);
+
+    if ($xmlfile) {
+        if (!empty($xmlfile->mail_natures->nature)) {
+            $natureCustomFieldId = 1; // Custom field 'Nature' is created with id 1 during migration
+            $natureCustomField = \CustomField\models\CustomFieldModel::getById(['id' => $natureCustomFieldId]);
+            $natureCustomField['values'] = json_decode($natureCustomField['values'], true);
+            foreach ($xmlfile->mail_natures->nature as $nature) {
+                if (in_array($nature->id, DEFAULT_NATURES)) {
+                    continue;
+                }
+                $natureLabel = (string)$nature->label;
+                if (!empty($natureLabel) && defined($natureLabel) && constant($natureLabel) != null) {
+                    $natureLabel = constant($natureLabel);
+                }
+                $natureCustomField['values'][] = $natureLabel;
+
+                $natureLabel = json_encode($natureLabel);
+                $natureLabel = str_replace("'", "''", $natureLabel);
+
+                \Resource\models\ResModel::update([
+                    'postSet'   => ['custom_fields' => "jsonb_set(custom_fields, '{{$natureCustomFieldId}}', '{$natureLabel}')"],
+                    'where'     => ['res_id in (select res_id from mlb_coll_ext where nature_id = ?)'],
+                    'data'      => [(string)$nature->id]
+                ]);
+                $migrated++;
+            }
+            \CustomField\models\CustomFieldModel::update([
+                'set'   => ['values' => json_encode($natureCustomField['values'])],
+                'where' => ['id = ?'],
+                'data'  => [$natureCustomFieldId]
+            ]);
+        }
+    }
+    printf("Migration nature personnalisé (CUSTOM {$custom}) : " . $migrated . " nature(s) personnalisé(s) migré(s).\n");
+}
+
-- 
GitLab