]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Made shelf listing more unique & efficient
[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, function($query) {
51             $query->with(['books']);
52         });
53         $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
54         $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
55         $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
56
57
58         $this->setPageTitle(trans('entities.shelves'));
59         return view('shelves.index', [
60             'shelves' => $shelves,
61             'recents' => $recents,
62             'popular' => $popular,
63             'new' => $new,
64             'view' => $view,
65             'sort' => $sort,
66             'order' => $order,
67             'sortOptions' => $sortOptions,
68         ]);
69     }
70
71     /**
72      * Show the form for creating a new bookshelf.
73      * @return Response
74      */
75     public function create()
76     {
77         $this->checkPermission('bookshelf-create-all');
78         $books = $this->entityRepo->getAll('book', false, 'update');
79         $this->setPageTitle(trans('entities.shelves_create'));
80         return view('shelves.create', ['books' => $books]);
81     }
82
83     /**
84      * Store a newly created bookshelf in storage.
85      * @param  Request $request
86      * @return Response
87      */
88     public function store(Request $request)
89     {
90         $this->checkPermission('bookshelf-create-all');
91         $this->validate($request, [
92             'name' => 'required|string|max:255',
93             'description' => 'string|max:1000',
94         ]);
95
96         $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
97         $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
98         Activity::add($bookshelf, 'bookshelf_create');
99
100         return redirect($bookshelf->getUrl());
101     }
102
103
104     /**
105      * Display the specified bookshelf.
106      * @param String $slug
107      * @return Response
108      * @throws \BookStack\Exceptions\NotFoundException
109      */
110     public function show(string $slug)
111     {
112         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
113         $this->checkOwnablePermission('book-view', $bookshelf);
114
115         $books = $this->entityRepo->getBookshelfChildren($bookshelf);
116         Views::add($bookshelf);
117
118         $this->setPageTitle($bookshelf->getShortName());
119         return view('shelves.show', [
120             'shelf' => $bookshelf,
121             'books' => $books,
122             'activity' => Activity::entityActivity($bookshelf, 20, 1)
123         ]);
124     }
125
126     /**
127      * Show the form for editing the specified bookshelf.
128      * @param $slug
129      * @return Response
130      * @throws \BookStack\Exceptions\NotFoundException
131      */
132     public function edit(string $slug)
133     {
134         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
135         $this->checkOwnablePermission('bookshelf-update', $bookshelf);
136
137         $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
138         $shelfBookIds = $shelfBooks->pluck('id');
139         $books = $this->entityRepo->getAll('book', false, 'update');
140         $books = $books->filter(function ($book) use ($shelfBookIds) {
141              return !$shelfBookIds->contains($book->id);
142         });
143
144         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
145         return view('shelves.edit', [
146             'shelf' => $bookshelf,
147             'books' => $books,
148             'shelfBooks' => $shelfBooks,
149         ]);
150     }
151
152
153     /**
154      * Update the specified bookshelf in storage.
155      * @param  Request $request
156      * @param string $slug
157      * @return Response
158      * @throws \BookStack\Exceptions\NotFoundException
159      */
160     public function update(Request $request, string $slug)
161     {
162         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
163         $this->checkOwnablePermission('bookshelf-update', $shelf);
164         $this->validate($request, [
165             'name' => 'required|string|max:255',
166             'description' => 'string|max:1000',
167         ]);
168
169          $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
170          $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
171          Activity::add($shelf, 'bookshelf_update');
172
173          return redirect($shelf->getUrl());
174     }
175
176
177     /**
178      * Shows the page to confirm deletion
179      * @param $slug
180      * @return \Illuminate\View\View
181      * @throws \BookStack\Exceptions\NotFoundException
182      */
183     public function showDelete(string $slug)
184     {
185         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
186         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
187
188         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
189         return view('shelves.delete', ['shelf' => $bookshelf]);
190     }
191
192     /**
193      * Remove the specified bookshelf from storage.
194      * @param string $slug
195      * @return Response
196      * @throws \BookStack\Exceptions\NotFoundException
197      * @throws \Throwable
198      */
199     public function destroy(string $slug)
200     {
201         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
202         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
203         Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
204         $this->entityRepo->destroyBookshelf($bookshelf);
205         return redirect('/shelves');
206     }
207
208     /**
209      * Show the permissions view.
210      * @param string $slug
211      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
212      * @throws \BookStack\Exceptions\NotFoundException
213      */
214     public function showPermissions(string $slug)
215     {
216         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
217         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
218
219         $roles = $this->userRepo->getRestrictableRoles();
220         return view('shelves.permissions', [
221             'shelf' => $bookshelf,
222             'roles' => $roles
223         ]);
224     }
225
226     /**
227      * Set the permissions for this bookshelf.
228      * @param string $slug
229      * @param Request $request
230      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
231      * @throws \BookStack\Exceptions\NotFoundException
232      * @throws \Throwable
233      */
234     public function permissions(string $slug, Request $request)
235     {
236         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
237         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
238
239         $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
240         session()->flash('success', trans('entities.shelves_permissions_updated'));
241         return redirect($bookshelf->getUrl());
242     }
243
244     /**
245      * Copy the permissions of a bookshelf to the child books.
246      * @param string $slug
247      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
248      * @throws \BookStack\Exceptions\NotFoundException
249      */
250     public function copyPermissions(string $slug)
251     {
252         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
253         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
254
255         $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
256         session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
257         return redirect($bookshelf->getUrl());
258     }
259 }