+
+ return redirect($chapter->book->getUrl());
+ }
+
+ /**
+ * Show the page for moving a chapter.
+ *
+ * @throws NotFoundException
+ */
+ public function showMove(string $bookSlug, string $chapterSlug)
+ {
+ $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 (PermissionsException $exception) {
+ $this->showPermissionError();
+ } catch (MoveOperationException $exception) {
+ $this->showErrorNotification(trans('errors.selected_book_not_found'));
+
+ return redirect()->back();
+ }
+
+ $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
+
+ return redirect($chapter->getUrl());
+ }
+
+ /**
+ * Show the view to copy a chapter.
+ *
+ * @throws NotFoundException
+ */
+ public function showCopy(string $bookSlug, string $chapterSlug)
+ {
+ $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $this->checkOwnablePermission('chapter-view', $chapter);
+
+ session()->flashInput(['name' => $chapter->name]);
+
+ return view('chapters.copy', [
+ 'book' => $chapter->book,
+ 'chapter' => $chapter,
+ ]);
+ }
+
+ /**
+ * Create a copy of a chapter within the requested target destination.
+ *
+ * @throws NotFoundException
+ * @throws Throwable
+ */
+ public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
+ {
+ $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
+ $this->checkOwnablePermission('chapter-view', $chapter);
+
+ $entitySelection = $request->get('entity_selection') ?: null;
+ $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
+
+ if (is_null($newParentBook)) {
+ $this->showErrorNotification(trans('errors.selected_book_not_found'));
+
+ return redirect()->back();
+ }
+
+ $this->checkOwnablePermission('chapter-create', $newParentBook);
+
+ $newName = $request->get('name') ?: $chapter->name;
+ $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
+ $this->showSuccessNotification(trans('entities.chapters_copy_success'));
+
+ return redirect($chapterCopy->getUrl());