3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\UserEntityWatchOptions;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Repos\ChapterRepo;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\Cloner;
11 use BookStack\Entities\Tools\HierarchyTransformer;
12 use BookStack\Entities\Tools\NextPreviousContentLocator;
13 use BookStack\Exceptions\MoveOperationException;
14 use BookStack\Exceptions\NotFoundException;
15 use BookStack\Exceptions\NotifyException;
16 use BookStack\Exceptions\PermissionsException;
17 use BookStack\Http\Controller;
18 use BookStack\References\ReferenceFetcher;
19 use Illuminate\Http\Request;
20 use Illuminate\Validation\ValidationException;
23 class ChapterController extends Controller
25 protected ChapterRepo $chapterRepo;
26 protected ReferenceFetcher $referenceFetcher;
28 public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
30 $this->chapterRepo = $chapterRepo;
31 $this->referenceFetcher = $referenceFetcher;
35 * Show the form for creating a new chapter.
37 public function create(string $bookSlug)
39 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
40 $this->checkOwnablePermission('chapter-create', $book);
42 $this->setPageTitle(trans('entities.chapters_create'));
44 return view('chapters.create', ['book' => $book, 'current' => $book]);
48 * Store a newly created chapter in storage.
50 * @throws ValidationException
52 public function store(Request $request, string $bookSlug)
54 $this->validate($request, [
55 'name' => ['required', 'string', 'max:255'],
58 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
59 $this->checkOwnablePermission('chapter-create', $book);
61 $chapter = $this->chapterRepo->create($request->all(), $book);
63 return redirect($chapter->getUrl());
67 * Display the specified chapter.
69 public function show(string $bookSlug, string $chapterSlug)
71 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
72 $this->checkOwnablePermission('chapter-view', $chapter);
74 $sidebarTree = (new BookContents($chapter->book))->getTree();
75 $pages = $chapter->getVisiblePages();
76 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
77 View::incrementFor($chapter);
79 $this->setPageTitle($chapter->getShortName());
81 return view('chapters.show', [
82 'book' => $chapter->book,
83 'chapter' => $chapter,
84 'current' => $chapter,
85 'sidebarTree' => $sidebarTree,
86 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
88 'next' => $nextPreviousLocator->getNext(),
89 'previous' => $nextPreviousLocator->getPrevious(),
90 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
95 * Show the form for editing the specified chapter.
97 public function edit(string $bookSlug, string $chapterSlug)
99 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
100 $this->checkOwnablePermission('chapter-update', $chapter);
102 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
104 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
108 * Update the specified chapter in storage.
110 * @throws NotFoundException
112 public function update(Request $request, string $bookSlug, string $chapterSlug)
114 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
115 $this->checkOwnablePermission('chapter-update', $chapter);
117 $this->chapterRepo->update($chapter, $request->all());
119 return redirect($chapter->getUrl());
123 * Shows the page to confirm deletion of this chapter.
125 * @throws NotFoundException
127 public function showDelete(string $bookSlug, string $chapterSlug)
129 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
130 $this->checkOwnablePermission('chapter-delete', $chapter);
132 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
134 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
138 * Remove the specified chapter from storage.
140 * @throws NotFoundException
143 public function destroy(string $bookSlug, string $chapterSlug)
145 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
146 $this->checkOwnablePermission('chapter-delete', $chapter);
148 $this->chapterRepo->destroy($chapter);
150 return redirect($chapter->book->getUrl());
154 * Show the page for moving a chapter.
156 * @throws NotFoundException
158 public function showMove(string $bookSlug, string $chapterSlug)
160 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
161 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
162 $this->checkOwnablePermission('chapter-update', $chapter);
163 $this->checkOwnablePermission('chapter-delete', $chapter);
165 return view('chapters.move', [
166 'chapter' => $chapter,
167 'book' => $chapter->book,
172 * Perform the move action for a chapter.
174 * @throws NotFoundException|NotifyException
176 public function move(Request $request, string $bookSlug, string $chapterSlug)
178 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
179 $this->checkOwnablePermission('chapter-update', $chapter);
180 $this->checkOwnablePermission('chapter-delete', $chapter);
182 $entitySelection = $request->get('entity_selection', null);
183 if ($entitySelection === null || $entitySelection === '') {
184 return redirect($chapter->getUrl());
188 $this->chapterRepo->move($chapter, $entitySelection);
189 } catch (PermissionsException $exception) {
190 $this->showPermissionError();
191 } catch (MoveOperationException $exception) {
192 $this->showErrorNotification(trans('errors.selected_book_not_found'));
194 return redirect($chapter->getUrl('/move'));
197 return redirect($chapter->getUrl());
201 * Show the view to copy a chapter.
203 * @throws NotFoundException
205 public function showCopy(string $bookSlug, string $chapterSlug)
207 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
208 $this->checkOwnablePermission('chapter-view', $chapter);
210 session()->flashInput(['name' => $chapter->name]);
212 return view('chapters.copy', [
213 'book' => $chapter->book,
214 'chapter' => $chapter,
219 * Create a copy of a chapter within the requested target destination.
221 * @throws NotFoundException
224 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
226 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
227 $this->checkOwnablePermission('chapter-view', $chapter);
229 $entitySelection = $request->get('entity_selection') ?: null;
230 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
232 if (is_null($newParentBook)) {
233 $this->showErrorNotification(trans('errors.selected_book_not_found'));
235 return redirect($chapter->getUrl('/copy'));
238 $this->checkOwnablePermission('chapter-create', $newParentBook);
240 $newName = $request->get('name') ?: $chapter->name;
241 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
242 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
244 return redirect($chapterCopy->getUrl());
248 * Convert the chapter to a book.
250 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
252 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
253 $this->checkOwnablePermission('chapter-update', $chapter);
254 $this->checkOwnablePermission('chapter-delete', $chapter);
255 $this->checkPermission('book-create-all');
257 $book = $transformer->transformChapterToBook($chapter);
259 return redirect($book->getUrl());