]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookshelfRepo.php
Update entities.php
[bookstack] / app / Entities / Repos / BookshelfRepo.php
1 <?php namespace BookStack\Entities\Repos;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5 use BookStack\Entities\Managers\TrashCan;
6 use BookStack\Exceptions\ImageUploadException;
7 use BookStack\Exceptions\NotFoundException;
8 use Exception;
9 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
10 use Illuminate\Http\UploadedFile;
11 use Illuminate\Support\Collection;
12
13 class BookshelfRepo
14 {
15     protected $baseRepo;
16
17     /**
18      * BookshelfRepo constructor.
19      * @param $baseRepo
20      */
21     public function __construct(BaseRepo $baseRepo)
22     {
23         $this->baseRepo = $baseRepo;
24     }
25
26     /**
27      * Get all bookshelves in a paginated format.
28      */
29     public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
30     {
31         return Bookshelf::visible()->with('visibleBooks')
32             ->orderBy($sort, $order)->paginate($count);
33     }
34
35     /**
36      * Get the bookshelves that were most recently viewed by this user.
37      */
38     public function getRecentlyViewed(int $count = 20): Collection
39     {
40         return Bookshelf::visible()->withLastView()
41             ->having('last_viewed_at', '>', 0)
42             ->orderBy('last_viewed_at', 'desc')
43             ->take($count)->get();
44     }
45
46     /**
47      * Get the most popular bookshelves in the system.
48      */
49     public function getPopular(int $count = 20): Collection
50     {
51         return Bookshelf::visible()->withViewCount()
52             ->having('view_count', '>', 0)
53             ->orderBy('view_count', 'desc')
54             ->take($count)->get();
55     }
56
57     /**
58      * Get the most recently created bookshelves from the system.
59      */
60     public function getRecentlyCreated(int $count = 20): Collection
61     {
62         return Bookshelf::visible()->orderBy('created_at', 'desc')
63             ->take($count)->get();
64     }
65
66     /**
67      * Get a shelf by its slug.
68      */
69     public function getBySlug(string $slug): Bookshelf
70     {
71         $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
72
73         if ($shelf === null) {
74             throw new NotFoundException(trans('errors.bookshelf_not_found'));
75         }
76
77         return $shelf;
78     }
79
80     /**
81      * Create a new shelf in the system.
82      */
83     public function create(array $input, array $bookIds): Bookshelf
84     {
85         $shelf = new Bookshelf();
86         $this->baseRepo->create($shelf, $input);
87         $this->updateBooks($shelf, $bookIds);
88         return $shelf;
89     }
90
91     /**
92      * Create a new shelf in the system.
93      */
94     public function update(Bookshelf $shelf, array $input, array $bookIds): Bookshelf
95     {
96         $this->baseRepo->update($shelf, $input);
97         $this->updateBooks($shelf, $bookIds);
98         return $shelf;
99     }
100
101     /**
102      * Update which books are assigned to this shelf by
103      * syncing the given book ids.
104      * Function ensures the books are visible to the current user and existing.
105      */
106     protected function updateBooks(Bookshelf $shelf, array $bookIds)
107     {
108         $numericIDs = collect($bookIds)->map(function ($id) {
109             return intval($id);
110         });
111
112         $syncData = Book::visible()
113             ->whereIn('id', $bookIds)
114             ->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) {
115                 return [$bookId => ['order' => $numericIDs->search($bookId)]];
116             });
117
118         $shelf->books()->sync($syncData);
119     }
120
121     /**
122      * Update the given shelf cover image, or clear it.
123      * @throws ImageUploadException
124      * @throws Exception
125      */
126     public function updateCoverImage(Bookshelf $shelf, UploadedFile $coverImage = null, bool $removeImage = false)
127     {
128         $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
129     }
130
131     /**
132      * Update the permissions of a bookshelf.
133      */
134     public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null)
135     {
136         $this->baseRepo->updatePermissions($shelf, $restricted, $permissions);
137     }
138
139     /**
140      * Copy down the permissions of the given shelf to all child books.
141      */
142     public function copyDownPermissions(Bookshelf $shelf): int
143     {
144         $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray();
145         $shelfBooks = $shelf->books()->get();
146         $updatedBookCount = 0;
147
148         /** @var Book $book */
149         foreach ($shelfBooks as $book) {
150             if (!userCan('restrictions-manage', $book)) {
151                 continue;
152             }
153             $book->permissions()->delete();
154             $book->restricted = $shelf->restricted;
155             $book->permissions()->createMany($shelfPermissions);
156             $book->save();
157             $book->rebuildPermissions();
158             $updatedBookCount++;
159         }
160
161         return $updatedBookCount;
162     }
163
164     /**
165      * Remove a bookshelf from the system.
166      * @throws Exception
167      */
168     public function destroy(Bookshelf $shelf)
169     {
170         $trashCan = new TrashCan();
171         $trashCan->destroyShelf($shelf);
172     }
173 }