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'],
57 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
58 $this->checkOwnablePermission('chapter-create', $book);
60 $chapter = $this->chapterRepo->create($validated, $book);
62 return redirect($chapter->getUrl());
66 * Display the specified chapter.
68 public function show(string $bookSlug, string $chapterSlug)
70 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
71 $this->checkOwnablePermission('chapter-view', $chapter);
73 $sidebarTree = (new BookContents($chapter->book))->getTree();
74 $pages = $chapter->getVisiblePages();
75 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
76 View::incrementFor($chapter);
78 $this->setPageTitle($chapter->getShortName());
80 return view('chapters.show', [
81 'book' => $chapter->book,
82 'chapter' => $chapter,
83 'current' => $chapter,
84 'sidebarTree' => $sidebarTree,
85 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
87 'next' => $nextPreviousLocator->getNext(),
88 'previous' => $nextPreviousLocator->getPrevious(),
89 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
94 * Show the form for editing the specified chapter.
96 public function edit(string $bookSlug, string $chapterSlug)
98 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
99 $this->checkOwnablePermission('chapter-update', $chapter);
101 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
103 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
107 * Update the specified chapter in storage.
109 * @throws NotFoundException
111 public function update(Request $request, string $bookSlug, string $chapterSlug)
113 $validated = $this->validate($request, [
114 'name' => ['required', 'string', 'max:255'],
115 'description_html' => ['string', 'max:2000'],
119 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
120 $this->checkOwnablePermission('chapter-update', $chapter);
122 $this->chapterRepo->update($chapter, $validated);
124 return redirect($chapter->getUrl());
128 * Shows the page to confirm deletion of this chapter.
130 * @throws NotFoundException
132 public function showDelete(string $bookSlug, string $chapterSlug)
134 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
135 $this->checkOwnablePermission('chapter-delete', $chapter);
137 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
139 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
143 * Remove the specified chapter from storage.
145 * @throws NotFoundException
148 public function destroy(string $bookSlug, string $chapterSlug)
150 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
151 $this->checkOwnablePermission('chapter-delete', $chapter);
153 $this->chapterRepo->destroy($chapter);
155 return redirect($chapter->book->getUrl());
159 * Show the page for moving a chapter.
161 * @throws NotFoundException
163 public function showMove(string $bookSlug, string $chapterSlug)
165 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
166 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
167 $this->checkOwnablePermission('chapter-update', $chapter);
168 $this->checkOwnablePermission('chapter-delete', $chapter);
170 return view('chapters.move', [
171 'chapter' => $chapter,
172 'book' => $chapter->book,
177 * Perform the move action for a chapter.
179 * @throws NotFoundException|NotifyException
181 public function move(Request $request, string $bookSlug, string $chapterSlug)
183 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
184 $this->checkOwnablePermission('chapter-update', $chapter);
185 $this->checkOwnablePermission('chapter-delete', $chapter);
187 $entitySelection = $request->get('entity_selection', null);
188 if ($entitySelection === null || $entitySelection === '') {
189 return redirect($chapter->getUrl());
193 $this->chapterRepo->move($chapter, $entitySelection);
194 } catch (PermissionsException $exception) {
195 $this->showPermissionError();
196 } catch (MoveOperationException $exception) {
197 $this->showErrorNotification(trans('errors.selected_book_not_found'));
199 return redirect($chapter->getUrl('/move'));
202 return redirect($chapter->getUrl());
206 * Show the view to copy a chapter.
208 * @throws NotFoundException
210 public function showCopy(string $bookSlug, string $chapterSlug)
212 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
213 $this->checkOwnablePermission('chapter-view', $chapter);
215 session()->flashInput(['name' => $chapter->name]);
217 return view('chapters.copy', [
218 'book' => $chapter->book,
219 'chapter' => $chapter,
224 * Create a copy of a chapter within the requested target destination.
226 * @throws NotFoundException
229 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
231 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
232 $this->checkOwnablePermission('chapter-view', $chapter);
234 $entitySelection = $request->get('entity_selection') ?: null;
235 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
237 if (is_null($newParentBook)) {
238 $this->showErrorNotification(trans('errors.selected_book_not_found'));
240 return redirect($chapter->getUrl('/copy'));
243 $this->checkOwnablePermission('chapter-create', $newParentBook);
245 $newName = $request->get('name') ?: $chapter->name;
246 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
247 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
249 return redirect($chapterCopy->getUrl());
253 * Convert the chapter to a book.
255 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
257 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
258 $this->checkOwnablePermission('chapter-update', $chapter);
259 $this->checkOwnablePermission('chapter-delete', $chapter);
260 $this->checkPermission('book-create-all');
262 $book = $transformer->transformChapterToBook($chapter);
264 return redirect($book->getUrl());