3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\TrashCan;
11 use BookStack\Exceptions\MoveOperationException;
12 use BookStack\Exceptions\NotFoundException;
13 use BookStack\Exceptions\PermissionsException;
14 use BookStack\Facades\Activity;
19 public function __construct(
20 protected BaseRepo $baseRepo
25 * Get a chapter via the slug.
27 * @throws NotFoundException
29 public function getBySlug(string $bookSlug, string $chapterSlug): Chapter
31 $chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->first();
33 if ($chapter === null) {
34 throw new NotFoundException(trans('errors.chapter_not_found'));
41 * Create a new chapter in the system.
43 public function create(array $input, Book $parentBook): Chapter
45 $chapter = new Chapter();
46 $chapter->book_id = $parentBook->id;
47 $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
48 $this->baseRepo->create($chapter, $input);
49 Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
55 * Update the given chapter.
57 public function update(Chapter $chapter, array $input): Chapter
59 $this->baseRepo->update($chapter, $input);
60 Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
66 * Remove a chapter from the system.
70 public function destroy(Chapter $chapter)
72 $trashCan = new TrashCan();
73 $trashCan->softDestroyChapter($chapter);
74 Activity::add(ActivityType::CHAPTER_DELETE, $chapter);
75 $trashCan->autoClearOld();
79 * Move the given chapter into a new parent book.
80 * The $parentIdentifier must be a string of the following format:
81 * 'book:<id>' (book:5).
83 * @throws MoveOperationException
84 * @throws PermissionsException
86 public function move(Chapter $chapter, string $parentIdentifier): Book
88 $parent = $this->findParentByIdentifier($parentIdentifier);
89 if (is_null($parent)) {
90 throw new MoveOperationException('Book to move chapter into not found');
93 if (!userCan('chapter-create', $parent)) {
94 throw new PermissionsException('User does not have permission to create a chapter within the chosen book');
97 $chapter->changeBook($parent->id);
98 $chapter->rebuildPermissions();
99 Activity::add(ActivityType::CHAPTER_MOVE, $chapter);
105 * Find a page parent entity via an identifier string in the format:
109 * @throws MoveOperationException
111 public function findParentByIdentifier(string $identifier): ?Book
113 $stringExploded = explode(':', $identifier);
114 $entityType = $stringExploded[0];
115 $entityId = intval($stringExploded[1]);
117 if ($entityType !== 'book') {
118 throw new MoveOperationException('Chapters can only be in books');
121 return Book::visible()->where('id', '=', $entityId)->first();