wip
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Infolists\Components;
|
||||
|
||||
use Closure;
|
||||
use Filament\Infolists\Components\Entry;
|
||||
use Filament\Support\Concerns\HasMediaFilter;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
use Throwable;
|
||||
|
||||
class SpatieMediaLibraryFileEntry extends Entry
|
||||
{
|
||||
use HasMediaFilter;
|
||||
|
||||
protected string $view = 'filament.infolists.components.spatie-media-library-file-entry';
|
||||
|
||||
protected string | Closure | null $collection = null;
|
||||
|
||||
protected string | Closure | null $conversion = null;
|
||||
|
||||
protected string | Closure $visibility = 'private';
|
||||
|
||||
protected bool | Closure $isDownloadable = true;
|
||||
|
||||
protected bool | Closure $isPreviewable = true;
|
||||
|
||||
public function collection(string | Closure | null $collection): static
|
||||
{
|
||||
$this->collection = $collection;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function conversion(string | Closure | null $conversion): static
|
||||
{
|
||||
$this->conversion = $conversion;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function visibility(string | Closure $visibility): static
|
||||
{
|
||||
$this->visibility = $visibility;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function downloadable(bool | Closure $condition = true): static
|
||||
{
|
||||
$this->isDownloadable = $condition;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function previewable(bool | Closure $condition = true): static
|
||||
{
|
||||
$this->isPreviewable = $condition;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCollection(): ?string
|
||||
{
|
||||
return $this->evaluate($this->collection);
|
||||
}
|
||||
|
||||
public function getConversion(): ?string
|
||||
{
|
||||
return $this->evaluate($this->conversion);
|
||||
}
|
||||
|
||||
public function getVisibility(): string
|
||||
{
|
||||
return $this->evaluate($this->visibility);
|
||||
}
|
||||
|
||||
public function isDownloadable(): bool
|
||||
{
|
||||
return (bool) $this->evaluate($this->isDownloadable);
|
||||
}
|
||||
|
||||
public function isPreviewable(): bool
|
||||
{
|
||||
return (bool) $this->evaluate($this->isPreviewable);
|
||||
}
|
||||
|
||||
public function getMediaUrl(Media $media, ?string $conversion = null): string
|
||||
{
|
||||
if ($this->getVisibility() === 'private') {
|
||||
try {
|
||||
return $media->getTemporaryUrl(
|
||||
now()->addMinutes(30)->endOfHour(),
|
||||
$conversion ?? '',
|
||||
);
|
||||
} catch (Throwable $exception) {
|
||||
// This driver does not support creating temporary URLs.
|
||||
}
|
||||
}
|
||||
|
||||
return $media->getUrl($conversion ?? '');
|
||||
}
|
||||
|
||||
public function getMedia(): Collection
|
||||
{
|
||||
$record = $this->getRecord();
|
||||
|
||||
if (! $record) {
|
||||
return collect([]);
|
||||
}
|
||||
|
||||
if ($this->hasStateRelationship($record)) {
|
||||
$record = $this->getStateRelationshipResults($record);
|
||||
}
|
||||
|
||||
$records = Arr::wrap($record);
|
||||
|
||||
$allMedia = collect([]);
|
||||
|
||||
$collection = $this->getCollection() ?? 'default';
|
||||
|
||||
foreach ($records as $record) {
|
||||
/** @var Model $record */
|
||||
$media = $record->getRelationValue('media');
|
||||
|
||||
if (! $media) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$filteredMedia = $media
|
||||
->when(
|
||||
is_string($collection),
|
||||
fn (MediaCollection $mediaCollection) => $mediaCollection->filter(fn (Media $media): bool => $media->getAttributeValue('collection_name') === $collection),
|
||||
)
|
||||
->when(
|
||||
$this->hasMediaFilter(),
|
||||
fn (Collection $media) => $this->filterMedia($media)
|
||||
)
|
||||
->sortBy('order_column');
|
||||
|
||||
$allMedia = $allMedia->merge($filteredMedia);
|
||||
}
|
||||
|
||||
return $allMedia;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user