| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | <?php namespace BookStack\Entities\Managers; | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use BookStack\Entities\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Bookshelf; | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  | use Illuminate\Session\Store; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | class EntityContext | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     protected $session; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * EntityContextManager constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     public function __construct(Store $session) | 
					
						
							| 
									
										
										
										
											2019-04-08 01:28:11 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->session = $session; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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
										 |  |  |     { | 
					
						
							|  |  |  |         $contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |         $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. | 
					
						
							|  |  |  |      * @param int $shelfId | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function setShelfContext(int $shelfId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Clear the session stored shelf context id. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function clearShelfContext() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->session->forget($this->KEY_SHELF_CONTEXT_ID); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-05 21:54:37 +08:00
										 |  |  | } |