| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Repos; | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 06:37:27 +08:00
										 |  |  | use BookStack\Actions\ActivityType; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use BookStack\Actions\TagRepo; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  | use BookStack\Entities\Tools\TrashCan; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use BookStack\Exceptions\ImageUploadException; | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  | use BookStack\Exceptions\NotFoundException; | 
					
						
							| 
									
										
										
										
											2020-11-08 06:37:27 +08:00
										 |  |  | use BookStack\Facades\Activity; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use BookStack\Uploads\ImageRepo; | 
					
						
							|  |  |  | use Exception; | 
					
						
							|  |  |  | use Illuminate\Contracts\Pagination\LengthAwarePaginator; | 
					
						
							|  |  |  | use Illuminate\Http\UploadedFile; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | class BookRepo | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     protected $baseRepo; | 
					
						
							|  |  |  |     protected $tagRepo; | 
					
						
							|  |  |  |     protected $imageRepo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * BookRepo constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->baseRepo = $baseRepo; | 
					
						
							|  |  |  |         $this->tagRepo = $tagRepo; | 
					
						
							|  |  |  |         $this->imageRepo = $imageRepo; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get all books in a paginated format. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-01-11 07:12:51 +08:00
										 |  |  |         return Book::visible()->with('cover')->orderBy($sort, $order)->paginate($count); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the books that were most recently viewed by this user. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getRecentlyViewed(int $count = 20): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Book::visible()->withLastView() | 
					
						
							|  |  |  |             ->having('last_viewed_at', '>', 0) | 
					
						
							|  |  |  |             ->orderBy('last_viewed_at', 'desc') | 
					
						
							|  |  |  |             ->take($count)->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the most popular books in the system. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getPopular(int $count = 20): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Book::visible()->withViewCount() | 
					
						
							|  |  |  |             ->having('view_count', '>', 0) | 
					
						
							|  |  |  |             ->orderBy('view_count', 'desc') | 
					
						
							|  |  |  |             ->take($count)->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the most recently created books from the system. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getRecentlyCreated(int $count = 20): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Book::visible()->orderBy('created_at', 'desc') | 
					
						
							|  |  |  |             ->take($count)->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * Get a book by its slug. | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getBySlug(string $slug): Book | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $book = Book::visible()->where('slug', '=', $slug)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($book === null) { | 
					
						
							|  |  |  |             throw new NotFoundException(trans('errors.book_not_found')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  |         return $book; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * Create a new book in the system. | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     public function create(array $input): Book | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $book = new Book(); | 
					
						
							|  |  |  |         $this->baseRepo->create($book, $input); | 
					
						
							| 
									
										
										
										
											2022-06-14 23:42:29 +08:00
										 |  |  |         $this->baseRepo->updateCoverImage($book, $input['image'] ?? null); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::BOOK_CREATE, $book); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return $book; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update the given book. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(Book $book, array $input): Book | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->baseRepo->update($book, $input); | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-14 23:42:29 +08:00
										 |  |  |         if (array_key_exists('image', $input)) { | 
					
						
							| 
									
										
										
										
											2022-06-14 00:20:21 +08:00
										 |  |  |             $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::BOOK_UPDATE, $book); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return $book; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update the given book's cover image, or clear it. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws ImageUploadException | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-02-16 02:38:36 +08:00
										 |  |  |     public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false) | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage); | 
					
						
							| 
									
										
										
										
											2019-09-16 06:28:23 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Remove a book from the system. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |      * @throws Exception | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function destroy(Book $book) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $trashCan = new TrashCan(); | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |         $trashCan->softDestroyBook($book); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::BOOK_DELETE, $book); | 
					
						
							| 
									
										
										
										
											2020-11-08 06:37:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 21:58:23 +08:00
										 |  |  |         $trashCan->autoClearOld(); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |