X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/d676e1e824e0377cfcb1736dd1ff622e383d8d02..refs/pull/5676/head:/app/Entities/Tools/HierarchyTransformer.php diff --git a/app/Entities/Tools/HierarchyTransformer.php b/app/Entities/Tools/HierarchyTransformer.php index 17e153e05..cd6c548fe 100644 --- a/app/Entities/Tools/HierarchyTransformer.php +++ b/app/Entities/Tools/HierarchyTransformer.php @@ -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,19 +18,23 @@ class HierarchyTransformer protected Cloner $cloner; protected TrashCan $trashCan; - // TODO - Test setting book cover image from API - // Ensure we can update without resetting image accidentally - // Ensure api docs correct. - // TODO - As above but for shelves. + 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); - - // TODO - Copy permissions + $this->cloner->copyEntityPermissions($chapter, $book); /** @var Page $page */ foreach ($chapter->pages as $page) { @@ -38,18 +44,20 @@ 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, []); - - // TODO - Copy permissions? + $this->cloner->copyEntityPermissions($book, $shelf); $shelfBookSyncData = []; @@ -57,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 +}