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.
This commit is contained in:
Mike Colagrosso 2025-02-12 15:09:10 -07:00 committed by Alex Cabal
parent a3ce3f1ec1
commit 55e0428006
4 changed files with 33 additions and 11 deletions

View file

@ -2444,6 +2444,15 @@ final class Ebook{
break; 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'; $orderBy = 'e.EbookCreated desc';
if($sort == Enums\EbookSortType::AuthorAlpha){ if($sort == Enums\EbookSortType::AuthorAlpha){
$joinContributors = 'inner join Contributors con using (EbookId)'; $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(?) '; $whereCondition .= ' and match(e.IndexableText, e.Title, e.IndexableAuthors, e.IndexableCollections) against(?) ';
$params[] = $query; $params[] = $query;
if($sort == null || $sort == Enums\EbookSortType::Relevance || $sort == Enums\EbookSortType::Newest){ if($sort == Enums\EbookSortType::Relevance){
$orderBy = '( $orderBy = '(
match(e.Title) against (?) * ' . EBOOK_SEARCH_WEIGHT_TITLE . ' + match(e.Title) against (?) * ' . EBOOK_SEARCH_WEIGHT_TITLE . ' +
match(e.IndexableAuthors) against (?) * ' . EBOOK_SEARCH_WEIGHT_AUTHORS . ' + match(e.IndexableAuthors) against (?) * ' . EBOOK_SEARCH_WEIGHT_AUTHORS . ' +

View file

@ -7,4 +7,5 @@ enum EbookSortType: string{
case ReadingEase = 'reading-ease'; case ReadingEase = 'reading-ease';
case Length = 'length'; case Length = 'length';
case Relevance = 'relevance'; case Relevance = 'relevance';
case Default = 'default'; // Interpreted as `Relevance` if a query is present, `Newest` if not.
} }

View file

@ -26,8 +26,9 @@ $isAllSelected = sizeof($tags) == 0 || in_array('all', $tags);
<select name="sort"> <select name="sort">
<? if(isset($query) && $query != ''){ ?> <? if(isset($query) && $query != ''){ ?>
<option value="<?= Enums\EbookSortType::Relevance->value ?>"<? if($sort == Enums\EbookSortType::Relevance){ ?> selected="selected"<? } ?>>Relevance</option> <option value="<?= Enums\EbookSortType::Relevance->value ?>"<? if($sort == Enums\EbookSortType::Relevance){ ?> selected="selected"<? } ?>>Relevance</option>
<? }else{ ?>
<option value="<?= Enums\EbookSortType::Newest->value ?>"<? if($sort == Enums\EbookSortType::Newest){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option> <option value="<?= Enums\EbookSortType::Newest->value ?>"<? if($sort == Enums\EbookSortType::Newest){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option>
<? }else{ ?>
<option value="<?= Enums\EbookSortType::Default->value ?>"<? if($sort == Enums\EbookSortType::Newest){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option>
<? } ?> <? } ?>
<option value="<?= Enums\EbookSortType::AuthorAlpha->value ?>"<? if($sort == Enums\EbookSortType::AuthorAlpha){ ?> selected="selected"<? } ?>>Author name (a &#x2192; z)</option> <option value="<?= Enums\EbookSortType::AuthorAlpha->value ?>"<? if($sort == Enums\EbookSortType::AuthorAlpha){ ?> selected="selected"<? } ?>>Author name (a &#x2192; z)</option>
<option value="<?= Enums\EbookSortType::ReadingEase->value ?>"<? if($sort == Enums\EbookSortType::ReadingEase){ ?> selected="selected"<? } ?>>Reading ease (easy &#x2192; hard)</option> <option value="<?= Enums\EbookSortType::ReadingEase->value ?>"<? if($sort == Enums\EbookSortType::ReadingEase){ ?> selected="selected"<? } ?>>Reading ease (easy &#x2192; hard)</option>

View file

@ -23,20 +23,27 @@ try{
$perPage = EBOOKS_PER_PAGE; $perPage = EBOOKS_PER_PAGE;
} }
if($sort == Enums\EbookSortType::Default){
if($query != ''){
$sort = Enums\EbookSortType::Relevance;
}
else{
$sort = Enums\EbookSortType::Newest;
}
}
// Malformed query: Can't sort by `Relevance` if `$query` is empty.
// This could happen if the user was looking at `Relevance` results, then deleted the query and hit the Filter button.
if($sort == Enums\EbookSortType::Relevance && $query == ''){
$sort = Enums\EbookSortType::Newest;
}
// If we're passed string values that are the same as the defaults, set them to null so that we can have cleaner query strings in the navigation footer. // If we're passed string values that are the same as the defaults, set them to null so that we can have cleaner query strings in the navigation footer.
if($view === Enums\ViewType::Grid){ if($view === Enums\ViewType::Grid){
$view = null; $view = null;
} }
if($query != ''){ if(($sort == Enums\EbookSortType::Newest && $query == '') || ($sort == Enums\EbookSortType::Relevance && $query != '')){
$queryStringParams['query'] = $query;
// If the user entered a query with the default sort order, change it to relevance sort.
if($sort == Enums\EbookSortType::Newest){
$sort = Enums\EbookSortType::Relevance;
}
}
if($sort == Enums\EbookSortType::Newest){
$sort = null; $sort = null;
} }
@ -46,6 +53,10 @@ try{
$pageDescription = 'Page ' . $page . ' of the Standard Ebooks free ebook library'; $pageDescription = 'Page ' . $page . ' of the Standard Ebooks free ebook library';
if($query != ''){
$queryStringParams['query'] = $query;
}
if(sizeof($tags) > 0){ if(sizeof($tags) > 0){
$queryStringParams['tags'] = $tags; $queryStringParams['tags'] = $tags;
} }