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;
10 use Illuminate\Support\Collection;
18 * ChapterRepo constructor.
20 public function __construct(BaseRepo $baseRepo)
22 $this->baseRepo = $baseRepo;
26 * 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);
53 * Update the given chapter.
55 public function update(Chapter $chapter, array $input): Chapter
57 $this->baseRepo->update($chapter, $input);
62 * Update the permissions of a chapter.
64 public function updatePermissions(Chapter $chapter, bool $restricted, Collection $permissions = null)
66 $this->baseRepo->updatePermissions($chapter, $restricted, $permissions);
70 * Remove a chapter from the system.
73 public function destroy(Chapter $chapter)
75 $trashCan = new TrashCan();
76 $trashCan->softDestroyChapter($chapter);
77 $trashCan->autoClearOld();
81 * Move the given chapter into a new parent book.
82 * The $parentIdentifier must be a string of the following format:
83 * 'book:<id>' (book:5)
84 * @throws MoveOperationException
86 public function move(Chapter $chapter, string $parentIdentifier): Book
88 $stringExploded = explode(':', $parentIdentifier);
89 $entityType = $stringExploded[0];
90 $entityId = intval($stringExploded[1]);
92 if ($entityType !== 'book') {
93 throw new MoveOperationException('Chapters can only be moved into books');
96 $parent = Book::visible()->where('id', '=', $entityId)->first();
97 if ($parent === null) {
98 throw new MoveOperationException('Book to move chapter into not found');
101 $chapter->changeBook($parent->id);
102 $chapter->rebuildPermissions();