3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Queries\BookshelfQueries;
11 protected string $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
13 public function __construct(
14 protected BookshelfQueries $shelfQueries,
19 * Get the current bookshelf context for the given book.
21 public function getContextualShelfForBook(Book $book): ?Bookshelf
23 $contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
25 if (!is_int($contextBookshelfId)) {
29 $shelf = $this->shelfQueries->findVisibleById($contextBookshelfId);
30 $shelfContainsBook = $shelf && $shelf->contains($book);
32 return $shelfContainsBook ? $shelf : null;
36 * Store the current contextual shelf ID.
38 public function setShelfContext(int $shelfId): void
40 session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
44 * Clear the session stored shelf context id.
46 public function clearShelfContext(): void
48 session()->forget($this->KEY_SHELF_CONTEXT_ID);