Skip to content

Commit

Permalink
[5.x] Enable asset folder sort across pages (#11130)
Browse files Browse the repository at this point in the history
  • Loading branch information
daun authored Nov 21, 2024
1 parent b02a86d commit fe173ff
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/Http/Controllers/CP/Assets/BrowserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,30 @@ public function folder(Request $request, $container, $path = '/')

$folders = $folder->assetFolders();
$totalFolders = $folders->count();
$folders = $folders->slice(($page - 1) * $perPage, $perPage);
$lastFolderPage = (int) ceil($totalFolders / $perPage) ?: 1;

$totalAssets = $folder->queryAssets()->count();
$totalItems = $totalAssets + $totalFolders;

if ($page >= $lastFolderPage) {
$query = $folder->queryAssets();

if ($sort = $request->sort) {
$sortByMethod = $request->order === 'desc' ? 'sortByDesc' : 'sortBy';

$folders = $folders->$sortByMethod(
fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename()
);
if ($request->sort) {
$sort = $request->sort;
$order = $request->order ?? 'asc';
} else {
$sort = $container->sortField();
$order = $container->sortDirection();
}

$query->orderBy($request->sort, $request->order ?? 'asc');
} else {
$sort = $container->sortField();
$order = $container->sortDirection();
$sortByMethod = $request->order === 'desc' ? 'sortByDesc' : 'sortBy';
$sortByMethod = $order === 'desc' ? 'sortByDesc' : 'sortBy';

$folders = $folders->$sortByMethod(
fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename()
);
$folders = $folders->$sortByMethod(
fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename()
);

$query->orderBy($sort, $order);
}
$folders = $folders->slice(($page - 1) * $perPage, $perPage);

if ($page >= $lastFolderPage) {
$query = $folder->queryAssets();
$query->orderBy($sort, $order);
$this->applyQueryScopes($query, $request->all());

$offset = $page === $lastFolderPage
Expand Down

0 comments on commit fe173ff

Please sign in to comment.