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 Illuminate\Http\Request;
9 use Illuminate\Http\Response;
12 class BookshelfController extends Controller
15 protected $entityRepo;
17 protected $entityContextManager;
20 * BookController constructor.
21 * @param EntityRepo $entityRepo
22 * @param UserRepo $userRepo
23 * @param EntityContextManager $entityContextManager
25 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, EntityContextManager $entityContextManager)
27 $this->entityRepo = $entityRepo;
28 $this->userRepo = $userRepo;
29 $this->entityContextManager = $entityContextManager;
30 parent::__construct();
34 * Display a listing of the book.
37 public function index()
39 $view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
40 $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
41 $order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc');
43 'name' => trans('common.sort_name'),
44 'created_at' => trans('common.sort_created_at'),
45 'updated_at' => trans('common.sort_updated_at'),
48 $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order);
49 foreach ($shelves as $shelf) {
50 $shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
53 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
54 $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
55 $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
57 $this->entityContextManager->clearShelfContext();
58 $this->setPageTitle(trans('entities.shelves'));
59 return view('shelves.index', [
60 'shelves' => $shelves,
61 'recents' => $recents,
62 'popular' => $popular,
67 'sortOptions' => $sortOptions,
72 * Show the form for creating a new bookshelf.
75 public function create()
77 $this->checkPermission('bookshelf-create-all');
78 $books = $this->entityRepo->getAll('book', false, 'update');
79 $this->setPageTitle(trans('entities.shelves_create'));
80 return view('shelves.create', ['books' => $books]);
84 * Store a newly created bookshelf in storage.
85 * @param Request $request
88 public function store(Request $request)
90 $this->checkPermission('bookshelf-create-all');
91 $this->validate($request, [
92 'name' => 'required|string|max:255',
93 'description' => 'string|max:1000',
96 $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
97 $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
98 Activity::add($bookshelf, 'bookshelf_create');
100 return redirect($bookshelf->getUrl());
105 * Display the specified bookshelf.
106 * @param String $slug
108 * @throws \BookStack\Exceptions\NotFoundException
110 public function show(string $slug)
112 /** @var Bookshelf $bookshelf */
113 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
114 $this->checkOwnablePermission('book-view', $bookshelf);
116 $books = $this->entityRepo->getBookshelfChildren($bookshelf);
117 Views::add($bookshelf);
118 $this->entityContextManager->setShelfContext($bookshelf->id);
120 $this->setPageTitle($bookshelf->getShortName());
121 return view('shelves.show', [
122 'shelf' => $bookshelf,
124 'activity' => Activity::entityActivity($bookshelf, 20, 1)
129 * Show the form for editing the specified bookshelf.
132 * @throws \BookStack\Exceptions\NotFoundException
134 public function edit(string $slug)
136 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
137 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
139 $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
140 $shelfBookIds = $shelfBooks->pluck('id');
141 $books = $this->entityRepo->getAll('book', false, 'update');
142 $books = $books->filter(function ($book) use ($shelfBookIds) {
143 return !$shelfBookIds->contains($book->id);
146 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
147 return view('shelves.edit', [
148 'shelf' => $bookshelf,
150 'shelfBooks' => $shelfBooks,
156 * Update the specified bookshelf in storage.
157 * @param Request $request
158 * @param string $slug
160 * @throws \BookStack\Exceptions\NotFoundException
162 public function update(Request $request, string $slug)
164 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
165 $this->checkOwnablePermission('bookshelf-update', $shelf);
166 $this->validate($request, [
167 'name' => 'required|string|max:255',
168 'description' => 'string|max:1000',
171 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
172 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
173 Activity::add($shelf, 'bookshelf_update');
175 return redirect($shelf->getUrl());
180 * Shows the page to confirm deletion
182 * @return \Illuminate\View\View
183 * @throws \BookStack\Exceptions\NotFoundException
185 public function showDelete(string $slug)
187 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
188 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
190 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
191 return view('shelves.delete', ['shelf' => $bookshelf]);
195 * Remove the specified bookshelf from storage.
196 * @param string $slug
198 * @throws \BookStack\Exceptions\NotFoundException
201 public function destroy(string $slug)
203 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
204 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
205 Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
206 $this->entityRepo->destroyBookshelf($bookshelf);
207 return redirect('/shelves');
211 * Show the permissions view.
212 * @param string $slug
213 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
214 * @throws \BookStack\Exceptions\NotFoundException
216 public function showPermissions(string $slug)
218 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
219 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
221 $roles = $this->userRepo->getRestrictableRoles();
222 return view('shelves.permissions', [
223 'shelf' => $bookshelf,
229 * Set the permissions for this bookshelf.
230 * @param string $slug
231 * @param Request $request
232 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
233 * @throws \BookStack\Exceptions\NotFoundException
236 public function permissions(string $slug, Request $request)
238 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
239 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
241 $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
242 session()->flash('success', trans('entities.shelves_permissions_updated'));
243 return redirect($bookshelf->getUrl());
247 * Copy the permissions of a bookshelf to the child books.
248 * @param string $slug
249 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
250 * @throws \BookStack\Exceptions\NotFoundException
252 public function copyPermissions(string $slug)
254 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
255 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
257 $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
258 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
259 return redirect($bookshelf->getUrl());