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