Skip to content

Commit

Permalink
Fixes awcodes#529
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeFourkas committed Aug 30, 2024
1 parent 8bd6764 commit 1f3aae8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion config/curator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/Components/Forms/CuratorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ public function relationship(string | Closure $relationshipName, string | Closur
return;
}


if ($component->isMultiple()) {
if ($relationship instanceof BelongsToMany) {
$orderColumn = $component->getOrderColumn();
Expand Down
5 changes: 3 additions & 2 deletions src/Components/Tables/CuratorColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
30 changes: 9 additions & 21 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 1f3aae8

Please # to comment.