From 7c86f5c11624b966d7f68bcb34160f156e7671dd Mon Sep 17 00:00:00 2001 From: Laurent Constantin Date: Sat, 17 Aug 2024 09:56:37 +0200 Subject: [PATCH] Update bookmark when reading online --- src/Controller/BookController.php | 4 +++- src/Service/BookProgressionService.php | 28 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Controller/BookController.php b/src/Controller/BookController.php index 3cfc95bb..4899e538 100644 --- a/src/Controller/BookController.php +++ b/src/Controller/BookController.php @@ -328,16 +328,18 @@ public function relocate(Book $book, BookFileSystemManager $fileSystemManager, E private function updateProgression(Request $request, Book $book, User $user): JsonResponse { try { - /** @var array{percent?:string|float, cfi?:string} $json */ + /** @var array{percent?:string|float, cpi?:string} $json */ $json = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR); } catch (\JsonException $e) { throw new BadRequestException('Invalid percent in json', 0, $e); } $percent = $json['percent'] ?? null; + $cpi = $json['cpi'] ?? null; $percent = $percent === null ? null : floatval((string) $percent); if ($percent !== null && $percent <= 1.0 && $percent >= 0) { $this->bookProgressionService->setProgression($book, $user, $percent) + ->updateBookMarkFromProgressionAndCpi($book, $user, $percent, $cpi) ->flush(); return new JsonResponse([ diff --git a/src/Service/BookProgressionService.php b/src/Service/BookProgressionService.php index 65af053f..bdc5f8f4 100644 --- a/src/Service/BookProgressionService.php +++ b/src/Service/BookProgressionService.php @@ -4,6 +4,7 @@ use App\Entity\Book; use App\Entity\BookInteraction; +use App\Entity\BookmarkUser; use App\Entity\User; use Doctrine\ORM\EntityManagerInterface; use Kiwilan\Ebook\Ebook; @@ -18,6 +19,33 @@ public function __construct( ) { } + public function updateBookMarkFromProgressionAndCpi(Book $book, User $user, float $progress, ?string $cpi, ?float $sourcePercent = null): self + { + if ($cpi === null) { + return $this; + } + + $nbPages = $this->processPageNumber($book); + if ($nbPages === null || $nbPages === 0) { + return $this; + } + + $bookMark = $user->getBookmarkForBook($book); + if (!$bookMark instanceof BookmarkUser) { + $bookMark = new BookmarkUser($book, $user); + $this->em->persist($bookMark); + $user->addBookmarkUser($bookMark); + } + + $bookMark->setPercent($progress); + $bookMark->setSourcePercent($sourcePercent); + $bookMark->setLocationValue('kobo.1.1'); + $bookMark->setLocationType('KoboSpan'); + $bookMark->setLocationSource($cpi); + + return $this; + } + /** * @return float|null Progression between 0 and 1 or null if the total number of pages of a book is unknown */