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 Illuminate\Http\Request;
22 use Illuminate\Validation\ValidationException;
25 class ChapterController extends Controller
27 public function __construct(
28 protected ChapterRepo $chapterRepo,
29 protected ChapterQueries $queries,
30 protected EntityQueries $entityQueries,
31 protected ReferenceFetcher $referenceFetcher,
36 * Show the form for creating a new chapter.
38 public function create(string $bookSlug)
40 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
41 $this->checkOwnablePermission('chapter-create', $book);
43 $this->setPageTitle(trans('entities.chapters_create'));
45 return view('chapters.create', [
52 * Store a newly created chapter in storage.
54 * @throws ValidationException
56 public function store(Request $request, string $bookSlug)
58 $validated = $this->validate($request, [
59 'name' => ['required', 'string', 'max:255'],
60 'description_html' => ['string', 'max:2000'],
62 'default_template_id' => ['nullable', 'integer'],
65 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
66 $this->checkOwnablePermission('chapter-create', $book);
68 $chapter = $this->chapterRepo->create($validated, $book);
70 return redirect($chapter->getUrl());
74 * Display the specified chapter.
76 public function show(string $bookSlug, string $chapterSlug)
78 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
79 $this->checkOwnablePermission('chapter-view', $chapter);
81 $sidebarTree = (new BookContents($chapter->book))->getTree();
82 $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)->get();
84 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
85 View::incrementFor($chapter);
87 $this->setPageTitle($chapter->getShortName());
89 return view('chapters.show', [
90 'book' => $chapter->book,
91 'chapter' => $chapter,
92 'current' => $chapter,
93 'sidebarTree' => $sidebarTree,
94 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
96 'next' => $nextPreviousLocator->getNext(),
97 'previous' => $nextPreviousLocator->getPrevious(),
98 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
103 * Show the form for editing the specified chapter.
105 public function edit(string $bookSlug, string $chapterSlug)
107 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
108 $this->checkOwnablePermission('chapter-update', $chapter);
110 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
112 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
116 * Update the specified chapter in storage.
118 * @throws NotFoundException
120 public function update(Request $request, string $bookSlug, string $chapterSlug)
122 $validated = $this->validate($request, [
123 'name' => ['required', 'string', 'max:255'],
124 'description_html' => ['string', 'max:2000'],
126 'default_template_id' => ['nullable', 'integer'],
129 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
130 $this->checkOwnablePermission('chapter-update', $chapter);
132 $this->chapterRepo->update($chapter, $validated);
134 return redirect($chapter->getUrl());
138 * Shows the page to confirm deletion of this chapter.
140 * @throws NotFoundException
142 public function showDelete(string $bookSlug, string $chapterSlug)
144 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
145 $this->checkOwnablePermission('chapter-delete', $chapter);
147 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
149 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
153 * Remove the specified chapter from storage.
155 * @throws NotFoundException
158 public function destroy(string $bookSlug, string $chapterSlug)
160 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
161 $this->checkOwnablePermission('chapter-delete', $chapter);
163 $this->chapterRepo->destroy($chapter);
165 return redirect($chapter->book->getUrl());
169 * Show the page for moving a chapter.
171 * @throws NotFoundException
173 public function showMove(string $bookSlug, string $chapterSlug)
175 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
176 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
177 $this->checkOwnablePermission('chapter-update', $chapter);
178 $this->checkOwnablePermission('chapter-delete', $chapter);
180 return view('chapters.move', [
181 'chapter' => $chapter,
182 'book' => $chapter->book,
187 * Perform the move action for a chapter.
189 * @throws NotFoundException|NotifyException
191 public function move(Request $request, string $bookSlug, string $chapterSlug)
193 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
194 $this->checkOwnablePermission('chapter-update', $chapter);
195 $this->checkOwnablePermission('chapter-delete', $chapter);
197 $entitySelection = $request->get('entity_selection', null);
198 if ($entitySelection === null || $entitySelection === '') {
199 return redirect($chapter->getUrl());
203 $this->chapterRepo->move($chapter, $entitySelection);
204 } catch (PermissionsException $exception) {
205 $this->showPermissionError();
206 } catch (MoveOperationException $exception) {
207 $this->showErrorNotification(trans('errors.selected_book_not_found'));
209 return redirect($chapter->getUrl('/move'));
212 return redirect($chapter->getUrl());
216 * Show the view to copy a chapter.
218 * @throws NotFoundException
220 public function showCopy(string $bookSlug, string $chapterSlug)
222 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
223 $this->checkOwnablePermission('chapter-view', $chapter);
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);
242 $this->checkOwnablePermission('chapter-view', $chapter);
244 $entitySelection = $request->get('entity_selection') ?: null;
245 $newParentBook = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $chapter->getParent();
247 if (!$newParentBook instanceof Book) {
248 $this->showErrorNotification(trans('errors.selected_book_not_found'));
250 return redirect($chapter->getUrl('/copy'));
253 $this->checkOwnablePermission('chapter-create', $newParentBook);
255 $newName = $request->get('name') ?: $chapter->name;
256 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
257 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
259 return redirect($chapterCopy->getUrl());
263 * Convert the chapter to a book.
265 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
267 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
268 $this->checkOwnablePermission('chapter-update', $chapter);
269 $this->checkOwnablePermission('chapter-delete', $chapter);
270 $this->checkPermission('book-create-all');
272 $book = $transformer->transformChapterToBook($chapter);
274 return redirect($book->getUrl());