3 namespace BookStack\Entities\Repos;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Exceptions\NotFoundException;
11 use BookStack\Facades\Activity;
13 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
14 use Illuminate\Http\UploadedFile;
15 use Illuminate\Support\Collection;
22 * BookshelfRepo constructor.
24 public function __construct(BaseRepo $baseRepo)
26 $this->baseRepo = $baseRepo;
30 * Get all bookshelves in a paginated format.
32 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
34 return Bookshelf::visible()
35 ->with(['visibleBooks', 'cover'])
36 ->orderBy($sort, $order)
41 * Get the bookshelves that were most recently viewed by this user.
43 public function getRecentlyViewed(int $count = 20): Collection
45 return Bookshelf::visible()->withLastView()
46 ->having('last_viewed_at', '>', 0)
47 ->orderBy('last_viewed_at', 'desc')
48 ->take($count)->get();
52 * Get the most popular bookshelves in the system.
54 public function getPopular(int $count = 20): Collection
56 return Bookshelf::visible()->withViewCount()
57 ->having('view_count', '>', 0)
58 ->orderBy('view_count', 'desc')
59 ->take($count)->get();
63 * Get the most recently created bookshelves from the system.
65 public function getRecentlyCreated(int $count = 20): Collection
67 return Bookshelf::visible()->orderBy('created_at', 'desc')
68 ->take($count)->get();
72 * Get a shelf by its slug.
74 public function getBySlug(string $slug): Bookshelf
76 $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
78 if ($shelf === null) {
79 throw new NotFoundException(trans('errors.bookshelf_not_found'));
86 * Create a new shelf in the system.
88 public function create(array $input, array $bookIds): Bookshelf
90 $shelf = new Bookshelf();
91 $this->baseRepo->create($shelf, $input);
92 $this->updateBooks($shelf, $bookIds);
93 Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
99 * Update an existing shelf in the system using the given input.
101 public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
103 $this->baseRepo->update($shelf, $input);
105 if (!is_null($bookIds)) {
106 $this->updateBooks($shelf, $bookIds);
109 Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
115 * Update which books are assigned to this shelf by
116 * syncing the given book ids.
117 * Function ensures the books are visible to the current user and existing.
119 protected function updateBooks(Bookshelf $shelf, array $bookIds)
121 $numericIDs = collect($bookIds)->map(function ($id) {
125 $syncData = Book::visible()
126 ->whereIn('id', $bookIds)
128 ->mapWithKeys(function ($bookId) use ($numericIDs) {
129 return [$bookId => ['order' => $numericIDs->search($bookId)]];
132 $shelf->books()->sync($syncData);
136 * Update the given shelf cover image, or clear it.
138 * @throws ImageUploadException
141 public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false)
143 $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
147 * Copy down the permissions of the given shelf to all child books.
149 public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int
151 $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray();
152 $shelfBooks = $shelf->books()->get(['id', 'restricted']);
153 $updatedBookCount = 0;
155 /** @var Book $book */
156 foreach ($shelfBooks as $book) {
157 if ($checkUserPermissions && !userCan('restrictions-manage', $book)) {
160 $book->permissions()->delete();
161 $book->restricted = $shelf->restricted;
162 $book->permissions()->createMany($shelfPermissions);
164 $book->rebuildPermissions();
168 return $updatedBookCount;
172 * Remove a bookshelf from the system.
176 public function destroy(Bookshelf $shelf)
178 $trashCan = new TrashCan();
179 $trashCan->softDestroyShelf($shelf);
180 Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);
181 $trashCan->autoClearOld();