From cb9097e0199cf6afca3f144edb7d7addaec1cb59 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Thu, 22 Jul 2021 21:04:02 +0200 Subject: Add pagination for search results Add pagination for search results in the admin and default theme. --- admin/post/search.php | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'admin/post') diff --git a/admin/post/search.php b/admin/post/search.php index b438201..a270ccb 100644 --- a/admin/post/search.php +++ b/admin/post/search.php @@ -10,25 +10,54 @@ const AUTHENTICATION = TRUE; #=============================================================================== require '../../core/application.php'; +#=============================================================================== +# Get repositories +#=============================================================================== +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); + +#=============================================================================== +# Pagination +#=============================================================================== +$site_size = Application::get('ADMIN.POST.LIST_SIZE'); +$site_sort = Application::get('ADMIN.POST.LIST_SORT'); + +$currentSite = HTTP::GET('site') ?? 1; +$currentSite = intval($currentSite); +$offset = ($currentSite-1) * $site_size; + #=============================================================================== # Check for search request #=============================================================================== if($search = HTTP::GET('q')) { - $PostRepository = Application::getRepository('Post'); - $UserRepository = Application::getRepository('User'); - - foreach($PostRepository->search($search) as $Post) { + foreach($PostRepository->search($search, [], $site_size, $offset) as $Post) { $User = $UserRepository->find($Post->get('user')); $templates[] = generatePostItemTemplate($Post, $User); } } +#=============================================================================== +# Create pagination only if there are results +#=============================================================================== +if($count = $PostRepository->getLastSearchOverallCount()) { + $last = ceil($count / $site_size); + + $pagination_data = [ + 'THIS' => $currentSite, + 'LAST' => $last, + 'HTML' => createPaginationTemplate( + $currentSite, $last, Application::getAdminURL('post/search.php') + ) + ]; +} + #=============================================================================== # Build document #=============================================================================== $SearchTemplate = Template\Factory::build('post/search'); $SearchTemplate->set('QUERY', $search); $SearchTemplate->set('POSTS', $templates ?? []); +$SearchTemplate->set('PAGINATION', $pagination_data ?? []); $MainTemplate = Template\Factory::build('main'); $MainTemplate->set('NAME', $Language->text('title_post_search')); -- cgit v1.2.3