Skip to content

Commit

Permalink
add pagination and update collections page to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
MuchQuak committed Oct 30, 2024
1 parent 1c518d1 commit 8241e09
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
1 change: 1 addition & 0 deletions resources/views/core/pages/collections/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</div>

</div>
<x-pagination :lengthAwarePaginator="$occurrences"/>
<div class="grid grid-col-1 gap-4">
@foreach ($occurrences as $occurrence)
<x-collections.list.item :occurrence="$occurrence" />
Expand Down
49 changes: 49 additions & 0 deletions resources/views/core/pagination.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{-- See Laravel Paginator Docs For LengthAwarePaginator properties and methods --}}
@props(['lengthAwarePaginator'])
@php
$start = ($lengthAwarePaginator->currentPage() - 1 ) * $lengthAwarePaginator->perPage();
$end = $start + $lengthAwarePaginator->perPage();
if($end > $lengthAwarePaginator->total()) {
$end = $lengthAwarePaginator->total();
}
@endphp

<div class="flex items-center justify-between w-full h-16">
<p class="text-sm text-base-content">Showing <span class="font-medium">{{ $start }}</span> to <span class="font-medium">{{ $end }}</span> of <span class="font-medium">{{ $lengthAwarePaginator->total() }}</span> results</p>
<nav>
<ul class="flex items-center text-sm leading-tight bg-base-100 border divide-x rounded h-9 text-base-content/75 divide-base-300 border-base-300">
<li class="h-full divide-r">
<a href="{{ $lengthAwarePaginator->previousPageUrl() }}" class="relative inline-flex items-center h-full px-3 ml-0 rounded-l group hover:text-base-content outline-none focus:ring ring-inset focus:ring-accent">
<span>Previous</span>
<span class="box-content absolute bottom-0 w-0 h-px -mx-px duration-200 ease-out translate-y-px border-transparent bg-base-content group-hover:border-l group-hover:border-r group-hover:border-base-300 left-1/2 group-hover:left-0 group-hover:w-full"></span>
</a>
</li>
@for ($i = 1; $i <= ceil($lengthAwarePaginator->total() / $lengthAwarePaginator->perPage()); $i++)
@php $isCurrent = $lengthAwarePaginator->currentPage() === $i; @endphp
@if($isCurrent)
<li class="hidden h-full md:block">
<a href="{{$lengthAwarePaginator->url($i)}}" class="relative inline-flex items-center h-full px-3 text-base-content group bg-base-200/50 outline-none ring-inset focus:ring focus:ring-accent">
<span>{{ $i }}</span>
<span class="box-content absolute bottom-0 left-0 w-full h-px -mx-px translate-y-px border-l border-r bg-base-content border-base-300"></span>
</a>
</li>
@else
<li class="hidden h-full md:block">
<a href="{{$lengthAwarePaginator->url($i)}}" class="relative inline-flex items-center h-full px-3 group hover:text-base-content outline-none focus:ring ring-inset focus:ring-accent">
<span>{{ $i }}</span>
<span class="box-content absolute bottom-0 w-0 h-px -mx-px duration-200 ease-out translate-y-px border-transparent bg-base-content group-hover:border-l group-hover:border-r group-hover:border-base-300 left-1/2 group-hover:left-0 group-hover:w-full"></span>
</a>
</li>
@endif
@endfor

<li class="h-full">
<a href="{{ $lengthAwarePaginator->nextPageUrl() }}" class="relative inline-flex items-center h-full px-3 rounded-r group hover:text-base-content outline-none focus:ring ring-inset focus:ring-accent">
<span>Next</span>
<span class="box-content absolute bottom-0 w-0 h-px -mx-px duration-200 ease-out translate-y-px border-transparent bg-base-content group-hover:border-l group-hover:border-r group-hover:border-base-300 left-1/2 group-hover:left-0 group-hover:w-full"></span>
</a>
</li>
</ul>
</nav>
</div>
38 changes: 24 additions & 14 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Query\JoinClause;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -37,20 +38,29 @@

// Collection
Route::get('/collections/list', function(Request $request) {
$params = $request->all();
$media_cnt = DB::table('media as m')->select(
'm.occid',
DB::raw('sum(if(media_type = "image", 1, 0)) as image_cnt'),
DB::raw('sum(if(media_type = "audio", 1, 0)) as audio_cnt')
)->groupBy('m.occid');

$occurrences = DB::table('omoccurrences as o')
->select('o.*','image_cnt','audio_cnt')
->leftJoinSub($media_cnt, 'media_cnt', function(JoinClause $join) {
$join->on('media_cnt.occid', '=', 'o.occid');
})
->limit(30)
->get();
$params = $request->except(['page', '_token']);

Cache::forget($request->fullUrl());
$occurrences = Cache::remember($request->fullUrl(), now()->addMinutes(1), function() use ($params) {
if(count($params) === 0) {
return [];
}

$query = DB::table('omoccurrences as o')
->select(
'o.*',
DB::raw('sum(if(media_type = "image", 1, 0)) as image_cnt'),
DB::raw('sum(if(media_type = "audio", 1, 0)) as audio_cnt'))
->leftJoin('media as m', 'm.occid', '=', 'o.occid')
->join('omcollections as c', 'c.collid', '=', 'o.collid')
->groupBy('o.occid');

if(isset($params['taxa'])) {
$query->whereLike('o.sciname', $params['taxa']);
}

return $query->paginate(30)->appends($params);
});

return view('pages/collections/list', ['occurrences' => $occurrences]);
});
Expand Down

0 comments on commit 8241e09

Please sign in to comment.