]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Fixed some mis-refactoring and split search service
[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\ShelfContext;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Uploads\ImageRepo;
10 use Exception;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
13 use Views;
14
15 class BookshelfController extends Controller
16 {
17
18     protected $bookshelfRepo;
19     protected $entityContextManager;
20     protected $imageRepo;
21
22     /**
23      * BookController constructor.
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', config('app.views.bookshelves', 'grid'));
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         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::hasPermission('update')->get();
72         $this->setPageTitle(trans('entities.shelves_create'));
73         return view('shelves.create', ['books' => $books]);
74     }
75
76     /**
77      * Store a newly created bookshelf in storage.
78      * @throws ValidationException
79      * @throws ImageUploadException
80      */
81     public function store(Request $request)
82     {
83         $this->checkPermission('bookshelf-create-all');
84         $this->validate($request, [
85             'name' => 'required|string|max:255',
86             'description' => 'string|max:1000',
87             'image' => 'nullable|' . $this->getImageValidationRules(),
88         ]);
89
90         $bookIds = explode(',', $request->get('books', ''));
91         $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
92         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
93
94         return redirect($shelf->getUrl());
95     }
96
97     /**
98      * Display the bookshelf of the given slug.
99      * @throws NotFoundException
100      */
101     public function show(string $slug)
102     {
103         $shelf = $this->bookshelfRepo->getBySlug($slug);
104         $this->checkOwnablePermission('book-view', $shelf);
105
106         Views::add($shelf);
107         $this->entityContextManager->setShelfContext($shelf->id);
108         $view = setting()->getForCurrentUser('bookshelf_view_type', config('app.views.books'));
109
110         $this->setPageTitle($shelf->getShortName());
111         return view('shelves.show', [
112             'shelf' => $shelf,
113             'view' => $view,
114             'activity' => Activity::entityActivity($shelf, 20, 1)
115         ]);
116     }
117
118     /**
119      * Show the form for editing the specified bookshelf.
120      */
121     public function edit(string $slug)
122     {
123         $shelf = $this->bookshelfRepo->getBySlug($slug);
124         $this->checkOwnablePermission('bookshelf-update', $shelf);
125
126         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
127         $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
128
129         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
130         return view('shelves.edit', [
131             'shelf' => $shelf,
132             'books' => $books,
133         ]);
134     }
135
136     /**
137      * Update the specified bookshelf in storage.
138      * @throws ValidationException
139      * @throws ImageUploadException
140      * @throws NotFoundException
141      */
142     public function update(Request $request, string $slug)
143     {
144         $shelf = $this->bookshelfRepo->getBySlug($slug);
145         $this->checkOwnablePermission('bookshelf-update', $shelf);
146         $this->validate($request, [
147             'name' => 'required|string|max:255',
148             'description' => 'string|max:1000',
149             'image' => 'nullable|' . $this->getImageValidationRules(),
150         ]);
151
152
153         $bookIds = explode(',', $request->get('books', ''));
154         $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
155         $resetCover = $request->has('image_reset');
156         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
157
158         return redirect($shelf->getUrl());
159     }
160
161     /**
162      * Shows the page to confirm deletion
163      */
164     public function showDelete(string $slug)
165     {
166         $shelf = $this->bookshelfRepo->getBySlug($slug);
167         $this->checkOwnablePermission('bookshelf-delete', $shelf);
168
169         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
170         return view('shelves.delete', ['shelf' => $shelf]);
171     }
172
173     /**
174      * Remove the specified bookshelf from storage.
175      * @throws Exception
176      */
177     public function destroy(string $slug)
178     {
179         $shelf = $this->bookshelfRepo->getBySlug($slug);
180         $this->checkOwnablePermission('bookshelf-delete', $shelf);
181
182         $this->bookshelfRepo->destroy($shelf);
183
184         return redirect('/shelves');
185     }
186
187     /**
188      * Show the permissions view.
189      */
190     public function showPermissions(string $slug)
191     {
192         $shelf = $this->bookshelfRepo->getBySlug($slug);
193         $this->checkOwnablePermission('restrictions-manage', $shelf);
194
195         return view('shelves.permissions', [
196             'shelf' => $shelf,
197         ]);
198     }
199
200     /**
201      * Set the permissions for this bookshelf.
202      */
203     public function permissions(Request $request, string $slug)
204     {
205         $shelf = $this->bookshelfRepo->getBySlug($slug);
206         $this->checkOwnablePermission('restrictions-manage', $shelf);
207
208         $restricted = $request->get('restricted') === 'true';
209         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
210         $this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
211
212         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
213         return redirect($shelf->getUrl());
214     }
215
216     /**
217      * Copy the permissions of a bookshelf to the child books.
218      */
219     public function copyPermissions(string $slug)
220     {
221         $shelf = $this->bookshelfRepo->getBySlug($slug);
222         $this->checkOwnablePermission('restrictions-manage', $shelf);
223
224         $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
225         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
226         return redirect($shelf->getUrl());
227     }
228 }