| 
									
										
										
										
											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\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  | use BookStack\Entities\Tools\BookContents; | 
					
						
							|  |  |  | use BookStack\Entities\Tools\TrashCan; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use BookStack\Exceptions\MoveOperationException; | 
					
						
							|  |  |  | use BookStack\Exceptions\NotFoundException; | 
					
						
							| 
									
										
										
										
											2022-01-06 00:11:11 +08:00
										 |  |  | use BookStack\Exceptions\PermissionsException; | 
					
						
							| 
									
										
										
										
											2020-11-08 06:37:27 +08:00
										 |  |  | use BookStack\Facades\Activity; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ChapterRepo | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-21 22:40:53 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected BaseRepo $baseRepo | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a chapter via the slug. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws NotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBySlug(string $bookSlug, string $chapterSlug): Chapter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($chapter === null) { | 
					
						
							|  |  |  |             throw new NotFoundException(trans('errors.chapter_not_found')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $chapter; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new chapter in the system. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function create(array $input, Book $parentBook): Chapter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapter = new Chapter(); | 
					
						
							|  |  |  |         $chapter->book_id = $parentBook->id; | 
					
						
							| 
									
										
										
										
											2023-08-21 22:40:53 +08:00
										 |  |  |         $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $this->baseRepo->create($chapter, $input); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::CHAPTER_CREATE, $chapter); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return $chapter; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the given chapter. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(Chapter $chapter, array $input): Chapter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->baseRepo->update($chapter, $input); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::CHAPTER_UPDATE, $chapter); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return $chapter; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Remove a chapter from the system. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function destroy(Chapter $chapter) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $trashCan = new TrashCan(); | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |         $trashCan->softDestroyChapter($chapter); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::CHAPTER_DELETE, $chapter); | 
					
						
							| 
									
										
										
										
											2020-11-07 21:58:23 +08:00
										 |  |  |         $trashCan->autoClearOld(); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Move the given chapter into a new parent book. | 
					
						
							|  |  |  |      * The $parentIdentifier must be a string of the following format: | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * 'book:<id>' (book:5). | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      * @throws MoveOperationException | 
					
						
							| 
									
										
										
										
											2022-01-06 00:11:11 +08:00
										 |  |  |      * @throws PermissionsException | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function move(Chapter $chapter, string $parentIdentifier): Book | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  |         $parent = $this->findParentByIdentifier($parentIdentifier); | 
					
						
							|  |  |  |         if (is_null($parent)) { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             throw new MoveOperationException('Book to move chapter into not found'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 00:11:11 +08:00
										 |  |  |         if (!userCan('chapter-create', $parent)) { | 
					
						
							|  |  |  |             throw new PermissionsException('User does not have permission to create a chapter within the chosen book'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-05 23:42:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $chapter->changeBook($parent->id); | 
					
						
							|  |  |  |         $chapter->rebuildPermissions(); | 
					
						
							| 
									
										
										
										
											2021-12-12 01:29:33 +08:00
										 |  |  |         Activity::add(ActivityType::CHAPTER_MOVE, $chapter); | 
					
						
							| 
									
										
										
										
											2020-11-08 06:37:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return $parent; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Find a page parent entity via an identifier string in the format: | 
					
						
							|  |  |  |      * {type}:{id} | 
					
						
							|  |  |  |      * Example: (book:5). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws MoveOperationException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findParentByIdentifier(string $identifier): ?Book | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $stringExploded = explode(':', $identifier); | 
					
						
							|  |  |  |         $entityType = $stringExploded[0]; | 
					
						
							|  |  |  |         $entityId = intval($stringExploded[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($entityType !== 'book') { | 
					
						
							|  |  |  |             throw new MoveOperationException('Chapters can only be in books'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Book::visible()->where('id', '=', $entityId)->first(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | } |