generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add : Json Media Column for filament table
- Loading branch information
1 parent
f0e4e84
commit cfd321d
Showing
9 changed files
with
368 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="flex max-w-max"> | ||
<figure class="" style="width: {{ $media->width }}px"> | ||
<img class="object-cover w-full aspect-video" loading="lazy" | ||
src="{{ $media->getCropUrl(width: $media->width,height: $media->height) }}" | ||
alt="{{ $media->getCustomProperty('alt') }}" | ||
width="{{ $media->width }}" | ||
height="{{ $media->height }}" | ||
> | ||
</figure> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@php | ||
use WebplusMultimedia\GalleryJsonMedia\JsonMedia\Contracts\HasMedia; | ||
/** @var HasMedia $record */ | ||
$record = $getRecord(); | ||
@endphp | ||
<div style="width: {{ $getThumbWidth() }}px"> | ||
@if(($record)) | ||
{{ $record->getFirstMedia($getName())?->withImageProperties($getThumbWidth(),$getThumbHeight()) }} | ||
@endif | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace WebplusMultimedia\GalleryJsonMedia\JsonMedia\Concerns; | ||
|
||
use Bkwld\Croppa\Facades\Croppa; | ||
use WebplusMultimedia\GalleryJsonMedia\JsonMedia\Contracts\CanDeleteMedia; | ||
use WebplusMultimedia\GalleryJsonMedia\JsonMedia\Contracts\HasMedia; | ||
use WebplusMultimedia\GalleryJsonMedia\JsonMedia\Document; | ||
use WebplusMultimedia\GalleryJsonMedia\JsonMedia\Media; | ||
|
||
/** | ||
* @method static deleting(\Closure $param) | ||
*/ | ||
trait InteractWithMedias | ||
{ | ||
protected static function InteractWithMedia(): void | ||
{ | ||
static::deleting(function (HasMedia $model) { | ||
foreach ($model->getFieldsToDeleteMedia() as $field) { | ||
$model->deleteFilesFrom($field); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return Media[] | ||
*/ | ||
public function getMedias(string $fieldName): array | ||
{ | ||
$medias = []; | ||
if (is_null($this->{$fieldName})) { | ||
return $medias; | ||
} | ||
foreach ($this->{$fieldName} as $image) { | ||
if (Media::isImage(data_get($image, 'mime_type'))) { | ||
$medias[] = Media::make($image); | ||
} | ||
} | ||
|
||
return $medias; | ||
} | ||
|
||
/** | ||
* @return Media[] | ||
*/ | ||
public function getMediasWithoutFirst(string $fieldName): array | ||
{ | ||
$medias = $this->getMedias($fieldName); | ||
array_shift($medias); | ||
|
||
return $medias; | ||
} | ||
|
||
/** | ||
* @return array<int,Document> | ||
*/ | ||
public function getDocuments(string $fieldName): array | ||
{ | ||
$documents = []; | ||
if (is_null($this->{$fieldName})) { | ||
return []; | ||
} | ||
foreach ($this->{$fieldName} as $document) { | ||
if (! Media::isImage(data_get($document, 'mime_type'))) { | ||
$documents[] = Document::make($document); | ||
} | ||
} | ||
|
||
return $documents; | ||
} | ||
|
||
public function getFirstMedia(string $fieldName): ?Media | ||
{ | ||
return collect($this->getMedias($fieldName))->first(); | ||
} | ||
|
||
public function getFirstMediaUrl(string $fieldName): ?string | ||
{ | ||
if ($media = $this->getFirstMedia($fieldName)) { | ||
return $media->getUrl(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getFirstMediaCropUrl(string $fieldName, ?int $width = null, ?int $height = null, ?array $options = null): ?string | ||
{ | ||
if (! $firstMedia = $this->getFirstMediaUrl($fieldName)) { | ||
return null; | ||
} | ||
|
||
return url(Croppa::url($firstMedia, $width, $height, $options)); | ||
} | ||
|
||
protected function getFieldsToDeleteMedia(): array | ||
{ | ||
return []; | ||
} | ||
|
||
protected function deleteFilesFrom(string $field): void | ||
{ | ||
/** @var CanDeleteMedia[] $medias */ | ||
$medias = array_merge($this->getMedias($field), $this->getDocuments($field)); | ||
foreach ($medias as $media) { | ||
$media->delete(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* | ||
* @category Category | ||
* | ||
* @author daniel | ||
* | ||
* @link http://webplusm.net | ||
* Date: 21/02/2024 12:37 | ||
*/ | ||
|
||
namespace WebplusMultimedia\GalleryJsonMedia\JsonMedia\Contracts; | ||
|
||
interface CanDeleteMedia | ||
{ | ||
public function delete(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace WebplusMultimedia\GalleryJsonMedia\JsonMedia\Contracts; | ||
|
||
use WebplusMultimedia\GalleryJsonMedia\JsonMedia\Media; | ||
|
||
/** | ||
* @method void deleteFilesFrom(string $field) | ||
* @method array<int,string> getFieldsToDeleteMedia() | ||
*/ | ||
interface HasMedia | ||
{ | ||
public function getMedias(string $fieldName): array; | ||
|
||
public function getFirstMedia(string $fieldName): ?Media; | ||
|
||
public function getMediasWithoutFirst(string $fieldName): array; | ||
|
||
public function getFirstMediaUrl(string $fieldName): ?string; | ||
|
||
public function getFirstMediaCropUrl(string $fieldName, ?int $width = null, ?int $height = null, ?array $options = null): ?string; | ||
|
||
public function getDocuments(string $fieldName): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace WebplusMultimedia\GalleryJsonMedia\JsonMedia; | ||
|
||
use Bkwld\Croppa\Facades\Croppa; | ||
use Illuminate\Support\Facades\Storage; | ||
use Stringable; | ||
use WebplusMultimedia\GalleryJsonMedia\JsonMedia\Contracts\CanDeleteMedia; | ||
|
||
class Document implements CanDeleteMedia, Stringable | ||
{ | ||
public function __construct( | ||
protected array $content, | ||
) { | ||
|
||
} | ||
|
||
public static function make(array $content): Document | ||
{ | ||
return new self($content); | ||
} | ||
|
||
public function getUrl(): ?string | ||
{ | ||
if ($fileName = $this->getContentKeyValue('file')) { | ||
$disk = Storage::disk($this->getContentKeyValue('disk')); | ||
if ($disk->exists($fileName)) { | ||
return $disk->url($fileName); | ||
} | ||
|
||
} | ||
|
||
return null; | ||
} | ||
|
||
private function getContentKeyValue(string $key): mixed | ||
{ | ||
return data_get($this->content, $key); | ||
} | ||
|
||
public function getCustomProperty(string $property): mixed | ||
{ | ||
return $this->getContentKeyValue('customProperties.' . $property); | ||
} | ||
|
||
public function delete(): void | ||
{ | ||
if ($this->getUrl()) { | ||
Croppa::delete($this->getUrl()); | ||
} | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->getUrl() ?? ''; | ||
} | ||
} |
Oops, something went wrong.