- /**
- * Copy an existing page in the system.
- * Optionally providing a new parent via string identifier and a new name.
- *
- * @throws MoveOperationException
- * @throws PermissionsException
- */
- public function copy(Page $page, string $parentIdentifier = null, string $newName = null): Page
- {
- $parent = $parentIdentifier ? $this->findParentByIdentifier($parentIdentifier) : $page->getParent();
- if ($parent === null) {
- throw new MoveOperationException('Book or chapter to move page into not found');
- }
-
- 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);
- }
-