- $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
- $this->checkOwnablePermission('restrictions-manage', $page);
- $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
- session()->flash('success', trans('entities.pages_permissions_success'));
- return redirect($page->getUrl());
+ $page = $this->pageRepo->getBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('page-view', $page);
+
+ $entitySelection = $request->get('entity_selection', null);
+ if ($entitySelection === null || $entitySelection === '') {
+ $parent = $page->chapter ? $page->chapter : $page->book;
+ } else {
+ $stringExploded = explode(':', $entitySelection);
+ $entityType = $stringExploded[0];
+ $entityId = intval($stringExploded[1]);
+
+ try {
+ $parent = $this->pageRepo->getById($entityType, $entityId);
+ } catch (Exception $e) {
+ $this->showErrorNotification(trans('entities.selected_book_chapter_not_found'));
+ return redirect()->back();
+ }
+ }
+
+ $this->checkOwnablePermission('page-create', $parent);
+
+ $pageCopy = $this->pageRepo->copyPage($page, $parent, $request->get('name', ''));
+
+ Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
+ $this->showSuccessNotification( trans('entities.pages_copy_success'));
+
+ return redirect($pageCopy->getUrl());