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. --- core/include/search/main.php | 23 ++++++++++++++++++++++- core/namespace/ORM/Repository.php | 24 ++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/include/search/main.php b/core/include/search/main.php index 564f0dd..14c2ac9 100644 --- a/core/include/search/main.php +++ b/core/include/search/main.php @@ -10,6 +10,16 @@ $Language = Application::getLanguage(); $PostRepository = Application::getRepository('Post'); $UserRepository = Application::getRepository('User'); +#=============================================================================== +# Pagination +#=============================================================================== +$site_size = Application::get('POST.LIST_SIZE'); +$site_sort = Application::get('POST.LIST_SORT'); + +$currentSite = HTTP::GET('site') ?? 1; +$currentSite = intval($currentSite); +$offset = ($currentSite-1) * $site_size; + if($search = HTTP::GET('q')) { $filter = [ 'day' => HTTP::GET('d'), @@ -17,7 +27,7 @@ if($search = HTTP::GET('q')) { 'year' => HTTP::GET('y') ]; - if(!$posts = $PostRepository->search($search, $filter)) { + if(!$posts = $PostRepository->search($search, $filter, $site_size, $offset)) { $message = $Language->text('search_no_results', escapeHTML($search)); } } @@ -49,6 +59,9 @@ if(!empty($posts)) { $templates[] = generatePostItemTemplate($Post, $User); } + $count = $PostRepository->getLastSearchOverallCount(); + $last = ceil($count / $site_size); + $ResultTemplate = Template\Factory::build('search/result'); $ResultTemplate->set('FORM', $form_data); $ResultTemplate->set('SEARCH', $search_data); @@ -56,6 +69,14 @@ if(!empty($posts)) { 'LIST' => $templates ?? [] ]); + $ResultTemplate->set('PAGINATION', [ + 'THIS' => $currentSite, + 'LAST' => $last, + 'HTML' => createPaginationTemplate( + $currentSite, $last, Application::getURL('search/') + ) + ]); + $MainTemplate = Template\Factory::build('main'); $MainTemplate->set('HTML', $ResultTemplate); $MainTemplate->set('HEAD', [ diff --git a/core/namespace/ORM/Repository.php b/core/namespace/ORM/Repository.php index b2778f4..44bd62e 100644 --- a/core/namespace/ORM/Repository.php +++ b/core/namespace/ORM/Repository.php @@ -7,6 +7,9 @@ abstract class Repository { protected $Database; protected $entities = []; + # See "search" method for more details. + private $lastSearchOverallCount = 0; + abstract public static function getTableName(): string; abstract public static function getClassName(): string; @@ -262,10 +265,6 @@ abstract class Repository { # Get entities based on search query #=============================================================================== public function search(string $search, array $filter = [], int $limit = NULL, int $offset = 0): array { - if($search === '*') { - return $this->getAll([], NULL, 20); - } - if(strlen($filter['year'] ?? '') !== 0) { $extend[] = 'YEAR(time_insert) = ? AND'; $params[] = $filter['year']; @@ -287,14 +286,27 @@ abstract class Repository { $dateparts = implode(' ', $extend ?? []); - $query = 'SELECT * FROM %s WHERE %s MATCH(name, body) + $query = 'SELECT *, COUNT(*) OVER() AS _count FROM %s WHERE %s MATCH(name, body) AGAINST(? IN BOOLEAN MODE) %s'; $query = sprintf($query, static::getTableName(), $dateparts, $limit ?? 'LIMIT 20'); $Statement = $this->Database->prepare($query); $Statement->execute(array_merge($params ?? [], [$search])); - return $this->fetchEntities($Statement); + if($entities = $this->fetchEntities($Statement)) { + # Temporary (maybe crappy) solution to prevent a second count query. + # Virtual column "_count" does not belong into the entities. + $this->lastSearchOverallCount = $entities[0]->get('_count'); + } + + return $entities; + } + + #=============================================================================== + # Get the number of overall results for the last performed search + #=============================================================================== + public function getLastSearchOverallCount(): int { + return $this->lastSearchOverallCount; } #=============================================================================== -- cgit v1.2.3