1 <?php namespace BookStack\Entities\Repos;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Actions\TagRepo;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Tools\TrashCan;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Facades\Activity;
10 use BookStack\Uploads\ImageRepo;
12 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13 use Illuminate\Http\UploadedFile;
14 use Illuminate\Support\Collection;
24 * BookRepo constructor.
26 public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo)
28 $this->baseRepo = $baseRepo;
29 $this->tagRepo = $tagRepo;
30 $this->imageRepo = $imageRepo;
34 * Get all books in a paginated format.
36 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
38 return Book::visible()->orderBy($sort, $order)->paginate($count);
42 * Get the books that were most recently viewed by this user.
44 public function getRecentlyViewed(int $count = 20): Collection
46 return Book::visible()->withLastView()
47 ->having('last_viewed_at', '>', 0)
48 ->orderBy('last_viewed_at', 'desc')
49 ->take($count)->get();
53 * Get the most popular books in the system.
55 public function getPopular(int $count = 20): Collection
57 return Book::visible()->withViewCount()
58 ->having('view_count', '>', 0)
59 ->orderBy('view_count', 'desc')
60 ->take($count)->get();
64 * Get the most recently created books from the system.
66 public function getRecentlyCreated(int $count = 20): Collection
68 return Book::visible()->orderBy('created_at', 'desc')
69 ->take($count)->get();
73 * Get a book by its slug.
75 public function getBySlug(string $slug): Book
77 $book = Book::visible()->where('slug', '=', $slug)->first();
80 throw new NotFoundException(trans('errors.book_not_found'));
87 * Create a new book in the system
89 public function create(array $input): Book
92 $this->baseRepo->create($book, $input);
93 Activity::addForEntity($book, ActivityType::BOOK_CREATE);
98 * Update the given book.
100 public function update(Book $book, array $input): Book
102 $this->baseRepo->update($book, $input);
103 Activity::addForEntity($book, ActivityType::BOOK_UPDATE);
108 * Update the given book's cover image, or clear it.
109 * @throws ImageUploadException
112 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
114 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
118 * Update the permissions of a book.
120 public function updatePermissions(Book $book, bool $restricted, Collection $permissions = null)
122 $this->baseRepo->updatePermissions($book, $restricted, $permissions);
126 * Remove a book from the system.
129 public function destroy(Book $book)
131 $trashCan = new TrashCan();
132 $trashCan->softDestroyBook($book);
133 Activity::addForEntity($book, ActivityType::BOOK_DELETE);
135 $trashCan->autoClearOld();