diff --git a/lib/Library.php b/lib/Library.php index 0bce2943..cc9117a4 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -19,12 +19,12 @@ class Library{ * @param EbookSort $sort * @return array */ - 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; diff --git a/templates/SearchForm.php b/templates/SearchForm.php index 7c63a6b3..c91f7b5c 100644 --- a/templates/SearchForm.php +++ b/templates/SearchForm.php @@ -17,10 +17,10 @@ $allSelected = sizeof($tags) == 0 || in_array('all', $tags); Sort diff --git a/www/ebooks/index.php b/www/ebooks/index.php index decaff5a..f5e1ce5a 100644 --- a/www/ebooks/index.php +++ b/www/ebooks/index.php @@ -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 .= '&sort=' . urlencode($sort); + $queryString .= '&sort=' . urlencode($sort->value); } if($perPage !== EBOOKS_PER_PAGE){