From 3986136fa039604635ed39096e21528ab1ead55e Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Tue, 21 Apr 2020 16:09:12 +0100
Subject: [PATCH] FEAT #13679 TIME 2:10 Use config.xml in notification script +
 docker + doc migration

---
 apps/maarch_entreprise/xml/config.xml.default |  3 +
 bin/notification/config/config.xml.default    |  9 --
 bin/notification/scripts/BASKETS.sh           |  2 +-
 bin/notification/scripts/nct-ncc-and-anc.sh   |  8 +-
 bin/notification/scripts/ret1-ret2.sh         |  6 +-
 bin/notification/scripts/sendmail.sh          |  2 +-
 migration/20.10/migrate.sh                    |  1 +
 .../20.10/migrateNotificationsConfig.php      | 82 +++++++++++++++++++
 .../controllers/NotificationController.php    | 11 +--
 .../NotificationScheduleController.php        | 10 +--
 .../NotificationScheduleModelAbstract.php     | 19 +----
 11 files changed, 102 insertions(+), 51 deletions(-)
 delete mode 100644 bin/notification/config/config.xml.default
 create mode 100755 migration/20.10/migrateNotificationsConfig.php

diff --git a/apps/maarch_entreprise/xml/config.xml.default b/apps/maarch_entreprise/xml/config.xml.default
index e0a6712e8be..0f214138b4f 100755
--- a/apps/maarch_entreprise/xml/config.xml.default
+++ b/apps/maarch_entreprise/xml/config.xml.default
@@ -20,6 +20,9 @@
         <timezone>Europe/Paris</timezone>
         <userdefaultpassword>maarch</userdefaultpassword> <!-- default user's password -->
         <usePHPIDS>true</usePHPIDS>
+        <MaarchDirectory>/var/www/html/MaarchCourrier/</MaarchDirectory>
+        <customID></customID>
+        <MaarchUrl>http://preview.maarchcourrier.com/</MaarchUrl>
     </CONFIG>
     <CRYPT>
         <encrypt>false</encrypt>
diff --git a/bin/notification/config/config.xml.default b/bin/notification/config/config.xml.default
deleted file mode 100644
index 5022b2e30a9..00000000000
--- a/bin/notification/config/config.xml.default
+++ /dev/null
@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<ROOT>
-<!-- le nom du fichier doit etre config.xml ou config_{custom_id}.xml -->
-    <CONFIG>
-        <MaarchDirectory>/var/www/html/MaarchCourrier/</MaarchDirectory>
-        <customID></customID>
-        <MaarchUrl>http://preview.maarchcourrier.com</MaarchUrl>
-    </CONFIG>
-</ROOT>
diff --git a/bin/notification/scripts/BASKETS.sh b/bin/notification/scripts/BASKETS.sh
index 3ff53615b55..99946615dd1 100755
--- a/bin/notification/scripts/BASKETS.sh
+++ b/bin/notification/scripts/BASKETS.sh
@@ -1,3 +1,3 @@
 #!/bin/sh
 eventStackPath='/var/www/html/MaarchCourrier/bin/notification/basket_event_stack.php'
-php $eventStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml -n BASKETS
\ No newline at end of file
+php $eventStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml -n BASKETS
\ No newline at end of file
diff --git a/bin/notification/scripts/nct-ncc-and-anc.sh b/bin/notification/scripts/nct-ncc-and-anc.sh
index 3bad6f66ce5..d8c997224dd 100755
--- a/bin/notification/scripts/nct-ncc-and-anc.sh
+++ b/bin/notification/scripts/nct-ncc-and-anc.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 eventStackPath='/var/www/html/MaarchCourrier/bin/notification/process_event_stack.php'
-php $eventStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml -n NCC
-php $eventStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml -n ANC
-php $eventStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml -n AND
-php $eventStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml -n RED
+php $eventStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml -n NCC
+php $eventStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml -n ANC
+php $eventStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml -n AND
+php $eventStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml -n RED
diff --git a/bin/notification/scripts/ret1-ret2.sh b/bin/notification/scripts/ret1-ret2.sh
index 5a24d960442..894fda09b1c 100755
--- a/bin/notification/scripts/ret1-ret2.sh
+++ b/bin/notification/scripts/ret1-ret2.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 mlbStackPath='/var/www/html/MaarchCourrier/bin/notification/stack_letterbox_alerts.php'
 eventStackPath='/var/www/html/MaarchCourrier/bin/notification/process_event_stack.php'
-php $mlbStackPath   -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml 
-php $eventStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml -n RET1
-php $eventStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml -n RET2
+php $mlbStackPath   -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml 
+php $eventStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml -n RET1
+php $eventStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml -n RET2
diff --git a/bin/notification/scripts/sendmail.sh b/bin/notification/scripts/sendmail.sh
index d6902e1a6be..7d1eb49fcca 100755
--- a/bin/notification/scripts/sendmail.sh
+++ b/bin/notification/scripts/sendmail.sh
@@ -1,4 +1,4 @@
 #!/bin/sh
 emailStackPath='/var/www/html/MaarchCourrier/bin/notification/process_email_stack.php'
-php $emailStackPath -c /var/www/html/MaarchCourrier/bin/notification/config/config.xml
+php $emailStackPath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.xml
 
diff --git a/migration/20.10/migrate.sh b/migration/20.10/migrate.sh
index 67776eabffa..a2502b2cf82 100755
--- a/migration/20.10/migrate.sh
+++ b/migration/20.10/migrate.sh
@@ -1,4 +1,5 @@
 #!/bin/sh
 php ./migrateModulesConfig.php
 php ./migrateNotificationsProperties.php
+php ./migrateNotificationsConfig.php
 php ./migrateCustomXml.php # mettre en dernier
diff --git a/migration/20.10/migrateNotificationsConfig.php b/migration/20.10/migrateNotificationsConfig.php
new file mode 100755
index 00000000000..9a16547c0db
--- /dev/null
+++ b/migration/20.10/migrateNotificationsConfig.php
@@ -0,0 +1,82 @@
+<?php
+
+require '../../vendor/autoload.php';
+
+chdir('../..');
+
+$nonReadableFiles = [];
+$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]);
+    $GLOBALS['customId'] = $custom;
+
+    $configPath             = "custom/{$custom}/apps/maarch_entreprise/xml/config.xml";
+    $notificationConfigPath = "modules/notifications/batch/config/config.xml";
+    if (file_exists($configPath)) {
+        if (!is_readable($configPath) || !is_writable($configPath)) {
+            $nonReadableFiles[] = $configPath;
+            continue;
+        }
+        if (is_readable($notificationConfigPath) && is_writable($notificationConfigPath)) {
+            $notificationFilePath = $notificationConfigPath;
+        } elseif (is_readable("custom/{$custom}/{$notificationConfigPath}") && is_writable("custom/{$custom}/{$notificationConfigPath}")) {
+            $notificationFilePath = "custom/{$custom}/{$notificationConfigPath}";
+        } elseif (is_readable("custom/{$custom}/modules/notifications/batch/config/config_{$custom}.xml") && is_writable("custom/{$custom}/modules/notifications/batch/config/config_{$custom}.xml")) {
+            $notificationFilePath = "custom/{$custom}/modules/notifications/batch/config/config_{$custom}.xml";
+        } else {
+            printf("Aucun fichier de configuration de notification trouvé pour le custom {$custom}\n");
+            continue;
+        }
+        $loadedXml             = simplexml_load_file($configPath);
+        $loadedNotificationXml = simplexml_load_file($notificationFilePath);
+        
+        if ($loadedXml && $loadedNotificationXml) {
+            $loadedXml->CONFIG->MaarchDirectory = (string)$loadedNotificationXml->CONFIG->MaarchDirectory;
+            $loadedXml->CONFIG->customID        = (string)$loadedNotificationXml->CONFIG->customID;
+            $loadedXml->CONFIG->MaarchUrl       = (string)$loadedNotificationXml->CONFIG->MaarchUrl;
+
+            $res = formatXml($loadedXml);
+            $fp  = fopen($configPath, "w+");
+            if ($fp) {
+                fwrite($fp, $res);
+            }
+
+            $notifications = \Notification\models\NotificationModel::get(['select' => ['notification_sid', 'notification_id']]);
+            $user          = \User\models\UserModel::get(['select' => ['id'], 'orderBy' => ["user_id='superadmin' desc"], 'limit' => 1]);
+            $GLOBALS['id'] = $user[0]['id'];
+            $language      = \SrcCore\models\CoreConfigModel::getLanguage();
+            if (file_exists("custom/{$custom}/src/core/lang/lang-{$language}.php")) {
+                require_once("custom/{$custom}/src/core/lang/lang-{$language}.php");
+            }
+            require_once("src/core/lang/lang-{$language}.php");
+            foreach ($notifications as $notification) {
+                \Notification\models\NotificationScheduleModel::createScriptNotification(['notification_sid' => $notification['notification_sid'], 'notification_id' => $notification['notification_id']]);
+            }
+
+            $migrated++;
+        }
+    }
+}
+
+foreach ($nonReadableFiles as $file) {
+    printf("The file %s it is not readable or not writable.\n", $file);
+}
+
+printf($migrated . " custom(s) avec config.xml (notifications) trouvé(s) et migré(s).\n");
+
+function formatXml($simpleXMLElement)
+{
+    $xmlDocument = new DOMDocument('1.0');
+    $xmlDocument->preserveWhiteSpace = false;
+    $xmlDocument->formatOutput = true;
+    $xmlDocument->loadXML($simpleXMLElement->asXML());
+
+    return $xmlDocument->saveXML();
+}
diff --git a/src/app/notification/controllers/NotificationController.php b/src/app/notification/controllers/NotificationController.php
index 6ea0e5a1283..a4e89f08354 100755
--- a/src/app/notification/controllers/NotificationController.php
+++ b/src/app/notification/controllers/NotificationController.php
@@ -89,7 +89,7 @@ class NotificationController
 
         $notification['scriptcreated'] = false;
 
-        if (file_exists($pathToFolow.'modules/notifications/batch/scripts/'.$filename) || file_exists($pathToFolow.'bin/notification/scripts/'.$filename)) {
+        if (file_exists($pathToFolow.'bin/notification/scripts/'.$filename)) {
             $notification['scriptcreated'] = true;
         }
 
@@ -232,7 +232,7 @@ class NotificationController
             }
 
             foreach ($cronTab as $key => $value) {
-                if (in_array($value['cmd'], [$pathToFolow.'modules/notifications/batch/scripts/'.$filename, $pathToFolow.'bin/notification/scripts/'.$filename])) {
+                if (in_array($value['cmd'], [$pathToFolow.'bin/notification/scripts/'.$filename])) {
                     $cronTab[$key]['state'] = 'deleted';
                     $flagCron = true;
                     break;
@@ -243,14 +243,9 @@ class NotificationController
                 NotificationScheduleModel::saveCrontab($cronTab);
             }
 
-            $filePath = $pathToFolow.'modules/notifications/batch/scripts/'.$filename;
+            $filePath = $pathToFolow.'bin/notification/scripts/'.$filename;
             if (file_exists($filePath)) {
                 unlink($filePath);
-            } else {
-                $filePath = $pathToFolow.'bin/notification/scripts/'.$filename;
-                if (file_exists($filePath)) {
-                    unlink($filePath);
-                }
             }
         }
 
diff --git a/src/app/notification/controllers/NotificationScheduleController.php b/src/app/notification/controllers/NotificationScheduleController.php
index 31c3d229ed6..c93df8334b0 100755
--- a/src/app/notification/controllers/NotificationScheduleController.php
+++ b/src/app/notification/controllers/NotificationScheduleController.php
@@ -89,14 +89,9 @@ class NotificationScheduleController
                 $pathToFolow = $corePath;
             }
 
-            $path = $pathToFolow.'modules/notifications/batch/scripts/'.$filename;
+            $path = $pathToFolow.'bin/notification/scripts/'.$filename;
             if (file_exists($path)) {
                 $notificationsArray[] = ['description' => $result['description'], 'path' => $path];
-            } else {
-                $path = $pathToFolow.'bin/notification/scripts/'.$filename;
-                if (file_exists($path)) {
-                    $notificationsArray[] = ['description' => $result['description'], 'path' => $path];
-                }
             }
         }
 
@@ -125,8 +120,7 @@ class NotificationScheduleController
                     $pathToFolow = $corePath;
                 }
                 $returnValue = true;
-                if (strpos($crontabToSave[$id]['cmd'], $pathToFolow.'modules/notifications/batch/scripts/') !== 0
-                    && strpos($crontabToSave[$id]['cmd'], $pathToFolow.'bin/notification/scripts/') !== 0) {
+                if (strpos($crontabToSave[$id]['cmd'], $pathToFolow.'bin/notification/scripts/') !== 0) {
                     $returnValue = false;
                     break;
                 }
diff --git a/src/app/notification/models/NotificationScheduleModelAbstract.php b/src/app/notification/models/NotificationScheduleModelAbstract.php
index 6e1912324af..40f52b2b239 100755
--- a/src/app/notification/models/NotificationScheduleModelAbstract.php
+++ b/src/app/notification/models/NotificationScheduleModelAbstract.php
@@ -107,8 +107,7 @@ abstract class NotificationScheduleModelAbstract
             }
 
             $state = 'normal';
-            if (strpos($cmd, $pathToFolow.'bin/notification/scripts/') !== 0 && strpos($cmd, $pathToFolow.'modules/notifications/batch/scripts/') !== 0
-                    && $aArgs['setHiddenValue']) {
+            if (strpos($cmd, $pathToFolow.'bin/notification/scripts/') !== 0 && $aArgs['setHiddenValue']) {
                 $cmd = 'hidden';
                 $state = 'hidden';
             }
@@ -148,21 +147,7 @@ abstract class NotificationScheduleModelAbstract
         $corePath = str_replace('custom/'.$customId.'/src/app/notification/models', '', __DIR__);
         $corePath = str_replace('src/app/notification/models', '', $corePath);
 
-        if (file_exists($corePath.'custom/'.$customId.'/modules/notifications/batch/config/config.xml')) {
-            $ConfigNotif = $corePath.'custom/'.$customId.'/modules/notifications/batch/config/config.xml';
-        } elseif (file_exists($corePath.'custom/'.$customId.'/modules/notifications/batch/config/config_'.$customId.'.xml')) {
-            $ConfigNotif = $corePath.'custom/'.$customId.'/modules/notifications/batch/config/config_'.$customId.'.xml';
-        } elseif (file_exists($corePath.'modules/notifications/batch/config/config_'.$customId.'.xml')) {
-            $ConfigNotif = $corePath.'modules/notifications/batch/config/config_'.$customId.'.xml';
-        } elseif (file_exists($corePath.'custom/'.$customId.'/bin/notification/config/config.xml')) {
-            $ConfigNotif = $corePath.'custom/'.$customId.'/bin/notification/config/config.xml';
-        } elseif (file_exists($corePath.'custom/'.$customId.'/bin/notification/config/config_'.$customId.'.xml')) {
-            $ConfigNotif = $corePath.'custom/'.$customId.'/bin/notification/config/config_'.$customId.'.xml';
-        } elseif (file_exists($corePath.'bin/notification/config/config_'.$customId.'.xml')) {
-            $ConfigNotif = $corePath.'bin/notification/config/config_'.$customId.'.xml';
-        } else {
-            $ConfigNotif = $corePath.'modules/notifications/batch/config/config.xml';
-        }
+        $ConfigNotif = $corePath . CoreConfigModel::getConfigPath();
 
         if ($customId != '') {
             $pathToFolow = $corePath.'custom/'.$customId.'/';
-- 
GitLab