From 55e042800675e2f3587ad0998a7c32af5b0e2bab Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Wed, 12 Feb 2025 15:09:10 -0700 Subject: [PATCH] Add a `Default` EbookSortType This allows the user to run a keyword search and then change the sort order. `Default` is interpreted as `Relevance` if a query is present, `Newest` if not. --- lib/Ebook.php | 11 ++++++++++- lib/Enums/EbookSortType.php | 1 + templates/SearchForm.php | 3 ++- www/ebooks/index.php | 29 ++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/Ebook.php b/lib/Ebook.php index 85b5c259..d4037136 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -2444,6 +2444,15 @@ final class Ebook{ break; } + if($sort === null || $sort == Enums\EbookSortType::Default){ + if($query !== null && $query != ''){ + $sort = Enums\EbookSortType::Relevance; + } + else{ + $sort = Enums\EbookSortType::Newest; + } + } + $orderBy = 'e.EbookCreated desc'; if($sort == Enums\EbookSortType::AuthorAlpha){ $joinContributors = 'inner join Contributors con using (EbookId)'; @@ -2471,7 +2480,7 @@ final class Ebook{ $whereCondition .= ' and match(e.IndexableText, e.Title, e.IndexableAuthors, e.IndexableCollections) against(?) '; $params[] = $query; - if($sort == null || $sort == Enums\EbookSortType::Relevance || $sort == Enums\EbookSortType::Newest){ + if($sort == Enums\EbookSortType::Relevance){ $orderBy = '( match(e.Title) against (?) * ' . EBOOK_SEARCH_WEIGHT_TITLE . ' + match(e.IndexableAuthors) against (?) * ' . EBOOK_SEARCH_WEIGHT_AUTHORS . ' + diff --git a/lib/Enums/EbookSortType.php b/lib/Enums/EbookSortType.php index 324e744e..04fe27cf 100644 --- a/lib/Enums/EbookSortType.php +++ b/lib/Enums/EbookSortType.php @@ -7,4 +7,5 @@ enum EbookSortType: string{ case ReadingEase = 'reading-ease'; case Length = 'length'; case Relevance = 'relevance'; + case Default = 'default'; // Interpreted as `Relevance` if a query is present, `Newest` if not. } diff --git a/templates/SearchForm.php b/templates/SearchForm.php index 766ac302..ae93200b 100644 --- a/templates/SearchForm.php +++ b/templates/SearchForm.php @@ -26,8 +26,9 @@ $isAllSelected = sizeof($tags) == 0 || in_array('all', $tags);