]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookshelfRepo.php
Update settings.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
98         if (!is_null($bookIds)) {
99             $this->updateBooks($shelf, $bookIds);
100         }
101
102         return $shelf;
103     }
104
105     /**
106      * Update which books are assigned to this shelf by
107      * syncing the given book ids.
108      * Function ensures the books are visible to the current user and existing.
109      */
110     protected function updateBooks(Bookshelf $shelf, array $bookIds)
111     {
112         $numericIDs = collect($bookIds)->map(function ($id) {
113             return intval($id);
114         });
115
116         $syncData = Book::visible()
117             ->whereIn('id', $bookIds)
118             ->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) {
119                 return [$bookId => ['order' => $numericIDs->search($bookId)]];
120             });
121
122         $shelf->books()->sync($syncData);
123     }
124
125     /**
126      * Update the given shelf cover image, or clear it.
127      * @throws ImageUploadException
128      * @throws Exception
129      */
130     public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false)
131     {
132         $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
133     }
134
135     /**
136      * Update the permissions of a bookshelf.
137      */
138     public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null)
139     {
140         $this->baseRepo->updatePermissions($shelf, $restricted, $permissions);
141     }
142
143     /**
144      * Copy down the permissions of the given shelf to all child books.
145      */
146     public function copyDownPermissions(Bookshelf $shelf, $checkUserPermissions = true): int
147     {
148         $shelfPermissions = $shelf->permissions()->get(['role_id', 'action'])->toArray();
149         $shelfBooks = $shelf->books()->get(['id', 'restricted']);
150         $updatedBookCount = 0;
151
152         /** @var Book $book */
153         foreach ($shelfBooks as $book) {
154             if ($checkUserPermissions && !userCan('restrictions-manage', $book)) {
155                 continue;
156             }
157             $book->permissions()->delete();
158             $book->restricted = $shelf->restricted;
159             $book->permissions()->createMany($shelfPermissions);
160             $book->save();
161             $book->rebuildPermissions();
162             $updatedBookCount++;
163         }
164
165         return $updatedBookCount;
166     }
167
168     /**
169      * Remove a bookshelf from the system.
170      * @throws Exception
171      */
172     public function destroy(Bookshelf $shelf)
173     {
174         $trashCan = new TrashCan();
175         $trashCan->destroyShelf($shelf);
176     }
177 }