]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/ShelfContext.php
Vectors: Added command to regenerate for all
[bookstack] / app / Entities / Tools / ShelfContext.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Queries\BookshelfQueries;
8
9 class ShelfContext
10 {
11     protected string $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
12
13     public function __construct(
14         protected BookshelfQueries $shelfQueries,
15     ) {
16     }
17
18     /**
19      * Get the current bookshelf context for the given book.
20      */
21     public function getContextualShelfForBook(Book $book): ?Bookshelf
22     {
23         $contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
24
25         if (!is_int($contextBookshelfId)) {
26             return null;
27         }
28
29         $shelf = $this->shelfQueries->findVisibleById($contextBookshelfId);
30         $shelfContainsBook = $shelf && $shelf->contains($book);
31
32         return $shelfContainsBook ? $shelf : null;
33     }
34
35     /**
36      * Store the current contextual shelf ID.
37      */
38     public function setShelfContext(int $shelfId): void
39     {
40         session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
41     }
42
43     /**
44      * Clear the session stored shelf context id.
45      */
46     public function clearShelfContext(): void
47     {
48         session()->forget($this->KEY_SHELF_CONTEXT_ID);
49     }
50 }