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->findVisibleBySlug($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->findVisibleBySlug($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->findVisibleBySlugs($bookSlug, $chapterSlug);
79 $this->checkOwnablePermission('chapter-view', $chapter);
81 $sidebarTree = (new BookContents($chapter->book))->getTree();
82 $pages = $chapter->getVisiblePages();
83 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
84 View::incrementFor($chapter);
86 $this->setPageTitle($chapter->getShortName());
88 return view('chapters.show', [
89 'book' => $chapter->book,
90 'chapter' => $chapter,
91 'current' => $chapter,
92 'sidebarTree' => $sidebarTree,
93 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
95 'next' => $nextPreviousLocator->getNext(),
96 'previous' => $nextPreviousLocator->getPrevious(),
97 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
102 * Show the form for editing the specified chapter.
104 public function edit(string $bookSlug, string $chapterSlug)
106 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
107 $this->checkOwnablePermission('chapter-update', $chapter);
109 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
111 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
115 * Update the specified chapter in storage.
117 * @throws NotFoundException
119 public function update(Request $request, string $bookSlug, string $chapterSlug)
121 $validated = $this->validate($request, [
122 'name' => ['required', 'string', 'max:255'],
123 'description_html' => ['string', 'max:2000'],
125 'default_template_id' => ['nullable', 'integer'],
128 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
129 $this->checkOwnablePermission('chapter-update', $chapter);
131 $this->chapterRepo->update($chapter, $validated);
133 return redirect($chapter->getUrl());
137 * Shows the page to confirm deletion of this chapter.
139 * @throws NotFoundException
141 public function showDelete(string $bookSlug, string $chapterSlug)
143 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
144 $this->checkOwnablePermission('chapter-delete', $chapter);
146 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
148 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
152 * Remove the specified chapter from storage.
154 * @throws NotFoundException
157 public function destroy(string $bookSlug, string $chapterSlug)
159 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
160 $this->checkOwnablePermission('chapter-delete', $chapter);
162 $this->chapterRepo->destroy($chapter);
164 return redirect($chapter->book->getUrl());
168 * Show the page for moving a chapter.
170 * @throws NotFoundException
172 public function showMove(string $bookSlug, string $chapterSlug)
174 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
175 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
176 $this->checkOwnablePermission('chapter-update', $chapter);
177 $this->checkOwnablePermission('chapter-delete', $chapter);
179 return view('chapters.move', [
180 'chapter' => $chapter,
181 'book' => $chapter->book,
186 * Perform the move action for a chapter.
188 * @throws NotFoundException|NotifyException
190 public function move(Request $request, string $bookSlug, string $chapterSlug)
192 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
193 $this->checkOwnablePermission('chapter-update', $chapter);
194 $this->checkOwnablePermission('chapter-delete', $chapter);
196 $entitySelection = $request->get('entity_selection', null);
197 if ($entitySelection === null || $entitySelection === '') {
198 return redirect($chapter->getUrl());
202 $this->chapterRepo->move($chapter, $entitySelection);
203 } catch (PermissionsException $exception) {
204 $this->showPermissionError();
205 } catch (MoveOperationException $exception) {
206 $this->showErrorNotification(trans('errors.selected_book_not_found'));
208 return redirect($chapter->getUrl('/move'));
211 return redirect($chapter->getUrl());
215 * Show the view to copy a chapter.
217 * @throws NotFoundException
219 public function showCopy(string $bookSlug, string $chapterSlug)
221 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
222 $this->checkOwnablePermission('chapter-view', $chapter);
224 session()->flashInput(['name' => $chapter->name]);
226 return view('chapters.copy', [
227 'book' => $chapter->book,
228 'chapter' => $chapter,
233 * Create a copy of a chapter within the requested target destination.
235 * @throws NotFoundException
238 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
240 $chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
241 $this->checkOwnablePermission('chapter-view', $chapter);
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('chapter-create', $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->findVisibleBySlugs($bookSlug, $chapterSlug);
267 $this->checkOwnablePermission('chapter-update', $chapter);
268 $this->checkOwnablePermission('chapter-delete', $chapter);
269 $this->checkPermission('book-create-all');
271 $book = $transformer->transformChapterToBook($chapter);
273 return redirect($book->getUrl());