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