]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookRepo.php
consistent of book translation
[bookstack] / app / Entities / Repos / BookRepo.php
1 <?php namespace BookStack\Entities\Repos;
2
3 use BookStack\Actions\TagRepo;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Managers\TrashCan;
6 use BookStack\Exceptions\ImageUploadException;
7 use BookStack\Exceptions\NotFoundException;
8 use BookStack\Exceptions\NotifyException;
9 use BookStack\Uploads\ImageRepo;
10 use Exception;
11 use Illuminate\Contracts\Container\BindingResolutionException;
12 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13 use Illuminate\Http\UploadedFile;
14 use Illuminate\Support\Collection;
15
16 class BookRepo
17 {
18
19     protected $baseRepo;
20     protected $tagRepo;
21     protected $imageRepo;
22
23     /**
24      * BookRepo constructor.
25      * @param $tagRepo
26      */
27     public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo)
28     {
29         $this->baseRepo = $baseRepo;
30         $this->tagRepo = $tagRepo;
31         $this->imageRepo = $imageRepo;
32     }
33
34     /**
35      * Get all books in a paginated format.
36      */
37     public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
38     {
39         return Book::visible()->orderBy($sort, $order)->paginate($count);
40     }
41
42     /**
43      * Get the books that were most recently viewed by this user.
44      */
45     public function getRecentlyViewed(int $count = 20): Collection
46     {
47         return Book::visible()->withLastView()
48             ->having('last_viewed_at', '>', 0)
49             ->orderBy('last_viewed_at', 'desc')
50             ->take($count)->get();
51     }
52
53     /**
54      * Get the most popular books in the system.
55      */
56     public function getPopular(int $count = 20): Collection
57     {
58         return Book::visible()->withViewCount()
59             ->having('view_count', '>', 0)
60             ->orderBy('view_count', 'desc')
61             ->take($count)->get();
62     }
63
64     /**
65      * Get the most recently created books from the system.
66      */
67     public function getRecentlyCreated(int $count = 20): Collection
68     {
69         return Book::visible()->orderBy('created_at', 'desc')
70             ->take($count)->get();
71     }
72
73     /**
74      * Get a book by its slug.
75      */
76     public function getBySlug(string $slug): Book
77     {
78         $book = Book::visible()->where('slug', '=', $slug)->first();
79
80         if ($book === null) {
81             throw new NotFoundException(trans('errors.book_not_found'));
82         }
83
84         return $book;
85     }
86
87     /**
88      * Create a new book in the system
89      */
90     public function create(array $input): Book
91     {
92         $book = new Book();
93         $this->baseRepo->create($book, $input);
94         return $book;
95     }
96
97     /**
98      * Update the given book.
99      */
100     public function update(Book $book, array $input): Book
101     {
102         $this->baseRepo->update($book, $input);
103         return $book;
104     }
105
106     /**
107      * Update the given book's cover image, or clear it.
108      * @throws ImageUploadException
109      * @throws Exception
110      */
111     public function updateCoverImage(Book $book, UploadedFile $coverImage = null, bool $removeImage = false)
112     {
113         $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
114     }
115
116     /**
117      * Update the permissions of a book.
118      */
119     public function updatePermissions(Book $book, bool $restricted, Collection $permissions = null)
120     {
121         $this->baseRepo->updatePermissions($book, $restricted, $permissions);
122     }
123
124     /**
125      * Remove a book from the system.
126      * @throws NotifyException
127      * @throws BindingResolutionException
128      */
129     public function destroy(Book $book)
130     {
131         $trashCan = new TrashCan();
132         $trashCan->destroyBook($book);
133     }
134 }