1 <?php namespace BookStack\Entities\Repos;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\Managers\TrashCan;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Facades\Activity;
11 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
12 use Illuminate\Http\UploadedFile;
13 use Illuminate\Support\Collection;
20 * BookshelfRepo constructor.
23 public function __construct(BaseRepo $baseRepo)
25 $this->baseRepo = $baseRepo;
29 * Get all bookshelves in a paginated format.
31 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
33 return Bookshelf::visible()
34 ->with('visibleBooks')
35 ->orderBy($sort, $order)
40 * Get the bookshelves that were most recently viewed by this user.
42 public function getRecentlyViewed(int $count = 20): Collection
44 return Bookshelf::visible()->withLastView()
45 ->having('last_viewed_at', '>', 0)
46 ->orderBy('last_viewed_at', 'desc')
47 ->take($count)->get();
51 * Get the most popular bookshelves in the system.
53 public function getPopular(int $count = 20): Collection
55 return Bookshelf::visible()->withViewCount()
56 ->having('view_count', '>', 0)
57 ->orderBy('view_count', 'desc')
58 ->take($count)->get();
62 * Get the most recently created bookshelves from the system.
64 public function getRecentlyCreated(int $count = 20): Collection
66 return Bookshelf::visible()->orderBy('created_at', 'desc')
67 ->take($count)->get();
71 * Get a shelf by its slug.
73 public function getBySlug(string $slug): Bookshelf
75 $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
77 if ($shelf === null) {
78 throw new NotFoundException(trans('errors.bookshelf_not_found'));
85 * Create a new shelf in the system.
87 public function create(array $input, array $bookIds): Bookshelf
89 $shelf = new Bookshelf();
90 $this->baseRepo->create($shelf, $input);
91 $this->updateBooks($shelf, $bookIds);
92 Activity::addForEntity($shelf, ActivityType::BOOKSHELF_CREATE);
97 * Update an existing shelf in the system using the given input.
99 public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
101 $this->baseRepo->update($shelf, $input);
103 if (!is_null($bookIds)) {
104 $this->updateBooks($shelf, $bookIds);
107 Activity::addForEntity($shelf, ActivityType::BOOKSHELF_UPDATE);
112 * Update which books are assigned to this shelf by
113 * syncing the given book ids.
114 * Function ensures the books are visible to the current user and existing.
116 protected function updateBooks(Bookshelf $shelf, array $bookIds)
118 $numericIDs = collect($bookIds)->map(function ($id) {
122 $syncData = Book::visible()
123 ->whereIn('id', $bookIds)
124 ->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) {
125 return [$bookId => ['order' => $numericIDs->search($bookId)]];
128 $shelf->books()->sync($syncData);
132 * Update the given shelf cover image, or clear it.
133 * @throws ImageUploadException
136 public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false)
138 $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
142 * Update the permissions of a bookshelf.
144 public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null)
146 $this->baseRepo->updatePermissions($shelf, $restricted, $permissions);
150 * Copy down the permissions of the given shelf to all child books.
152 public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int
154 $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray();
155 $shelfBooks = $shelf->books()->get(['id', 'restricted']);
156 $updatedBookCount = 0;
158 /** @var Book $book */
159 foreach ($shelfBooks as $book) {
160 if ($checkUserPermissions && !userCan('restrictions-manage', $book)) {
163 $book->permissions()->delete();
164 $book->restricted = $shelf->restricted;
165 $book->permissions()->createMany($shelfPermissions);
167 $book->rebuildPermissions();
171 return $updatedBookCount;
175 * Remove a bookshelf from the system.
178 public function destroy(Bookshelf $shelf)
180 $trashCan = new TrashCan();
181 $trashCan->softDestroyShelf($shelf);
182 Activity::addForEntity($shelf, ActivityType::BOOKSHELF_DELETE);
183 $trashCan->autoClearOld();