Skip to content

Commit

Permalink
add : Json Media Column for filament table
Browse files Browse the repository at this point in the history
  • Loading branch information
webplusmultimedia committed Feb 26, 2024
1 parent f0e4e84 commit cfd321d
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resources/dist/gallery-json-media.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions resources/views/media.blade.php
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>

10 changes: 10 additions & 0 deletions resources/views/table/gallery-column.blade.php
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>
109 changes: 109 additions & 0 deletions src/JsonMedia/Concerns/InteractWithMedias.php
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();
}

}
}
18 changes: 18 additions & 0 deletions src/JsonMedia/Contracts/CanDeleteMedia.php
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;
}
24 changes: 24 additions & 0 deletions src/JsonMedia/Contracts/HasMedia.php
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;
}
57 changes: 57 additions & 0 deletions src/JsonMedia/Document.php
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() ?? '';
}
}
Loading

0 comments on commit cfd321d

Please sign in to comment.