]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Merge pull request #1698 from 3mmarg97/master
[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' => $this->getImageValidationRules(),
89         ]);
90
91         $bookIds = explode(',', $request->get('books', ''));
92         $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
93         $this->bookshelfRepo->updateCoverImage($shelf);
94
95         Activity::add($shelf, 'bookshelf_create');
96         return redirect($shelf->getUrl());
97     }
98
99     /**
100      * Display the bookshelf of the given slug.
101      * @throws NotFoundException
102      */
103     public function show(string $slug)
104     {
105         $shelf = $this->bookshelfRepo->getBySlug($slug);
106         $this->checkOwnablePermission('book-view', $shelf);
107
108         Views::add($shelf);
109         $this->entityContextManager->setShelfContext($shelf->id);
110
111         $this->setPageTitle($shelf->getShortName());
112         return view('shelves.show', [
113             'shelf' => $shelf,
114             'activity' => Activity::entityActivity($shelf, 20, 1)
115         ]);
116     }
117
118     /**
119      * Show the form for editing the specified bookshelf.
120      */
121     public function edit(string $slug)
122     {
123         $shelf = $this->bookshelfRepo->getBySlug($slug);
124         $this->checkOwnablePermission('bookshelf-update', $shelf);
125
126         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
127         $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
128
129         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
130         return view('shelves.edit', [
131             'shelf' => $shelf,
132             'books' => $books,
133         ]);
134     }
135
136     /**
137      * Update the specified bookshelf in storage.
138      * @throws ValidationException
139      * @throws ImageUploadException
140      * @throws NotFoundException
141      */
142     public function update(Request $request, string $slug)
143     {
144         $shelf = $this->bookshelfRepo->getBySlug($slug);
145         $this->checkOwnablePermission('bookshelf-update', $shelf);
146         $this->validate($request, [
147             'name' => 'required|string|max:255',
148             'description' => 'string|max:1000',
149             'image' => $this->imageRepo->getImageValidationRules(),
150         ]);
151
152
153         $bookIds = explode(',', $request->get('books', ''));
154         $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
155         $resetCover = $request->has('image_reset');
156         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
157         Activity::add($shelf, 'bookshelf_update');
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         Activity::addMessage('bookshelf_delete', $shelf->name);
184         $this->bookshelfRepo->destroy($shelf);
185
186         return redirect('/shelves');
187     }
188
189     /**
190      * Show the permissions view.
191      */
192     public function showPermissions(string $slug)
193     {
194         $shelf = $this->bookshelfRepo->getBySlug($slug);
195         $this->checkOwnablePermission('restrictions-manage', $shelf);
196
197         return view('shelves.permissions', [
198             'shelf' => $shelf,
199         ]);
200     }
201
202     /**
203      * Set the permissions for this bookshelf.
204      */
205     public function permissions(Request $request, string $slug)
206     {
207         $shelf = $this->bookshelfRepo->getBySlug($slug);
208         $this->checkOwnablePermission('restrictions-manage', $shelf);
209
210         $restricted = $request->get('restricted') === 'true';
211         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
212         $this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
213
214         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
215         return redirect($shelf->getUrl());
216     }
217
218     /**
219      * Copy the permissions of a bookshelf to the child books.
220      */
221     public function copyPermissions(string $slug)
222     {
223         $shelf = $this->bookshelfRepo->getBySlug($slug);
224         $this->checkOwnablePermission('restrictions-manage', $shelf);
225
226         $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
227         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
228         return redirect($shelf->getUrl());
229     }
230 }