1 <?php namespace BookStack\Entities\Repos;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Managers\BookContents;
6 use BookStack\Entities\Managers\TrashCan;
7 use BookStack\Exceptions\MoveOperationException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Exceptions\NotifyException;
11 use Illuminate\Contracts\Container\BindingResolutionException;
12 use Illuminate\Database\Eloquent\Builder;
13 use Illuminate\Support\Collection;
21 * ChapterRepo constructor.
24 public function __construct(BaseRepo $baseRepo)
26 $this->baseRepo = $baseRepo;
30 * Get a chapter via the slug.
31 * @throws NotFoundException
33 public function getBySlug(string $bookSlug, string $chapterSlug): Chapter
35 $chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->first();
37 if ($chapter === null) {
38 throw new NotFoundException(trans('errors.chapter_not_found'));
45 * Create a new chapter in the system.
47 public function create(array $input, Book $parentBook): Chapter
49 $chapter = new Chapter();
50 $chapter->book_id = $parentBook->id;
51 $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
52 $this->baseRepo->create($chapter, $input);
57 * Update the given chapter.
59 public function update(Chapter $chapter, array $input): Chapter
61 $this->baseRepo->update($chapter, $input);
66 * Update the permissions of a chapter.
68 public function updatePermissions(Chapter $chapter, bool $restricted, Collection $permissions = null)
70 $this->baseRepo->updatePermissions($chapter, $restricted, $permissions);
74 * Remove a chapter from the system.
77 public function destroy(Chapter $chapter)
79 $trashCan = new TrashCan();
80 $trashCan->destroyChapter($chapter);
84 * Move the given chapter into a new parent book.
85 * The $parentIdentifier must be a string of the following format:
86 * 'book:<id>' (book:5)
87 * @throws MoveOperationException
89 public function move(Chapter $chapter, string $parentIdentifier): Book
91 $stringExploded = explode(':', $parentIdentifier);
92 $entityType = $stringExploded[0];
93 $entityId = intval($stringExploded[1]);
95 if ($entityType !== 'book') {
96 throw new MoveOperationException('Chapters can only be moved into books');
99 $parent = Book::visible()->where('id', '=', $entityId)->first();
100 if ($parent === null) {
101 throw new MoveOperationException('Book to move chapter into not found');
104 $chapter->changeBook($parent->id);
105 $chapter->rebuildPermissions();