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