Improve how collections are organized and internally and printed to HTML

This commit is contained in:
Alex Cabal 2020-05-22 16:22:46 -05:00
parent c8cacc4ace
commit aed7db7fc1
4 changed files with 23 additions and 15 deletions

View file

@ -7,6 +7,6 @@ class Collection{
public function __construct(string $name){ public function __construct(string $name){
$this->Name = $name; $this->Name = $name;
$this->Url = '/collections/' . strtolower(str_replace(' ', '-', Formatter::ToPlainText(Formatter::RemoveDiacritics($this->Name)))); $this->Url = '/collections/' . Formatter::MakeUrlSafe($this->Name);
} }
} }

View file

@ -0,0 +1,3 @@
<?
class InvalidCollectionException extends \Exception{
}

View file

@ -163,16 +163,16 @@ class Library{
// Create the collections cache // Create the collections cache
foreach($ebook->Collections as $collection){ foreach($ebook->Collections as $collection){
$lcCollection = strtolower(Formatter::RemoveDiacritics($collection->Name)); $urlSafeCollection = Formatter::MakeUrlSafe($collection->Name);
if(!array_key_exists($lcCollection, $collections)){ if(!array_key_exists($urlSafeCollection, $collections)){
$collections[$lcCollection] = []; $collections[$urlSafeCollection] = [];
} }
if($collection->SequenceNumber !== null){ if($collection->SequenceNumber !== null){
$collections[$lcCollection][$collection->SequenceNumber] = $ebook; $collections[$urlSafeCollection][$collection->SequenceNumber] = $ebook;
} }
else{ else{
$collections[$lcCollection][] = $ebook; $collections[$urlSafeCollection][] = $ebook;
} }
} }

View file

@ -40,21 +40,26 @@ try{
$ebooks = array_slice($ebooks, ($page - 1) * EBOOKS_PER_PAGE, EBOOKS_PER_PAGE); $ebooks = array_slice($ebooks, ($page - 1) * EBOOKS_PER_PAGE, EBOOKS_PER_PAGE);
} }
elseif($collection !== null){ elseif($collection !== null){
$collection = strtolower(str_replace('-', ' ', Formatter::RemoveDiacritics($collection)));
$ebooks = Library::GetEbooksByCollection($collection); $ebooks = Library::GetEbooksByCollection($collection);
$collectionObject = null;
if(sizeof($ebooks) > 0){
// Get the *actual* name of the collection, in case there are accent marks (like "Arsène Lupin") // Get the *actual* name of the collection, in case there are accent marks (like "Arsène Lupin")
if(sizeof($ebooks) > 0){
foreach($ebooks[0]->Collections as $c){ foreach($ebooks[0]->Collections as $c){
if($collection == strtolower(str_replace('-', ' ', Formatter::RemoveDiacritics($c->Name)))){ if($collection == Formatter::MakeUrlSafe($c->Name)){
$collection = (string)$c->Name; // Explicit typecast to string to satisfy PHPStan $collectionObject = $c;
} }
} }
} }
$collectionName = ucwords(preg_replace('/^The /ius', '', $collection) ?? ''); if($collectionObject !== null){
$pageTitle = 'Browse ebooks in the ' . Formatter::ToPlainText($collectionName) . ' collection'; $collectionName = preg_replace('/^The /ius', '', $collectionObject->Name) ?? '';
$pageDescription = 'A list of ebooks in the ' . Formatter::ToPlainText($collectionName) . ' collection'; $collectionType = $collectionObject->Type ?? 'collection';
$pageHeader = 'Ebooks in the ' . Formatter::ToPlainText($collectionName) . ' collection'; $pageTitle = 'Browse ebooks in the ' . Formatter::ToPlainText($collectionName) . ' ' . $collectionType;
$pageDescription = 'A list of ebooks in the ' . Formatter::ToPlainText($collectionName) . ' ' . $collectionType;
$pageHeader = 'Ebooks in the ' . Formatter::ToPlainText($collectionName) . ' ' . $collectionType;
}
else{
throw new InvalidCollectionException();
}
} }
else{ else{
$pageTitle = 'Browse Standard Ebooks'; $pageTitle = 'Browse Standard Ebooks';