Add 'reading ease' and 'length' sorting methods

This commit is contained in:
Evan Hall 2019-04-13 17:02:28 -05:00 committed by Alex Cabal
parent 3171b7e583
commit 52a5e6056f
3 changed files with 59 additions and 1 deletions

View file

@ -4,6 +4,8 @@
const EBOOKS_PER_PAGE = 12; const EBOOKS_PER_PAGE = 12;
const SORT_NEWEST = 'newest'; const SORT_NEWEST = 'newest';
const SORT_AUTHOR_ALPHA = 'author-alpha'; const SORT_AUTHOR_ALPHA = 'author-alpha';
const SORT_READING_EASE = 'reading-ease';
const SORT_LENGTH = 'length';
const GET = 0; const GET = 0;
const POST = 1; const POST = 1;

View file

@ -50,6 +50,56 @@ class Library{
} }
break; break;
case SORT_READING_EASE:
// Get all ebooks, sorted by easiest first.
try{
$ebooks = apcu_fetch('ebooks-reading-ease');
}
catch(Safe\Exceptions\ApcuException $ex){
$ebooks = Library::GetEbooks();
usort($ebooks, function($a, $b){
if($a->ReadingEase < $b->ReadingEase){
return -1;
}
elseif($a->ReadingEase == $b->ReadingEase){
return 0;
}
else{
return 1;
}
});
$ebooks = array_reverse($ebooks);
apcu_store('ebooks-reading-ease', $ebooks);
}
break;
case SORT_LENGTH:
// Get all ebooks, sorted by fewest words first.
try{
$ebooks = apcu_fetch('ebooks-length');
}
catch(Safe\Exceptions\ApcuException $ex){
$ebooks = Library::GetEbooks();
usort($ebooks, function($a, $b){
if($a->WordCount < $b->WordCount){
return -1;
}
elseif($a->WordCount == $b->WordCount){
return 0;
}
else{
return 1;
}
});
apcu_store('ebooks-length', $ebooks);
}
break;
default: default:
// Get all ebooks, unsorted. // Get all ebooks, unsorted.
try{ try{

View file

@ -16,7 +16,7 @@ try{
$page = 1; $page = 1;
} }
if($sort != SORT_AUTHOR_ALPHA && $sort != SORT_NEWEST){ if($sort != SORT_AUTHOR_ALPHA && $sort != SORT_NEWEST && $sort != SORT_READING_EASE && $sort != SORT_LENGTH){
$sort = SORT_NEWEST; $sort = SORT_NEWEST;
} }
@ -72,6 +72,12 @@ try{
case SORT_AUTHOR_ALPHA: case SORT_AUTHOR_ALPHA:
$pageDescription .= 'alphabetically by author name.'; $pageDescription .= 'alphabetically by author name.';
break; break;
case SORT_READING_EASE:
$pageDescription .= 'by easiest ebooks first.';
break;
case SORT_LENGTH:
$pageDescription .= 'by shortest ebooks first.';
break;
} }
} }
} }