]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Organised activity types and moved most to repos
[bookstack] / app / Http / Controllers / BookshelfController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Managers\EntityContext;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Uploads\ImageRepo;
10 use Exception;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
13 use Views;
14
15 class BookshelfController extends Controller
16 {
17
18     protected $bookshelfRepo;
19     protected $entityContextManager;
20     protected $imageRepo;
21
22     /**
23      * BookController constructor.
24      */
25     public function __construct(BookshelfRepo $bookshelfRepo, EntityContext $entityContextManager, ImageRepo $imageRepo)
26     {
27         $this->bookshelfRepo = $bookshelfRepo;
28         $this->entityContextManager = $entityContextManager;
29         $this->imageRepo = $imageRepo;
30         parent::__construct();
31     }
32
33     /**
34      * Display a listing of the book.
35      */
36     public function index()
37     {
38         $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
39         $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
40         $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
41         $sortOptions = [
42             'name' => trans('common.sort_name'),
43             'created_at' => trans('common.sort_created_at'),
44             'updated_at' => trans('common.sort_updated_at'),
45         ];
46
47         $shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
48         $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
49         $popular = $this->bookshelfRepo->getPopular(4);
50         $new = $this->bookshelfRepo->getRecentlyCreated(4);
51
52         $this->entityContextManager->clearShelfContext();
53         $this->setPageTitle(trans('entities.shelves'));
54         return view('shelves.index', [
55             'shelves' => $shelves,
56             'recents' => $recents,
57             'popular' => $popular,
58             'new' => $new,
59             'view' => $view,
60             'sort' => $sort,
61             'order' => $order,
62             'sortOptions' => $sortOptions,
63         ]);
64     }
65
66     /**
67      * Show the form for creating a new bookshelf.
68      */
69     public function create()
70     {
71         $this->checkPermission('bookshelf-create-all');
72         $books = Book::hasPermission('update')->get();
73         $this->setPageTitle(trans('entities.shelves_create'));
74         return view('shelves.create', ['books' => $books]);
75     }
76
77     /**
78      * Store a newly created bookshelf in storage.
79      * @throws ValidationException
80      * @throws ImageUploadException
81      */
82     public function store(Request $request)
83     {
84         $this->checkPermission('bookshelf-create-all');
85         $this->validate($request, [
86             'name' => 'required|string|max:255',
87             'description' => 'string|max:1000',
88             'image' => 'nullable|' . $this->getImageValidationRules(),
89         ]);
90
91         $bookIds = explode(',', $request->get('books', ''));
92         $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
93         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
94
95         return redirect($shelf->getUrl());
96     }
97
98     /**
99      * Display the bookshelf of the given slug.
100      * @throws NotFoundException
101      */
102     public function show(string $slug)
103     {
104         $shelf = $this->bookshelfRepo->getBySlug($slug);
105         $this->checkOwnablePermission('book-view', $shelf);
106
107         Views::add($shelf);
108         $this->entityContextManager->setShelfContext($shelf->id);
109         $view = setting()->getForCurrentUser('bookshelf_view_type', config('app.views.books'));
110
111         $this->setPageTitle($shelf->getShortName());
112         return view('shelves.show', [
113             'shelf' => $shelf,
114             'view' => $view,
115             'activity' => Activity::entityActivity($shelf, 20, 1)
116         ]);
117     }
118
119     /**
120      * Show the form for editing the specified bookshelf.
121      */
122     public function edit(string $slug)
123     {
124         $shelf = $this->bookshelfRepo->getBySlug($slug);
125         $this->checkOwnablePermission('bookshelf-update', $shelf);
126
127         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
128         $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
129
130         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
131         return view('shelves.edit', [
132             'shelf' => $shelf,
133             'books' => $books,
134         ]);
135     }
136
137     /**
138      * Update the specified bookshelf in storage.
139      * @throws ValidationException
140      * @throws ImageUploadException
141      * @throws NotFoundException
142      */
143     public function update(Request $request, string $slug)
144     {
145         $shelf = $this->bookshelfRepo->getBySlug($slug);
146         $this->checkOwnablePermission('bookshelf-update', $shelf);
147         $this->validate($request, [
148             'name' => 'required|string|max:255',
149             'description' => 'string|max:1000',
150             'image' => 'nullable|' . $this->getImageValidationRules(),
151         ]);
152
153
154         $bookIds = explode(',', $request->get('books', ''));
155         $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
156         $resetCover = $request->has('image_reset');
157         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
158
159         return redirect($shelf->getUrl());
160     }
161
162     /**
163      * Shows the page to confirm deletion
164      */
165     public function showDelete(string $slug)
166     {
167         $shelf = $this->bookshelfRepo->getBySlug($slug);
168         $this->checkOwnablePermission('bookshelf-delete', $shelf);
169
170         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
171         return view('shelves.delete', ['shelf' => $shelf]);
172     }
173
174     /**
175      * Remove the specified bookshelf from storage.
176      * @throws Exception
177      */
178     public function destroy(string $slug)
179     {
180         $shelf = $this->bookshelfRepo->getBySlug($slug);
181         $this->checkOwnablePermission('bookshelf-delete', $shelf);
182
183         $this->bookshelfRepo->destroy($shelf);
184
185         return redirect('/shelves');
186     }
187
188     /**
189      * Show the permissions view.
190      */
191     public function showPermissions(string $slug)
192     {
193         $shelf = $this->bookshelfRepo->getBySlug($slug);
194         $this->checkOwnablePermission('restrictions-manage', $shelf);
195
196         return view('shelves.permissions', [
197             'shelf' => $shelf,
198         ]);
199     }
200
201     /**
202      * Set the permissions for this bookshelf.
203      */
204     public function permissions(Request $request, string $slug)
205     {
206         $shelf = $this->bookshelfRepo->getBySlug($slug);
207         $this->checkOwnablePermission('restrictions-manage', $shelf);
208
209         $restricted = $request->get('restricted') === 'true';
210         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
211         $this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
212
213         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
214         return redirect($shelf->getUrl());
215     }
216
217     /**
218      * Copy the permissions of a bookshelf to the child books.
219      */
220     public function copyPermissions(string $slug)
221     {
222         $shelf = $this->bookshelfRepo->getBySlug($slug);
223         $this->checkOwnablePermission('restrictions-manage', $shelf);
224
225         $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
226         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
227         return redirect($shelf->getUrl());
228     }
229 }