From 5985939cd516ac633fae938a65652bd0918f7ed8 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sat, 30 Apr 2022 13:19:02 -0500 Subject: [PATCH] Make 'more ebooks' carousel in ebook detail page show ebooks from a random tag of the current ebook --- lib/Library.php | 7 ++++++- www/ebooks/ebook.php | 27 +++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/Library.php b/lib/Library.php index 547ab60e..46ee082f 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -116,7 +116,12 @@ class Library{ * @return array */ public static function GetEbooksByTag(string $tag): array{ - return self::GetFromApcu('tag-' . $tag); + try{ + return apcu_fetch('tag-' . $tag) ?? []; + } + catch(Safe\Exceptions\ApcuException $ex){ + return []; + } } /** diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php index 00ffb73a..358111db 100644 --- a/www/ebooks/ebook.php +++ b/www/ebooks/ebook.php @@ -42,8 +42,17 @@ try{ } // Generate the bottom carousel. + // Pick a random tag from this ebook, and get ebooks in the same tag $carousel = []; - $ebooks = Library::GetEbooks(); + $ebooks = []; + if(sizeof($ebook->Tags) > 0){ + $carouselTag = $ebook->Tags[rand(0, sizeof($ebook->Tags) - 1)]; + $ebooks = Library::GetEbooksByTag(strtolower($carouselTag->Name)); + } + else{ + $ebooks = Library::GetEbooks(); + } + shuffle($ebooks); $targetCarouselSize = 5; @@ -51,13 +60,15 @@ try{ $targetCarouselSize = sizeof($ebooks) - 1; } - $i = 0; - while(sizeof($carousel) < $targetCarouselSize){ - if(isset($ebooks[$i]) && $ebooks[$i]->Url !== $ebook->Url){ - $carousel[] = $ebooks[$i]; - } + if($targetCarouselSize > 0){ + $i = 0; + while(sizeof($carousel) < $targetCarouselSize){ + if(isset($ebooks[$i]) && $ebooks[$i]->Url !== $ebook->Url){ + $carousel[] = $ebooks[$i]; + } - $i++; + $i++; + } } } catch(Exceptions\SeeOtherEbookException $ex){ @@ -313,7 +324,7 @@ catch(Exceptions\InvalidEbookException $ex){ 0){ ?>