]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/ShelfContext.php
Only output hidden user filters when not set to 'me'
[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         /** @var Bookshelf $shelf */
24         $shelf = Bookshelf::visible()->find($contextBookshelfId);
25         $shelfContainsBook = $shelf && $shelf->contains($book);
26
27         return $shelfContainsBook ? $shelf : null;
28     }
29
30     /**
31      * Store the current contextual shelf ID.
32      */
33     public function setShelfContext(int $shelfId)
34     {
35         session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
36     }
37
38     /**
39      * Clear the session stored shelf context id.
40      */
41     public function clearShelfContext()
42     {
43         session()->forget($this->KEY_SHELF_CONTEXT_ID);
44     }
45 }