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\NextPreviousContentLocator;
11 use BookStack\Entities\Tools\PermissionsUpdater;
12 use BookStack\Exceptions\MoveOperationException;
13 use BookStack\Exceptions\NotFoundException;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
18 class ChapterController extends Controller
20 protected $chapterRepo;
23 * ChapterController constructor.
25 public function __construct(ChapterRepo $chapterRepo)
27 $this->chapterRepo = $chapterRepo;
31 * Show the form for creating a new chapter.
33 public function create(string $bookSlug)
35 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
36 $this->checkOwnablePermission('chapter-create', $book);
38 $this->setPageTitle(trans('entities.chapters_create'));
40 return view('chapters.create', ['book' => $book, 'current' => $book]);
44 * Store a newly created chapter in storage.
46 * @throws ValidationException
48 public function store(Request $request, string $bookSlug)
50 $this->validate($request, [
51 'name' => ['required', 'string', 'max:255'],
54 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
55 $this->checkOwnablePermission('chapter-create', $book);
57 $chapter = $this->chapterRepo->create($request->all(), $book);
59 return redirect($chapter->getUrl());
63 * Display the specified chapter.
65 public function show(string $bookSlug, string $chapterSlug)
67 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
68 $this->checkOwnablePermission('chapter-view', $chapter);
70 $sidebarTree = (new BookContents($chapter->book))->getTree();
71 $pages = $chapter->getVisiblePages();
72 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
73 View::incrementFor($chapter);
75 $this->setPageTitle($chapter->getShortName());
77 return view('chapters.show', [
78 'book' => $chapter->book,
79 'chapter' => $chapter,
80 'current' => $chapter,
81 'sidebarTree' => $sidebarTree,
83 'next' => $nextPreviousLocator->getNext(),
84 'previous' => $nextPreviousLocator->getPrevious(),
89 * Show the form for editing the specified chapter.
91 public function edit(string $bookSlug, string $chapterSlug)
93 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
94 $this->checkOwnablePermission('chapter-update', $chapter);
96 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
98 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
102 * Update the specified chapter in storage.
104 * @throws NotFoundException
106 public function update(Request $request, string $bookSlug, string $chapterSlug)
108 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
109 $this->checkOwnablePermission('chapter-update', $chapter);
111 $this->chapterRepo->update($chapter, $request->all());
113 return redirect($chapter->getUrl());
117 * Shows the page to confirm deletion of this chapter.
119 * @throws NotFoundException
121 public function showDelete(string $bookSlug, string $chapterSlug)
123 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
124 $this->checkOwnablePermission('chapter-delete', $chapter);
126 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
128 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
132 * Remove the specified chapter from storage.
134 * @throws NotFoundException
137 public function destroy(string $bookSlug, string $chapterSlug)
139 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
140 $this->checkOwnablePermission('chapter-delete', $chapter);
142 $this->chapterRepo->destroy($chapter);
144 return redirect($chapter->book->getUrl());
148 * Show the page for moving a chapter.
150 * @throws NotFoundException
152 public function showMove(string $bookSlug, string $chapterSlug)
154 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
155 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
156 $this->checkOwnablePermission('chapter-update', $chapter);
157 $this->checkOwnablePermission('chapter-delete', $chapter);
159 return view('chapters.move', [
160 'chapter' => $chapter,
161 'book' => $chapter->book,
166 * Perform the move action for a chapter.
168 * @throws NotFoundException
170 public function move(Request $request, string $bookSlug, string $chapterSlug)
172 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
173 $this->checkOwnablePermission('chapter-update', $chapter);
174 $this->checkOwnablePermission('chapter-delete', $chapter);
176 $entitySelection = $request->get('entity_selection', null);
177 if ($entitySelection === null || $entitySelection === '') {
178 return redirect($chapter->getUrl());
182 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
183 } catch (MoveOperationException $exception) {
184 $this->showErrorNotification(trans('errors.selected_book_not_found'));
186 return redirect()->back();
189 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
191 return redirect($chapter->getUrl());
195 * Show the view to copy a chapter.
197 * @throws NotFoundException
199 public function showCopy(string $bookSlug, string $chapterSlug)
201 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
202 $this->checkOwnablePermission('chapter-view', $chapter);
204 session()->flashInput(['name' => $chapter->name]);
206 return view('chapters.copy', [
207 'book' => $chapter->book,
208 'chapter' => $chapter,
213 * Create a copy of a chapter within the requested target destination.
215 * @throws NotFoundException
218 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
220 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
221 $this->checkOwnablePermission('chapter-view', $chapter);
223 $entitySelection = $request->get('entity_selection') ?: null;
224 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
226 if (is_null($newParentBook)) {
227 $this->showErrorNotification(trans('errors.selected_book_not_found'));
229 return redirect()->back();
232 $this->checkOwnablePermission('chapter-create', $newParentBook);
234 $newName = $request->get('name') ?: $chapter->name;
235 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
236 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
238 return redirect($chapterCopy->getUrl());
242 * Show the Restrictions view.
244 * @throws NotFoundException
246 public function showPermissions(string $bookSlug, string $chapterSlug)
248 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
249 $this->checkOwnablePermission('restrictions-manage', $chapter);
251 return view('chapters.permissions', [
252 'chapter' => $chapter,
257 * Set the restrictions for this chapter.
259 * @throws NotFoundException
261 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
263 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
264 $this->checkOwnablePermission('restrictions-manage', $chapter);
266 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
268 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
270 return redirect($chapter->getUrl());