1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\EntityContextManager;
7 use BookStack\Entities\Repos\EntityRepo;
8 use BookStack\Uploads\ImageRepo;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
13 class BookshelfController extends Controller
16 protected $entityRepo;
18 protected $entityContextManager;
22 * BookController constructor.
23 * @param EntityRepo $entityRepo
24 * @param UserRepo $userRepo
25 * @param EntityContextManager $entityContextManager
26 * @param ImageRepo $imageRepo
28 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, EntityContextManager $entityContextManager, ImageRepo $imageRepo)
30 $this->entityRepo = $entityRepo;
31 $this->userRepo = $userRepo;
32 $this->entityContextManager = $entityContextManager;
33 $this->imageRepo = $imageRepo;
34 parent::__construct();
38 * Display a listing of the book.
41 public function index()
43 $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
44 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
45 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
47 'name' => trans('common.sort_name'),
48 'created_at' => trans('common.sort_created_at'),
49 'updated_at' => trans('common.sort_updated_at'),
52 $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order);
53 foreach ($shelves as $shelf) {
54 $shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
57 $recents = $this->isSignedIn() ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
58 $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
59 $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
61 $this->entityContextManager->clearShelfContext();
62 $this->setPageTitle(trans('entities.shelves'));
63 return view('shelves.index', [
64 'shelves' => $shelves,
65 'recents' => $recents,
66 'popular' => $popular,
71 'sortOptions' => $sortOptions,
76 * Show the form for creating a new bookshelf.
79 public function create()
81 $this->checkPermission('bookshelf-create-all');
82 $books = $this->entityRepo->getAll('book', false, 'update');
83 $this->setPageTitle(trans('entities.shelves_create'));
84 return view('shelves.create', ['books' => $books]);
88 * Store a newly created bookshelf in storage.
89 * @param Request $request
91 * @throws \BookStack\Exceptions\ImageUploadException
93 public function store(Request $request)
95 $this->checkPermission('bookshelf-create-all');
96 $this->validate($request, [
97 'name' => 'required|string|max:255',
98 'description' => 'string|max:1000',
99 'image' => $this->imageRepo->getImageValidationRules(),
102 $shelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
103 $this->shelfUpdateActions($shelf, $request);
105 Activity::add($shelf, 'bookshelf_create');
106 return redirect($shelf->getUrl());
111 * Display the specified bookshelf.
112 * @param String $slug
114 * @throws \BookStack\Exceptions\NotFoundException
116 public function show(string $slug)
118 /** @var Bookshelf $shelf */
119 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
120 $this->checkOwnablePermission('book-view', $shelf);
122 $books = $this->entityRepo->getBookshelfChildren($shelf);
124 $this->entityContextManager->setShelfContext($shelf->id);
126 $this->setPageTitle($shelf->getShortName());
128 return view('shelves.show', [
131 'activity' => Activity::entityActivity($shelf, 20, 1)
136 * Show the form for editing the specified bookshelf.
139 * @throws \BookStack\Exceptions\NotFoundException
141 public function edit(string $slug)
143 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
144 $this->checkOwnablePermission('bookshelf-update', $shelf);
146 $shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
147 $shelfBookIds = $shelfBooks->pluck('id');
148 $books = $this->entityRepo->getAll('book', false, 'update');
149 $books = $books->filter(function ($book) use ($shelfBookIds) {
150 return !$shelfBookIds->contains($book->id);
153 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
154 return view('shelves.edit', [
157 'shelfBooks' => $shelfBooks,
163 * Update the specified bookshelf in storage.
164 * @param Request $request
165 * @param string $slug
167 * @throws \BookStack\Exceptions\NotFoundException
168 * @throws \BookStack\Exceptions\ImageUploadException
170 public function update(Request $request, string $slug)
172 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
173 $this->checkOwnablePermission('bookshelf-update', $shelf);
174 $this->validate($request, [
175 'name' => 'required|string|max:255',
176 'description' => 'string|max:1000',
177 'image' => $this->imageRepo->getImageValidationRules(),
180 $shelf = $this->entityRepo->updateFromInput($shelf, $request->all());
181 $this->shelfUpdateActions($shelf, $request);
183 Activity::add($shelf, 'bookshelf_update');
185 return redirect($shelf->getUrl());
190 * Shows the page to confirm deletion
192 * @return \Illuminate\View\View
193 * @throws \BookStack\Exceptions\NotFoundException
195 public function showDelete(string $slug)
197 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
198 $this->checkOwnablePermission('bookshelf-delete', $shelf);
200 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
201 return view('shelves.delete', ['shelf' => $shelf]);
205 * Remove the specified bookshelf from storage.
206 * @param string $slug
208 * @throws \BookStack\Exceptions\NotFoundException
211 public function destroy(string $slug)
213 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
214 $this->checkOwnablePermission('bookshelf-delete', $shelf);
215 Activity::addMessage('bookshelf_delete', $shelf->name);
218 $this->imageRepo->destroyImage($shelf->cover);
220 $this->entityRepo->destroyBookshelf($shelf);
222 return redirect('/shelves');
226 * Show the permissions view.
227 * @param string $slug
228 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
229 * @throws \BookStack\Exceptions\NotFoundException
231 public function showPermissions(string $slug)
233 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
234 $this->checkOwnablePermission('restrictions-manage', $shelf);
236 $roles = $this->userRepo->getRestrictableRoles();
237 return view('shelves.permissions', [
244 * Set the permissions for this bookshelf.
245 * @param Request $request
246 * @param string $slug
247 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
248 * @throws \BookStack\Exceptions\NotFoundException
251 public function permissions(Request $request, string $slug)
253 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
254 $this->checkOwnablePermission('restrictions-manage', $shelf);
256 $this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
257 $this->showSuccessNotification( trans('entities.shelves_permissions_updated'));
258 return redirect($shelf->getUrl());
262 * Copy the permissions of a bookshelf to the child books.
263 * @param string $slug
264 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
265 * @throws \BookStack\Exceptions\NotFoundException
267 public function copyPermissions(string $slug)
269 $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
270 $this->checkOwnablePermission('restrictions-manage', $shelf);
272 $updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
273 $this->showSuccessNotification( trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
274 return redirect($shelf->getUrl());
278 * Common actions to run on bookshelf update.
279 * @param Bookshelf $shelf
280 * @param Request $request
281 * @throws \BookStack\Exceptions\ImageUploadException
283 protected function shelfUpdateActions(Bookshelf $shelf, Request $request)
285 // Update the books that the shelf references
286 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
288 // Update the cover image if in request
289 if ($request->has('image')) {
290 $newImage = $request->file('image');
291 $this->imageRepo->destroyImage($shelf->cover);
292 $image = $this->imageRepo->saveNew($newImage, 'cover_shelf', $shelf->id, 512, 512, true);
293 $shelf->image_id = $image->id;
297 if ($request->has('image_reset')) {
298 $this->imageRepo->destroyImage($shelf->cover);
299 $shelf->image_id = 0;