From b0b2813c582c7cb0a7db7308045c0d0e95a206c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Sun, 31 Dec 2023 15:55:16 +0100 Subject: [PATCH] overridable templates --- src/Fields/Relation.php | 8 ++++++- src/Http/Controllers/RelationController.php | 24 +++++++++++++-------- src/Http/Controllers/ResourceController.php | 20 ++++++++++------- src/Resources/Resource.php | 4 ++++ src/Traits/InteractsWithTurbo.php | 4 ++-- src/Widgets/Metric.php | 2 +- src/Widgets/Trend.php | 2 +- src/Widgets/Widget.php | 2 +- 8 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index c1025b67..12244078 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -497,7 +497,7 @@ public function paginate(Request $request, Model $model): LengthAwarePaginator ->with($this->with) ->withCount($this->withCount) ->latest() - ->paginate($request->input($this->getPerPageKey(), $this->isTurboRequest($request) ? 5 : $relation->getRelated()->getPerPage())) + ->paginate($request->input($this->getPerPageKey(), $this->isTurboFrameRequest($request) ? 5 : $relation->getRelated()->getPerPage())) ->withQueryString(); } @@ -564,6 +564,8 @@ public function registerRoutes(Request $request, Router $router): void { $this->__registerRoutes($request, $router); + // Gate::allowIf($field->authorized($request, $model)); + $router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void { $this->resolveActions($request)->registerRoutes($request, $router); @@ -660,6 +662,7 @@ public function toSubResource(Request $request, Model $model): array public function toIndex(Request $request, Model $model): array { return array_merge($this->toSubResource($request, $model), [ + 'template' => $this->isTurboFrameRequest($request) ? 'root::resources.relation' : 'root::resources.index', 'title' => $this->label, 'model' => $this->getRelation($model)->make(), 'modelName' => $this->getRelatedName(), @@ -691,6 +694,7 @@ public function toIndex(Request $request, Model $model): array public function toCreate(Request $request, Model $model): array { return array_merge($this->toSubResource($request, $model), [ + 'template' => 'root::resources.form', 'title' => __('Create :model', ['model' => $this->getRelatedName()]), 'model' => $related = $this->getRelation($model)->make(), 'action' => $this->modelUrl($model), @@ -709,6 +713,7 @@ public function toCreate(Request $request, Model $model): array public function toShow(Request $request, Model $model, Model $related): array { return array_merge($this->toSubResource($request, $model), [ + 'template' => 'root::resources.show', 'title' => $this->resolveDisplay($related), 'model' => $related, 'action' => $this->relatedUrl($model, $related), @@ -730,6 +735,7 @@ public function toShow(Request $request, Model $model, Model $related): array public function toEdit(Request $request, Model $model, Model $related): array { return array_merge($this->toSubResource($request, $model), [ + 'template' => 'root::resources.form', 'title' => __('Edit :model', ['model' => $this->resolveDisplay($related)]), 'model' => $related, 'action' => $this->relatedUrl($model, $related), diff --git a/src/Http/Controllers/RelationController.php b/src/Http/Controllers/RelationController.php index cfff4582..8351d5ae 100644 --- a/src/Http/Controllers/RelationController.php +++ b/src/Http/Controllers/RelationController.php @@ -20,11 +20,14 @@ public function index(Request $request, Model $model): Response { $field = $request->route('field'); - // Gate::allowIf($field->authorized($request, $model)); + // if ($field->getPolicy()) { + // $this->authorize('viewAny', $field->getModel()); + // } + + $data = $field->toIndex($request, $model); return ResponseFactory::view( - $request->hasHeader('Turbo-Frame') ? 'root::resources.relation' : 'root::resources.index', - $field->toIndex($request, $model) + $data['template'], $data ); } @@ -35,9 +38,10 @@ public function create(Request $request, Model $model): Response { $field = $request->route('field'); + $data = $field->toCreate($request, $model); + return ResponseFactory::view( - 'root::resources.form', - $field->toCreate($request, $model) + $data['template'], $data ); } @@ -63,9 +67,10 @@ public function show(Request $request, Model $model, Model $related): Response { $field = $request->route('field'); + $data = $field->toShow($request, $model, $related); + return ResponseFactory::view( - 'root::resources.show', - $field->toShow($request, $model, $related) + $data['template'], $data ); } @@ -76,9 +81,10 @@ public function edit(Request $request, Model $model, Model $related): Response { $field = $request->route('field'); + $data = $field->toEdit($request, $model, $related); + return ResponseFactory::view( - 'root::resources.form', - $field->toEdit($request, $model, $related) + $data['template'], $data ); } diff --git a/src/Http/Controllers/ResourceController.php b/src/Http/Controllers/ResourceController.php index c66097e2..18ce085c 100644 --- a/src/Http/Controllers/ResourceController.php +++ b/src/Http/Controllers/ResourceController.php @@ -32,9 +32,10 @@ public function index(Request $request, Resource $resource): Response $this->authorize('viewAny', $resource->getModel()); } + $data = $resource->toIndex($request); + return ResponseFactory::view( - 'root::resources.index', - $resource->toIndex($request) + $data['template'], $data ); } @@ -47,9 +48,10 @@ public function create(Request $request, Resource $resource): Response $this->authorize('create', $resource->getModel()); } + $data = $resource->toCreate($request); + return ResponseFactory::view( - 'root::resources.form', - $resource->toCreate($request) + $data['template'], $data ); } @@ -79,9 +81,10 @@ public function show(Request $request, Resource $resource, Model $model): Respon $this->authorize('view', $model); } + $data = $resource->toShow($request, $model); + return ResponseFactory::view( - 'root::resources.show', - $resource->toShow($request, $model) + $data['template'], $data ); } @@ -94,9 +97,10 @@ public function edit(Request $request, Resource $resource, Model $model): Respon $this->authorize('update', $model); } + $data = $resource->toEdit($request, $model); + return ResponseFactory::view( - 'root::resources.form', - $resource->toEdit($request, $model) + $data['template'], $data ); } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 63cf8cfa..50671f31 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -426,6 +426,7 @@ public function toArray(): array public function toIndex(Request $request): array { return array_merge($this->toArray(), [ + 'template' => 'root::resources.index', 'title' => $this->getName(), 'actions' => $this->resolveActions($request) ->authorized($request, $this->getModelInstance()) @@ -457,6 +458,7 @@ public function toIndex(Request $request): array public function toCreate(Request $request): array { return array_merge($this->toArray(), [ + 'template' => 'root::resources.form', 'title' => __('Create :resource', ['resource' => $this->getModelName()]), 'model' => $model = $this->getModelInstance(), 'action' => $this->getUri(), @@ -475,6 +477,7 @@ public function toCreate(Request $request): array public function toShow(Request $request, Model $model): array { return array_merge($this->toArray(), [ + 'template' => 'root::resources.show', 'title' => sprintf('%s: %s', $this->getModelName(), $this->modelTitle($model)), 'model' => $model, 'action' => $this->modelUrl($model), @@ -508,6 +511,7 @@ public function toShow(Request $request, Model $model): array public function toEdit(Request $request, Model $model): array { return array_merge($this->toArray(), [ + 'template' => 'root::resources.form', 'title' => __('Edit :resource: :model', ['resource' => $this->getModelName(), 'model' => $this->modelTitle($model)]), 'model' => $model, 'action' => $this->modelUrl($model), diff --git a/src/Traits/InteractsWithTurbo.php b/src/Traits/InteractsWithTurbo.php index 225a42f2..927a573d 100644 --- a/src/Traits/InteractsWithTurbo.php +++ b/src/Traits/InteractsWithTurbo.php @@ -7,9 +7,9 @@ trait InteractsWithTurbo { /** - * Determine if the request is Turbo request. + * Determine if the request is Turbo Frame request. */ - public function isTurboRequest(Request $request): bool + public function isTurboFrameRequest(Request $request): bool { return $request->hasHeader('Turbo-Frame'); } diff --git a/src/Widgets/Metric.php b/src/Widgets/Metric.php index 79b62082..785d2b25 100644 --- a/src/Widgets/Metric.php +++ b/src/Widgets/Metric.php @@ -129,7 +129,7 @@ public function data(Request $request): array { return array_merge(parent::data($request), [ 'ranges' => $this->ranges(), - 'data' => ! $this->async || $this->isTurboRequest($request) ? $this->calculate($request) : [], + 'data' => ! $this->async || $this->isTurboFrameRequest($request) ? $this->calculate($request) : [], ]); } diff --git a/src/Widgets/Trend.php b/src/Widgets/Trend.php index d6e4a0ec..ceb0c0dd 100644 --- a/src/Widgets/Trend.php +++ b/src/Widgets/Trend.php @@ -458,7 +458,7 @@ public function data(Request $request): array { return array_replace_recursive([ 'data' => [ - 'chart' => $this->isTurboRequest($request) ? $this->config : [], + 'chart' => $this->isTurboFrameRequest($request) ? $this->config : [], 'current' => null, ], ], parent::data($request)); diff --git a/src/Widgets/Widget.php b/src/Widgets/Widget.php index a2c18bd9..83bc5a69 100644 --- a/src/Widgets/Widget.php +++ b/src/Widgets/Widget.php @@ -71,7 +71,7 @@ public function getUriKey(): string public function data(Request $request): array { return array_merge($this->toArray(), [ - 'isTurbo' => $this->isTurboRequest($request), + 'isTurbo' => $this->isTurboFrameRequest($request), ]); }