]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/HierarchyTransformer.php
Perms: Fixed some issues made when adding transactions
[bookstack] / app / Entities / Tools / HierarchyTransformer.php
index c95d5fa5307e4d4e7d9bd930d8a031f7b15de804..b0d8880f402ecb0efd2a1241cd7d750312061d4e 100644 (file)
@@ -2,24 +2,31 @@
 
 namespace BookStack\Entities\Tools;
 
+use BookStack\Activity\ActivityType;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Bookshelf;
 use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Repos\BookRepo;
 use BookStack\Entities\Repos\BookshelfRepo;
+use BookStack\Facades\Activity;
 
 class HierarchyTransformer
 {
-    protected BookRepo $bookRepo;
-    protected BookshelfRepo $shelfRepo;
-    protected Cloner $cloner;
-    protected TrashCan $trashCan;
+    public function __construct(
+        protected BookRepo $bookRepo,
+        protected BookshelfRepo $shelfRepo,
+        protected Cloner $cloner,
+        protected TrashCan $trashCan
+    ) {
+    }
 
+    /**
+     * Transform a chapter into a book.
+     * Does not check permissions, check before calling.
+     */
     public function transformChapterToBook(Chapter $chapter): Book
     {
-        // TODO - Check permissions before call
-        //   Permissions: edit-chapter, delete-chapter, create-book
         $inputData = $this->cloner->entityToInputData($chapter);
         $book = $this->bookRepo->create($inputData);
         $this->cloner->copyEntityPermissions($chapter, $book);
@@ -32,14 +39,17 @@ class HierarchyTransformer
 
         $this->trashCan->destroyEntity($chapter);
 
-        // TODO - Log activity for change
+        Activity::add(ActivityType::BOOK_CREATE_FROM_CHAPTER, $book);
+
         return $book;
     }
 
+    /**
+     * Transform a book into a shelf.
+     * Does not check permissions, check before calling.
+     */
     public function transformBookToShelf(Book $book): Bookshelf
     {
-        // TODO - Check permissions before call
-        //   Permissions: edit-book, delete-book, create-shelf
         $inputData = $this->cloner->entityToInputData($book);
         $shelf = $this->shelfRepo->create($inputData, []);
         $this->cloner->copyEntityPermissions($book, $shelf);
@@ -50,17 +60,23 @@ class HierarchyTransformer
         foreach ($book->chapters as $index => $chapter) {
             $newBook = $this->transformChapterToBook($chapter);
             $shelfBookSyncData[$newBook->id] = ['order' => $index];
+            if (!$newBook->hasPermissions()) {
+                $this->cloner->copyEntityPermissions($shelf, $newBook);
+            }
         }
 
-        $shelf->books()->sync($shelfBookSyncData);
-
         if ($book->directPages->count() > 0) {
             $book->name .= ' ' . trans('entities.pages');
+            $shelfBookSyncData[$book->id] = ['order' => count($shelfBookSyncData) + 1];
+            $book->save();
         } else {
             $this->trashCan->destroyEntity($book);
         }
 
-        // TODO - Log activity for change
+        $shelf->books()->sync($shelfBookSyncData);
+
+        Activity::add(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $shelf);
+
         return $shelf;
     }
-}
\ No newline at end of file
+}