From 3d75e4057453c859966daee912fe2bcf06d79cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Tue, 7 Nov 2023 20:19:47 +0100 Subject: [PATCH] wip --- resources/views/fields/file-option.blade.php | 4 ++-- src/Actions/Action.php | 6 +++--- src/Actions/Actions.php | 6 +++--- src/Fields/File.php | 11 ++++++++++- src/Models/Medium.php | 2 +- src/Resources/Resource.php | 12 ++++++++++-- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/resources/views/fields/file-option.blade.php b/resources/views/fields/file-option.blade.php index 7766394c..c93b227f 100644 --- a/resources/views/fields/file-option.blade.php +++ b/resources/views/fields/file-option.blade.php @@ -1,13 +1,13 @@
@if($isImage) - {{ $label }} + {{ $fileName }} @else @endif - {{ $label }} + {{ $fileName }}
diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 42066138..529b8aad 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Concerns\HasAttributes; +use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Redirect; @@ -213,12 +214,11 @@ public function toArray(): array /** * {@inheritdoc} */ - public function toForm(Request $request): array + public function toForm(Request $request, Model $model): array { return array_merge($this->toArray(), [ 'open' => $this->errors($request)->isNotEmpty(), - 'fields' => $this->resolveFields($request) - ->mapToInputs($request, $this->query->getModel()), + 'fields' => $this->resolveFields($request)->mapToInputs($request, $model), ]); } } diff --git a/src/Actions/Actions.php b/src/Actions/Actions.php index 37f8bb35..c74b1ac6 100644 --- a/src/Actions/Actions.php +++ b/src/Actions/Actions.php @@ -40,11 +40,11 @@ public function visible(string|array $context): static } /** - * Map the action to table components. + * Map the action to forms. */ - public function mapToForms(Request $request): array + public function mapToForms(Request $request, Model $model): array { - return $this->map->toForm($request)->all(); + return $this->map->toForm($request, $model)->all(); } /** diff --git a/src/Fields/File.php b/src/Fields/File.php index c6713fb9..df5dc102 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -36,6 +36,11 @@ class File extends MorphToMany */ protected string $disk; + /** + * The displayable conversion name. + */ + protected ?string $displayConversion = 'original'; + /** * Create a new field instance. */ @@ -94,7 +99,11 @@ public function collection(string $value): static public function resolveDisplay(Model $related): mixed { if (is_null($this->displayResolver)) { - $this->display('file_name'); + $this->display(function (Medium $related): string { + return $related->isImage + ? sprintf('', $related->getUrl($this->displayConversion)) + : sprintf('%s', $related->getUrl(), $related->file_name); + }); } return parent::resolveDisplay($related); diff --git a/src/Models/Medium.php b/src/Models/Medium.php index 096014fc..a324a7ca 100644 --- a/src/Models/Medium.php +++ b/src/Models/Medium.php @@ -239,7 +239,7 @@ public function getPath(string $conversion = null, bool $absolute = false): stri { $path = sprintf('%s/%s', $this->uuid, $this->file_name); - if (! is_null($conversion)) { + if (! is_null($conversion) && $conversion !== 'original') { $path = substr_replace( $path, "-{$conversion}", -(mb_strlen(Str::afterLast($path, '.')) + 1), -mb_strlen("-{$conversion}") ); diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 6c051498..0b518a18 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -334,9 +334,9 @@ public function toIndex(Request $request): array return array_merge($this->toArray(), [ 'title' => $this->getName(), 'actions' => $this->resolveActions($request) - ->authorized($request) + ->authorized($request, $this->getModelInstance()) ->visible('index') - ->mapToForms($request), + ->mapToForms($request, $this->getModelInstance()), 'data' => $this->paginate($request), 'widgets' => $this->resolveWidgets($request) ->authorized($request) @@ -385,6 +385,14 @@ public function toShow(Request $request, Model $model): array ->authorized($request, $model) ->visible('show') ->mapToDisplay($request, $model), + 'actions' => $this->resolveActions($request) + ->authorized($request, $model) + ->visible('show') + ->mapToForms($request, $model), + 'widgets' => $this->resolveWidgets($request) + ->authorized($request, $model) + ->visible('show') + ->toArray(), ]); }