3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\UserEntityWatchOptions;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Repos\ChapterRepo;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\Cloner;
11 use BookStack\Entities\Tools\HierarchyTransformer;
12 use BookStack\Entities\Tools\NextPreviousContentLocator;
13 use BookStack\Exceptions\MoveOperationException;
14 use BookStack\Exceptions\NotFoundException;
15 use BookStack\Exceptions\PermissionsException;
16 use BookStack\Http\Controller;
17 use BookStack\References\ReferenceFetcher;
18 use Illuminate\Http\Request;
19 use Illuminate\Validation\ValidationException;
22 class ChapterController extends Controller
24 protected ChapterRepo $chapterRepo;
25 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,
85 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
87 'next' => $nextPreviousLocator->getNext(),
88 'previous' => $nextPreviousLocator->getPrevious(),
89 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
94 * Show the form for editing the specified chapter.
96 public function edit(string $bookSlug, string $chapterSlug)
98 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
99 $this->checkOwnablePermission('chapter-update', $chapter);
101 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
103 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
107 * Update the specified chapter in storage.
109 * @throws NotFoundException
111 public function update(Request $request, string $bookSlug, string $chapterSlug)
113 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
114 $this->checkOwnablePermission('chapter-update', $chapter);
116 $this->chapterRepo->update($chapter, $request->all());
118 return redirect($chapter->getUrl());
122 * Shows the page to confirm deletion of this chapter.
124 * @throws NotFoundException
126 public function showDelete(string $bookSlug, string $chapterSlug)
128 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
129 $this->checkOwnablePermission('chapter-delete', $chapter);
131 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
133 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
137 * Remove the specified chapter from storage.
139 * @throws NotFoundException
142 public function destroy(string $bookSlug, string $chapterSlug)
144 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
145 $this->checkOwnablePermission('chapter-delete', $chapter);
147 $this->chapterRepo->destroy($chapter);
149 return redirect($chapter->book->getUrl());
153 * Show the page for moving a chapter.
155 * @throws NotFoundException
157 public function showMove(string $bookSlug, string $chapterSlug)
159 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
160 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
161 $this->checkOwnablePermission('chapter-update', $chapter);
162 $this->checkOwnablePermission('chapter-delete', $chapter);
164 return view('chapters.move', [
165 'chapter' => $chapter,
166 'book' => $chapter->book,
171 * Perform the move action for a chapter.
173 * @throws NotFoundException
175 public function move(Request $request, string $bookSlug, string $chapterSlug)
177 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
178 $this->checkOwnablePermission('chapter-update', $chapter);
179 $this->checkOwnablePermission('chapter-delete', $chapter);
181 $entitySelection = $request->get('entity_selection', null);
182 if ($entitySelection === null || $entitySelection === '') {
183 return redirect($chapter->getUrl());
187 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
188 } catch (PermissionsException $exception) {
189 $this->showPermissionError();
190 } catch (MoveOperationException $exception) {
191 $this->showErrorNotification(trans('errors.selected_book_not_found'));
193 return redirect()->back();
196 return redirect($chapter->getUrl());
200 * Show the view to copy a chapter.
202 * @throws NotFoundException
204 public function showCopy(string $bookSlug, string $chapterSlug)
206 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
207 $this->checkOwnablePermission('chapter-view', $chapter);
209 session()->flashInput(['name' => $chapter->name]);
211 return view('chapters.copy', [
212 'book' => $chapter->book,
213 'chapter' => $chapter,
218 * Create a copy of a chapter within the requested target destination.
220 * @throws NotFoundException
223 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
225 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
226 $this->checkOwnablePermission('chapter-view', $chapter);
228 $entitySelection = $request->get('entity_selection') ?: null;
229 $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
231 if (is_null($newParentBook)) {
232 $this->showErrorNotification(trans('errors.selected_book_not_found'));
234 return redirect()->back();
237 $this->checkOwnablePermission('chapter-create', $newParentBook);
239 $newName = $request->get('name') ?: $chapter->name;
240 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
241 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
243 return redirect($chapterCopy->getUrl());
247 * Convert the chapter to a book.
249 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
251 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
252 $this->checkOwnablePermission('chapter-update', $chapter);
253 $this->checkOwnablePermission('chapter-delete', $chapter);
254 $this->checkPermission('book-create-all');
256 $book = $transformer->transformChapterToBook($chapter);
258 return redirect($book->getUrl());