- Cleaned up imports and improved code formatting for better readability. - Adjusted layout in VisaMasterPaymentOrderInfolist schema for consistent column spans. - Enhanced SpatieMediaLibraryFileEntry component by streamlining property definitions and ensuring consistent formatting.
170 lines
4.4 KiB
PHP
170 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Infolists\Components;
|
|
|
|
use Closure;
|
|
use Filament\Actions\Action;
|
|
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;
|
|
|
|
public function getViewMediaActionName(): string
|
|
{
|
|
return 'view_media_'.$this->getName();
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->registerActions([
|
|
Action::make($this->getViewMediaActionName())
|
|
->label(__('Watch Full'))
|
|
->modalContent(fn (array $arguments) => view('filament.infolists.components.image-modal', ['url' => $arguments['url']]))
|
|
->modalSubmitAction(false)
|
|
->modalCancelAction(false)
|
|
->modalWidth('5xl'),
|
|
]);
|
|
}
|
|
|
|
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 (string) $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;
|
|
}
|
|
}
|