]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Updated php dependancies for minor release
[bookstack] / app / Http / Controllers / BookshelfController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Actions\View;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Tools\PermissionsUpdater;
7 use BookStack\Entities\Tools\ShelfContext;
8 use BookStack\Entities\Repos\BookshelfRepo;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Exceptions\NotFoundException;
11 use BookStack\Uploads\ImageRepo;
12 use Exception;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\ValidationException;
15 use Views;
16
17 class BookshelfController extends Controller
18 {
19
20     protected $bookshelfRepo;
21     protected $entityContextManager;
22     protected $imageRepo;
23
24     public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
25     {
26         $this->bookshelfRepo = $bookshelfRepo;
27         $this->entityContextManager = $entityContextManager;
28         $this->imageRepo = $imageRepo;
29     }
30
31     /**
32      * Display a listing of the book.
33      */
34     public function index()
35     {
36         $view = setting()->getForCurrentUser('bookshelves_view_type');
37         $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
38         $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
39         $sortOptions = [
40             'name' => trans('common.sort_name'),
41             'created_at' => trans('common.sort_created_at'),
42             'updated_at' => trans('common.sort_updated_at'),
43         ];
44
45         $shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
46         $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
47         $popular = $this->bookshelfRepo->getPopular(4);
48         $new = $this->bookshelfRepo->getRecentlyCreated(4);
49
50         $this->entityContextManager->clearShelfContext();
51         $this->setPageTitle(trans('entities.shelves'));
52         return view('shelves.index', [
53             'shelves' => $shelves,
54             'recents' => $recents,
55             'popular' => $popular,
56             'new' => $new,
57             'view' => $view,
58             'sort' => $sort,
59             'order' => $order,
60             'sortOptions' => $sortOptions,
61         ]);
62     }
63
64     /**
65      * Show the form for creating a new bookshelf.
66      */
67     public function create()
68     {
69         $this->checkPermission('bookshelf-create-all');
70         $books = Book::hasPermission('update')->get();
71         $this->setPageTitle(trans('entities.shelves_create'));
72         return view('shelves.create', ['books' => $books]);
73     }
74
75     /**
76      * Store a newly created bookshelf in storage.
77      * @throws ValidationException
78      * @throws ImageUploadException
79      */
80     public function store(Request $request)
81     {
82         $this->checkPermission('bookshelf-create-all');
83         $this->validate($request, [
84             'name' => 'required|string|max:255',
85             'description' => 'string|max:1000',
86             'image' => 'nullable|' . $this->getImageValidationRules(),
87         ]);
88
89         $bookIds = explode(',', $request->get('books', ''));
90         $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
91         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
92
93         return redirect($shelf->getUrl());
94     }
95
96     /**
97      * Display the bookshelf of the given slug.
98      * @throws NotFoundException
99      */
100     public function show(string $slug)
101     {
102         $shelf = $this->bookshelfRepo->getBySlug($slug);
103         $this->checkOwnablePermission('book-view', $shelf);
104
105         $sort = setting()->getForCurrentUser('shelf_books_sort', 'default');
106         $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
107
108         $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
109             ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
110             ->values()
111             ->all();
112
113         View::incrementFor($shelf);
114         $this->entityContextManager->setShelfContext($shelf->id);
115         $view = setting()->getForCurrentUser('bookshelf_view_type');
116
117         $this->setPageTitle($shelf->getShortName());
118         return view('shelves.show', [
119             'shelf' => $shelf,
120             'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
121             'view' => $view,
122             'activity' => Activity::entityActivity($shelf, 20, 1),
123             'order' => $order,
124             'sort' => $sort
125         ]);
126     }
127
128     /**
129      * Show the form for editing the specified bookshelf.
130      */
131     public function edit(string $slug)
132     {
133         $shelf = $this->bookshelfRepo->getBySlug($slug);
134         $this->checkOwnablePermission('bookshelf-update', $shelf);
135
136         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
137         $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
138
139         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
140         return view('shelves.edit', [
141             'shelf' => $shelf,
142             'books' => $books,
143         ]);
144     }
145
146     /**
147      * Update the specified bookshelf in storage.
148      * @throws ValidationException
149      * @throws ImageUploadException
150      * @throws NotFoundException
151      */
152     public function update(Request $request, string $slug)
153     {
154         $shelf = $this->bookshelfRepo->getBySlug($slug);
155         $this->checkOwnablePermission('bookshelf-update', $shelf);
156         $this->validate($request, [
157             'name' => 'required|string|max:255',
158             'description' => 'string|max:1000',
159             'image' => 'nullable|' . $this->getImageValidationRules(),
160         ]);
161
162
163         $bookIds = explode(',', $request->get('books', ''));
164         $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
165         $resetCover = $request->has('image_reset');
166         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
167
168         return redirect($shelf->getUrl());
169     }
170
171     /**
172      * Shows the page to confirm deletion
173      */
174     public function showDelete(string $slug)
175     {
176         $shelf = $this->bookshelfRepo->getBySlug($slug);
177         $this->checkOwnablePermission('bookshelf-delete', $shelf);
178
179         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
180         return view('shelves.delete', ['shelf' => $shelf]);
181     }
182
183     /**
184      * Remove the specified bookshelf from storage.
185      * @throws Exception
186      */
187     public function destroy(string $slug)
188     {
189         $shelf = $this->bookshelfRepo->getBySlug($slug);
190         $this->checkOwnablePermission('bookshelf-delete', $shelf);
191
192         $this->bookshelfRepo->destroy($shelf);
193
194         return redirect('/shelves');
195     }
196
197     /**
198      * Show the permissions view.
199      */
200     public function showPermissions(string $slug)
201     {
202         $shelf = $this->bookshelfRepo->getBySlug($slug);
203         $this->checkOwnablePermission('restrictions-manage', $shelf);
204
205         return view('shelves.permissions', [
206             'shelf' => $shelf,
207         ]);
208     }
209
210     /**
211      * Set the permissions for this bookshelf.
212      */
213     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
214     {
215         $shelf = $this->bookshelfRepo->getBySlug($slug);
216         $this->checkOwnablePermission('restrictions-manage', $shelf);
217
218         $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
219
220         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
221         return redirect($shelf->getUrl());
222     }
223
224     /**
225      * Copy the permissions of a bookshelf to the child books.
226      */
227     public function copyPermissions(string $slug)
228     {
229         $shelf = $this->bookshelfRepo->getBySlug($slug);
230         $this->checkOwnablePermission('restrictions-manage', $shelf);
231
232         $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
233         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
234         return redirect($shelf->getUrl());
235     }
236 }