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