From 1f3aae864732357882ffe81421c9048586f64284 Mon Sep 17 00:00:00 2001 From: GeorgeFourkas <66795055+GeorgeFourkas@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:17:25 +0300 Subject: [PATCH 1/7] Fixes #529 --- config/curator.php | 2 +- src/Components/Forms/CuratorPicker.php | 1 + src/Components/Tables/CuratorColumn.php | 5 +++-- src/helpers.php | 30 ++++++++----------------- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/config/curator.php b/config/curator.php index 8ddf001..b84c18b 100644 --- a/config/curator.php +++ b/config/curator.php @@ -30,10 +30,10 @@ 'fallbacks' => [], 'route_path' => 'curator', ], + 'intermediate_model' => null, 'image_crop_aspect_ratio' => null, 'image_resize_mode' => null, 'image_resize_target_height' => null, - 'image_resize_target_width' => null, 'is_limited_to_directory' => false, 'is_tenant_aware' => true, 'tenant_ownership_relationship_name' => 'tenant', diff --git a/src/Components/Forms/CuratorPicker.php b/src/Components/Forms/CuratorPicker.php index f427021..1b92eb2 100644 --- a/src/Components/Forms/CuratorPicker.php +++ b/src/Components/Forms/CuratorPicker.php @@ -549,6 +549,7 @@ public function relationship(string | Closure $relationshipName, string | Closur return; } + if ($component->isMultiple()) { if ($relationship instanceof BelongsToMany) { $orderColumn = $component->getOrderColumn(); diff --git a/src/Components/Tables/CuratorColumn.php b/src/Components/Tables/CuratorColumn.php index 6c98add..db80788 100644 --- a/src/Components/Tables/CuratorColumn.php +++ b/src/Components/Tables/CuratorColumn.php @@ -25,11 +25,12 @@ public function getMedia(): Media | Collection | array | null if (! is_a($record, Media::class)) { $state = $this->getState(); + if (is_a($state, Collection::class)) { $state = $state->take($this->limit); - if (! is_null($state) && is_related_to_media_through_pivot(get_class(($state->first())), Media::class)) { - $mediaIds = collect($state)->map(fn ($model) => $model?->media_id)->toArray(); + if (! is_null($state) && is_related_to_media_through_pivot($state->first())) { + $mediaIds = $state->map(fn ($model) => $model?->media_id)->toArray(); return Media::whereIn('id', $mediaIds)->get(); } diff --git a/src/helpers.php b/src/helpers.php index e3a3741..e1146d7 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,7 +4,6 @@ use Awcodes\Curator\Models\Media; use Illuminate\Database\Eloquent\Collection; -use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Facades\Request; use Illuminate\Support\Str; @@ -18,14 +17,17 @@ function is_media_resizable(string $ext): bool if (! function_exists('get_media_items')) { function get_media_items(array | Media | int $ids): Collection | array { - if ($ids instanceof Media) { + $mediaModel = config('curator.model'); + + if ($ids instanceof $mediaModel) { return [$ids]; } - if (is_array($ids) && is_related_to_media_through_pivot(get_class(current($ids)), Media::class)) { + + if (is_array($ids) && is_related_to_media_through_pivot(current($ids))) { $mediaIds = collect($ids)->map(fn ($model) => $model?->media_id)->toArray(); - return Media::whereIn('id', $mediaIds)->get(); + return config('curator.model')::whereIn('id', $mediaIds)->get(); } $ids = array_values($ids); @@ -47,25 +49,11 @@ function get_media_items(array | Media | int $ids): Collection | array } if (! function_exists('is_related_to_media_through_pivot')) { - function is_related_to_media_through_pivot(string $modelClass, string $relatedClass): bool + function is_related_to_media_through_pivot(mixed $type): bool { - $model = new $modelClass; - $reflector = new \ReflectionClass($model); - $methods = $reflector->getMethods(\ReflectionMethod::IS_PUBLIC); - foreach ($methods ?? [] as $method) { - if ($method?->class === $modelClass) { - $returnType = $method?->getReturnType(); - if ($returnType) { - $relationInstance = $model->{$method->getName()}(); - if ($relationInstance instanceof BelongsTo && - get_class($relationInstance->getRelated()) === $relatedClass) { - return true; - } - } - } - } + $intermediateModelType = config('curator.intermediate_model'); - return false; + return (! is_null(config('curator.intermediate_model'))) && $type instanceof $intermediateModelType; } } From 826a37b1f3075d688b2d0e0a1beac7bad0aad94a Mon Sep 17 00:00:00 2001 From: GeorgeFourkas Date: Fri, 30 Aug 2024 18:25:19 +0000 Subject: [PATCH 2/7] Format Code --- src/Components/Forms/CuratorPicker.php | 1 - src/helpers.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/Components/Forms/CuratorPicker.php b/src/Components/Forms/CuratorPicker.php index b09b4c2..57821f1 100644 --- a/src/Components/Forms/CuratorPicker.php +++ b/src/Components/Forms/CuratorPicker.php @@ -502,7 +502,6 @@ public function relationship(string | Closure $relationshipName, string | Closur return; } - if ($component->isMultiple()) { if ($relationship instanceof BelongsToMany) { $orderColumn = $component->getOrderColumn(); diff --git a/src/helpers.php b/src/helpers.php index baeb1b7..dc9fb64 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,7 +4,6 @@ use Awcodes\Curator\Models\Media; use Illuminate\Database\Eloquent\Collection; -use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Facades\Request; use Illuminate\Support\Str; @@ -24,7 +23,6 @@ function get_media_items(array | Media | int $ids): Collection | array return [$ids]; } - if (is_array($ids) && is_related_to_media_through_pivot(current($ids))) { $mediaIds = collect($ids)->map(fn ($model) => $model?->media_id)->toArray(); From fbd7e59e93894965e1297c0c0f692b5b71c4c57a Mon Sep 17 00:00:00 2001 From: GeorgeFourkas <66795055+GeorgeFourkas@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:28:52 +0300 Subject: [PATCH 3/7] merge --- src/Components/Forms/CuratorPicker.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Components/Forms/CuratorPicker.php b/src/Components/Forms/CuratorPicker.php index 1b92eb2..479abe6 100644 --- a/src/Components/Forms/CuratorPicker.php +++ b/src/Components/Forms/CuratorPicker.php @@ -436,6 +436,7 @@ public function orderColumn(string $column): static return $this; } + public function typeColumn(string $column): static { $this->typeColumn = $column; From 9a2efbc989fa4673364576250adf1692b31946ef Mon Sep 17 00:00:00 2001 From: GeorgeFourkas Date: Fri, 30 Aug 2024 18:29:33 +0000 Subject: [PATCH 4/7] Format Code --- src/Components/Forms/CuratorPicker.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Components/Forms/CuratorPicker.php b/src/Components/Forms/CuratorPicker.php index 105ad81..57821f1 100644 --- a/src/Components/Forms/CuratorPicker.php +++ b/src/Components/Forms/CuratorPicker.php @@ -428,7 +428,6 @@ public function orderColumn(string $column): static return $this; } - public function typeColumn(string $column): static { $this->typeColumn = $column; From c2888ceba9dca5bcc08d5057ecb8cd2b539c46b3 Mon Sep 17 00:00:00 2001 From: GeorgeFourkas <66795055+GeorgeFourkas@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:31:58 +0300 Subject: [PATCH 5/7] Fixes #529 --- src/Components/Forms/CuratorPicker.php | 100 +++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 6 deletions(-) diff --git a/src/Components/Forms/CuratorPicker.php b/src/Components/Forms/CuratorPicker.php index 57821f1..b056c21 100644 --- a/src/Components/Forms/CuratorPicker.php +++ b/src/Components/Forms/CuratorPicker.php @@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Storage; @@ -54,6 +55,8 @@ class CuratorPicker extends Field protected ?string $orderColumn = null; + protected ?string $collection = null; + protected ?string $typeColumn = null; protected ?string $typeValue = null; @@ -194,6 +197,11 @@ public function getTypeColumn(): string return $this->typeColumn ?? 'type'; } + public function getCollection() + { + return $this->collection ?? null; + } + public function getTypeValue(): ?string { return $this->typeValue ?? null; @@ -435,6 +443,13 @@ public function typeColumn(string $column): static return $this; } + public function collection(string $collection) + { + $this->collection = $collection; + + return $this; + } + public function typeValue(string $value): static { $this->typeValue = $value; @@ -458,11 +473,18 @@ public function relationship(string | Closure $relationshipName, string | Closur if ($relationship instanceof MorphMany) { $typeColumn = $component->getTypeColumn(); $typeValue = $component->getTypeValue(); + $collection = $component->getCollection(); $query = $relationship->with('media'); if ($typeColumn && $typeValue) { $query->where($typeColumn, $typeValue); } + + if ($collection) { + + $query->where('collection', $collection); + } + $relatedMediaItems = $query->get(); $relatedMedia = $relatedMediaItems->map(function ($item) { @@ -480,6 +502,32 @@ public function relationship(string | Closure $relationshipName, string | Closur return; } + if ($relationship instanceof MorphOne) { + + $typeColumn = $component->getTypeColumn(); + $typeValue = $component->getTypeValue(); + $collection = $component->getCollection(); + + $query = $relationship->with('media'); + if ($typeColumn && $typeValue) { + $query->where($typeColumn, $typeValue); + } + + if ($collection) { + $query->where('collection', $collection); + } + + $relatedMediaItems = $query->get(); + + $relatedMedia = $relatedMediaItems->map(function ($item) { + return $item->media->toArray(); + })->toArray(); + + $component->state($relatedMedia); + + return; + } + /** @var BelongsTo $relationship */ $relatedModel = $relationship->getResults(); @@ -502,6 +550,7 @@ public function relationship(string | Closure $relationshipName, string | Closur return; } + if ($component->isMultiple()) { if ($relationship instanceof BelongsToMany) { $orderColumn = $component->getOrderColumn(); @@ -525,10 +574,11 @@ public function relationship(string | Closure $relationshipName, string | Closur $orderColumn = $component->getOrderColumn(); $typeColumn = $component->getTypeColumn(); $typeValue = $component->getTypeValue(); - $existingItems = $component->getRelationship()->where($typeColumn, $typeValue)->get()->keyBy('media_id')->toArray(); + $collection = $component->getCollection(); + $existingItems = $relationship->where($typeColumn, $typeValue)->get()->keyBy('media_id')->toArray(); $newIds = collect($state)->pluck('id')->toArray(); - $component->getRelationship()->whereNotIn('media_id', $newIds) + $relationship->whereNotIn('media_id', $newIds) ->where($typeColumn, $typeValue) ->delete(); @@ -538,15 +588,17 @@ public function relationship(string | Closure $relationshipName, string | Closur $data = [ 'media_id' => $itemId, $orderColumn => $i, + 'collection' => $collection, ]; if ($typeValue) { $data[$typeColumn] = $typeValue; } + if ($collection) { + $data['collection'] = $collection; + } + if (isset($existingItems[$itemId])) { - $component->getRelationship() - ->where('media_id', $itemId) - ->where($typeColumn, $typeValue) - ->update($data); + $relationship->where('media_id', $itemId)->update($data); } else { $relationship->create($data); } @@ -557,6 +609,42 @@ public function relationship(string | Closure $relationshipName, string | Closur } } + if ($relationship instanceof MorphOne) { + $orderColumn = $component->getOrderColumn(); + $typeColumn = $component->getTypeColumn(); + $typeValue = $component->getTypeValue(); + $collection = $component->getCollection(); + $existingItems = $relationship->where($typeColumn, $typeValue)->get()->keyBy('media_id')->toArray(); + $newIds = collect($state)->pluck('id')->toArray(); + + $relationship->whereNotIn('media_id', $newIds) + ->where($typeColumn, $typeValue) + ->delete(); + + $i = count($existingItems) + 1; + + foreach ($state as $item) { + $itemId = $item['id']; + $data = [ + 'media_id' => $itemId, + 'collection' => $collection, + $orderColumn => $i, + ]; + if ($typeValue) { + $data[$typeColumn] = $typeValue; + } + + if (isset($existingItems[$itemId])) { + $relationship->where('media_id', $itemId)->update($data); + } else { + $relationship->create($data); + } + $i++; + } + + return; + } + if (blank($state) && $relationship->exists()) { $relationship->disassociate(); From b116bec7896c90d9f8f60934685698733fab1667 Mon Sep 17 00:00:00 2001 From: GeorgeFourkas Date: Fri, 30 Aug 2024 18:37:06 +0000 Subject: [PATCH 6/7] Format Code --- src/Components/Forms/CuratorPicker.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Components/Forms/CuratorPicker.php b/src/Components/Forms/CuratorPicker.php index b056c21..fe80889 100644 --- a/src/Components/Forms/CuratorPicker.php +++ b/src/Components/Forms/CuratorPicker.php @@ -550,7 +550,6 @@ public function relationship(string | Closure $relationshipName, string | Closur return; } - if ($component->isMultiple()) { if ($relationship instanceof BelongsToMany) { $orderColumn = $component->getOrderColumn(); From 57f001647a73d719868666d3288bcdf3710b26a1 Mon Sep 17 00:00:00 2001 From: GeorgeFourkas <66795055+GeorgeFourkas@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:38:16 +0300 Subject: [PATCH 7/7] fixes #529 --- src/Components/Forms/CuratorPicker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Forms/CuratorPicker.php b/src/Components/Forms/CuratorPicker.php index fe80889..584c97b 100644 --- a/src/Components/Forms/CuratorPicker.php +++ b/src/Components/Forms/CuratorPicker.php @@ -207,7 +207,7 @@ public function getTypeValue(): ?string return $this->typeValue ?? null; } - public function getRelationship(): BelongsTo | BelongsToMany | MorphMany | null + public function getRelationship(): BelongsTo | BelongsToMany | MorphMany | MorphOne | null { $name = $this->getRelationshipName();