diff --git a/lib/Collection.php b/lib/Collection.php index 7247cc8f..259847bf 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -7,6 +7,6 @@ class Collection{ public function __construct(string $name){ $this->Name = $name; - $this->Url = '/collections/' . strtolower(str_replace(' ', '-', Formatter::ToPlainText(Formatter::RemoveDiacritics($this->Name)))); + $this->Url = '/collections/' . Formatter::MakeUrlSafe($this->Name); } } diff --git a/lib/InvalidCollectionException.php b/lib/InvalidCollectionException.php new file mode 100644 index 00000000..01789939 --- /dev/null +++ b/lib/InvalidCollectionException.php @@ -0,0 +1,3 @@ +Collections as $collection){ - $lcCollection = strtolower(Formatter::RemoveDiacritics($collection->Name)); - if(!array_key_exists($lcCollection, $collections)){ - $collections[$lcCollection] = []; + $urlSafeCollection = Formatter::MakeUrlSafe($collection->Name); + if(!array_key_exists($urlSafeCollection, $collections)){ + $collections[$urlSafeCollection] = []; } if($collection->SequenceNumber !== null){ - $collections[$lcCollection][$collection->SequenceNumber] = $ebook; + $collections[$urlSafeCollection][$collection->SequenceNumber] = $ebook; } else{ - $collections[$lcCollection][] = $ebook; + $collections[$urlSafeCollection][] = $ebook; } } diff --git a/www/ebooks/index.php b/www/ebooks/index.php index fa8c2ec6..a20eba9b 100644 --- a/www/ebooks/index.php +++ b/www/ebooks/index.php @@ -40,21 +40,26 @@ try{ $ebooks = array_slice($ebooks, ($page - 1) * EBOOKS_PER_PAGE, EBOOKS_PER_PAGE); } elseif($collection !== null){ - $collection = strtolower(str_replace('-', ' ', Formatter::RemoveDiacritics($collection))); $ebooks = Library::GetEbooksByCollection($collection); - + $collectionObject = null; + // Get the *actual* name of the collection, in case there are accent marks (like "Arsène Lupin") if(sizeof($ebooks) > 0){ - // Get the *actual* name of the collection, in case there are accent marks (like "Arsène Lupin") foreach($ebooks[0]->Collections as $c){ - if($collection == strtolower(str_replace('-', ' ', Formatter::RemoveDiacritics($c->Name)))){ - $collection = (string)$c->Name; // Explicit typecast to string to satisfy PHPStan + if($collection == Formatter::MakeUrlSafe($c->Name)){ + $collectionObject = $c; } } } - $collectionName = ucwords(preg_replace('/^The /ius', '', $collection) ?? ''); - $pageTitle = 'Browse ebooks in the ' . Formatter::ToPlainText($collectionName) . ' collection'; - $pageDescription = 'A list of ebooks in the ' . Formatter::ToPlainText($collectionName) . ' collection'; - $pageHeader = 'Ebooks in the ' . Formatter::ToPlainText($collectionName) . ' collection'; + if($collectionObject !== null){ + $collectionName = preg_replace('/^The /ius', '', $collectionObject->Name) ?? ''; + $collectionType = $collectionObject->Type ?? '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{ $pageTitle = 'Browse Standard Ebooks';