1 <?php namespace BookStack\Entities\Repos;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Tools\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.
22 public function __construct(BaseRepo $baseRepo)
24 $this->baseRepo = $baseRepo;
28 * Get all bookshelves in a paginated format.
30 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
32 return Bookshelf::visible()
33 ->with('visibleBooks')
34 ->orderBy($sort, $order)
39 * Get the bookshelves that were most recently viewed by this user.
41 public function getRecentlyViewed(int $count = 20): Collection
43 return Bookshelf::visible()->withLastView()
44 ->having('last_viewed_at', '>', 0)
45 ->orderBy('last_viewed_at', 'desc')
46 ->take($count)->get();
50 * Get the most popular bookshelves in the system.
52 public function getPopular(int $count = 20): Collection
54 return Bookshelf::visible()->withViewCount()
55 ->having('view_count', '>', 0)
56 ->orderBy('view_count', 'desc')
57 ->take($count)->get();
61 * Get the most recently created bookshelves from the system.
63 public function getRecentlyCreated(int $count = 20): Collection
65 return Bookshelf::visible()->orderBy('created_at', 'desc')
66 ->take($count)->get();
70 * Get a shelf by its slug.
72 public function getBySlug(string $slug): Bookshelf
74 $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
76 if ($shelf === null) {
77 throw new NotFoundException(trans('errors.bookshelf_not_found'));
84 * Create a new shelf in the system.
86 public function create(array $input, array $bookIds): Bookshelf
88 $shelf = new Bookshelf();
89 $this->baseRepo->create($shelf, $input);
90 $this->updateBooks($shelf, $bookIds);
91 Activity::addForEntity($shelf, ActivityType::BOOKSHELF_CREATE);
96 * Update an existing shelf in the system using the given input.
98 public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
100 $this->baseRepo->update($shelf, $input);
102 if (!is_null($bookIds)) {
103 $this->updateBooks($shelf, $bookIds);
106 Activity::addForEntity($shelf, ActivityType::BOOKSHELF_UPDATE);
111 * Update which books are assigned to this shelf by
112 * syncing the given book ids.
113 * Function ensures the books are visible to the current user and existing.
115 protected function updateBooks(Bookshelf $shelf, array $bookIds)
117 $numericIDs = collect($bookIds)->map(function ($id) {
121 $syncData = Book::visible()
122 ->whereIn('id', $bookIds)
123 ->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) {
124 return [$bookId => ['order' => $numericIDs->search($bookId)]];
127 $shelf->books()->sync($syncData);
131 * Update the given shelf cover image, or clear it.
132 * @throws ImageUploadException
135 public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false)
137 $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
141 * Update the permissions of a bookshelf.
143 public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null)
145 $this->baseRepo->updatePermissions($shelf, $restricted, $permissions);
149 * Copy down the permissions of the given shelf to all child books.
151 public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int
153 $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray();
154 $shelfBooks = $shelf->books()->get(['id', 'restricted']);
155 $updatedBookCount = 0;
157 /** @var Book $book */
158 foreach ($shelfBooks as $book) {
159 if ($checkUserPermissions && !userCan('restrictions-manage', $book)) {
162 $book->permissions()->delete();
163 $book->restricted = $shelf->restricted;
164 $book->permissions()->createMany($shelfPermissions);
166 $book->rebuildPermissions();
170 return $updatedBookCount;
174 * Remove a bookshelf from the system.
177 public function destroy(Bookshelf $shelf)
179 $trashCan = new TrashCan();
180 $trashCan->softDestroyShelf($shelf);
181 Activity::addForEntity($shelf, ActivityType::BOOKSHELF_DELETE);
182 $trashCan->autoClearOld();