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