1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Tools\ShelfContext;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Uploads\ImageRepo;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
15 class BookshelfController extends Controller
18 protected $bookshelfRepo;
19 protected $entityContextManager;
23 * BookController constructor.
25 public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
27 $this->bookshelfRepo = $bookshelfRepo;
28 $this->entityContextManager = $entityContextManager;
29 $this->imageRepo = $imageRepo;
33 * Display a listing of the book.
35 public function index()
37 $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
38 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
39 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
41 'name' => trans('common.sort_name'),
42 'created_at' => trans('common.sort_created_at'),
43 'updated_at' => trans('common.sort_updated_at'),
46 $shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
47 $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
48 $popular = $this->bookshelfRepo->getPopular(4);
49 $new = $this->bookshelfRepo->getRecentlyCreated(4);
51 $this->entityContextManager->clearShelfContext();
52 $this->setPageTitle(trans('entities.shelves'));
53 return view('shelves.index', [
54 'shelves' => $shelves,
55 'recents' => $recents,
56 'popular' => $popular,
61 'sortOptions' => $sortOptions,
66 * Show the form for creating a new bookshelf.
68 public function create()
70 $this->checkPermission('bookshelf-create-all');
71 $books = Book::hasPermission('update')->get();
72 $this->setPageTitle(trans('entities.shelves_create'));
73 return view('shelves.create', ['books' => $books]);
77 * Store a newly created bookshelf in storage.
78 * @throws ValidationException
79 * @throws ImageUploadException
81 public function store(Request $request)
83 $this->checkPermission('bookshelf-create-all');
84 $this->validate($request, [
85 'name' => 'required|string|max:255',
86 'description' => 'string|max:1000',
87 'image' => 'nullable|' . $this->getImageValidationRules(),
90 $bookIds = explode(',', $request->get('books', ''));
91 $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
92 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
94 return redirect($shelf->getUrl());
98 * Display the bookshelf of the given slug.
99 * @throws NotFoundException
101 public function show(string $slug)
103 $shelf = $this->bookshelfRepo->getBySlug($slug);
104 $this->checkOwnablePermission('book-view', $shelf);
107 $this->entityContextManager->setShelfContext($shelf->id);
108 $view = setting()->getForCurrentUser('bookshelf_view_type', config('app.views.books'));
110 $this->setPageTitle($shelf->getShortName());
111 return view('shelves.show', [
114 'activity' => Activity::entityActivity($shelf, 20, 1)
119 * Show the form for editing the specified bookshelf.
121 public function edit(string $slug)
123 $shelf = $this->bookshelfRepo->getBySlug($slug);
124 $this->checkOwnablePermission('bookshelf-update', $shelf);
126 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
127 $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
129 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
130 return view('shelves.edit', [
137 * Update the specified bookshelf in storage.
138 * @throws ValidationException
139 * @throws ImageUploadException
140 * @throws NotFoundException
142 public function update(Request $request, string $slug)
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' => 'nullable|' . $this->getImageValidationRules(),
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);
158 return redirect($shelf->getUrl());
162 * Shows the page to confirm deletion
164 public function showDelete(string $slug)
166 $shelf = $this->bookshelfRepo->getBySlug($slug);
167 $this->checkOwnablePermission('bookshelf-delete', $shelf);
169 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
170 return view('shelves.delete', ['shelf' => $shelf]);
174 * Remove the specified bookshelf from storage.
177 public function destroy(string $slug)
179 $shelf = $this->bookshelfRepo->getBySlug($slug);
180 $this->checkOwnablePermission('bookshelf-delete', $shelf);
182 $this->bookshelfRepo->destroy($shelf);
184 return redirect('/shelves');
188 * Show the permissions view.
190 public function showPermissions(string $slug)
192 $shelf = $this->bookshelfRepo->getBySlug($slug);
193 $this->checkOwnablePermission('restrictions-manage', $shelf);
195 return view('shelves.permissions', [
201 * Set the permissions for this bookshelf.
203 public function permissions(Request $request, string $slug)
205 $shelf = $this->bookshelfRepo->getBySlug($slug);
206 $this->checkOwnablePermission('restrictions-manage', $shelf);
208 $restricted = $request->get('restricted') === 'true';
209 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
210 $this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
212 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
213 return redirect($shelf->getUrl());
217 * Copy the permissions of a bookshelf to the child books.
219 public function copyPermissions(string $slug)
221 $shelf = $this->bookshelfRepo->getBySlug($slug);
222 $this->checkOwnablePermission('restrictions-manage', $shelf);
224 $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
225 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
226 return redirect($shelf->getUrl());