- if (!userCan('page-create', $parent)) {
- throw new PermissionsException('User does not have permission to create a page within the new parent');
- }
-
- $copyPage = $this->getNewDraftPage($parent);
- $pageData = $page->getAttributes();
-
- // Update name
- if (!empty($newName)) {
- $pageData['name'] = $newName;
- }
-
- // Copy tags from previous page if set
- if ($page->tags) {
- $pageData['tags'] = [];
- foreach ($page->tags as $tag) {
- $pageData['tags'][] = ['name' => $tag->name, 'value' => $tag->value];
- }
- }
-
- return $this->publishDraft($copyPage, $pageData);
- }
-
- /**
- * Find a page parent entity via a identifier string in the format:
- * {type}:{id}
- * Example: (book:5).
- *
- * @throws MoveOperationException
- */
- protected function findParentByIdentifier(string $identifier): ?Entity
- {
- $stringExploded = explode(':', $identifier);
- $entityType = $stringExploded[0];
- $entityId = intval($stringExploded[1]);
-
- if ($entityType !== 'book' && $entityType !== 'chapter') {
- throw new MoveOperationException('Pages can only be in books or chapters');
- }
-
- $parentClass = $entityType === 'book' ? Book::class : Chapter::class;
-
- return $parentClass::visible()->where('id', '=', $entityId)->first();
- }
-
- /**
- * Change the page's parent to the given entity.
- */
- protected function changeParent(Page $page, Entity $parent)
- {
- $book = ($parent instanceof Book) ? $parent : $parent->book;
- $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : 0;
- $page->save();
-
- if ($page->book->id !== $book->id) {
- $page->changeBook($book->id);
- }
-
- $page->load('book');
- $book->rebuildPermissions();
- }
-
- /**
- * Get a page revision to update for the given page.
- * Checks for an existing revisions before providing a fresh one.
- */
- protected function getPageRevisionToUpdate(Page $page): PageRevision
- {
- $drafts = $this->getUserDraftQuery($page)->get();
- if ($drafts->count() > 0) {
- return $drafts->first();
- }
-
- $draft = new PageRevision();
- $draft->page_id = $page->id;
- $draft->slug = $page->slug;
- $draft->book_slug = $page->book->slug;
- $draft->created_by = user()->id;
- $draft->type = 'update_draft';
-
- return $draft;
- }
-
- /**
- * Delete old revisions, for the given page, from the system.
- */
- protected function deleteOldRevisions(Page $page)
- {
- $revisionLimit = config('app.revision_limit');
- if ($revisionLimit === false) {
- return;
- }
-
- $revisionsToDelete = PageRevision::query()
- ->where('page_id', '=', $page->id)
- ->orderBy('created_at', 'desc')
- ->skip(intval($revisionLimit))
- ->take(10)
- ->get(['id']);
- if ($revisionsToDelete->count() > 0) {
- PageRevision::query()->whereIn('id', $revisionsToDelete->pluck('id'))->delete();
- }