From a5fd1686d755bac2e32cec5d2f02b74bd782a5aa Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 6 Mar 2020 15:57:56 +0100 Subject: [PATCH] wip #3473 --- .../Controllers/CubistMagicController.php | 2 -- .../Magic/Models/CubistMagicAbstractModel.php | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 264a7e4..426787c 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -96,8 +96,6 @@ class CubistMagicController extends CubistCrudController */ $this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => 'number', 'label' => "#", 'searchLogic' => 'text']); - $this->getModelInstance()->setupButtons($this->crud); - $this->updateFieldsFromModel($model); } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 324fe91..5a6f1b1 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -604,6 +604,28 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $f->filterValue($value); } + /** + * @param string $fieldname + * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $filepath + * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist + * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist + * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig + */ + public function addMediaToField($fieldname, $filepath) + { + $collection = $this->getAttribute($fieldname); + $save = false; + if (!$collection) { + $collection = substr(hash('sha256', uniqid($fieldname, true)), 0, 10); + $this->setAttribute($fieldname, $collection); + $save = true; + } + $this->addMedia($filepath)->toMediaCollection($collection); + if ($save) { + $this->save(); + } + } + public function replicate(array $except = null) { -- 2.39.5