From 82a44e4c9301aefaad7daa8ae265afc34f3f75f1 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Tue, 21 Jul 2020 16:20:46 +0200
Subject: [PATCH] FEAT #13671 TIME 1:35 migrate img folder

---
 migration/20.10/migrate.sh        |  1 +
 migration/20.10/migrateImages.php | 48 +++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100755 migration/20.10/migrateImages.php

diff --git a/migration/20.10/migrate.sh b/migration/20.10/migrate.sh
index 76c7c412b6b..3e23c1cb1df 100755
--- a/migration/20.10/migrate.sh
+++ b/migration/20.10/migrate.sh
@@ -3,4 +3,5 @@ php ./migrateConfigXml.php # mettre en premier
 php ./migrateNotificationsProperties.php
 php ./migrateNotificationsConfig.php
 php ./migrateRemoteSignatureBookConfig.php
+php ./migrateImages.php
 php ./migrateCustomXml.php # mettre en dernier
diff --git a/migration/20.10/migrateImages.php b/migration/20.10/migrateImages.php
new file mode 100755
index 00000000000..4b1ea00e861
--- /dev/null
+++ b/migration/20.10/migrateImages.php
@@ -0,0 +1,48 @@
+<?php
+
+require '../../vendor/autoload.php';
+
+chdir('../..');
+
+$migrated = 0;
+$customs  = scandir('custom');
+
+foreach ($customs as $custom) {
+    if (in_array($custom, ['custom.json', 'custom.xml', '.', '..'])) {
+        continue;
+    }
+
+    $oldImgFolderPath = "custom/{$custom}/apps/maarch_entreprise/img";
+    if (is_dir($oldImgFolderPath)) {
+        if (!is_readable($oldImgFolderPath) || !is_writable($oldImgFolderPath)) {
+            printf("WARNING : The folder %s is not readable or not writable.\n", $oldImgFolderPath);
+            continue;
+        }
+
+        $newImgFolderPath = "custom/{$custom}/img";
+        if (!is_dir($newImgFolderPath)) {
+            if (!@mkdir($newImgFolderPath, 0755, true)) {
+                printf("WARNING : The folder %s can not be created.\n", $newImgFolderPath);
+                continue;
+            }
+        } elseif (!is_readable($newImgFolderPath) || !is_writable($newImgFolderPath)) {
+            printf("WARNING : The folder %s is not readable or not writable.\n", $newImgFolderPath);
+            continue;
+        }
+
+        $images = scandir($oldImgFolderPath);
+        foreach ($images as $image) {
+            if (in_array($image, ['.', '..'])) {
+                continue;
+            }
+            if (!rename($oldImgFolderPath . '/' . $image, $newImgFolderPath . '/' . $image)) {
+                printf("WARNING : The file %s can not be moved to {$newImgFolderPath} : permission denied.\n", $image);
+            }
+        }
+        
+        rmdir($oldImgFolderPath);
+        $migrated++;
+    }
+}
+
+printf($migrated . " dossier(s) custom/custom_id/apps/maarch_entreprise/img trouvé(s) et migré(s).\n");
-- 
GitLab