3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\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\Http\Controllers\Controller;
16 use BookStack\References\ReferenceFetcher;
17 use Illuminate\Http\Request;
18 use Illuminate\Validation\ValidationException;
21 class ChapterController extends Controller
23 protected ChapterRepo $chapterRepo;
24 protected ReferenceFetcher $referenceFetcher;
26 public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
28 $this->chapterRepo = $chapterRepo;
29 $this->referenceFetcher = $referenceFetcher;
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(),
87 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
92 * Show the form for editing the specified chapter.
94 public function edit(string $bookSlug, string $chapterSlug)
96 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
97 $this->checkOwnablePermission('chapter-update', $chapter);
99 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
101 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
105 * Update the specified chapter in storage.
107 * @throws NotFoundException
109 public function update(Request $request, string $bookSlug, string $chapterSlug)
111 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
112 $this->checkOwnablePermission('chapter-update', $chapter);
114 $this->chapterRepo->update($chapter, $request->all());
116 return redirect($chapter->getUrl());
120 * Shows the page to confirm deletion of this chapter.
122 * @throws NotFoundException
124 public function showDelete(string $bookSlug, string $chapterSlug)
126 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
127 $this->checkOwnablePermission('chapter-delete', $chapter);
129 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
131 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
135 * Remove the specified chapter from storage.
137 * @throws NotFoundException
140 public function destroy(string $bookSlug, string $chapterSlug)
142 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
143 $this->checkOwnablePermission('chapter-delete', $chapter);
145 $this->chapterRepo->destroy($chapter);
147 return redirect($chapter->book->getUrl());
151 * Show the page for moving a chapter.
153 * @throws NotFoundException
155 public function showMove(string $bookSlug, string $chapterSlug)
157 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
158 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
159 $this->checkOwnablePermission('chapter-update', $chapter);
160 $this->checkOwnablePermission('chapter-delete', $chapter);
162 return view('chapters.move', [
163 'chapter' => $chapter,
164 'book' => $chapter->book,
169 * Perform the move action for a chapter.
171 * @throws NotFoundException
173 public function move(Request $request, string $bookSlug, string $chapterSlug)
175 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
176 $this->checkOwnablePermission('chapter-update', $chapter);
177 $this->checkOwnablePermission('chapter-delete', $chapter);
179 $entitySelection = $request->get('entity_selection', null);
180 if ($entitySelection === null || $entitySelection === '') {
181 return redirect($chapter->getUrl());
185 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
186 } catch (PermissionsException $exception) {
187 $this->showPermissionError();
188 } catch (MoveOperationException $exception) {
189 $this->showErrorNotification(trans('errors.selected_book_not_found'));
191 return redirect()->back();
194 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
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());