Refactor Library::FilterEbooks and Template::SearchForm to accept EbookSort

This commit is contained in:
Mike Colagrosso 2024-01-29 23:45:31 -07:00 committed by Alex Cabal
parent 980ed8cea9
commit d95d9b3349
3 changed files with 13 additions and 17 deletions

View file

@ -19,12 +19,12 @@ class Library{
* @param EbookSort $sort
* @return array<Ebook>
*/
public static function FilterEbooks(string $query = null, array $tags = [], string $sort = null){
public static function FilterEbooks(string $query = null, array $tags = [], EbookSort $sort = null){
$ebooks = Library::GetEbooks();
$matches = $ebooks;
if($sort === null){
$sort = EbookSort::Newest->value;
$sort = EbookSort::Newest;
}
if(sizeof($tags) > 0 && !in_array('all', $tags)){ // 0 tags means "all ebooks"
@ -51,13 +51,13 @@ class Library{
}
switch($sort){
case EbookSort::AuthorAlpha->value:
case EbookSort::AuthorAlpha:
usort($matches, function($a, $b){
return strcmp(mb_strtolower($a->Authors[0]->SortName), mb_strtolower($b->Authors[0]->SortName));
});
break;
case EbookSort::Newest->value:
case EbookSort::Newest:
usort($matches, function($a, $b){
if($a->Created < $b->Created){
return -1;
@ -73,7 +73,7 @@ class Library{
$matches = array_reverse($matches);
break;
case EbookSort::ReadingEase->value:
case EbookSort::ReadingEase:
usort($matches, function($a, $b){
if($a->ReadingEase < $b->ReadingEase){
return -1;
@ -89,7 +89,7 @@ class Library{
$matches = array_reverse($matches);
break;
case EbookSort::Length->value:
case EbookSort::Length:
usort($matches, function($a, $b){
if($a->WordCount < $b->WordCount){
return -1;

View file

@ -17,10 +17,10 @@ $allSelected = sizeof($tags) == 0 || in_array('all', $tags);
<span>Sort</span>
<span>
<select name="sort">
<option value="<?= EbookSort::Newest->value ?>"<? if($sort == EbookSort::Newest->value){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option>
<option value="<?= EbookSort::AuthorAlpha->value ?>"<? if($sort == EbookSort::AuthorAlpha->value){ ?> selected="selected"<? } ?>>Author name (a &#x2192; z)</option>
<option value="<?= EbookSort::ReadingEase->value ?>"<? if($sort == EbookSort::ReadingEase->value){ ?> selected="selected"<? } ?>>Reading ease (easy &#x2192; hard)</option>
<option value="<?= EbookSort::Length->value ?>"<? if($sort == EbookSort::Length->value){ ?> selected="selected"<? } ?>>Length (short &#x2192; long)</option>
<option value="<?= EbookSort::Newest->value ?>"<? if($sort == EbookSort::Newest){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option>
<option value="<?= EbookSort::AuthorAlpha->value ?>"<? if($sort == EbookSort::AuthorAlpha){ ?> selected="selected"<? } ?>>Author name (a &#x2192; z)</option>
<option value="<?= EbookSort::ReadingEase->value ?>"<? if($sort == EbookSort::ReadingEase){ ?> selected="selected"<? } ?>>Reading ease (easy &#x2192; hard)</option>
<option value="<?= EbookSort::Length->value ?>"<? if($sort == EbookSort::Length){ ?> selected="selected"<? } ?>>Length (short &#x2192; long)</option>
</select>
</span>
</label>

View file

@ -8,7 +8,7 @@ try{
$tags = HttpInput::GetArray('tags') ?? [];
$collection = HttpInput::Str(GET, 'collection');
$view = HttpInput::Str(GET, 'view');
$sort = HttpInput::Str(GET, 'sort');
$sort = EbookSort::tryFrom(HttpInput::Str(GET, 'sort') ?? '');
$pages = 0;
$totalEbooks = 0;
$collectionObject = null;
@ -35,15 +35,11 @@ try{
$view = mb_strtolower($view);
}
if($sort !== null){
$sort = mb_strtolower($sort);
}
if($view === 'grid'){
$view = null;
}
if($sort === 'newest'){
if($sort == EbookSort::Newest){
$sort = null;
}
@ -103,7 +99,7 @@ try{
}
if($sort !== null){
$queryString .= '&amp;sort=' . urlencode($sort);
$queryString .= '&amp;sort=' . urlencode($sort->value);
}
if($perPage !== EBOOKS_PER_PAGE){