]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/HierarchyTransformer.php
respective book and chapter structure added.
[bookstack] / app / Entities / Tools / HierarchyTransformer.php
index c95d5fa5307e4d4e7d9bd930d8a031f7b15de804..cd6c548fe581b1f9179e5332dd3174461ea4dbe1 100644 (file)
@@ -2,12 +2,14 @@
 
 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
 {
@@ -16,10 +18,20 @@ class HierarchyTransformer
     protected Cloner $cloner;
     protected TrashCan $trashCan;
 
+    public function __construct(BookRepo $bookRepo, BookshelfRepo $shelfRepo, Cloner $cloner, TrashCan $trashCan)
+    {
+        $this->bookRepo = $bookRepo;
+        $this->shelfRepo = $shelfRepo;
+        $this->cloner = $cloner;
+        $this->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 +44,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 +65,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
+}