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\Queries\ChapterQueries;
9 use BookStack\Entities\Queries\EntityQueries;
10 use BookStack\Entities\Repos\ChapterRepo;
11 use BookStack\Entities\Tools\BookContents;
12 use BookStack\Entities\Tools\Cloner;
13 use BookStack\Entities\Tools\HierarchyTransformer;
14 use BookStack\Entities\Tools\NextPreviousContentLocator;
15 use BookStack\Exceptions\MoveOperationException;
16 use BookStack\Exceptions\NotFoundException;
17 use BookStack\Exceptions\NotifyException;
18 use BookStack\Exceptions\PermissionsException;
19 use BookStack\Http\Controller;
20 use BookStack\References\ReferenceFetcher;
21 use BookStack\Util\DatabaseTransaction;
22 use Illuminate\Http\Request;
23 use Illuminate\Validation\ValidationException;
26 class ChapterController extends Controller
28 public function __construct(
29 protected ChapterRepo $chapterRepo,
30 protected ChapterQueries $queries,
31 protected EntityQueries $entityQueries,
32 protected ReferenceFetcher $referenceFetcher,
37 * Show the form for creating a new chapter.
39 public function create(string $bookSlug)
41 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
42 $this->checkOwnablePermission('chapter-create', $book);
44 $this->setPageTitle(trans('entities.chapters_create'));
46 return view('chapters.create', [
53 * Store a newly created chapter in storage.
55 * @throws ValidationException
57 public function store(Request $request, string $bookSlug)
59 $validated = $this->validate($request, [
60 'name' => ['required', 'string', 'max:255'],
61 'description_html' => ['string', 'max:2000'],
63 'default_template_id' => ['nullable', 'integer'],
66 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
67 $this->checkOwnablePermission('chapter-create', $book);
69 $chapter = $this->chapterRepo->create($validated, $book);
71 return redirect($chapter->getUrl());
75 * Display the specified chapter.
77 public function show(string $bookSlug, string $chapterSlug)
79 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
80 $this->checkOwnablePermission('chapter-view', $chapter);
82 $sidebarTree = (new BookContents($chapter->book))->getTree();
83 $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)->get();
85 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
86 View::incrementFor($chapter);
88 $this->setPageTitle($chapter->getShortName());
90 return view('chapters.show', [
91 'book' => $chapter->book,
92 'chapter' => $chapter,
93 'current' => $chapter,
94 'sidebarTree' => $sidebarTree,
95 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
97 'next' => $nextPreviousLocator->getNext(),
98 'previous' => $nextPreviousLocator->getPrevious(),
99 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
104 * Show the form for editing the specified chapter.
106 public function edit(string $bookSlug, string $chapterSlug)
108 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
109 $this->checkOwnablePermission('chapter-update', $chapter);
111 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
113 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
117 * Update the specified chapter in storage.
119 * @throws NotFoundException
121 public function update(Request $request, string $bookSlug, string $chapterSlug)
123 $validated = $this->validate($request, [
124 'name' => ['required', 'string', 'max:255'],
125 'description_html' => ['string', 'max:2000'],
127 'default_template_id' => ['nullable', 'integer'],
130 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
131 $this->checkOwnablePermission('chapter-update', $chapter);
133 $this->chapterRepo->update($chapter, $validated);
135 return redirect($chapter->getUrl());
139 * Shows the page to confirm deletion of this chapter.
141 * @throws NotFoundException
143 public function showDelete(string $bookSlug, string $chapterSlug)
145 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
146 $this->checkOwnablePermission('chapter-delete', $chapter);
148 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
150 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
154 * Remove the specified chapter from storage.
156 * @throws NotFoundException
159 public function destroy(string $bookSlug, string $chapterSlug)
161 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
162 $this->checkOwnablePermission('chapter-delete', $chapter);
164 $this->chapterRepo->destroy($chapter);
166 return redirect($chapter->book->getUrl());
170 * Show the page for moving a chapter.
172 * @throws NotFoundException
174 public function showMove(string $bookSlug, string $chapterSlug)
176 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
177 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
178 $this->checkOwnablePermission('chapter-update', $chapter);
179 $this->checkOwnablePermission('chapter-delete', $chapter);
181 return view('chapters.move', [
182 'chapter' => $chapter,
183 'book' => $chapter->book,
188 * Perform the move action for a chapter.
190 * @throws NotFoundException|NotifyException
192 public function move(Request $request, string $bookSlug, string $chapterSlug)
194 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
195 $this->checkOwnablePermission('chapter-update', $chapter);
196 $this->checkOwnablePermission('chapter-delete', $chapter);
198 $entitySelection = $request->get('entity_selection', null);
199 if ($entitySelection === null || $entitySelection === '') {
200 return redirect($chapter->getUrl());
204 $this->chapterRepo->move($chapter, $entitySelection);
205 } catch (PermissionsException $exception) {
206 $this->showPermissionError();
207 } catch (MoveOperationException $exception) {
208 $this->showErrorNotification(trans('errors.selected_book_not_found'));
210 return redirect($chapter->getUrl('/move'));
213 return redirect($chapter->getUrl());
217 * Show the view to copy a chapter.
219 * @throws NotFoundException
221 public function showCopy(string $bookSlug, string $chapterSlug)
223 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
224 $this->checkOwnablePermission('chapter-view', $chapter);
226 session()->flashInput(['name' => $chapter->name]);
228 return view('chapters.copy', [
229 'book' => $chapter->book,
230 'chapter' => $chapter,
235 * Create a copy of a chapter within the requested target destination.
237 * @throws NotFoundException
240 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
242 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
243 $this->checkOwnablePermission('chapter-view', $chapter);
245 $entitySelection = $request->get('entity_selection') ?: null;
246 $newParentBook = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $chapter->getParent();
248 if (!$newParentBook instanceof Book) {
249 $this->showErrorNotification(trans('errors.selected_book_not_found'));
251 return redirect($chapter->getUrl('/copy'));
254 $this->checkOwnablePermission('chapter-create', $newParentBook);
256 $newName = $request->get('name') ?: $chapter->name;
257 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
258 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
260 return redirect($chapterCopy->getUrl());
264 * Convert the chapter to a book.
266 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
268 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
269 $this->checkOwnablePermission('chapter-update', $chapter);
270 $this->checkOwnablePermission('chapter-delete', $chapter);
271 $this->checkPermission('book-create-all');
273 $book = (new DatabaseTransaction(function () use ($chapter, $transformer) {
274 return $transformer->transformChapterToBook($chapter);
277 return redirect($book->getUrl());