3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\TagRepo;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Tools\TrashCan;
10 use BookStack\Exceptions\ImageUploadException;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Facades\Activity;
13 use BookStack\Uploads\ImageRepo;
15 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
16 use Illuminate\Http\UploadedFile;
17 use Illuminate\Support\Collection;
21 public function __construct(
22 protected BaseRepo $baseRepo,
23 protected TagRepo $tagRepo,
24 protected ImageRepo $imageRepo
29 * Get all books in a paginated format.
31 public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
33 return Book::visible()->with('cover')->orderBy($sort, $order)->paginate($count);
37 * Get the books that were most recently viewed by this user.
39 public function getRecentlyViewed(int $count = 20): Collection
41 return Book::visible()->withLastView()
42 ->having('last_viewed_at', '>', 0)
43 ->orderBy('last_viewed_at', 'desc')
44 ->take($count)->get();
48 * Get the most popular books in the system.
50 public function getPopular(int $count = 20): Collection
52 return Book::visible()->withViewCount()
53 ->having('view_count', '>', 0)
54 ->orderBy('view_count', 'desc')
55 ->take($count)->get();
59 * Get the most recently created books from the system.
61 public function getRecentlyCreated(int $count = 20): Collection
63 return Book::visible()->orderBy('created_at', 'desc')
64 ->take($count)->get();
68 * Get a book by its slug.
70 public function getBySlug(string $slug): Book
72 $book = Book::visible()->where('slug', '=', $slug)->first();
75 throw new NotFoundException(trans('errors.book_not_found'));
82 * Create a new book in the system.
84 public function create(array $input): Book
87 $this->baseRepo->create($book, $input);
88 $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
89 Activity::add(ActivityType::BOOK_CREATE, $book);
95 * Update the given book.
97 public function update(Book $book, array $input): Book
99 $this->baseRepo->update($book, $input);
101 if (array_key_exists('default_template', $input)) {
102 $this->updateBookDefaultTemplate($book, intval($input['default_template']));
105 if (array_key_exists('image', $input)) {
106 $this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
109 Activity::add(ActivityType::BOOK_UPDATE, $book);
115 * Update the default page template used for this book.
116 * Checks that, if changing, the provided value is a valid template and the user
117 * has visibility of the provided page template id.
119 protected function updateBookDefaultTemplate(Book $book, int $templateId): void
121 $changing = $templateId !== intval($book->default_template);
126 if ($templateId === 0) {
127 $book->default_template = null;
132 $templateExists = Page::query()->visible()
133 ->where('template', '=', true)
134 ->where('id', '=', $templateId)
137 $book->default_template = $templateExists ? $templateId : null;
142 * Update the given book's cover image, or clear it.
144 * @throws ImageUploadException
147 public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
149 $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
153 * Remove a book from the system.
157 public function destroy(Book $book)
159 $trashCan = new TrashCan();
160 $trashCan->softDestroyBook($book);
161 Activity::add(ActivityType::BOOK_DELETE, $book);
163 $trashCan->autoClearOld();