]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Started diversion to not using image manager for cover/system/user
[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      */
92     public function store(Request $request)
93     {
94         $this->checkPermission('bookshelf-create-all');
95         $this->validate($request, [
96             'name' => 'required|string|max:255',
97             'description' => 'string|max:1000',
98             'image' => $this->imageRepo->getImageValidationRules(),
99         ]);
100
101         $shelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
102         $this->shelfUpdateActions($shelf, $request);
103
104         Activity::add($shelf, 'bookshelf_create');
105         return redirect($shelf->getUrl());
106     }
107
108
109     /**
110      * Display the specified bookshelf.
111      * @param String $slug
112      * @return Response
113      * @throws \BookStack\Exceptions\NotFoundException
114      */
115     public function show(string $slug)
116     {
117         /** @var Bookshelf $shelf */
118         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
119         $this->checkOwnablePermission('book-view', $shelf);
120
121         $books = $this->entityRepo->getBookshelfChildren($shelf);
122         Views::add($shelf);
123         $this->entityContextManager->setShelfContext($shelf->id);
124
125         $this->setPageTitle($shelf->getShortName());
126         return view('shelves.show', [
127             'shelf' => $shelf,
128             'books' => $books,
129             'activity' => Activity::entityActivity($shelf, 20, 1)
130         ]);
131     }
132
133     /**
134      * Show the form for editing the specified bookshelf.
135      * @param $slug
136      * @return Response
137      * @throws \BookStack\Exceptions\NotFoundException
138      */
139     public function edit(string $slug)
140     {
141         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
142         $this->checkOwnablePermission('bookshelf-update', $shelf);
143
144         $shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
145         $shelfBookIds = $shelfBooks->pluck('id');
146         $books = $this->entityRepo->getAll('book', false, 'update');
147         $books = $books->filter(function ($book) use ($shelfBookIds) {
148              return !$shelfBookIds->contains($book->id);
149         });
150
151         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
152         return view('shelves.edit', [
153             'shelf' => $shelf,
154             'books' => $books,
155             'shelfBooks' => $shelfBooks,
156         ]);
157     }
158
159
160     /**
161      * Update the specified bookshelf in storage.
162      * @param Request $request
163      * @param string $slug
164      * @return Response
165      * @throws \BookStack\Exceptions\NotFoundException
166      * @throws \BookStack\Exceptions\ImageUploadException
167      */
168     public function update(Request $request, string $slug)
169     {
170         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
171         $this->checkOwnablePermission('bookshelf-update', $shelf);
172         $this->validate($request, [
173             'name' => 'required|string|max:255',
174             'description' => 'string|max:1000',
175             'image' => $this->imageRepo->getImageValidationRules(),
176         ]);
177
178          $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
179          $this->shelfUpdateActions($shelf, $request);
180
181          Activity::add($shelf, 'bookshelf_update');
182
183          return redirect($shelf->getUrl());
184     }
185
186
187     /**
188      * Shows the page to confirm deletion
189      * @param $slug
190      * @return \Illuminate\View\View
191      * @throws \BookStack\Exceptions\NotFoundException
192      */
193     public function showDelete(string $slug)
194     {
195         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
196         $this->checkOwnablePermission('bookshelf-delete', $shelf);
197
198         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
199         return view('shelves.delete', ['shelf' => $shelf]);
200     }
201
202     /**
203      * Remove the specified bookshelf from storage.
204      * @param string $slug
205      * @return Response
206      * @throws \BookStack\Exceptions\NotFoundException
207      * @throws \Throwable
208      */
209     public function destroy(string $slug)
210     {
211         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
212         $this->checkOwnablePermission('bookshelf-delete', $shelf);
213         Activity::addMessage('bookshelf_delete', 0, $shelf->name);
214
215         if ($shelf->cover) {
216             $this->imageRepo->destroyImage($shelf->cover);
217         }
218         $this->entityRepo->destroyBookshelf($shelf);
219
220         return redirect('/shelves');
221     }
222
223     /**
224      * Show the permissions view.
225      * @param string $slug
226      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
227      * @throws \BookStack\Exceptions\NotFoundException
228      */
229     public function showPermissions(string $slug)
230     {
231         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
232         $this->checkOwnablePermission('restrictions-manage', $shelf);
233
234         $roles = $this->userRepo->getRestrictableRoles();
235         return view('shelves.permissions', [
236             'shelf' => $shelf,
237             'roles' => $roles
238         ]);
239     }
240
241     /**
242      * Set the permissions for this bookshelf.
243      * @param string $slug
244      * @param Request $request
245      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
246      * @throws \BookStack\Exceptions\NotFoundException
247      * @throws \Throwable
248      */
249     public function permissions(string $slug, Request $request)
250     {
251         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
252         $this->checkOwnablePermission('restrictions-manage', $shelf);
253
254         $this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
255         session()->flash('success', trans('entities.shelves_permissions_updated'));
256         return redirect($shelf->getUrl());
257     }
258
259     /**
260      * Copy the permissions of a bookshelf to the child books.
261      * @param string $slug
262      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
263      * @throws \BookStack\Exceptions\NotFoundException
264      */
265     public function copyPermissions(string $slug)
266     {
267         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
268         $this->checkOwnablePermission('restrictions-manage', $shelf);
269
270         $updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
271         session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
272         return redirect($shelf->getUrl());
273     }
274
275     /**
276      * Common actions to run on bookshelf update.
277      * @param Bookshelf $shelf
278      * @param Request $request
279      * @throws \BookStack\Exceptions\ImageUploadException
280      */
281     protected function shelfUpdateActions(Bookshelf $shelf, Request $request)
282     {
283         // Update the books that the shelf references
284         $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
285
286         // Update the cover image if in request
287         if ($request->has('image') && userCan('image-create-all')) {
288             $image = $this->imageRepo->saveNew($request->file('image'), 'cover', $shelf->id);
289             $shelf->image_id = $image->id;
290             $shelf->save();
291         }
292     }
293 }