X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/f413fc528a8136c2ad8d33a57b3615fa22e55e93..refs/pull/3391/head:/app/Entities/Repos/BookshelfRepo.php diff --git a/app/Entities/Repos/BookshelfRepo.php b/app/Entities/Repos/BookshelfRepo.php index ab4a51805..ceabba59a 100644 --- a/app/Entities/Repos/BookshelfRepo.php +++ b/app/Entities/Repos/BookshelfRepo.php @@ -1,10 +1,14 @@ -with('visibleBooks') - ->orderBy($sort, $order)->paginate($count); + return Bookshelf::visible() + ->with(['visibleBooks', 'cover']) + ->orderBy($sort, $order) + ->paginate($count); } /** @@ -85,16 +90,24 @@ class BookshelfRepo $shelf = new Bookshelf(); $this->baseRepo->create($shelf, $input); $this->updateBooks($shelf, $bookIds); + Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf); + return $shelf; } /** - * Create a new shelf in the system. + * Update an existing shelf in the system using the given input. */ - public function update(Bookshelf $shelf, array $input, array $bookIds): Bookshelf + public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf { $this->baseRepo->update($shelf, $input); - $this->updateBooks($shelf, $bookIds); + + if (!is_null($bookIds)) { + $this->updateBooks($shelf, $bookIds); + } + + Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf); + return $shelf; } @@ -111,7 +124,8 @@ 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)]]; }); @@ -120,34 +134,27 @@ class BookshelfRepo /** * Update the given shelf cover image, or clear it. + * * @throws ImageUploadException * @throws Exception */ - public function updateCoverImage(Bookshelf $shelf, UploadedFile $coverImage = null, bool $removeImage = false) + 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): int + public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int { $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray(); - $shelfBooks = $shelf->books()->get(); + $shelfBooks = $shelf->books()->get(['id', 'restricted']); $updatedBookCount = 0; /** @var Book $book */ foreach ($shelfBooks as $book) { - if (!userCan('restrictions-manage', $book)) { + if ($checkUserPermissions && !userCan('restrictions-manage', $book)) { continue; } $book->permissions()->delete(); @@ -163,11 +170,14 @@ class BookshelfRepo /** * Remove a bookshelf from the system. + * * @throws Exception */ public function destroy(Bookshelf $shelf) { $trashCan = new TrashCan(); - $trashCan->destroyShelf($shelf); + $trashCan->softDestroyShelf($shelf); + Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf); + $trashCan->autoClearOld(); } }