mirror of
https://github.com/standardebooks/web.git
synced 2025-07-13 01:52:02 -04:00
Get carousel books from the DB
This commit is contained in:
parent
402dae95ff
commit
2d5e66f2f2
2 changed files with 30 additions and 40 deletions
|
@ -126,19 +126,6 @@ class Library{
|
||||||
return self::GetFromApcu('author-' . $wwwFilesystemPath);
|
return self::GetFromApcu('author-' . $wwwFilesystemPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array<Ebook>
|
|
||||||
*/
|
|
||||||
public static function GetEbooksByTag(string $tag): array{
|
|
||||||
try{
|
|
||||||
/** @var array<Ebook> */
|
|
||||||
return apcu_fetch('tag-' . $tag) ?? [];
|
|
||||||
}
|
|
||||||
catch(Safe\Exceptions\ApcuException){
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<Collection>
|
* @return array<Collection>
|
||||||
* @throws Exceptions\AppException
|
* @throws Exceptions\AppException
|
||||||
|
@ -173,6 +160,34 @@ class Library{
|
||||||
return $ebooks;
|
return $ebooks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<Ebook>
|
||||||
|
*/
|
||||||
|
public static function GetRelatedEbooks(Ebook $ebook, int $count, ?EbookTag $relatedTag): array{
|
||||||
|
if($relatedTag !== null){
|
||||||
|
$relatedEbooks = Db::Query('
|
||||||
|
SELECT e.*
|
||||||
|
from Ebooks e
|
||||||
|
inner join EbookTags et using (EbookId)
|
||||||
|
where et.TagId = ?
|
||||||
|
and et.EbookId != ?
|
||||||
|
order by RAND()
|
||||||
|
limit ?
|
||||||
|
', [$relatedTag->TagId, $ebook->EbookId, $count], Ebook::class);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$relatedEbooks = Db::Query('
|
||||||
|
SELECT *
|
||||||
|
from Ebooks
|
||||||
|
where EbookId != ?
|
||||||
|
order by RAND()
|
||||||
|
limit ?
|
||||||
|
', [$ebook->EbookId, $count], Ebook::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $relatedEbooks;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<EbookTag>
|
* @return array<EbookTag>
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -12,6 +12,7 @@ $scanSources = [];
|
||||||
$otherSources = [];
|
$otherSources = [];
|
||||||
$carousel = [];
|
$carousel = [];
|
||||||
$carouselTag = null;
|
$carouselTag = null;
|
||||||
|
$targetCarouselSize = 5;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
$urlPath = trim(str_replace('.', '', HttpInput::Str(GET, 'url-path') ?? ''), '/'); // Contains the portion of the URL (without query string) that comes after https://standardebooks.org/ebooks/
|
$urlPath = trim(str_replace('.', '', HttpInput::Str(GET, 'url-path') ?? ''), '/'); // Contains the portion of the URL (without query string) that comes after https://standardebooks.org/ebooks/
|
||||||
|
@ -64,36 +65,10 @@ try{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the bottom carousel.
|
|
||||||
// Pick a random tag from this ebook, and get ebooks in the same tag
|
|
||||||
$ebooks = [];
|
|
||||||
if(sizeof($ebook->Tags) > 0){
|
if(sizeof($ebook->Tags) > 0){
|
||||||
$carouselTag = $ebook->Tags[rand(0, sizeof($ebook->Tags) - 1)];
|
$carouselTag = $ebook->Tags[rand(0, sizeof($ebook->Tags) - 1)];
|
||||||
$ebooks = Library::GetEbooksByTag(strtolower($carouselTag->Name));
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$ebooks = Library::GetEbooks();
|
|
||||||
}
|
|
||||||
|
|
||||||
shuffle($ebooks);
|
|
||||||
|
|
||||||
$targetCarouselSize = 5;
|
|
||||||
// Use <= here because we want to exclude the ebook we're looking at from the carousel.
|
|
||||||
// One of the matching ebooks will always be the current ebook.
|
|
||||||
if(sizeof($ebooks) <= $targetCarouselSize){
|
|
||||||
$targetCarouselSize = sizeof($ebooks) - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($targetCarouselSize > 0){
|
|
||||||
$i = 0;
|
|
||||||
while(sizeof($carousel) < $targetCarouselSize){
|
|
||||||
if(isset($ebooks[$i]) && $ebooks[$i]->Url !== $ebook->Url){
|
|
||||||
$carousel[] = $ebooks[$i];
|
|
||||||
}
|
|
||||||
|
|
||||||
$i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$carousel = Library::GetRelatedEbooks($ebook, $targetCarouselSize, $carouselTag);
|
||||||
}
|
}
|
||||||
catch(Exceptions\SeeOtherEbookException $ex){
|
catch(Exceptions\SeeOtherEbookException $ex){
|
||||||
http_response_code(301);
|
http_response_code(301);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue