From 7907c193d4612600c4832c6dcc9c23315c8e9a96 Mon Sep 17 00:00:00 2001 From: Daniel Pett Date: Fri, 27 May 2022 00:02:36 +0100 Subject: [PATCH] Add pagination for functions 1. Profiles 2. Resources 3. Projects This closes #416 --- app/Http/Controllers/researchController.php | 14 +++++++------ app/Models/OnlineResources.php | 19 +++++++++++++---- app/Models/ResearchProjects.php | 22 ++++++++++++++------ app/Models/StaffProfiles.php | 18 ++++++++++++---- resources/views/research/profiles.blade.php | 4 +++- resources/views/research/projects.blade.php | 3 ++- resources/views/research/resources.blade.php | 4 +++- 7 files changed, 61 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/researchController.php b/app/Http/Controllers/researchController.php index 17738eb7..d4156b36 100644 --- a/app/Http/Controllers/researchController.php +++ b/app/Http/Controllers/researchController.php @@ -49,11 +49,12 @@ public function index(): View } /** + * @param Request $request * @return View */ - public function projects(): View + public function projects(Request $request): View { - $projects = ResearchProjects::list(); + $projects = ResearchProjects::list($request); return view('research.projects', compact('projects')); } @@ -74,11 +75,12 @@ public function project(string $slug): View|Response } /** + * @param Request $request * @return View */ - public function profiles(): View + public function profiles(Request $request): View { - $profiles = StaffProfiles::list(); + $profiles = StaffProfiles::list($request); return view('research.profiles', compact('profiles')); } @@ -137,9 +139,9 @@ public function resource(string $slug): View|Response /** * @return View */ - public function resources(): View + public function resources(Request $request): View { - $resources = OnlineResources::list(); + $resources = OnlineResources::list($request); return view('research.resources', compact('resources')); } diff --git a/app/Models/OnlineResources.php b/app/Models/OnlineResources.php index a7eb9920..8cfe1f42 100644 --- a/app/Models/OnlineResources.php +++ b/app/Models/OnlineResources.php @@ -3,25 +3,36 @@ namespace App\Models; use App\DirectUs; +use Illuminate\Http\Request; +use Illuminate\Pagination\LengthAwarePaginator; class OnlineResources extends Model { /** - * @return array + * @param Request $request + * @return LengthAwarePaginator */ - public static function list(): array + public static function list(Request $request): LengthAwarePaginator { + $perPage = 12; + $offset = ($request['page'] - 1) * $perPage; $api = new DirectUs; $api->setEndpoint('online_resources'); $api->setArguments( array( 'fields' => '*.*.*.*', - 'limit' => 100, + 'limit' => $perPage, + 'offset' => $offset, 'meta' => '*', 'sort' => 'id' ) ); - return $api->getData(); + $resources = $api->getData(); + $currentPage = LengthAwarePaginator::resolveCurrentPage(); + $total = $resources['meta']['total_count']; + $paginator = new LengthAwarePaginator($resources, $total, $perPage, $currentPage); + $paginator->setPath(route('resources')); + return $paginator; } /** diff --git a/app/Models/ResearchProjects.php b/app/Models/ResearchProjects.php index 108511ee..c0517764 100644 --- a/app/Models/ResearchProjects.php +++ b/app/Models/ResearchProjects.php @@ -3,27 +3,37 @@ namespace App\Models; use App\DirectUs; +use Illuminate\Http\Request; +use Illuminate\Pagination\LengthAwarePaginator; class ResearchProjects extends Model { /** + * @param Request $request * @param string $sort - * @param int $limit - * @return array + * @return LengthAwarePaginator */ - public static function list(string $sort = 'title', int $limit = 100): array + public static function list(Request $request, string $sort = 'title'): LengthAwarePaginator { + $perPage = 12; + $offset = ($request['page'] - 1) * $perPage; $api = new DirectUs; $api->setEndpoint('research_projects'); $api->setArguments( array( 'fields' => '*.*.*.*', - 'meta' => 'result_count,total_count,type', + 'meta' => '*', 'sort' => $sort, - 'limit' => $limit + 'limit' => $perPage, + 'offset' => $offset ) ); - return $api->getData(); + $projects = $api->getData(); + $currentPage = LengthAwarePaginator::resolveCurrentPage(); + $total = $projects['meta']['total_count']; + $paginator = new LengthAwarePaginator($projects, $total, $perPage, $currentPage); + $paginator->setPath(route('research-projects')); + return $paginator; } /** diff --git a/app/Models/StaffProfiles.php b/app/Models/StaffProfiles.php index 35f880e3..92469e69 100644 --- a/app/Models/StaffProfiles.php +++ b/app/Models/StaffProfiles.php @@ -9,21 +9,31 @@ class StaffProfiles extends Model { /** - * @return array + * @param Request $request + * @return LengthAwarePaginator */ - public static function list(): array + public static function list(Request $request): LengthAwarePaginator { + $perPage = 12; + $offset = ($request['page'] - 1) * $perPage; $api = new DirectUs; $api->setEndpoint('staff_profiles'); $api->setArguments( array( 'fields' => '*.*.*.*', - 'meta' => 'result_count,total_count,type', + 'meta' => '*', + 'limit' => $perPage, + 'offset' => $offset, 'sort' => 'last_name', 'filter[research_active][in]' => 'yes' ) ); - return $api->getData(); + $staff = $api->getData(); + $currentPage = LengthAwarePaginator::resolveCurrentPage(); + $total = $staff['meta']['filter_count']; + $paginator = new LengthAwarePaginator($staff, $total, $perPage, $currentPage); + $paginator->setPath(route('research-profiles')); + return $paginator; } /** diff --git a/resources/views/research/profiles.blade.php b/resources/views/research/profiles.blade.php index efc4ecac..221b586a 100644 --- a/resources/views/research/profiles.blade.php +++ b/resources/views/research/profiles.blade.php @@ -7,7 +7,7 @@ @section('content')
- @foreach($profiles['data'] as $profile) + @foreach($profiles->items()['data'] as $profile) @endforeach
+ {{ $profiles->appends(request()->except('page'))->links() }} + @endsection diff --git a/resources/views/research/projects.blade.php b/resources/views/research/projects.blade.php index 6afe4fea..a6519943 100644 --- a/resources/views/research/projects.blade.php +++ b/resources/views/research/projects.blade.php @@ -5,7 +5,7 @@ @section('description', 'An overview of Fitzwilliam Museum research projects') @section('content')
- @foreach($projects['data'] as $project) + @foreach($projects->items()['data'] as $project) @endforeach
+ {{ $projects->appends(request()->except('page'))->links() }} @endsection diff --git a/resources/views/research/resources.blade.php b/resources/views/research/resources.blade.php index 1d27d1d0..48acf2bd 100644 --- a/resources/views/research/resources.blade.php +++ b/resources/views/research/resources.blade.php @@ -5,7 +5,7 @@ @section('description', 'An overview of Fitzwilliam legacy digital resources') @section('content')
- @foreach($resources['data'] as $project) + @foreach($resources->items()['data'] as $project) @endforeach
+ {{ $resources->appends(request()->except('page'))->links() }} + @endsection