]> _ Git - cubist_cms-back.git/commitdiff
wip #6542 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Dec 2023 11:57:51 +0000 (12:57 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Dec 2023 11:57:51 +0000 (12:57 +0100)
src/app/Media/Util.php

index 7bef5d7f448736cfd722b978e8860f8de1c6be78..0249b11c8d91db30570bc15ffe258c50c2e521c5 100644 (file)
@@ -2,10 +2,48 @@
 
 namespace Cubist\Backpack\Media;
 
+use Cubist\Backpack\Magic\Fields\Files;
+use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
+use Cubist\Util\CommandLine\Rsync;
+use Illuminate\Support\Facades\DB;
+
 class Util
 {
-    public static function copyMedia($models, $connection)
+    /**
+     * @param $models CubistMagicAbstractModel[]
+     * @param $connection string
+     * @return void
+     */
+    public static function copyMedia($models, $old_db, $old_root, $new_root)
     {
 
+        $old_root = \Cubist\Util\Files\Files::mkdir($old_root);
+        $new_root = \Cubist\Util\Files\Files::mkdir($new_root);
+
+        foreach ($models as $model) {
+            $type = get_class($model);
+            $id = $model->id;
+            foreach ($model->getFields() as $field) {
+                if ($field instanceof Files) {
+                    $collection = $model->getAttribute($field->getName());
+                    $mediaCollection = $table = DB::table($old_db . '.media')->where('collection_name', $collection)
+                        ->where('model_id', $id)
+                        ->where('model_type', $type)
+                        ->get();
+
+                    foreach ($mediaCollection as $media) {
+                        $from = \Cubist\Util\Files\Files::mkdir($old_root . $media->id);
+
+                        $insert = (array)$media;
+                        unset($insert['id']);
+
+                        $newId = DB::table('media')->insertGetId($insert);
+                        $to = \Cubist\Util\Files\Files::mkdir($new_root . $newId);
+
+                        (new Rsync($from, $to))->execute();
+                    }
+                }
+            }
+        }
     }
 }