Skip to content

Commit

Permalink
INIT
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Apr 8, 2023
1 parent 5f2ff0b commit 99d6a85
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
18 changes: 3 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
}
],
"require": {
"php": "^8.0|^8.1|^8.2",
"lee-to/moonshine": "@dev",
"laravel/framework": "^9.0|^10.0"
"php": "^8.0|^8.1|^8.2"
},
"autoload": {
"psr-4": {
"VI\\MoonShine\\Fields\\Spatie\\MediaLibrary\\": "src"
"VI\\MoonShineSpatieMediaLibrary\\": "src"
},
"files": [
]
Expand All @@ -46,15 +44,5 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true,

"repositories": [
{
"type": "path",
"url": "/usr/local/var/www/visual-ideas/moonshine",
"options": {
"symlink": true
}
}
]
"prefer-stable": true
}
50 changes: 28 additions & 22 deletions src/MediaLibrary.php → src/Fields/MediaLibrary.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
<?php

namespace VI\MoonShine\Fields\Spatie\MediaLibrary;
namespace VI\MoonShineSpatieMediaLibrary\Fields;

use Illuminate\Database\Eloquent\Model;
use Leeto\MoonShine\Fields\Field;
use Leeto\MoonShine\Traits\Fields\CanBeMultiple;
use Illuminate\Support\Collection;
use Leeto\MoonShine\Fields\Image;

class MediaLibrary extends Field
class MediaLibrary extends Image
{
use CanBeMultiple;

public static string $view = 'moonshine::fields.image';

public static string $type = 'file';

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

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

foreach ($this->requestValue() as $file) {
$item->addMedia($file)
->preservingOriginal()
->toMediaCollection($this->field());
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 ($media = $item->getFirstMedia($this->field())) {
$media->delete();
if ($requestValue) {
$item->addMedia($requestValue)
->preservingOriginal()
->toMediaCollection($this->field());
}

$item->addMedia($this->requestValue())
->preservingOriginal()
->toMediaCollection($this->field());
}
}
}
Expand All @@ -63,11 +69,11 @@ public function indexViewValue(Model $item, bool $container = true): string
])->render();
}

public function formViewValue(Model $item): mixed
public function formViewValue(Model $item): Collection|string
{
if ($this->isMultiple()) {
return $item->getMedia($this->field())
->map(fn ($value) => $value->getUrl())->toArray();
->map(fn($value) => $value->getUrl());
}

return $item->getFirstMediaUrl($this->field());
Expand Down

0 comments on commit 99d6a85

Please sign in to comment.