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 public function __construct(
26 protected ChapterRepo $chapterRepo,
27 protected ReferenceFetcher $referenceFetcher
32 * Show the form for creating a new chapter.
34 public function create(string $bookSlug)
36 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
37 $this->checkOwnablePermission('chapter-create', $book);
39 $this->setPageTitle(trans('entities.chapters_create'));
41 return view('chapters.create', ['book' => $book, 'current' => $book]);
45 * Store a newly created chapter in storage.
47 * @throws ValidationException
49 public function store(Request $request, string $bookSlug)
51 $validated = $this->validate($request, [
52 'name' => ['required', 'string', 'max:255'],
53 'description_html' => ['string', 'max:2000'],
55 'default_template_id' => ['nullable', 'integer'],
58 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
59 $this->checkOwnablePermission('chapter-create', $book);
61 $chapter = $this->chapterRepo->create($validated, $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->getReferenceCountToEntity($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 $validated = $this->validate($request, [
115 'name' => ['required', 'string', 'max:255'],
116 'description_html' => ['string', 'max:2000'],
118 'default_template_id' => ['nullable', 'integer'],
121 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
122 $this->checkOwnablePermission('chapter-update', $chapter);
124 $this->chapterRepo->update($chapter, $validated);
126 return redirect($chapter->getUrl());
130 * Shows the page to confirm deletion of this chapter.
132 * @throws NotFoundException
134 public function showDelete(string $bookSlug, string $chapterSlug)
136 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
137 $this->checkOwnablePermission('chapter-delete', $chapter);
139 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
141 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
145 * Remove the specified chapter from storage.
147 * @throws NotFoundException
150 public function destroy(string $bookSlug, string $chapterSlug)
152 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
153 $this->checkOwnablePermission('chapter-delete', $chapter);
155 $this->chapterRepo->destroy($chapter);
157 return redirect($chapter->book->getUrl());
161 * Show the page for moving a chapter.
163 * @throws NotFoundException
165 public function showMove(string $bookSlug, string $chapterSlug)
167 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
168 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
169 $this->checkOwnablePermission('chapter-update', $chapter);
170 $this->checkOwnablePermission('chapter-delete', $chapter);
172 return view('chapters.move', [
173 'chapter' => $chapter,
174 'book' => $chapter->book,
179 * Perform the move action for a chapter.
181 * @throws NotFoundException|NotifyException
183 public function move(Request $request, string $bookSlug, string $chapterSlug)
185 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
186 $this->checkOwnablePermission('chapter-update', $chapter);
187 $this->checkOwnablePermission('chapter-delete', $chapter);
189 $entitySelection = $request->get('entity_selection', null);
190 if ($entitySelection === null || $entitySelection === '') {
191 return redirect($chapter->getUrl());
195 $this->chapterRepo->move($chapter, $entitySelection);
196 } catch (PermissionsException $exception) {
197 $this->showPermissionError();
198 } catch (MoveOperationException $exception) {
199 $this->showErrorNotification(trans('errors.selected_book_not_found'));
201 return redirect($chapter->getUrl('/move'));
204 return redirect($chapter->getUrl());
208 * Show the view to copy a chapter.
210 * @throws NotFoundException
212 public function showCopy(string $bookSlug, string $chapterSlug)
214 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
215 $this->checkOwnablePermission('chapter-view', $chapter);
217 session()->flashInput(['name' => $chapter->name]);
219 return view('chapters.copy', [
220 'book' => $chapter->book,
221 'chapter' => $chapter,
226 * Create a copy of a chapter within the requested target destination.
228 * @throws NotFoundException
231 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
233 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
234 $this->checkOwnablePermission('chapter-view', $chapter);
236 $entitySelection = $request->get('entity_selection') ?: null;
237 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
239 if (is_null($newParentBook)) {
240 $this->showErrorNotification(trans('errors.selected_book_not_found'));
242 return redirect($chapter->getUrl('/copy'));
245 $this->checkOwnablePermission('chapter-create', $newParentBook);
247 $newName = $request->get('name') ?: $chapter->name;
248 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
249 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
251 return redirect($chapterCopy->getUrl());
255 * Convert the chapter to a book.
257 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
259 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
260 $this->checkOwnablePermission('chapter-update', $chapter);
261 $this->checkOwnablePermission('chapter-delete', $chapter);
262 $this->checkPermission('book-create-all');
264 $book = $transformer->transformChapterToBook($chapter);
266 return redirect($book->getUrl());