]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/ShelfContext.php
Fixed some typos and corrected grammar.
[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
8 class ShelfContext
9 {
10     protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
11
12     /**
13      * Get the current bookshelf context for the given book.
14      */
15     public function getContextualShelfForBook(Book $book): ?Bookshelf
16     {
17         $contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
18
19         if (!is_int($contextBookshelfId)) {
20             return null;
21         }
22
23         $shelf = Bookshelf::visible()->find($contextBookshelfId);
24         $shelfContainsBook = $shelf && $shelf->contains($book);
25
26         return $shelfContainsBook ? $shelf : null;
27     }
28
29     /**
30      * Store the current contextual shelf ID.
31      */
32     public function setShelfContext(int $shelfId)
33     {
34         session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
35     }
36
37     /**
38      * Clear the session stored shelf context id.
39      */
40     public function clearShelfContext()
41     {
42         session()->forget($this->KEY_SHELF_CONTEXT_ID);
43     }
44 }