Skip to content

Commit

Permalink
Many miscallenous fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarpsvo committed Feb 9, 2021
1 parent 61b23d8 commit 72228da
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/Traits/HasSortableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,25 @@ public static function getSortability(NovaRequest $request, $resource = null)
if (!$model || !self::canSort($request, $model)) {
return (object)['canSort' => false];
}
}

$sortable = self::getSortabilityConfiguration($model);
$sortOnBelongsTo = !empty($sortable);

// Check for `only_sort_on` and `dont_sort_on`
$hasOnlySortOn = is_array($sortable) && key_exists('only_sort_on', $sortable);
$onlySortOnMatches = $hasOnlySortOn && $request->viaResource() === $sortable['only_sort_on'];

// Disable when `only_sort_on` does not match
if ($hasOnlySortOn && !$onlySortOnMatches) $sortOnHasMany = $sortOnBelongsTo = false;
$sortable = self::getSortabilityConfiguration($model);

// Disable sorting on `dont_sort_on` models
if (is_array($sortable) && key_exists('dont_sort_on', $sortable)) {
foreach ($sortable['dont_sort_on'] as $item) {
if ($item === $request->viaResource()) {
$sortOnBelongsTo = $sortOnHasMany = false;
break;
}
// Check for `only_sort_on` and `dont_sort_on`
$hasOnlySortOn = is_array($sortable) && key_exists('only_sort_on', $sortable);
$onlySortOnMatches = $hasOnlySortOn && $request->viaResource() === $sortable['only_sort_on'];

// Disable when `only_sort_on` does not match
if ($hasOnlySortOn && !$onlySortOnMatches) $sortOnHasMany = $sortOnBelongsTo = false;

// Disable sorting on `dont_sort_on` models
if (is_array($sortable) && key_exists('dont_sort_on', $sortable)) {
$dontSortOn = $sortable['dont_sort_on'];
if (!is_array($dontSortOn)) $dontSortOn = [$dontSortOn];
foreach ($dontSortOn as $item) {
if ($item === $request->viaResource()) {
$sortOnBelongsTo = $sortOnHasMany = false;
break;
}
}
}
Expand All @@ -80,7 +81,7 @@ public static function getSortability(NovaRequest $request, $resource = null)

public function serializeForIndex(NovaRequest $request, $fields = null)
{
$sortability = static::getSortability($request, $this->resource);
$sortability = static::getSortability($request, $this);

if (is_null($sortability)) {
return parent::serializeForIndex($request, $fields);
Expand All @@ -92,7 +93,7 @@ public function serializeForIndex(NovaRequest $request, $fields = null)
'sort_on_index' => $sortability->sortable && !$sortability->sortOnHasMany && !$sortability->sortOnBelongsTo,
'sort_on_has_many' => $sortability->sortable && $sortability->sortOnHasMany,
'sort_on_belongs_to' => $sortability->sortable && $sortability->sortOnBelongsTo,
] : ['sort_not_allowed' => !($sortability->canSort ?? false)]);
] : ['sort_not_allowed' => !($sortability->canSort ?? true)]);

return array_merge(parent::serializeForIndex($request, $fields), $sortabilityData);
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public static function getSortabilityConfiguration($model): ?array
if (is_null($model)) return null;

// Check if spatie trait is in the model.
if (! in_array(SortableTrait::class, class_uses_recursive($model))) {
if (!in_array(SortableTrait::class, class_uses_recursive($model))) {
return null;
}

Expand Down

0 comments on commit 72228da

Please sign in to comment.