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