1 <?php namespace BookStack\Entities\Repos;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Actions\TagRepo;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\Managers\TrashCan;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Exceptions\NotifyException;
10 use BookStack\Facades\Activity;
11 use BookStack\Uploads\ImageRepo;
13 use Illuminate\Contracts\Container\BindingResolutionException;
14 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
15 use Illuminate\Http\UploadedFile;
16 use Illuminate\Support\Collection;
26 * BookRepo constructor.
29 public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo)
31 $this->baseRepo = $baseRepo;
32 $this->tagRepo = $tagRepo;
33 $this->imageRepo = $imageRepo;
37 * Get all books in a paginated format.
39 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
41 return Book::visible()->orderBy($sort, $order)->paginate($count);
45 * Get the books that were most recently viewed by this user.
47 public function getRecentlyViewed(int $count = 20): Collection
49 return Book::visible()->withLastView()
50 ->having('last_viewed_at', '>', 0)
51 ->orderBy('last_viewed_at', 'desc')
52 ->take($count)->get();
56 * Get the most popular books in the system.
58 public function getPopular(int $count = 20): Collection
60 return Book::visible()->withViewCount()
61 ->having('view_count', '>', 0)
62 ->orderBy('view_count', 'desc')
63 ->take($count)->get();
67 * Get the most recently created books from the system.
69 public function getRecentlyCreated(int $count = 20): Collection
71 return Book::visible()->orderBy('created_at', 'desc')
72 ->take($count)->get();
76 * Get a book by its slug.
78 public function getBySlug(string $slug): Book
80 $book = Book::visible()->where('slug', '=', $slug)->first();
83 throw new NotFoundException(trans('errors.book_not_found'));
90 * Create a new book in the system
92 public function create(array $input): Book
95 $this->baseRepo->create($book, $input);
96 Activity::addForEntity($book, ActivityType::BOOK_CREATE);
101 * Update the given book.
103 public function update(Book $book, array $input): Book
105 $this->baseRepo->update($book, $input);
106 Activity::addForEntity($book, ActivityType::BOOK_UPDATE);
111 * Update the given book's cover image, or clear it.
112 * @throws ImageUploadException
115 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
117 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
121 * Update the permissions of a book.
123 public function updatePermissions(Book $book, bool $restricted, Collection $permissions = null)
125 $this->baseRepo->updatePermissions($book, $restricted, $permissions);
129 * Remove a book from the system.
132 public function destroy(Book $book)
134 $trashCan = new TrashCan();
135 $trashCan->softDestroyBook($book);
136 Activity::addForEntity($book, ActivityType::BOOK_DELETE);
138 $trashCan->autoClearOld();