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