X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/c7a2d568bf693add30c8402d68d1f46f09a44c5b..refs/pull/3918/head:/app/Entities/Repos/BookshelfRepo.php diff --git a/app/Entities/Repos/BookshelfRepo.php b/app/Entities/Repos/BookshelfRepo.php index 8f12e9dc0..d7759deb4 100644 --- a/app/Entities/Repos/BookshelfRepo.php +++ b/app/Entities/Repos/BookshelfRepo.php @@ -1,15 +1,15 @@ -with('visibleBooks') + ->with(['visibleBooks', 'cover']) ->orderBy($sort, $order) ->paginate($count); } @@ -87,8 +87,10 @@ class BookshelfRepo { $shelf = new Bookshelf(); $this->baseRepo->create($shelf, $input); + $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null); $this->updateBooks($shelf, $bookIds); - Activity::addForEntity($shelf, ActivityType::BOOKSHELF_CREATE); + Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf); + return $shelf; } @@ -103,13 +105,17 @@ class BookshelfRepo $this->updateBooks($shelf, $bookIds); } - Activity::addForEntity($shelf, ActivityType::BOOKSHELF_UPDATE); + if (array_key_exists('image', $input)) { + $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null); + } + + Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf); + return $shelf; } /** - * Update which books are assigned to this shelf by - * syncing the given book ids. + * Update which books are assigned to this shelf by syncing the given book ids. * Function ensures the books are visible to the current user and existing. */ protected function updateBooks(Bookshelf $shelf, array $bookIds) @@ -120,65 +126,24 @@ class BookshelfRepo $syncData = Book::visible() ->whereIn('id', $bookIds) - ->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) { + ->pluck('id') + ->mapWithKeys(function ($bookId) use ($numericIDs) { return [$bookId => ['order' => $numericIDs->search($bookId)]]; }); $shelf->books()->sync($syncData); } - /** - * Update the given shelf cover image, or clear it. - * @throws ImageUploadException - * @throws Exception - */ - public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false) - { - $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage); - } - - /** - * Update the permissions of a bookshelf. - */ - public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null) - { - $this->baseRepo->updatePermissions($shelf, $restricted, $permissions); - } - - /** - * Copy down the permissions of the given shelf to all child books. - */ - public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int - { - $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray(); - $shelfBooks = $shelf->books()->get(['id', 'restricted']); - $updatedBookCount = 0; - - /** @var Book $book */ - foreach ($shelfBooks as $book) { - if ($checkUserPermissions && !userCan('restrictions-manage', $book)) { - continue; - } - $book->permissions()->delete(); - $book->restricted = $shelf->restricted; - $book->permissions()->createMany($shelfPermissions); - $book->save(); - $book->rebuildPermissions(); - $updatedBookCount++; - } - - return $updatedBookCount; - } - /** * Remove a bookshelf from the system. + * * @throws Exception */ public function destroy(Bookshelf $shelf) { $trashCan = new TrashCan(); $trashCan->softDestroyShelf($shelf); - Activity::addForEntity($shelf, ActivityType::BOOKSHELF_DELETE); + Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf); $trashCan->autoClearOld(); } }