3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\View;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Repos\ChapterRepo;
8 use BookStack\Entities\Tools\BookContents;
9 use BookStack\Entities\Tools\Cloner;
10 use BookStack\Entities\Tools\HierarchyTransformer;
11 use BookStack\Entities\Tools\NextPreviousContentLocator;
12 use BookStack\Entities\Tools\PermissionsUpdater;
13 use BookStack\Exceptions\MoveOperationException;
14 use BookStack\Exceptions\NotFoundException;
15 use BookStack\Exceptions\PermissionsException;
16 use BookStack\References\ReferenceFetcher;
17 use Illuminate\Http\Request;
18 use Illuminate\Validation\ValidationException;
21 class ChapterController extends Controller
23 protected ChapterRepo $chapterRepo;
24 protected ReferenceFetcher $referenceFetcher;
27 public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
29 $this->chapterRepo = $chapterRepo;
30 $this->referenceFetcher = $referenceFetcher;
34 * Show the form for creating a new chapter.
36 public function create(string $bookSlug)
38 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
39 $this->checkOwnablePermission('chapter-create', $book);
41 $this->setPageTitle(trans('entities.chapters_create'));
43 return view('chapters.create', ['book' => $book, 'current' => $book]);
47 * Store a newly created chapter in storage.
49 * @throws ValidationException
51 public function store(Request $request, string $bookSlug)
53 $this->validate($request, [
54 'name' => ['required', 'string', 'max:255'],
57 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
58 $this->checkOwnablePermission('chapter-create', $book);
60 $chapter = $this->chapterRepo->create($request->all(), $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,
86 'next' => $nextPreviousLocator->getNext(),
87 'previous' => $nextPreviousLocator->getPrevious(),
88 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
93 * Show the form for editing the specified chapter.
95 public function edit(string $bookSlug, string $chapterSlug)
97 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
98 $this->checkOwnablePermission('chapter-update', $chapter);
100 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
102 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
106 * Update the specified chapter in storage.
108 * @throws NotFoundException
110 public function update(Request $request, string $bookSlug, string $chapterSlug)
112 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
113 $this->checkOwnablePermission('chapter-update', $chapter);
115 $this->chapterRepo->update($chapter, $request->all());
117 return redirect($chapter->getUrl());
121 * Shows the page to confirm deletion of this chapter.
123 * @throws NotFoundException
125 public function showDelete(string $bookSlug, string $chapterSlug)
127 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
128 $this->checkOwnablePermission('chapter-delete', $chapter);
130 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
132 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
136 * Remove the specified chapter from storage.
138 * @throws NotFoundException
141 public function destroy(string $bookSlug, string $chapterSlug)
143 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
144 $this->checkOwnablePermission('chapter-delete', $chapter);
146 $this->chapterRepo->destroy($chapter);
148 return redirect($chapter->book->getUrl());
152 * Show the page for moving a chapter.
154 * @throws NotFoundException
156 public function showMove(string $bookSlug, string $chapterSlug)
158 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
159 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
160 $this->checkOwnablePermission('chapter-update', $chapter);
161 $this->checkOwnablePermission('chapter-delete', $chapter);
163 return view('chapters.move', [
164 'chapter' => $chapter,
165 'book' => $chapter->book,
170 * Perform the move action for a chapter.
172 * @throws NotFoundException
174 public function move(Request $request, string $bookSlug, string $chapterSlug)
176 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
177 $this->checkOwnablePermission('chapter-update', $chapter);
178 $this->checkOwnablePermission('chapter-delete', $chapter);
180 $entitySelection = $request->get('entity_selection', null);
181 if ($entitySelection === null || $entitySelection === '') {
182 return redirect($chapter->getUrl());
186 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
187 } catch (PermissionsException $exception) {
188 $this->showPermissionError();
189 } catch (MoveOperationException $exception) {
190 $this->showErrorNotification(trans('errors.selected_book_not_found'));
192 return redirect()->back();
195 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
197 return redirect($chapter->getUrl());
201 * Show the view to copy a chapter.
203 * @throws NotFoundException
205 public function showCopy(string $bookSlug, string $chapterSlug)
207 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
208 $this->checkOwnablePermission('chapter-view', $chapter);
210 session()->flashInput(['name' => $chapter->name]);
212 return view('chapters.copy', [
213 'book' => $chapter->book,
214 'chapter' => $chapter,
219 * Create a copy of a chapter within the requested target destination.
221 * @throws NotFoundException
224 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
226 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
227 $this->checkOwnablePermission('chapter-view', $chapter);
229 $entitySelection = $request->get('entity_selection') ?: null;
230 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
232 if (is_null($newParentBook)) {
233 $this->showErrorNotification(trans('errors.selected_book_not_found'));
235 return redirect()->back();
238 $this->checkOwnablePermission('chapter-create', $newParentBook);
240 $newName = $request->get('name') ?: $chapter->name;
241 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
242 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
244 return redirect($chapterCopy->getUrl());
248 * Show the Restrictions view.
250 * @throws NotFoundException
252 public function showPermissions(string $bookSlug, string $chapterSlug)
254 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
255 $this->checkOwnablePermission('restrictions-manage', $chapter);
257 return view('chapters.permissions', [
258 'chapter' => $chapter,
263 * Set the restrictions for this chapter.
265 * @throws NotFoundException
267 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
269 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
270 $this->checkOwnablePermission('restrictions-manage', $chapter);
272 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
274 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
276 return redirect($chapter->getUrl());
280 * Convert the chapter to a book.
282 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
284 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
285 $this->checkOwnablePermission('chapter-update', $chapter);
286 $this->checkOwnablePermission('chapter-delete', $chapter);
287 $this->checkPermission('book-create-all');
289 $book = $transformer->transformChapterToBook($chapter);
291 return redirect($book->getUrl());