- $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
- $this->checkOwnablePermission('restrictions-manage', $chapter);
- $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
- session()->flash('success', trans('entities.chapters_permissions_success'));
- return redirect($chapter->getUrl());
+ $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());