]> _ Git - cubist_cms-back.git/commitdiff
fix #3314 @0:50
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Jan 2020 16:31:35 +0000 (17:31 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 8 Jan 2020 16:31:35 +0000 (17:31 +0100)
src/app/Magic/Models/CubistMagicAbstractModel.php

index 4f1bc9e1dc5a8012c3b111030234c9be95d55937..70fca65373d15b7184f386cbe1efdb0832d127e9 100644 (file)
@@ -14,7 +14,6 @@ use Cubist\Util\Json;
 use Doctrine\DBAL\Schema\Schema;
 use Doctrine\DBAL\Schema\Table;
 use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Facades\App;
 use Illuminate\Support\Str;
 use Spatie\MediaLibrary\HasMedia\HasMedia;
 use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
@@ -465,6 +464,9 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     }
 
 
+    /**
+     * @return Media[]
+     */
     public function getAllMedia()
     {
         return Media::where('model_id', $this->getKey())->where('model_type', get_class($this))->get();
@@ -574,4 +576,30 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         }
         return $f->filterValue($value);
     }
+
+    public function replicate(array $except = null)
+    {
+
+        /** @var static $new */
+        $new = parent::replicate($except);
+        $new->push();
+
+        $newId = $new->getIdValue();
+
+        foreach ($this->getAllMedia() as $media) {
+            $cloned = $media->replicate();
+            $cloned->model_id = $newId;
+            $cloned->push();
+            // Copy files
+            $from = public_path('storage/' . $media->id);
+            $to = public_path('storage/' . $cloned->id);
+            if (file_exists($to)) {
+                `rm -rf $to`;
+            }
+            $cp = "cp -R $from $to";
+            `$cp`;
+        }
+
+        return $new;
+    }
 }