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\Exceptions\MoveOperationException;
13 use BookStack\Exceptions\NotFoundException;
14 use BookStack\Exceptions\PermissionsException;
15 use BookStack\References\ReferenceFetcher;
16 use Illuminate\Http\Request;
17 use Illuminate\Validation\ValidationException;
20 class ChapterController extends Controller
22 protected ChapterRepo $chapterRepo;
23 protected ReferenceFetcher $referenceFetcher;
25 public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
27 $this->chapterRepo = $chapterRepo;
28 $this->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 $this->validate($request, [
52 'name' => ['required', 'string', 'max:255'],
55 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
56 $this->checkOwnablePermission('chapter-create', $book);
58 $chapter = $this->chapterRepo->create($request->all(), $book);
60 return redirect($chapter->getUrl());
64 * Display the specified chapter.
66 public function show(string $bookSlug, string $chapterSlug)
68 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
69 $this->checkOwnablePermission('chapter-view', $chapter);
71 $sidebarTree = (new BookContents($chapter->book))->getTree();
72 $pages = $chapter->getVisiblePages();
73 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
74 View::incrementFor($chapter);
76 $this->setPageTitle($chapter->getShortName());
78 return view('chapters.show', [
79 'book' => $chapter->book,
80 'chapter' => $chapter,
81 'current' => $chapter,
82 'sidebarTree' => $sidebarTree,
84 'next' => $nextPreviousLocator->getNext(),
85 'previous' => $nextPreviousLocator->getPrevious(),
86 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
91 * Show the form for editing the specified chapter.
93 public function edit(string $bookSlug, string $chapterSlug)
95 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
96 $this->checkOwnablePermission('chapter-update', $chapter);
98 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
100 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
104 * Update the specified chapter in storage.
106 * @throws NotFoundException
108 public function update(Request $request, string $bookSlug, string $chapterSlug)
110 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
111 $this->checkOwnablePermission('chapter-update', $chapter);
113 $this->chapterRepo->update($chapter, $request->all());
115 return redirect($chapter->getUrl());
119 * Shows the page to confirm deletion of this chapter.
121 * @throws NotFoundException
123 public function showDelete(string $bookSlug, string $chapterSlug)
125 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
126 $this->checkOwnablePermission('chapter-delete', $chapter);
128 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
130 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
134 * Remove the specified chapter from storage.
136 * @throws NotFoundException
139 public function destroy(string $bookSlug, string $chapterSlug)
141 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
142 $this->checkOwnablePermission('chapter-delete', $chapter);
144 $this->chapterRepo->destroy($chapter);
146 return redirect($chapter->book->getUrl());
150 * Show the page for moving a chapter.
152 * @throws NotFoundException
154 public function showMove(string $bookSlug, string $chapterSlug)
156 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
157 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
158 $this->checkOwnablePermission('chapter-update', $chapter);
159 $this->checkOwnablePermission('chapter-delete', $chapter);
161 return view('chapters.move', [
162 'chapter' => $chapter,
163 'book' => $chapter->book,
168 * Perform the move action for a chapter.
170 * @throws NotFoundException
172 public function move(Request $request, string $bookSlug, string $chapterSlug)
174 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
175 $this->checkOwnablePermission('chapter-update', $chapter);
176 $this->checkOwnablePermission('chapter-delete', $chapter);
178 $entitySelection = $request->get('entity_selection', null);
179 if ($entitySelection === null || $entitySelection === '') {
180 return redirect($chapter->getUrl());
184 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
185 } catch (PermissionsException $exception) {
186 $this->showPermissionError();
187 } catch (MoveOperationException $exception) {
188 $this->showErrorNotification(trans('errors.selected_book_not_found'));
190 return redirect()->back();
193 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
195 return redirect($chapter->getUrl());
199 * Show the view to copy a chapter.
201 * @throws NotFoundException
203 public function showCopy(string $bookSlug, string $chapterSlug)
205 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
206 $this->checkOwnablePermission('chapter-view', $chapter);
208 session()->flashInput(['name' => $chapter->name]);
210 return view('chapters.copy', [
211 'book' => $chapter->book,
212 'chapter' => $chapter,
217 * Create a copy of a chapter within the requested target destination.
219 * @throws NotFoundException
222 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
224 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
225 $this->checkOwnablePermission('chapter-view', $chapter);
227 $entitySelection = $request->get('entity_selection') ?: null;
228 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
230 if (is_null($newParentBook)) {
231 $this->showErrorNotification(trans('errors.selected_book_not_found'));
233 return redirect()->back();
236 $this->checkOwnablePermission('chapter-create', $newParentBook);
238 $newName = $request->get('name') ?: $chapter->name;
239 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
240 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
242 return redirect($chapterCopy->getUrl());
246 * Convert the chapter to a book.
248 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
250 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
251 $this->checkOwnablePermission('chapter-update', $chapter);
252 $this->checkOwnablePermission('chapter-delete', $chapter);
253 $this->checkPermission('book-create-all');
255 $book = $transformer->transformChapterToBook($chapter);
257 return redirect($book->getUrl());