From 52419b32894b01bad57d2c449b4748e068faa956 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 8 Jan 2020 17:31:35 +0100 Subject: [PATCH] fix #3314 @0:50 --- .../Magic/Models/CubistMagicAbstractModel.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 4f1bc9e..70fca65 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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; + } } -- 2.39.5