]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Http / Controllers / BookshelfController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\EntityContextManager;
7 use BookStack\Entities\Repos\EntityRepo;
8 use BookStack\Uploads\ImageRepo;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
11 use Views;
12
13 class BookshelfController extends Controller
14 {
15
16     protected $entityRepo;
17     protected $userRepo;
18     protected $entityContextManager;
19     protected $imageRepo;
20
21     /**
22      * BookController constructor.
23      * @param EntityRepo $entityRepo
24      * @param UserRepo $userRepo
25      * @param EntityContextManager $entityContextManager
26      * @param ImageRepo $imageRepo
27      */
28     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, EntityContextManager $entityContextManager, ImageRepo $imageRepo)
29     {
30         $this->entityRepo = $entityRepo;
31         $this->userRepo = $userRepo;
32         $this->entityContextManager = $entityContextManager;
33         $this->imageRepo = $imageRepo;
34         parent::__construct();
35     }
36
37     /**
38      * Display a listing of the book.
39      * @return Response
40      */
41     public function index()
42     {
43         $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
44         $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
45         $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
46         $sortOptions = [
47             'name' => trans('common.sort_name'),
48             'created_at' => trans('common.sort_created_at'),
49             'updated_at' => trans('common.sort_updated_at'),
50         ];
51
52         $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order);
53         foreach ($shelves as $shelf) {
54             $shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
55         }
56
57         $recents = $this->isSignedIn() ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
58         $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
59         $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
60
61         $this->entityContextManager->clearShelfContext();
62         $this->setPageTitle(trans('entities.shelves'));
63         return view('shelves.index', [
64             'shelves' => $shelves,
65             'recents' => $recents,
66             'popular' => $popular,
67             'new' => $new,
68             'view' => $view,
69             'sort' => $sort,
70             'order' => $order,
71             'sortOptions' => $sortOptions,
72         ]);
73     }
74
75     /**
76      * Show the form for creating a new bookshelf.
77      * @return Response
78      */
79     public function create()
80     {
81         $this->checkPermission('bookshelf-create-all');
82         $books = $this->entityRepo->getAll('book', false, 'update');
83         $this->setPageTitle(trans('entities.shelves_create'));
84         return view('shelves.create', ['books' => $books]);
85     }
86
87     /**
88      * Store a newly created bookshelf in storage.
89      * @param Request $request
90      * @return Response
91      * @throws \BookStack\Exceptions\ImageUploadException
92      */
93     public function store(Request $request)
94     {
95         $this->checkPermission('bookshelf-create-all');
96         $this->validate($request, [
97             'name' => 'required|string|max:255',
98             'description' => 'string|max:1000',
99             'image' => $this->imageRepo->getImageValidationRules(),
100         ]);
101
102         $shelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
103         $this->shelfUpdateActions($shelf, $request);
104
105         Activity::add($shelf, 'bookshelf_create');
106         return redirect($shelf->getUrl());
107     }
108
109
110     /**
111      * Display the specified bookshelf.
112      * @param String $slug
113      * @return Response
114      * @throws \BookStack\Exceptions\NotFoundException
115      */
116     public function show(string $slug)
117     {
118         /** @var Bookshelf $shelf */
119         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
120         $this->checkOwnablePermission('book-view', $shelf);
121
122         $books = $this->entityRepo->getBookshelfChildren($shelf);
123         Views::add($shelf);
124         $this->entityContextManager->setShelfContext($shelf->id);
125
126         $this->setPageTitle($shelf->getShortName());
127
128         return view('shelves.show', [
129             'shelf' => $shelf,
130             'books' => $books,
131             'activity' => Activity::entityActivity($shelf, 20, 1)
132         ]);
133     }
134
135     /**
136      * Show the form for editing the specified bookshelf.
137      * @param $slug
138      * @return Response
139      * @throws \BookStack\Exceptions\NotFoundException
140      */
141     public function edit(string $slug)
142     {
143         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
144         $this->checkOwnablePermission('bookshelf-update', $shelf);
145
146         $shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
147         $shelfBookIds = $shelfBooks->pluck('id');
148         $books = $this->entityRepo->getAll('book', false, 'update');
149         $books = $books->filter(function ($book) use ($shelfBookIds) {
150              return !$shelfBookIds->contains($book->id);
151         });
152
153         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
154         return view('shelves.edit', [
155             'shelf' => $shelf,
156             'books' => $books,
157             'shelfBooks' => $shelfBooks,
158         ]);
159     }
160
161
162     /**
163      * Update the specified bookshelf in storage.
164      * @param Request $request
165      * @param string $slug
166      * @return Response
167      * @throws \BookStack\Exceptions\NotFoundException
168      * @throws \BookStack\Exceptions\ImageUploadException
169      */
170     public function update(Request $request, string $slug)
171     {
172         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
173         $this->checkOwnablePermission('bookshelf-update', $shelf);
174         $this->validate($request, [
175             'name' => 'required|string|max:255',
176             'description' => 'string|max:1000',
177             'image' => $this->imageRepo->getImageValidationRules(),
178         ]);
179
180          $shelf = $this->entityRepo->updateFromInput($shelf, $request->all());
181          $this->shelfUpdateActions($shelf, $request);
182
183          Activity::add($shelf, 'bookshelf_update');
184
185          return redirect($shelf->getUrl());
186     }
187
188
189     /**
190      * Shows the page to confirm deletion
191      * @param $slug
192      * @return \Illuminate\View\View
193      * @throws \BookStack\Exceptions\NotFoundException
194      */
195     public function showDelete(string $slug)
196     {
197         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
198         $this->checkOwnablePermission('bookshelf-delete', $shelf);
199
200         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
201         return view('shelves.delete', ['shelf' => $shelf]);
202     }
203
204     /**
205      * Remove the specified bookshelf from storage.
206      * @param string $slug
207      * @return Response
208      * @throws \BookStack\Exceptions\NotFoundException
209      * @throws \Throwable
210      */
211     public function destroy(string $slug)
212     {
213         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
214         $this->checkOwnablePermission('bookshelf-delete', $shelf);
215         Activity::addMessage('bookshelf_delete', $shelf->name);
216
217         if ($shelf->cover) {
218             $this->imageRepo->destroyImage($shelf->cover);
219         }
220         $this->entityRepo->destroyBookshelf($shelf);
221
222         return redirect('/shelves');
223     }
224
225     /**
226      * Show the permissions view.
227      * @param string $slug
228      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
229      * @throws \BookStack\Exceptions\NotFoundException
230      */
231     public function showPermissions(string $slug)
232     {
233         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
234         $this->checkOwnablePermission('restrictions-manage', $shelf);
235
236         $roles = $this->userRepo->getRestrictableRoles();
237         return view('shelves.permissions', [
238             'shelf' => $shelf,
239             'roles' => $roles
240         ]);
241     }
242
243     /**
244      * Set the permissions for this bookshelf.
245      * @param Request $request
246      * @param string $slug
247      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
248      * @throws \BookStack\Exceptions\NotFoundException
249      * @throws \Throwable
250      */
251     public function permissions(Request $request, string $slug)
252     {
253         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
254         $this->checkOwnablePermission('restrictions-manage', $shelf);
255
256         $this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
257         $this->showSuccessNotification( trans('entities.shelves_permissions_updated'));
258         return redirect($shelf->getUrl());
259     }
260
261     /**
262      * Copy the permissions of a bookshelf to the child books.
263      * @param string $slug
264      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
265      * @throws \BookStack\Exceptions\NotFoundException
266      */
267     public function copyPermissions(string $slug)
268     {
269         $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
270         $this->checkOwnablePermission('restrictions-manage', $shelf);
271
272         $updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
273         $this->showSuccessNotification( trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
274         return redirect($shelf->getUrl());
275     }
276
277     /**
278      * Common actions to run on bookshelf update.
279      * @param Bookshelf $shelf
280      * @param Request $request
281      * @throws \BookStack\Exceptions\ImageUploadException
282      */
283     protected function shelfUpdateActions(Bookshelf $shelf, Request $request)
284     {
285         // Update the books that the shelf references
286         $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
287
288         // Update the cover image if in request
289         if ($request->has('image')) {
290             $newImage = $request->file('image');
291             $this->imageRepo->destroyImage($shelf->cover);
292             $image = $this->imageRepo->saveNew($newImage, 'cover_shelf', $shelf->id, 512, 512, true);
293             $shelf->image_id = $image->id;
294             $shelf->save();
295         }
296
297         if ($request->has('image_reset')) {
298             $this->imageRepo->destroyImage($shelf->cover);
299             $shelf->image_id = 0;
300             $shelf->save();
301         }
302     }
303 }