]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Merge branch 'feature/sort-shelf-books' of git://github.com/guillaumehanotel/BookStac...
[bookstack] / app / Http / Controllers / BookshelfController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Tools\PermissionsUpdater;
6 use BookStack\Entities\Tools\ShelfContext;
7 use BookStack\Entities\Repos\BookshelfRepo;
8 use BookStack\Exceptions\ImageUploadException;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Uploads\ImageRepo;
11 use Exception;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
14 use Views;
15
16 class BookshelfController extends Controller
17 {
18
19     protected $bookshelfRepo;
20     protected $entityContextManager;
21     protected $imageRepo;
22
23     public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
24     {
25         $this->bookshelfRepo = $bookshelfRepo;
26         $this->entityContextManager = $entityContextManager;
27         $this->imageRepo = $imageRepo;
28     }
29
30     /**
31      * Display a listing of the book.
32      */
33     public function index()
34     {
35         $view = setting()->getForCurrentUser('bookshelves_view_type');
36         $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
37         $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
38         $sortOptions = [
39             'name' => trans('common.sort_name'),
40             'created_at' => trans('common.sort_created_at'),
41             'updated_at' => trans('common.sort_updated_at'),
42         ];
43
44         $shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
45         $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
46         $popular = $this->bookshelfRepo->getPopular(4);
47         $new = $this->bookshelfRepo->getRecentlyCreated(4);
48
49         $this->entityContextManager->clearShelfContext();
50         $this->setPageTitle(trans('entities.shelves'));
51         return view('shelves.index', [
52             'shelves' => $shelves,
53             'recents' => $recents,
54             'popular' => $popular,
55             'new' => $new,
56             'view' => $view,
57             'sort' => $sort,
58             'order' => $order,
59             'sortOptions' => $sortOptions,
60         ]);
61     }
62
63     /**
64      * Show the form for creating a new bookshelf.
65      */
66     public function create()
67     {
68         $this->checkPermission('bookshelf-create-all');
69         $books = Book::hasPermission('update')->get();
70         $this->setPageTitle(trans('entities.shelves_create'));
71         return view('shelves.create', ['books' => $books]);
72     }
73
74     /**
75      * Store a newly created bookshelf in storage.
76      * @throws ValidationException
77      * @throws ImageUploadException
78      */
79     public function store(Request $request)
80     {
81         $this->checkPermission('bookshelf-create-all');
82         $this->validate($request, [
83             'name' => 'required|string|max:255',
84             'description' => 'string|max:1000',
85             'image' => 'nullable|' . $this->getImageValidationRules(),
86         ]);
87
88         $bookIds = explode(',', $request->get('books', ''));
89         $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
90         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
91
92         return redirect($shelf->getUrl());
93     }
94
95     /**
96      * Display the bookshelf of the given slug.
97      * @throws NotFoundException
98      */
99     public function show(string $slug)
100     {
101         $shelf = $this->bookshelfRepo->getBySlug($slug);
102         $this->checkOwnablePermission('book-view', $shelf);
103
104         $sort = setting()->getForCurrentUser('shelf_books_sort', 'name');
105         $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
106
107         $visibleShelfBooks = $shelf->visibleBooks()->get();
108         $sortedVisibleShelfBooks = $visibleShelfBooks
109             ->sortBy($sort, SORT_REGULAR, $order === 'desc')
110             ->values()
111             ->all();
112
113         Views::add($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 }