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
92 public function store(Request $request)
94 $this->checkPermission('bookshelf-create-all');
95 $this->validate($request, [
96 'name' => 'required|string|max:255',
97 'description' => 'string|max:1000',
98 'image' => $this->imageRepo->getImageValidationRules(),
101 $shelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
102 $this->shelfUpdateActions($shelf, $request);
104 Activity::add($shelf, 'bookshelf_create');
105 return redirect($shelf->getUrl());
110 * Display the specified bookshelf.
111 * @param String $slug
113 * @throws \BookStack\Exceptions\NotFoundException
115 public function show(string $slug)
117 /** @var Bookshelf $shelf */
118 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
119 $this->checkOwnablePermission('book-view', $shelf);
121 $books = $this->entityRepo->getBookshelfChildren($shelf);
123 $this->entityContextManager->setShelfContext($shelf->id);
125 $this->setPageTitle($shelf->getShortName());
126 return view('shelves.show', [
129 'activity' => Activity::entityActivity($shelf, 20, 1)
134 * Show the form for editing the specified bookshelf.
137 * @throws \BookStack\Exceptions\NotFoundException
139 public function edit(string $slug)
141 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
142 $this->checkOwnablePermission('bookshelf-update', $shelf);
144 $shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
145 $shelfBookIds = $shelfBooks->pluck('id');
146 $books = $this->entityRepo->getAll('book', false, 'update');
147 $books = $books->filter(function ($book) use ($shelfBookIds) {
148 return !$shelfBookIds->contains($book->id);
151 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
152 return view('shelves.edit', [
155 'shelfBooks' => $shelfBooks,
161 * Update the specified bookshelf in storage.
162 * @param Request $request
163 * @param string $slug
165 * @throws \BookStack\Exceptions\NotFoundException
166 * @throws \BookStack\Exceptions\ImageUploadException
168 public function update(Request $request, string $slug)
170 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
171 $this->checkOwnablePermission('bookshelf-update', $shelf);
172 $this->validate($request, [
173 'name' => 'required|string|max:255',
174 'description' => 'string|max:1000',
175 'image' => $this->imageRepo->getImageValidationRules(),
178 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
179 $this->shelfUpdateActions($shelf, $request);
181 Activity::add($shelf, 'bookshelf_update');
183 return redirect($shelf->getUrl());
188 * Shows the page to confirm deletion
190 * @return \Illuminate\View\View
191 * @throws \BookStack\Exceptions\NotFoundException
193 public function showDelete(string $slug)
195 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
196 $this->checkOwnablePermission('bookshelf-delete', $shelf);
198 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
199 return view('shelves.delete', ['shelf' => $shelf]);
203 * Remove the specified bookshelf from storage.
204 * @param string $slug
206 * @throws \BookStack\Exceptions\NotFoundException
209 public function destroy(string $slug)
211 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
212 $this->checkOwnablePermission('bookshelf-delete', $shelf);
213 Activity::addMessage('bookshelf_delete', 0, $shelf->name);
216 $this->imageRepo->destroyImage($shelf->cover);
218 $this->entityRepo->destroyBookshelf($shelf);
220 return redirect('/shelves');
224 * Show the permissions view.
225 * @param string $slug
226 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
227 * @throws \BookStack\Exceptions\NotFoundException
229 public function showPermissions(string $slug)
231 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
232 $this->checkOwnablePermission('restrictions-manage', $shelf);
234 $roles = $this->userRepo->getRestrictableRoles();
235 return view('shelves.permissions', [
242 * Set the permissions for this bookshelf.
243 * @param string $slug
244 * @param Request $request
245 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
246 * @throws \BookStack\Exceptions\NotFoundException
249 public function permissions(string $slug, Request $request)
251 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
252 $this->checkOwnablePermission('restrictions-manage', $shelf);
254 $this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
255 session()->flash('success', trans('entities.shelves_permissions_updated'));
256 return redirect($shelf->getUrl());
260 * Copy the permissions of a bookshelf to the child books.
261 * @param string $slug
262 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
263 * @throws \BookStack\Exceptions\NotFoundException
265 public function copyPermissions(string $slug)
267 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug);
268 $this->checkOwnablePermission('restrictions-manage', $shelf);
270 $updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
271 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
272 return redirect($shelf->getUrl());
276 * Common actions to run on bookshelf update.
277 * @param Bookshelf $shelf
278 * @param Request $request
279 * @throws \BookStack\Exceptions\ImageUploadException
281 protected function shelfUpdateActions(Bookshelf $shelf, Request $request)
283 // Update the books that the shelf references
284 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
286 // Update the cover image if in request
287 if ($request->has('image') && userCan('image-create-all')) {
288 $image = $this->imageRepo->saveNew($request->file('image'), 'cover', $shelf->id);
289 $shelf->image_id = $image->id;