| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Repos; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\ActivityType; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  | use BookStack\Entities\Queries\BookQueries; | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  | use BookStack\Entities\Tools\TrashCan; | 
					
						
							| 
									
										
										
										
											2020-11-08 06:37:27 +08:00
										 |  |  | use BookStack\Facades\Activity; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BookshelfRepo | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-05 03:32:19 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected BaseRepo $baseRepo, | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         protected BookQueries $bookQueries, | 
					
						
							|  |  |  |         protected TrashCan $trashCan, | 
					
						
							| 
									
										
										
										
											2024-02-05 03:32:19 +08:00
										 |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new shelf in the system. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function create(array $input, array $bookIds): Bookshelf | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $shelf = new Bookshelf(); | 
					
						
							|  |  |  |         $this->baseRepo->create($shelf, $input); | 
					
						
							| 
									
										
										
										
											2022-06-15 22:05:08 +08:00
										 |  |  |         $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $this->updateBooks($shelf, $bookIds); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return $shelf; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-11-08 06:37:27 +08:00
										 |  |  |      * Update an existing shelf in the system using the given input. | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-10 22:19:18 +08:00
										 |  |  |     public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->baseRepo->update($shelf, $input); | 
					
						
							| 
									
										
										
										
											2020-04-10 22:19:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!is_null($bookIds)) { | 
					
						
							|  |  |  |             $this->updateBooks($shelf, $bookIds); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-15 22:05:08 +08:00
										 |  |  |         if (array_key_exists('image', $input)) { | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  |             $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return $shelf; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  |      * Update which books are assigned to this shelf by syncing the given book ids. | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * Function ensures the books are visible to the current user and existing. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function updateBooks(Bookshelf $shelf, array $bookIds) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $numericIDs = collect($bookIds)->map(function ($id) { | 
					
						
							|  |  |  |             return intval($id); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $syncData = $this->bookQueries->visibleForList() | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             ->whereIn('id', $bookIds) | 
					
						
							| 
									
										
										
										
											2021-11-06 00:18:06 +08:00
										 |  |  |             ->pluck('id') | 
					
						
							|  |  |  |             ->mapWithKeys(function ($bookId) use ($numericIDs) { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |                 return [$bookId => ['order' => $numericIDs->search($bookId)]]; | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $shelf->books()->sync($syncData); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Remove a bookshelf from the system. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function destroy(Bookshelf $shelf) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $this->trashCan->softDestroyShelf($shelf); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf); | 
					
						
							| 
									
										
										
										
											2024-02-08 00:37:36 +08:00
										 |  |  |         $this->trashCan->autoClearOld(); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |