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