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 BookStack\Exceptions\PermissionsException;
15 use Illuminate\Http\Request;
16 use Illuminate\Validation\ValidationException;
19 class ChapterController extends Controller
21 protected $chapterRepo;
24 * ChapterController constructor.
26 public function __construct(ChapterRepo $chapterRepo)
28 $this->chapterRepo = $chapterRepo;
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(),
90 * Show the form for editing the specified chapter.
92 public function edit(string $bookSlug, string $chapterSlug)
94 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
95 $this->checkOwnablePermission('chapter-update', $chapter);
97 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
99 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
103 * Update the specified chapter in storage.
105 * @throws NotFoundException
107 public function update(Request $request, string $bookSlug, string $chapterSlug)
109 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
110 $this->checkOwnablePermission('chapter-update', $chapter);
112 $this->chapterRepo->update($chapter, $request->all());
114 return redirect($chapter->getUrl());
118 * Shows the page to confirm deletion of this chapter.
120 * @throws NotFoundException
122 public function showDelete(string $bookSlug, string $chapterSlug)
124 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
125 $this->checkOwnablePermission('chapter-delete', $chapter);
127 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
129 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
133 * Remove the specified chapter from storage.
135 * @throws NotFoundException
138 public function destroy(string $bookSlug, string $chapterSlug)
140 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
141 $this->checkOwnablePermission('chapter-delete', $chapter);
143 $this->chapterRepo->destroy($chapter);
145 return redirect($chapter->book->getUrl());
149 * Show the page for moving a chapter.
151 * @throws NotFoundException
153 public function showMove(string $bookSlug, string $chapterSlug)
155 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
156 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
157 $this->checkOwnablePermission('chapter-update', $chapter);
158 $this->checkOwnablePermission('chapter-delete', $chapter);
160 return view('chapters.move', [
161 'chapter' => $chapter,
162 'book' => $chapter->book,
167 * Perform the move action for a chapter.
169 * @throws NotFoundException
171 public function move(Request $request, string $bookSlug, string $chapterSlug)
173 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
174 $this->checkOwnablePermission('chapter-update', $chapter);
175 $this->checkOwnablePermission('chapter-delete', $chapter);
177 $entitySelection = $request->get('entity_selection', null);
178 if ($entitySelection === null || $entitySelection === '') {
179 return redirect($chapter->getUrl());
183 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
184 } catch (PermissionsException $exception) {
185 $this->showPermissionError();
186 } catch (MoveOperationException $exception) {
187 $this->showErrorNotification(trans('errors.selected_book_not_found'));
189 return redirect()->back();
192 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
194 return redirect($chapter->getUrl());
198 * Show the view to copy a chapter.
200 * @throws NotFoundException
202 public function showCopy(string $bookSlug, string $chapterSlug)
204 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
205 $this->checkOwnablePermission('chapter-view', $chapter);
207 session()->flashInput(['name' => $chapter->name]);
209 return view('chapters.copy', [
210 'book' => $chapter->book,
211 'chapter' => $chapter,
216 * Create a copy of a chapter within the requested target destination.
218 * @throws NotFoundException
221 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
223 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
224 $this->checkOwnablePermission('chapter-view', $chapter);
226 $entitySelection = $request->get('entity_selection') ?: null;
227 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
229 if (is_null($newParentBook)) {
230 $this->showErrorNotification(trans('errors.selected_book_not_found'));
232 return redirect()->back();
235 $this->checkOwnablePermission('chapter-create', $newParentBook);
237 $newName = $request->get('name') ?: $chapter->name;
238 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
239 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
241 return redirect($chapterCopy->getUrl());
245 * Show the Restrictions view.
247 * @throws NotFoundException
249 public function showPermissions(string $bookSlug, string $chapterSlug)
251 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
252 $this->checkOwnablePermission('restrictions-manage', $chapter);
254 return view('chapters.permissions', [
255 'chapter' => $chapter,
260 * Set the restrictions for this chapter.
262 * @throws NotFoundException
264 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
266 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
267 $this->checkOwnablePermission('restrictions-manage', $chapter);
269 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
271 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
273 return redirect($chapter->getUrl());