]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Merge branch 'master' into 2019-design
[bookstack] / app / Http / Controllers / BookshelfController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Entities\ExportService;
8 use Illuminate\Http\Request;
9 use Illuminate\Http\Response;
10 use Views;
11
12 class BookshelfController extends Controller
13 {
14
15     protected $entityRepo;
16     protected $userRepo;
17     protected $exportService;
18
19     /**
20      * BookController constructor.
21      * @param \BookStack\Entities\Repos\EntityRepo $entityRepo
22      * @param UserRepo $userRepo
23      * @param \BookStack\Entities\ExportService $exportService
24      */
25     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
26     {
27         $this->entityRepo = $entityRepo;
28         $this->userRepo = $userRepo;
29         $this->exportService = $exportService;
30         parent::__construct();
31     }
32
33     /**
34      * Display a listing of the book.
35      * @return Response
36      */
37     public function index()
38     {
39
40         $view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
41
42         $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
43         $order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc');
44         $sortOptions = [
45             'name' => trans('common.sort_name'),
46             'created_at' => trans('common.sort_created_at'),
47             'updated_at' => trans('common.sort_updated_at'),
48         ];
49
50         $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order);
51         $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
52         $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
53         $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
54
55
56         $this->setPageTitle(trans('entities.shelves'));
57         return view('shelves.index', [
58             'shelves' => $shelves,
59             'recents' => $recents,
60             'popular' => $popular,
61             'new' => $new,
62             'view' => $view,
63             'sort' => $sort,
64             'order' => $order,
65             'sortOptions' => $sortOptions,
66         ]);
67     }
68
69     /**
70      * Show the form for creating a new bookshelf.
71      * @return Response
72      */
73     public function create()
74     {
75         $this->checkPermission('bookshelf-create-all');
76         $books = $this->entityRepo->getAll('book', false, 'update');
77         $this->setPageTitle(trans('entities.shelves_create'));
78         return view('shelves.create', ['books' => $books]);
79     }
80
81     /**
82      * Store a newly created bookshelf in storage.
83      * @param  Request $request
84      * @return Response
85      */
86     public function store(Request $request)
87     {
88         $this->checkPermission('bookshelf-create-all');
89         $this->validate($request, [
90             'name' => 'required|string|max:255',
91             'description' => 'string|max:1000',
92         ]);
93
94         $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
95         $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
96         Activity::add($bookshelf, 'bookshelf_create');
97
98         return redirect($bookshelf->getUrl());
99     }
100
101
102     /**
103      * Display the specified bookshelf.
104      * @param String $slug
105      * @return Response
106      * @throws \BookStack\Exceptions\NotFoundException
107      */
108     public function show(string $slug)
109     {
110         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
111         $this->checkOwnablePermission('book-view', $bookshelf);
112
113         $books = $this->entityRepo->getBookshelfChildren($bookshelf);
114         Views::add($bookshelf);
115
116         $this->setPageTitle($bookshelf->getShortName());
117         return view('shelves.show', [
118             'shelf' => $bookshelf,
119             'books' => $books,
120             'activity' => Activity::entityActivity($bookshelf, 20, 1)
121         ]);
122     }
123
124     /**
125      * Show the form for editing the specified bookshelf.
126      * @param $slug
127      * @return Response
128      * @throws \BookStack\Exceptions\NotFoundException
129      */
130     public function edit(string $slug)
131     {
132         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
133         $this->checkOwnablePermission('bookshelf-update', $bookshelf);
134
135         $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
136         $shelfBookIds = $shelfBooks->pluck('id');
137         $books = $this->entityRepo->getAll('book', false, 'update');
138         $books = $books->filter(function ($book) use ($shelfBookIds) {
139              return !$shelfBookIds->contains($book->id);
140         });
141
142         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
143         return view('shelves.edit', [
144             'shelf' => $bookshelf,
145             'books' => $books,
146             'shelfBooks' => $shelfBooks,
147         ]);
148     }
149
150
151     /**
152      * Update the specified bookshelf in storage.
153      * @param  Request $request
154      * @param string $slug
155      * @return Response
156      * @throws \BookStack\Exceptions\NotFoundException
157      */
158     public function update(Request $request, string $slug)
159     {
160         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
161         $this->checkOwnablePermission('bookshelf-update', $shelf);
162         $this->validate($request, [
163             'name' => 'required|string|max:255',
164             'description' => 'string|max:1000',
165         ]);
166
167          $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
168          $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
169          Activity::add($shelf, 'bookshelf_update');
170
171          return redirect($shelf->getUrl());
172     }
173
174
175     /**
176      * Shows the page to confirm deletion
177      * @param $slug
178      * @return \Illuminate\View\View
179      * @throws \BookStack\Exceptions\NotFoundException
180      */
181     public function showDelete(string $slug)
182     {
183         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
184         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
185
186         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
187         return view('shelves.delete', ['shelf' => $bookshelf]);
188     }
189
190     /**
191      * Remove the specified bookshelf from storage.
192      * @param string $slug
193      * @return Response
194      * @throws \BookStack\Exceptions\NotFoundException
195      * @throws \Throwable
196      */
197     public function destroy(string $slug)
198     {
199         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
200         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
201         Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
202         $this->entityRepo->destroyBookshelf($bookshelf);
203         return redirect('/shelves');
204     }
205
206     /**
207      * Show the permissions view.
208      * @param string $slug
209      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
210      * @throws \BookStack\Exceptions\NotFoundException
211      */
212     public function showPermissions(string $slug)
213     {
214         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
215         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
216
217         $roles = $this->userRepo->getRestrictableRoles();
218         return view('shelves.permissions', [
219             'shelf' => $bookshelf,
220             'roles' => $roles
221         ]);
222     }
223
224     /**
225      * Set the permissions for this bookshelf.
226      * @param string $slug
227      * @param Request $request
228      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
229      * @throws \BookStack\Exceptions\NotFoundException
230      * @throws \Throwable
231      */
232     public function permissions(string $slug, Request $request)
233     {
234         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
235         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
236
237         $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
238         session()->flash('success', trans('entities.shelves_permissions_updated'));
239         return redirect($bookshelf->getUrl());
240     }
241
242     /**
243      * Copy the permissions of a bookshelf to the child books.
244      * @param string $slug
245      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
246      * @throws \BookStack\Exceptions\NotFoundException
247      */
248     public function copyPermissions(string $slug)
249     {
250         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
251         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
252
253         $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
254         session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
255         return redirect($bookshelf->getUrl());
256     }
257 }