]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Updated shelf sort to allow default sort, added testing
[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', 'default');
105         $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
106
107         $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
108             ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
109             ->values()
110             ->all();
111
112         Views::add($shelf);
113         $this->entityContextManager->setShelfContext($shelf->id);
114         $view = setting()->getForCurrentUser('bookshelf_view_type');
115
116         $this->setPageTitle($shelf->getShortName());
117         return view('shelves.show', [
118             'shelf' => $shelf,
119             'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
120             'view' => $view,
121             'activity' => Activity::entityActivity($shelf, 20, 1),
122             'order' => $order,
123             'sort' => $sort
124         ]);
125     }
126
127     /**
128      * Show the form for editing the specified bookshelf.
129      */
130     public function edit(string $slug)
131     {
132         $shelf = $this->bookshelfRepo->getBySlug($slug);
133         $this->checkOwnablePermission('bookshelf-update', $shelf);
134
135         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
136         $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
137
138         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
139         return view('shelves.edit', [
140             'shelf' => $shelf,
141             'books' => $books,
142         ]);
143     }
144
145     /**
146      * Update the specified bookshelf in storage.
147      * @throws ValidationException
148      * @throws ImageUploadException
149      * @throws NotFoundException
150      */
151     public function update(Request $request, string $slug)
152     {
153         $shelf = $this->bookshelfRepo->getBySlug($slug);
154         $this->checkOwnablePermission('bookshelf-update', $shelf);
155         $this->validate($request, [
156             'name' => 'required|string|max:255',
157             'description' => 'string|max:1000',
158             'image' => 'nullable|' . $this->getImageValidationRules(),
159         ]);
160
161
162         $bookIds = explode(',', $request->get('books', ''));
163         $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
164         $resetCover = $request->has('image_reset');
165         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
166
167         return redirect($shelf->getUrl());
168     }
169
170     /**
171      * Shows the page to confirm deletion
172      */
173     public function showDelete(string $slug)
174     {
175         $shelf = $this->bookshelfRepo->getBySlug($slug);
176         $this->checkOwnablePermission('bookshelf-delete', $shelf);
177
178         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
179         return view('shelves.delete', ['shelf' => $shelf]);
180     }
181
182     /**
183      * Remove the specified bookshelf from storage.
184      * @throws Exception
185      */
186     public function destroy(string $slug)
187     {
188         $shelf = $this->bookshelfRepo->getBySlug($slug);
189         $this->checkOwnablePermission('bookshelf-delete', $shelf);
190
191         $this->bookshelfRepo->destroy($shelf);
192
193         return redirect('/shelves');
194     }
195
196     /**
197      * Show the permissions view.
198      */
199     public function showPermissions(string $slug)
200     {
201         $shelf = $this->bookshelfRepo->getBySlug($slug);
202         $this->checkOwnablePermission('restrictions-manage', $shelf);
203
204         return view('shelves.permissions', [
205             'shelf' => $shelf,
206         ]);
207     }
208
209     /**
210      * Set the permissions for this bookshelf.
211      */
212     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
213     {
214         $shelf = $this->bookshelfRepo->getBySlug($slug);
215         $this->checkOwnablePermission('restrictions-manage', $shelf);
216
217         $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
218
219         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
220         return redirect($shelf->getUrl());
221     }
222
223     /**
224      * Copy the permissions of a bookshelf to the child books.
225      */
226     public function copyPermissions(string $slug)
227     {
228         $shelf = $this->bookshelfRepo->getBySlug($slug);
229         $this->checkOwnablePermission('restrictions-manage', $shelf);
230
231         $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
232         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
233         return redirect($shelf->getUrl());
234     }
235 }