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()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
44 $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
45 $order = setting()->getUser($this->currentUser, '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->signedIn ? $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->getBySlug('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());
127 return view('shelves.show', [
130 'activity' => Activity::entityActivity($shelf, 20, 1)
135 * Show the form for editing the specified bookshelf.
138 * @throws \BookStack\Exceptions\NotFoundException
140 public function edit(string $slug)
142 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
143 $this->checkOwnablePermission('bookshelf-update', $shelf);
145 $shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
146 $shelfBookIds = $shelfBooks->pluck('id');
147 $books = $this->entityRepo->getAll('book', false, 'update');
148 $books = $books->filter(function ($book) use ($shelfBookIds) {
149 return !$shelfBookIds->contains($book->id);
152 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
153 return view('shelves.edit', [
156 'shelfBooks' => $shelfBooks,
162 * Update the specified bookshelf in storage.
163 * @param Request $request
164 * @param string $slug
166 * @throws \BookStack\Exceptions\NotFoundException
167 * @throws \BookStack\Exceptions\ImageUploadException
169 public function update(Request $request, string $slug)
171 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
172 $this->checkOwnablePermission('bookshelf-update', $shelf);
173 $this->validate($request, [
174 'name' => 'required|string|max:255',
175 'description' => 'string|max:1000',
176 'image' => $this->imageRepo->getImageValidationRules(),
179 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
180 $this->shelfUpdateActions($shelf, $request);
182 Activity::add($shelf, 'bookshelf_update');
184 return redirect($shelf->getUrl());
189 * Shows the page to confirm deletion
191 * @return \Illuminate\View\View
192 * @throws \BookStack\Exceptions\NotFoundException
194 public function showDelete(string $slug)
196 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
197 $this->checkOwnablePermission('bookshelf-delete', $shelf);
199 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
200 return view('shelves.delete', ['shelf' => $shelf]);
204 * Remove the specified bookshelf from storage.
205 * @param string $slug
207 * @throws \BookStack\Exceptions\NotFoundException
210 public function destroy(string $slug)
212 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
213 $this->checkOwnablePermission('bookshelf-delete', $shelf);
214 Activity::addMessage('bookshelf_delete', 0, $shelf->name);
217 $this->imageRepo->destroyImage($shelf->cover);
219 $this->entityRepo->destroyBookshelf($shelf);
221 return redirect('/shelves');
225 * Show the permissions view.
226 * @param string $slug
227 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
228 * @throws \BookStack\Exceptions\NotFoundException
230 public function showPermissions(string $slug)
232 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
233 $this->checkOwnablePermission('restrictions-manage', $shelf);
235 $roles = $this->userRepo->getRestrictableRoles();
236 return view('shelves.permissions', [
243 * Set the permissions for this bookshelf.
244 * @param string $slug
245 * @param Request $request
246 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
247 * @throws \BookStack\Exceptions\NotFoundException
250 public function permissions(string $slug, Request $request)
252 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
253 $this->checkOwnablePermission('restrictions-manage', $shelf);
255 $this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
256 session()->flash('success', trans('entities.shelves_permissions_updated'));
257 return redirect($shelf->getUrl());
261 * Copy the permissions of a bookshelf to the child books.
262 * @param string $slug
263 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
264 * @throws \BookStack\Exceptions\NotFoundException
266 public function copyPermissions(string $slug)
268 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
269 $this->checkOwnablePermission('restrictions-manage', $shelf);
271 $updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
272 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
273 return redirect($shelf->getUrl());
277 * Common actions to run on bookshelf update.
278 * @param Bookshelf $shelf
279 * @param Request $request
280 * @throws \BookStack\Exceptions\ImageUploadException
282 protected function shelfUpdateActions(Bookshelf $shelf, Request $request)
284 // Update the books that the shelf references
285 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
287 // Update the cover image if in request
288 if ($request->has('image')) {
289 $newImage = $request->file('image');
290 $image = $this->imageRepo->saveNew($newImage, 'cover_shelf', $shelf->id, 512, 512, true);
291 $shelf->image_id = $image->id;
295 if ($request->has('image_reset')) {
296 $this->imageRepo->destroyImage($shelf->cover);
297 $shelf->image_id = 0;