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){
$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
foreach($ebook->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;
}
}

View file

@ -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);
if(sizeof($ebooks) > 0){
$collectionObject = null;
// 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){
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';