| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Tools; | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  | class ShelfContext | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the current bookshelf context for the given book. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     public function getContextualShelfForBook(Book $book): ?Bookshelf | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  |         $contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null); | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         if (!is_int($contextBookshelfId)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 03:55:32 +08:00
										 |  |  |         /** @var Bookshelf $shelf */ | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $shelf = Bookshelf::visible()->find($contextBookshelfId); | 
					
						
							|  |  |  |         $shelfContainsBook = $shelf && $shelf->contains($book); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $shelfContainsBook ? $shelf : null; | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Store the current contextual shelf ID. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setShelfContext(int $shelfId) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  |         session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId); | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Clear the session stored shelf context id. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function clearShelfContext() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  |         session()->forget($this->KEY_SHELF_CONTEXT_ID); | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-05 21:54:37 +08:00
										 |  |  | } |