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 Illuminate\Http\Request;
17 use Illuminate\Validation\ValidationException;
20 class ChapterController extends Controller
22 protected $chapterRepo;
25 * ChapterController constructor.
27 public function __construct(ChapterRepo $chapterRepo)
29 $this->chapterRepo = $chapterRepo;
33 * Show the form for creating a new chapter.
35 public function create(string $bookSlug)
37 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
38 $this->checkOwnablePermission('chapter-create', $book);
40 $this->setPageTitle(trans('entities.chapters_create'));
42 return view('chapters.create', ['book' => $book, 'current' => $book]);
46 * Store a newly created chapter in storage.
48 * @throws ValidationException
50 public function store(Request $request, string $bookSlug)
52 $this->validate($request, [
53 'name' => ['required', 'string', 'max:255'],
56 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
57 $this->checkOwnablePermission('chapter-create', $book);
59 $chapter = $this->chapterRepo->create($request->all(), $book);
61 return redirect($chapter->getUrl());
65 * Display the specified chapter.
67 public function show(string $bookSlug, string $chapterSlug)
69 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
70 $this->checkOwnablePermission('chapter-view', $chapter);
72 $sidebarTree = (new BookContents($chapter->book))->getTree();
73 $pages = $chapter->getVisiblePages();
74 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
75 View::incrementFor($chapter);
77 $this->setPageTitle($chapter->getShortName());
79 return view('chapters.show', [
80 'book' => $chapter->book,
81 'chapter' => $chapter,
82 'current' => $chapter,
83 'sidebarTree' => $sidebarTree,
85 'next' => $nextPreviousLocator->getNext(),
86 'previous' => $nextPreviousLocator->getPrevious(),
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 * Show the Restrictions view.
248 * @throws NotFoundException
250 public function showPermissions(string $bookSlug, string $chapterSlug)
252 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
253 $this->checkOwnablePermission('restrictions-manage', $chapter);
255 return view('chapters.permissions', [
256 'chapter' => $chapter,
261 * Set the restrictions for this chapter.
263 * @throws NotFoundException
265 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
267 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
268 $this->checkOwnablePermission('restrictions-manage', $chapter);
270 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
272 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
274 return redirect($chapter->getUrl());
279 * Convert the chapter to a book.
281 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
283 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
284 $this->checkOwnablePermission('chapter-update', $chapter);
285 $this->checkOwnablePermission('chapter-delete', $chapter);
286 $this->checkPermission('book-create-all');
288 $book = $transformer->transformChapterToBook($chapter);
290 return redirect($book->getUrl());