]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Merge branch 'patch-1' of git://github.com/XVilka/BookStack into XVilka-patch-1
[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\EntityContextManager;
7 use BookStack\Entities\Repos\EntityRepo;
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 $entityContextManager;
18
19     /**
20      * BookController constructor.
21      * @param EntityRepo $entityRepo
22      * @param UserRepo $userRepo
23      * @param EntityContextManager $entityContextManager
24      */
25     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, EntityContextManager $entityContextManager)
26     {
27         $this->entityRepo = $entityRepo;
28         $this->userRepo = $userRepo;
29         $this->entityContextManager = $entityContextManager;
30         parent::__construct();
31     }
32
33     /**
34      * Display a listing of the book.
35      * @return Response
36      */
37     public function index()
38     {
39         $view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
40         $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
41         $order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc');
42         $sortOptions = [
43             'name' => trans('common.sort_name'),
44             'created_at' => trans('common.sort_created_at'),
45             'updated_at' => trans('common.sort_updated_at'),
46         ];
47
48         $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order);
49         foreach ($shelves as $shelf) {
50             $shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
51         }
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         $this->entityContextManager->clearShelfContext();
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         /** @var Bookshelf $bookshelf */
113         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
114         $this->checkOwnablePermission('book-view', $bookshelf);
115
116         $books = $this->entityRepo->getBookshelfChildren($bookshelf);
117         Views::add($bookshelf);
118         $this->entityContextManager->setShelfContext($bookshelf->id);
119
120         $this->setPageTitle($bookshelf->getShortName());
121         return view('shelves.show', [
122             'shelf' => $bookshelf,
123             'books' => $books,
124             'activity' => Activity::entityActivity($bookshelf, 20, 1)
125         ]);
126     }
127
128     /**
129      * Show the form for editing the specified bookshelf.
130      * @param $slug
131      * @return Response
132      * @throws \BookStack\Exceptions\NotFoundException
133      */
134     public function edit(string $slug)
135     {
136         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
137         $this->checkOwnablePermission('bookshelf-update', $bookshelf);
138
139         $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
140         $shelfBookIds = $shelfBooks->pluck('id');
141         $books = $this->entityRepo->getAll('book', false, 'update');
142         $books = $books->filter(function ($book) use ($shelfBookIds) {
143              return !$shelfBookIds->contains($book->id);
144         });
145
146         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
147         return view('shelves.edit', [
148             'shelf' => $bookshelf,
149             'books' => $books,
150             'shelfBooks' => $shelfBooks,
151         ]);
152     }
153
154
155     /**
156      * Update the specified bookshelf in storage.
157      * @param  Request $request
158      * @param string $slug
159      * @return Response
160      * @throws \BookStack\Exceptions\NotFoundException
161      */
162     public function update(Request $request, string $slug)
163     {
164         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
165         $this->checkOwnablePermission('bookshelf-update', $shelf);
166         $this->validate($request, [
167             'name' => 'required|string|max:255',
168             'description' => 'string|max:1000',
169         ]);
170
171          $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
172          $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
173          Activity::add($shelf, 'bookshelf_update');
174
175          return redirect($shelf->getUrl());
176     }
177
178
179     /**
180      * Shows the page to confirm deletion
181      * @param $slug
182      * @return \Illuminate\View\View
183      * @throws \BookStack\Exceptions\NotFoundException
184      */
185     public function showDelete(string $slug)
186     {
187         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
188         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
189
190         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
191         return view('shelves.delete', ['shelf' => $bookshelf]);
192     }
193
194     /**
195      * Remove the specified bookshelf from storage.
196      * @param string $slug
197      * @return Response
198      * @throws \BookStack\Exceptions\NotFoundException
199      * @throws \Throwable
200      */
201     public function destroy(string $slug)
202     {
203         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
204         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
205         Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
206         $this->entityRepo->destroyBookshelf($bookshelf);
207         return redirect('/shelves');
208     }
209
210     /**
211      * Show the permissions view.
212      * @param string $slug
213      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
214      * @throws \BookStack\Exceptions\NotFoundException
215      */
216     public function showPermissions(string $slug)
217     {
218         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
219         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
220
221         $roles = $this->userRepo->getRestrictableRoles();
222         return view('shelves.permissions', [
223             'shelf' => $bookshelf,
224             'roles' => $roles
225         ]);
226     }
227
228     /**
229      * Set the permissions for this bookshelf.
230      * @param string $slug
231      * @param Request $request
232      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
233      * @throws \BookStack\Exceptions\NotFoundException
234      * @throws \Throwable
235      */
236     public function permissions(string $slug, Request $request)
237     {
238         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
239         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
240
241         $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
242         session()->flash('success', trans('entities.shelves_permissions_updated'));
243         return redirect($bookshelf->getUrl());
244     }
245
246     /**
247      * Copy the permissions of a bookshelf to the child books.
248      * @param string $slug
249      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
250      * @throws \BookStack\Exceptions\NotFoundException
251      */
252     public function copyPermissions(string $slug)
253     {
254         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
255         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
256
257         $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
258         session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
259         return redirect($bookshelf->getUrl());
260     }
261 }