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\Permissions\Permission;
21 use BookStack\References\ReferenceFetcher;
22 use BookStack\Util\DatabaseTransaction;
23 use Illuminate\Http\Request;
24 use Illuminate\Validation\ValidationException;
27 class ChapterController extends Controller
29 public function __construct(
30 protected ChapterRepo $chapterRepo,
31 protected ChapterQueries $queries,
32 protected EntityQueries $entityQueries,
33 protected ReferenceFetcher $referenceFetcher,
38 * Show the form for creating a new chapter.
40 public function create(string $bookSlug)
42 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
43 $this->checkOwnablePermission(Permission::ChapterCreate, $book);
45 $this->setPageTitle(trans('entities.chapters_create'));
47 return view('chapters.create', [
54 * Store a newly created chapter in storage.
56 * @throws ValidationException
58 public function store(Request $request, string $bookSlug)
60 $validated = $this->validate($request, [
61 'name' => ['required', 'string', 'max:255'],
62 'description_html' => ['string', 'max:2000'],
64 'default_template_id' => ['nullable', 'integer'],
67 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
68 $this->checkOwnablePermission(Permission::ChapterCreate, $book);
70 $chapter = $this->chapterRepo->create($validated, $book);
72 return redirect($chapter->getUrl());
76 * Display the specified chapter.
78 public function show(string $bookSlug, string $chapterSlug)
80 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
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(Permission::ChapterUpdate, $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(Permission::ChapterUpdate, $chapter);
133 $chapter = $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(Permission::ChapterDelete, $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(Permission::ChapterDelete, $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(Permission::ChapterUpdate, $chapter);
179 $this->checkOwnablePermission(Permission::ChapterDelete, $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(Permission::ChapterUpdate, $chapter);
196 $this->checkOwnablePermission(Permission::ChapterDelete, $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);
225 session()->flashInput(['name' => $chapter->name]);
227 return view('chapters.copy', [
228 'book' => $chapter->book,
229 'chapter' => $chapter,
234 * Create a copy of a chapter within the requested target destination.
236 * @throws NotFoundException
239 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
241 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
243 $entitySelection = $request->get('entity_selection') ?: null;
244 $newParentBook = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $chapter->getParent();
246 if (!$newParentBook instanceof Book) {
247 $this->showErrorNotification(trans('errors.selected_book_not_found'));
249 return redirect($chapter->getUrl('/copy'));
252 $this->checkOwnablePermission(Permission::ChapterCreate, $newParentBook);
254 $newName = $request->get('name') ?: $chapter->name;
255 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
256 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
258 return redirect($chapterCopy->getUrl());
262 * Convert the chapter to a book.
264 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
266 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
267 $this->checkOwnablePermission(Permission::ChapterUpdate, $chapter);
268 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
269 $this->checkPermission(Permission::BookCreateAll);
271 $book = (new DatabaseTransaction(function () use ($chapter, $transformer) {
272 return $transformer->transformChapterToBook($chapter);
275 return redirect($book->getUrl());