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