Skip to content

Commit

Permalink
Merge pull request #2 from lee-to/fix-problems
Browse files Browse the repository at this point in the history
Fix problems
  • Loading branch information
alexvenga authored May 21, 2023
2 parents 327f588 + 2e3251e commit 58a0f39
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
]
},
"conflict": {
"moonshine/moonshine": "<1.52"
"moonshine/moonshine": "<1.57"
},
"extra": {
"laravel": {
Expand Down
87 changes: 55 additions & 32 deletions src/Fields/MediaLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,65 @@
namespace VI\MoonShineSpatieMediaLibrary\Fields;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use MoonShine\Fields\Image;

class MediaLibrary extends Image
{
protected bool $isDeleteFiles = false;

public static string $view = 'moonshine::fields.image';
protected array $wasRecentlyCreated = [];

public function save(Model $item): Model
public function hasManyOrOneSave($hiddenKey, array $values = [], Model $item = null): array
{
return $item;
}
$this->storeMedia(
$item,
$values[$this->field()] ?? null,
request()->collect($hiddenKey)->reject(fn($v) => is_numeric($v))
);

public function hasManyOrOneSave($hiddenKey, array $values = []): array
{
return $values;
}

public function afterSave(Model $item): void
{
if ($this->isCanSave()) {
$requestValue = $this->requestValue();

if ($this->isMultiple()) {
$oldValues = collect(request("hidden_{$this->field()}", []));

if ($oldValues->count() < $item->getMedia($this->field())->count()) {
foreach ($item->getMedia($this->field()) as $media) {
if (!$oldValues->contains($media->getUrl())) {
$media->delete();
}
}
}
if ($requestValue) {
foreach ($requestValue as $file) {
$item->addMedia($file)
->preservingOriginal()
->toMediaCollection($this->field());
}
}
} else {
if ($requestValue) {
$item->addMedia($requestValue)
->preservingOriginal()
->toMediaCollection($this->field());
}
$this->storeMedia(
$item,
$this->requestValue() !== false ? $this->requestValue() : null,
request()->collect("hidden_{$this->field()}")
);
}

public function storeMedia($item, array|UploadedFile|null $requestValue, Collection $oldValues): void
{
if ($requestValue) {
if (!$this->isMultiple()) {
$requestValue = [$requestValue];
}

foreach ($requestValue as $file) {
$this->addMedia($item, $file);
}
}

$this->removeOldMedia($item, $oldValues);
}

private function addMedia(Model $item, UploadedFile $file)
{
$media = $item->addMedia($file)
->preservingOriginal()
->toMediaCollection($this->field());

$this->wasRecentlyCreated[$media->getUrl()] = $media->getUrl();
}

private function removeOldMedia(Model $item, Collection $oldValues): void
{
foreach ($item->getMedia($this->field()) as $media) {
if (!isset($this->wasRecentlyCreated[$media->getUrl()]) && !$oldValues->contains($media->getUrl())) {
$media->delete();
}
}
}
Expand Down Expand Up @@ -89,4 +102,14 @@ public function path(string $value): string
{
return '';
}

public function getDir(): string
{
return '';
}

public function save(Model $item): Model
{
return $item;
}
}

0 comments on commit 58a0f39

Please sign in to comment.