]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Merge branch 'feature_change_view_in_shelves_show' of git://github.com/philjak/BookSt...
[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         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         $view = setting()->getForCurrentUser('books_view_type', config('app.views.books'));
107         $this->checkOwnablePermission('book-view', $shelf);
108
109         Views::add($shelf);
110         $this->entityContextManager->setShelfContext($shelf->id);
111
112         $this->setPageTitle($shelf->getShortName());
113         return view('shelves.show', [
114             'shelf' => $shelf,
115             'view' => $view,
116             'activity' => Activity::entityActivity($shelf, 20, 1)
117         ]);
118     }
119
120     /**
121      * Show the form for editing the specified bookshelf.
122      */
123     public function edit(string $slug)
124     {
125         $shelf = $this->bookshelfRepo->getBySlug($slug);
126         $this->checkOwnablePermission('bookshelf-update', $shelf);
127
128         $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
129         $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
130
131         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
132         return view('shelves.edit', [
133             'shelf' => $shelf,
134             'books' => $books,
135         ]);
136     }
137
138     /**
139      * Update the specified bookshelf in storage.
140      * @throws ValidationException
141      * @throws ImageUploadException
142      * @throws NotFoundException
143      */
144     public function update(Request $request, string $slug)
145     {
146         $shelf = $this->bookshelfRepo->getBySlug($slug);
147         $this->checkOwnablePermission('bookshelf-update', $shelf);
148         $this->validate($request, [
149             'name' => 'required|string|max:255',
150             'description' => 'string|max:1000',
151             'image' => 'nullable|' . $this->getImageValidationRules(),
152         ]);
153
154
155         $bookIds = explode(',', $request->get('books', ''));
156         $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
157         $resetCover = $request->has('image_reset');
158         $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
159         Activity::add($shelf, 'bookshelf_update');
160
161         return redirect($shelf->getUrl());
162     }
163
164     /**
165      * Shows the page to confirm deletion
166      */
167     public function showDelete(string $slug)
168     {
169         $shelf = $this->bookshelfRepo->getBySlug($slug);
170         $this->checkOwnablePermission('bookshelf-delete', $shelf);
171
172         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
173         return view('shelves.delete', ['shelf' => $shelf]);
174     }
175
176     /**
177      * Remove the specified bookshelf from storage.
178      * @throws Exception
179      */
180     public function destroy(string $slug)
181     {
182         $shelf = $this->bookshelfRepo->getBySlug($slug);
183         $this->checkOwnablePermission('bookshelf-delete', $shelf);
184
185         Activity::addMessage('bookshelf_delete', $shelf->name);
186         $this->bookshelfRepo->destroy($shelf);
187
188         return redirect('/shelves');
189     }
190
191     /**
192      * Show the permissions view.
193      */
194     public function showPermissions(string $slug)
195     {
196         $shelf = $this->bookshelfRepo->getBySlug($slug);
197         $this->checkOwnablePermission('restrictions-manage', $shelf);
198
199         return view('shelves.permissions', [
200             'shelf' => $shelf,
201         ]);
202     }
203
204     /**
205      * Set the permissions for this bookshelf.
206      */
207     public function permissions(Request $request, string $slug)
208     {
209         $shelf = $this->bookshelfRepo->getBySlug($slug);
210         $this->checkOwnablePermission('restrictions-manage', $shelf);
211
212         $restricted = $request->get('restricted') === 'true';
213         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
214         $this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
215
216         $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
217         return redirect($shelf->getUrl());
218     }
219
220     /**
221      * Copy the permissions of a bookshelf to the child books.
222      */
223     public function copyPermissions(string $slug)
224     {
225         $shelf = $this->bookshelfRepo->getBySlug($slug);
226         $this->checkOwnablePermission('restrictions-manage', $shelf);
227
228         $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
229         $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
230         return redirect($shelf->getUrl());
231     }
232 }