1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\Repos\EntityRepo;
7 use Illuminate\Http\Request;
8 use Illuminate\Http\Response;
11 class BookshelfController extends Controller
14 protected $entityRepo;
18 * BookController constructor.
19 * @param EntityRepo $entityRepo
20 * @param UserRepo $userRepo
22 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo)
24 $this->entityRepo = $entityRepo;
25 $this->userRepo = $userRepo;
26 parent::__construct();
30 * Display a listing of the book.
33 public function index()
36 $view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
38 $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
39 $order = setting()->getUser($this->currentUser, '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->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order, function($query) {
47 $query->with(['books']);
49 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
50 $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
51 $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
54 $this->setPageTitle(trans('entities.shelves'));
55 return view('shelves.index', [
56 'shelves' => $shelves,
57 'recents' => $recents,
58 'popular' => $popular,
63 'sortOptions' => $sortOptions,
68 * Show the form for creating a new bookshelf.
71 public function create()
73 $this->checkPermission('bookshelf-create-all');
74 $books = $this->entityRepo->getAll('book', false, 'update');
75 $this->setPageTitle(trans('entities.shelves_create'));
76 return view('shelves.create', ['books' => $books]);
80 * Store a newly created bookshelf in storage.
81 * @param Request $request
84 public function store(Request $request)
86 $this->checkPermission('bookshelf-create-all');
87 $this->validate($request, [
88 'name' => 'required|string|max:255',
89 'description' => 'string|max:1000',
92 $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
93 $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
94 Activity::add($bookshelf, 'bookshelf_create');
96 return redirect($bookshelf->getUrl());
101 * Display the specified bookshelf.
102 * @param String $slug
104 * @throws \BookStack\Exceptions\NotFoundException
106 public function show(string $slug)
108 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
109 $this->checkOwnablePermission('book-view', $bookshelf);
111 $books = $this->entityRepo->getBookshelfChildren($bookshelf);
112 Views::add($bookshelf);
114 $this->setPageTitle($bookshelf->getShortName());
115 return view('shelves.show', [
116 'shelf' => $bookshelf,
118 'activity' => Activity::entityActivity($bookshelf, 20, 1)
123 * Show the form for editing the specified bookshelf.
126 * @throws \BookStack\Exceptions\NotFoundException
128 public function edit(string $slug)
130 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
131 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
133 $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
134 $shelfBookIds = $shelfBooks->pluck('id');
135 $books = $this->entityRepo->getAll('book', false, 'update');
136 $books = $books->filter(function ($book) use ($shelfBookIds) {
137 return !$shelfBookIds->contains($book->id);
140 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
141 return view('shelves.edit', [
142 'shelf' => $bookshelf,
144 'shelfBooks' => $shelfBooks,
150 * Update the specified bookshelf in storage.
151 * @param Request $request
152 * @param string $slug
154 * @throws \BookStack\Exceptions\NotFoundException
156 public function update(Request $request, string $slug)
158 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
159 $this->checkOwnablePermission('bookshelf-update', $shelf);
160 $this->validate($request, [
161 'name' => 'required|string|max:255',
162 'description' => 'string|max:1000',
165 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
166 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
167 Activity::add($shelf, 'bookshelf_update');
169 return redirect($shelf->getUrl());
174 * Shows the page to confirm deletion
176 * @return \Illuminate\View\View
177 * @throws \BookStack\Exceptions\NotFoundException
179 public function showDelete(string $slug)
181 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
182 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
184 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
185 return view('shelves.delete', ['shelf' => $bookshelf]);
189 * Remove the specified bookshelf from storage.
190 * @param string $slug
192 * @throws \BookStack\Exceptions\NotFoundException
195 public function destroy(string $slug)
197 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
198 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
199 Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
200 $this->entityRepo->destroyBookshelf($bookshelf);
201 return redirect('/shelves');
205 * Show the permissions view.
206 * @param string $slug
207 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
208 * @throws \BookStack\Exceptions\NotFoundException
210 public function showPermissions(string $slug)
212 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
213 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
215 $roles = $this->userRepo->getRestrictableRoles();
216 return view('shelves.permissions', [
217 'shelf' => $bookshelf,
223 * Set the permissions for this bookshelf.
224 * @param string $slug
225 * @param Request $request
226 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
227 * @throws \BookStack\Exceptions\NotFoundException
230 public function permissions(string $slug, Request $request)
232 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
233 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
235 $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
236 session()->flash('success', trans('entities.shelves_permissions_updated'));
237 return redirect($bookshelf->getUrl());
241 * Copy the permissions of a bookshelf to the child books.
242 * @param string $slug
243 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
244 * @throws \BookStack\Exceptions\NotFoundException
246 public function copyPermissions(string $slug)
248 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
249 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
251 $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
252 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
253 return redirect($bookshelf->getUrl());