]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Updated existing image tests to reflect changes
[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()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
44         $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
45         $order = setting()->getUser($this->currentUser, '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->signedIn ? $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->getBySlug('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         return view('shelves.show', [
128             'shelf' => $shelf,
129             'books' => $books,
130             'activity' => Activity::entityActivity($shelf, 20, 1)
131         ]);
132     }
133
134     /**
135      * Show the form for editing the specified bookshelf.
136      * @param $slug
137      * @return Response
138      * @throws \BookStack\Exceptions\NotFoundException
139      */
140     public function edit(string $slug)
141     {
142         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
143         $this->checkOwnablePermission('bookshelf-update', $shelf);
144
145         $shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
146         $shelfBookIds = $shelfBooks->pluck('id');
147         $books = $this->entityRepo->getAll('book', false, 'update');
148         $books = $books->filter(function ($book) use ($shelfBookIds) {
149              return !$shelfBookIds->contains($book->id);
150         });
151
152         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
153         return view('shelves.edit', [
154             'shelf' => $shelf,
155             'books' => $books,
156             'shelfBooks' => $shelfBooks,
157         ]);
158     }
159
160
161     /**
162      * Update the specified bookshelf in storage.
163      * @param Request $request
164      * @param string $slug
165      * @return Response
166      * @throws \BookStack\Exceptions\NotFoundException
167      * @throws \BookStack\Exceptions\ImageUploadException
168      */
169     public function update(Request $request, string $slug)
170     {
171         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
172         $this->checkOwnablePermission('bookshelf-update', $shelf);
173         $this->validate($request, [
174             'name' => 'required|string|max:255',
175             'description' => 'string|max:1000',
176             'image' => $this->imageRepo->getImageValidationRules(),
177         ]);
178
179          $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
180          $this->shelfUpdateActions($shelf, $request);
181
182          Activity::add($shelf, 'bookshelf_update');
183
184          return redirect($shelf->getUrl());
185     }
186
187
188     /**
189      * Shows the page to confirm deletion
190      * @param $slug
191      * @return \Illuminate\View\View
192      * @throws \BookStack\Exceptions\NotFoundException
193      */
194     public function showDelete(string $slug)
195     {
196         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
197         $this->checkOwnablePermission('bookshelf-delete', $shelf);
198
199         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
200         return view('shelves.delete', ['shelf' => $shelf]);
201     }
202
203     /**
204      * Remove the specified bookshelf from storage.
205      * @param string $slug
206      * @return Response
207      * @throws \BookStack\Exceptions\NotFoundException
208      * @throws \Throwable
209      */
210     public function destroy(string $slug)
211     {
212         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
213         $this->checkOwnablePermission('bookshelf-delete', $shelf);
214         Activity::addMessage('bookshelf_delete', 0, $shelf->name);
215
216         if ($shelf->cover) {
217             $this->imageRepo->destroyImage($shelf->cover);
218         }
219         $this->entityRepo->destroyBookshelf($shelf);
220
221         return redirect('/shelves');
222     }
223
224     /**
225      * Show the permissions view.
226      * @param string $slug
227      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
228      * @throws \BookStack\Exceptions\NotFoundException
229      */
230     public function showPermissions(string $slug)
231     {
232         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
233         $this->checkOwnablePermission('restrictions-manage', $shelf);
234
235         $roles = $this->userRepo->getRestrictableRoles();
236         return view('shelves.permissions', [
237             'shelf' => $shelf,
238             'roles' => $roles
239         ]);
240     }
241
242     /**
243      * Set the permissions for this bookshelf.
244      * @param string $slug
245      * @param Request $request
246      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
247      * @throws \BookStack\Exceptions\NotFoundException
248      * @throws \Throwable
249      */
250     public function permissions(string $slug, Request $request)
251     {
252         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
253         $this->checkOwnablePermission('restrictions-manage', $shelf);
254
255         $this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
256         session()->flash('success', trans('entities.shelves_permissions_updated'));
257         return redirect($shelf->getUrl());
258     }
259
260     /**
261      * Copy the permissions of a bookshelf to the child books.
262      * @param string $slug
263      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
264      * @throws \BookStack\Exceptions\NotFoundException
265      */
266     public function copyPermissions(string $slug)
267     {
268         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
269         $this->checkOwnablePermission('restrictions-manage', $shelf);
270
271         $updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
272         session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
273         return redirect($shelf->getUrl());
274     }
275
276     /**
277      * Common actions to run on bookshelf update.
278      * @param Bookshelf $shelf
279      * @param Request $request
280      * @throws \BookStack\Exceptions\ImageUploadException
281      */
282     protected function shelfUpdateActions(Bookshelf $shelf, Request $request)
283     {
284         // Update the books that the shelf references
285         $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
286
287         // Update the cover image if in request
288         if ($request->has('image')) {
289             $newImage = $request->file('image');
290             $image = $this->imageRepo->saveNew($newImage, 'cover_shelf', $shelf->id, 512, 512, true);
291             $shelf->image_id = $image->id;
292             $shelf->save();
293         }
294
295         if ($request->has('image_reset')) {
296             $this->imageRepo->destroyImage($shelf->cover);
297             $shelf->image_id = 0;
298             $shelf->save();
299         }
300     }
301 }