X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/90ec40691a6e523475d336a8ffb8280c05347b98..refs/pull/5721/head:/app/Entities/Tools/HierarchyTransformer.php diff --git a/app/Entities/Tools/HierarchyTransformer.php b/app/Entities/Tools/HierarchyTransformer.php index c95d5fa53..b0d8880f4 100644 --- a/app/Entities/Tools/HierarchyTransformer.php +++ b/app/Entities/Tools/HierarchyTransformer.php @@ -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 +}