1 <?php namespace BookStack\Entities\Repos;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5 use BookStack\Entities\Managers\TrashCan;
6 use BookStack\Exceptions\ImageUploadException;
7 use BookStack\Exceptions\NotFoundException;
9 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
10 use Illuminate\Http\UploadedFile;
11 use Illuminate\Support\Collection;
18 * BookshelfRepo constructor.
21 public function __construct(BaseRepo $baseRepo)
23 $this->baseRepo = $baseRepo;
27 * Get all bookshelves in a paginated format.
29 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
31 return Bookshelf::visible()
32 ->with('visibleBooks')
33 ->orderBy($sort, $order)
38 * Get the bookshelves that were most recently viewed by this user.
40 public function getRecentlyViewed(int $count = 20): Collection
42 return Bookshelf::visible()->withLastView()
43 ->having('last_viewed_at', '>', 0)
44 ->orderBy('last_viewed_at', 'desc')
45 ->take($count)->get();
49 * Get the most popular bookshelves in the system.
51 public function getPopular(int $count = 20): Collection
53 return Bookshelf::visible()->withViewCount()
54 ->having('view_count', '>', 0)
55 ->orderBy('view_count', 'desc')
56 ->take($count)->get();
60 * Get the most recently created bookshelves from the system.
62 public function getRecentlyCreated(int $count = 20): Collection
64 return Bookshelf::visible()->orderBy('created_at', 'desc')
65 ->take($count)->get();
69 * Get a shelf by its slug.
71 public function getBySlug(string $slug): Bookshelf
73 $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
75 if ($shelf === null) {
76 throw new NotFoundException(trans('errors.bookshelf_not_found'));
83 * Create a new shelf in the system.
85 public function create(array $input, array $bookIds): Bookshelf
87 $shelf = new Bookshelf();
88 $this->baseRepo->create($shelf, $input);
89 $this->updateBooks($shelf, $bookIds);
94 * Create a new shelf in the system.
96 public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
98 $this->baseRepo->update($shelf, $input);
100 if (!is_null($bookIds)) {
101 $this->updateBooks($shelf, $bookIds);
108 * Update which books are assigned to this shelf by
109 * syncing the given book ids.
110 * Function ensures the books are visible to the current user and existing.
112 protected function updateBooks(Bookshelf $shelf, array $bookIds)
114 $numericIDs = collect($bookIds)->map(function ($id) {
118 $syncData = Book::visible()
119 ->whereIn('id', $bookIds)
120 ->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) {
121 return [$bookId => ['order' => $numericIDs->search($bookId)]];
124 $shelf->books()->sync($syncData);
128 * Update the given shelf cover image, or clear it.
129 * @throws ImageUploadException
132 public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false)
134 $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
138 * Update the permissions of a bookshelf.
140 public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null)
142 $this->baseRepo->updatePermissions($shelf, $restricted, $permissions);
146 * Copy down the permissions of the given shelf to all child books.
148 public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int
150 $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray();
151 $shelfBooks = $shelf->books()->get(['id', 'restricted']);
152 $updatedBookCount = 0;
154 /** @var Book $book */
155 foreach ($shelfBooks as $book) {
156 if ($checkUserPermissions && !userCan('restrictions-manage', $book)) {
159 $book->permissions()->delete();
160 $book->restricted = $shelf->restricted;
161 $book->permissions()->createMany($shelfPermissions);
163 $book->rebuildPermissions();
167 return $updatedBookCount;
171 * Remove a bookshelf from the system.
174 public function destroy(Bookshelf $shelf)
176 $trashCan = new TrashCan();
177 $trashCan->destroyShelf($shelf);