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\Queries\ChapterQueries;
9 use BookStack\Entities\Queries\EntityQueries;
10 use BookStack\Entities\Repos\ChapterRepo;
11 use BookStack\Entities\Tools\BookContents;
12 use BookStack\Entities\Tools\Cloner;
13 use BookStack\Entities\Tools\HierarchyTransformer;
14 use BookStack\Entities\Tools\NextPreviousContentLocator;
15 use BookStack\Exceptions\MoveOperationException;
16 use BookStack\Exceptions\NotFoundException;
17 use BookStack\Exceptions\NotifyException;
18 use BookStack\Exceptions\PermissionsException;
19 use BookStack\Http\Controller;
20 use BookStack\Permissions\Permission;
21 use BookStack\References\ReferenceFetcher;
22 use BookStack\Util\DatabaseTransaction;
23 use Illuminate\Http\Request;
24 use Illuminate\Validation\ValidationException;
27 class ChapterController extends Controller
29 public function __construct(
30 protected ChapterRepo $chapterRepo,
31 protected ChapterQueries $queries,
32 protected EntityQueries $entityQueries,
33 protected ReferenceFetcher $referenceFetcher,
38 * Show the form for creating a new chapter.
40 public function create(string $bookSlug)
42 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
43 $this->checkOwnablePermission(Permission::ChapterCreate, $book);
45 $this->setPageTitle(trans('entities.chapters_create'));
47 return view('chapters.create', [
54 * Store a newly created chapter in storage.
56 * @throws ValidationException
58 public function store(Request $request, string $bookSlug)
60 $validated = $this->validate($request, [
61 'name' => ['required', 'string', 'max:255'],
62 'description_html' => ['string', 'max:2000'],
64 'default_template_id' => ['nullable', 'integer'],
67 $book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
68 $this->checkOwnablePermission(Permission::ChapterCreate, $book);
70 $chapter = $this->chapterRepo->create($validated, $book);
72 return redirect($chapter->getUrl());
76 * Display the specified chapter.
78 public function show(string $bookSlug, string $chapterSlug)
81 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
82 } catch (NotFoundException $exception) {
83 $chapter = $this->entityQueries->findVisibleByOldSlugs('chapter', $chapterSlug, $bookSlug);
84 if (is_null($chapter)) {
87 return redirect($chapter->getUrl());
90 $sidebarTree = (new BookContents($chapter->book))->getTree();
91 $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)->get();
93 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
94 View::incrementFor($chapter);
96 $this->setPageTitle($chapter->getShortName());
98 return view('chapters.show', [
99 'book' => $chapter->book,
100 'chapter' => $chapter,
101 'current' => $chapter,
102 'sidebarTree' => $sidebarTree,
103 'watchOptions' => new UserEntityWatchOptions(user(), $chapter),
105 'next' => $nextPreviousLocator->getNext(),
106 'previous' => $nextPreviousLocator->getPrevious(),
107 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
112 * Show the form for editing the specified chapter.
114 public function edit(string $bookSlug, string $chapterSlug)
116 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
117 $this->checkOwnablePermission(Permission::ChapterUpdate, $chapter);
119 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
121 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
125 * Update the specified chapter in storage.
127 * @throws NotFoundException
129 public function update(Request $request, string $bookSlug, string $chapterSlug)
131 $validated = $this->validate($request, [
132 'name' => ['required', 'string', 'max:255'],
133 'description_html' => ['string', 'max:2000'],
135 'default_template_id' => ['nullable', 'integer'],
138 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
139 $this->checkOwnablePermission(Permission::ChapterUpdate, $chapter);
141 $chapter = $this->chapterRepo->update($chapter, $validated);
143 return redirect($chapter->getUrl());
147 * Shows the page to confirm deletion of this chapter.
149 * @throws NotFoundException
151 public function showDelete(string $bookSlug, string $chapterSlug)
153 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
154 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
156 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
158 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
162 * Remove the specified chapter from storage.
164 * @throws NotFoundException
167 public function destroy(string $bookSlug, string $chapterSlug)
169 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
170 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
172 $this->chapterRepo->destroy($chapter);
174 return redirect($chapter->book->getUrl());
178 * Show the page for moving a chapter.
180 * @throws NotFoundException
182 public function showMove(string $bookSlug, string $chapterSlug)
184 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
185 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
186 $this->checkOwnablePermission(Permission::ChapterUpdate, $chapter);
187 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
189 return view('chapters.move', [
190 'chapter' => $chapter,
191 'book' => $chapter->book,
196 * Perform the move action for a chapter.
198 * @throws NotFoundException|NotifyException
200 public function move(Request $request, string $bookSlug, string $chapterSlug)
202 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
203 $this->checkOwnablePermission(Permission::ChapterUpdate, $chapter);
204 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
206 $entitySelection = $request->get('entity_selection', null);
207 if ($entitySelection === null || $entitySelection === '') {
208 return redirect($chapter->getUrl());
212 $this->chapterRepo->move($chapter, $entitySelection);
213 } catch (PermissionsException $exception) {
214 $this->showPermissionError();
215 } catch (MoveOperationException $exception) {
216 $this->showErrorNotification(trans('errors.selected_book_not_found'));
218 return redirect($chapter->getUrl('/move'));
221 return redirect($chapter->getUrl());
225 * Show the view to copy a chapter.
227 * @throws NotFoundException
229 public function showCopy(string $bookSlug, string $chapterSlug)
231 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
233 session()->flashInput(['name' => $chapter->name]);
235 return view('chapters.copy', [
236 'book' => $chapter->book,
237 'chapter' => $chapter,
242 * Create a copy of a chapter within the requested target destination.
244 * @throws NotFoundException
247 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
249 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
251 $entitySelection = $request->get('entity_selection') ?: null;
252 $newParentBook = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $chapter->getParent();
254 if (!$newParentBook instanceof Book) {
255 $this->showErrorNotification(trans('errors.selected_book_not_found'));
257 return redirect($chapter->getUrl('/copy'));
260 $this->checkOwnablePermission(Permission::ChapterCreate, $newParentBook);
262 $newName = $request->get('name') ?: $chapter->name;
263 $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
264 $this->showSuccessNotification(trans('entities.chapters_copy_success'));
266 return redirect($chapterCopy->getUrl());
270 * Convert the chapter to a book.
272 public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
274 $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
275 $this->checkOwnablePermission(Permission::ChapterUpdate, $chapter);
276 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
277 $this->checkPermission(Permission::BookCreateAll);
279 $book = (new DatabaseTransaction(function () use ($chapter, $transformer) {
280 return $transformer->transformChapterToBook($chapter);
283 return redirect($book->getUrl());