- $this->checkPermission('chapter-delete');
- $book = $this->bookRepo->getBySlug($bookSlug);
- $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
- if (count($chapter->pages) > 0) {
- foreach ($chapter->pages as $page) {
- $page->chapter_id = 0;
- $page->save();
- }
+ $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
+ $this->checkOwnablePermission('chapter-update', $chapter);
+ $this->checkOwnablePermission('chapter-delete', $chapter);
+
+ return view('chapters.move', [
+ 'chapter' => $chapter,
+ 'book' => $chapter->book,
+ ]);
+ }
+
+ /**
+ * Perform the move action for a chapter.
+ *
+ * @throws NotFoundException
+ */
+ public function move(Request $request, string $bookSlug, string $chapterSlug)
+ {
+ $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $this->checkOwnablePermission('chapter-update', $chapter);
+ $this->checkOwnablePermission('chapter-delete', $chapter);
+
+ $entitySelection = $request->get('entity_selection', null);
+ if ($entitySelection === null || $entitySelection === '') {
+ return redirect($chapter->getUrl());
+ }
+
+ try {
+ $newBook = $this->chapterRepo->move($chapter, $entitySelection);
+ } catch (MoveOperationException $exception) {
+ $this->showErrorNotification(trans('errors.selected_book_not_found'));
+
+ return redirect()->back();