]> BookStack Code Mirror - bookstack/commitdiff
Extracted page copy to new cloner class
authorDan Brown <redacted>
Sun, 19 Dec 2021 12:56:27 +0000 (12:56 +0000)
committerDan Brown <redacted>
Sun, 19 Dec 2021 12:56:27 +0000 (12:56 +0000)
Fundemental refactor for planned additional clone operations.
No behaviour change intended in this commit.

app/Entities/Repos/PageRepo.php
app/Entities/Tools/Cloner.php [new file with mode: 0644]
app/Http/Controllers/PageController.php

index b315bead9b2b526454c9f6e25b6003fd3dee17c0..b914632b5f12629fc30211fca17fe8407e05d44b 100644 (file)
@@ -346,43 +346,6 @@ class PageRepo
         return $parent;
     }
 
-    /**
-     * 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);
-    }
-
     /**
      * Find a page parent entity via a identifier string in the format:
      * {type}:{id}
@@ -390,7 +353,7 @@ class PageRepo
      *
      * @throws MoveOperationException
      */
-    protected function findParentByIdentifier(string $identifier): ?Entity
+    public function findParentByIdentifier(string $identifier): ?Entity
     {
         $stringExploded = explode(':', $identifier);
         $entityType = $stringExploded[0];
diff --git a/app/Entities/Tools/Cloner.php b/app/Entities/Tools/Cloner.php
new file mode 100644 (file)
index 0000000..3ce4dff
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace BookStack\Entities\Tools;
+
+use BookStack\Entities\Models\Entity;
+use BookStack\Entities\Models\Page;
+use BookStack\Entities\Repos\PageRepo;
+
+class Cloner
+{
+
+    /**
+     * @var PageRepo
+     */
+    protected $pageRepo;
+
+    public function __construct(PageRepo $pageRepo)
+    {
+        $this->pageRepo = $pageRepo;
+    }
+
+    /**
+     * Clone the given page into the given parent using the provided name.
+     */
+    public function clonePage(Page $original, Entity $parent, string $newName): Page
+    {
+        $copyPage = $this->pageRepo->getNewDraftPage($parent);
+        $pageData = $original->getAttributes();
+
+        // Update name
+        $pageData['name'] = $newName;
+
+        // Copy tags from previous page if set
+        if ($original->tags) {
+            $pageData['tags'] = [];
+            foreach ($original->tags as $tag) {
+                $pageData['tags'][] = ['name' => $tag->name, 'value' => $tag->value];
+            }
+        }
+
+        return $this->pageRepo->publishDraft($copyPage, $pageData);
+    }
+
+}
\ No newline at end of file
index 7d5d93ffa5ab7839c26f328ce6e1324fd28f68a5..4a01dcc623692808ac33036b694a2679fbaa4284 100644 (file)
@@ -6,6 +6,7 @@ use BookStack\Actions\View;
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Repos\PageRepo;
 use BookStack\Entities\Tools\BookContents;
+use BookStack\Entities\Tools\Cloner;
 use BookStack\Entities\Tools\NextPreviousContentLocator;
 use BookStack\Entities\Tools\PageContent;
 use BookStack\Entities\Tools\PageEditActivity;
@@ -447,26 +448,23 @@ class PageController extends Controller
      * @throws NotFoundException
      * @throws Throwable
      */
-    public function copy(Request $request, string $bookSlug, string $pageSlug)
+    public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
     {
         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
         $this->checkOwnablePermission('page-view', $page);
 
-        $entitySelection = $request->get('entity_selection', null) ?? null;
-        $newName = $request->get('name', null);
-
-        try {
-            $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
-        } catch (Exception $exception) {
-            if ($exception instanceof PermissionsException) {
-                $this->showPermissionError();
-            }
+        $entitySelection = $request->get('entity_selection') ?: null;
+        $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
 
+        if (is_null($newParent)) {
             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
-
             return redirect()->back();
         }
 
+        $this->checkOwnablePermission('page-create', $newParent);
+
+        $newName = $request->get('name') ?: $page->name;
+        $pageCopy = $cloner->clonePage($page, $newParent, $newName);
         $this->showSuccessNotification(trans('entities.pages_copy_success'));
 
         return redirect($pageCopy->getUrl());
Morty Proxy This is a proxified and sanitized view of the page, visit original site.