From e27cbd4fafd00f04227e5f642518355a68f88c2d Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 30 Sep 2019 10:27:53 +0100
Subject: [PATCH] FEAT #11269 TIME 3 migrate old indexing models

---
 .gitignore                                    |   8 --
 migration/19.12/1912.sql                      |   2 -
 migration/19.12/migrate.sh                    |   2 +
 migration/19.12/migrateOldIndexingModels.php  | 100 +++++++++++++++---
 .../doctype/models/DoctypeModelAbstract.php   |   1 -
 5 files changed, 89 insertions(+), 24 deletions(-)

diff --git a/.gitignore b/.gitignore
index 394f4c4c845..1e98ca9c532 100755
--- a/.gitignore
+++ b/.gitignore
@@ -3,16 +3,8 @@ installed.lck
 apps/maarch_entreprise/xml/config.xml
 apps/maarch_entreprise/xml/mailevaConfig.xml
 apps/maarch_entreprise/xml/log4php.xml
-.vscode/
-.phplint-cache
-.phplint.yml
 *.log
-xdelete.sh
 test/unitTests/build/
-modules/convert/batch/scripts/launch_fulltext_letterbox.sh
-modules/convert/batch/scripts/launch_convert_letterbox.sh
-modules/convert/batch/config/config_only_indexes.xml
-modules/convert/batch/tmp/
 modules/visa/xml/remoteSignatoryBooks.xml
 dist/
 node_modules/
diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql
index 578496f8bbb..856022cd4c1 100644
--- a/migration/19.12/1912.sql
+++ b/migration/19.12/1912.sql
@@ -435,7 +435,6 @@ INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_val
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (1, 'arrivalDate', TRUE, '""', 'mail');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (1, 'subject', TRUE, '""', 'mail');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (1, 'indexingCustomField_1', FALSE, '""', 'mail');
-INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (1, 'indexingCustomField_2', FALSE, '""', 'mail');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (1, 'senders', TRUE, '""', 'contact');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (1, 'getRecipients', FALSE, '""', 'contact');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (1, 'initiator', TRUE, '""', 'process');
@@ -451,7 +450,6 @@ INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_val
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (2, 'docDate', TRUE, '""', 'mail');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (2, 'subject', TRUE, '""', 'mail');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (2, 'indexingCustomField_1', FALSE, '""', 'mail');
-INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (2, 'indexingCustomField_2', FALSE, '""', 'mail');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (2, 'senders', FALSE, '""', 'contact');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (2, 'getRecipients', TRUE, '""', 'contact');
 INSERT INTO indexing_models_fields (model_id, identifier, mandatory, default_value, unit) VALUES (2, 'initiator', TRUE, '""', 'process');
diff --git a/migration/19.12/migrate.sh b/migration/19.12/migrate.sh
index 31c23ccf43e..0bcf589fc8a 100755
--- a/migration/19.12/migrate.sh
+++ b/migration/19.12/migrate.sh
@@ -11,3 +11,5 @@ php ./exportCases.php
 php ./exportFileplans.php
 php ./exportFolders.php
 php ./removeProcessModes.php
+php ./migrateCategories.php
+php ./migrateOldIndexingModels.php
diff --git a/migration/19.12/migrateOldIndexingModels.php b/migration/19.12/migrateOldIndexingModels.php
index 876d42dd540..1679dc08c33 100644
--- a/migration/19.12/migrateOldIndexingModels.php
+++ b/migration/19.12/migrateOldIndexingModels.php
@@ -1,5 +1,65 @@
 <?php
 
+$aDataIncoming['incoming'] = [
+    'doctype'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'priority'              => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'confidential'          => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'docDate'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'arrivalDate'           => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'subject'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'indexingCustomField_1' => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'mail'],
+    'senders'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'contact'],
+    'getRecipients'         => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'contact'],
+    'initiator'             => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'destination'           => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'processLimitDate'      => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'folder'                => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'classement'],
+    'tags'                  => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'classement'],
+];
+
+$aDataIncoming['outgoing'] = [
+    'doctype'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'priority'              => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'confidential'          => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'docDate'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'subject'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'indexingCustomField_1' => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'mail'],
+    'senders'               => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'contact'],
+    'getRecipients'         => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'contact'],
+    'initiator'             => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'destination'           => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'processLimitDate'      => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'folder'                => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'classement'],
+    'tags'                  => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'classement'],
+];
+
+$aDataIncoming['internal'] = [
+    'doctype'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'priority'              => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'confidential'          => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'docDate'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'subject'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'indexingCustomField_1' => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'mail'],
+    'senders'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'contact'],
+    'getRecipients'         => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'contact'],
+    'initiator'             => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'destination'           => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'processLimitDate'      => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'folder'                => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'classement'],
+    'tags'                  => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'classement'],
+];
+
+$aDataIncoming['ged_doc'] = [
+    'doctype'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'confidential'          => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'docDate'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'subject'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'mail'],
+    'senders'               => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'contact'],
+    'getRecipients'         => ['mandatory' => 'false', 'default_value' => '""', 'unit' => 'contact'],
+    'initiator'             => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+    'destination'           => ['mandatory' => 'true', 'default_value' => '""', 'unit' => 'process'],
+];
+
 require '../../vendor/autoload.php';
 
 chdir('../..');
@@ -34,6 +94,7 @@ foreach ($customs as $custom) {
         if (empty($fieldContent['category_id'])) {
             continue;
         }
+        $datasToImport = $aDataIncoming[$fieldContent['category_id']];
 
         $modelId = \IndexingModel\models\IndexingModelModel::create([
             'label'     => $oldIndexingModel['label'],
@@ -44,23 +105,36 @@ foreach ($customs as $custom) {
         ]);
 
         foreach ($fieldContent as $key => $field) {
-            if (in_array($key, ['type_id', 'priority', 'subject', 'destination'])) {
-                if ($key == 'type_id') {
-                    $identifier = 'doctype';
-                } else {
-                    $identifier = $key;
+            if ($key == 'type_id') {
+                $doctype = \Doctype\models\DoctypeModel::get(['select' => [1], 'where' => ['type_id = ?', 'enabled = ?'], 'data' => [$field, 'Y']]);
+                if (!empty($doctype)) {
+                    $datasToImport['doctype']['default_value'] = json_encode($field);
                 }
-                \IndexingModel\models\IndexingModelFieldModel::create([
-                    'model_id'      => $modelId,
-                    'identifier'    => $identifier,
-                    'mandatory'     => 'false',
-                    'default_value' => json_encode($field),
-                    'unit'          => 'mail'
-                ]);
+            } elseif ($key == 'priority') {
+                $priority = \Priority\models\PriorityModel::getById(['select' => [1], 'id' => $field]);
+                if (!empty($priority)) {
+                    $datasToImport['priority']['default_value'] = json_encode($field);
+                }
+            } elseif ($key == 'destination') {
+                $destination = \Entity\models\EntityModel::get(['select' => ['id'], 'where' => ['entity_id = ?', 'enabled = ?'], 'data' => [$field, 'Y']]);
+                if (!empty($destination)) {
+                    $datasToImport['destination']['default_value'] = json_encode($destination[0]['id']);
+                }
+            } elseif ($key == 'subject') {
+                $datasToImport['subject']['default_value'] = json_encode($field);
             }
         }
+        foreach ($datasToImport as $id => $defaultValue) {
+            \IndexingModel\models\IndexingModelFieldModel::create([
+                'model_id'      => $modelId,
+                'identifier'    => $id,
+                'mandatory'     => $defaultValue['mandatory'],
+                'default_value' => $defaultValue['default_value'],
+                'unit'          => $defaultValue['unit']
+            ]);
+        }
         ++$migrated;
     }
 
-    printf("Migration ancien modèles d'indexation (CUSTOM {$custom}) : " . $migrated . " modèle utilisé(s) et migré(s).\n");
+    printf("Migration anciens modèles d'indexation (CUSTOM {$custom}) : " . $migrated . " modèle(s) utilisé(s) et migré(s).\n");
 }
diff --git a/src/app/doctype/models/DoctypeModelAbstract.php b/src/app/doctype/models/DoctypeModelAbstract.php
index acbd1a7002b..f5b7396fd85 100755
--- a/src/app/doctype/models/DoctypeModelAbstract.php
+++ b/src/app/doctype/models/DoctypeModelAbstract.php
@@ -12,7 +12,6 @@
 namespace Doctype\models;
 
 use SrcCore\models\ValidatorModel;
-use SrcCore\models\CoreConfigModel;
 use SrcCore\models\DatabaseModel;
 
 class DoctypeModelAbstract
-- 
GitLab