1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Entities\ExportService;
8 use Illuminate\Http\Request;
9 use Illuminate\Http\Response;
12 class BookshelfController extends Controller
15 protected $entityRepo;
17 protected $exportService;
20 * BookController constructor.
21 * @param \BookStack\Entities\Repos\EntityRepo $entityRepo
22 * @param UserRepo $userRepo
23 * @param \BookStack\Entities\ExportService $exportService
25 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
27 $this->entityRepo = $entityRepo;
28 $this->userRepo = $userRepo;
29 $this->exportService = $exportService;
30 parent::__construct();
34 * Display a listing of the book.
37 public function index()
40 $view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
42 $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
43 $order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc');
45 'name' => trans('common.sort_name'),
46 'created_at' => trans('common.sort_created_at'),
47 'updated_at' => trans('common.sort_updated_at'),
50 $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order, function($query) {
51 $query->with(['books']);
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);
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 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
113 $this->checkOwnablePermission('book-view', $bookshelf);
115 $books = $this->entityRepo->getBookshelfChildren($bookshelf);
116 Views::add($bookshelf);
118 $this->setPageTitle($bookshelf->getShortName());
119 return view('shelves.show', [
120 'shelf' => $bookshelf,
122 'activity' => Activity::entityActivity($bookshelf, 20, 1)
127 * Show the form for editing the specified bookshelf.
130 * @throws \BookStack\Exceptions\NotFoundException
132 public function edit(string $slug)
134 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
135 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
137 $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
138 $shelfBookIds = $shelfBooks->pluck('id');
139 $books = $this->entityRepo->getAll('book', false, 'update');
140 $books = $books->filter(function ($book) use ($shelfBookIds) {
141 return !$shelfBookIds->contains($book->id);
144 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
145 return view('shelves.edit', [
146 'shelf' => $bookshelf,
148 'shelfBooks' => $shelfBooks,
154 * Update the specified bookshelf in storage.
155 * @param Request $request
156 * @param string $slug
158 * @throws \BookStack\Exceptions\NotFoundException
160 public function update(Request $request, string $slug)
162 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
163 $this->checkOwnablePermission('bookshelf-update', $shelf);
164 $this->validate($request, [
165 'name' => 'required|string|max:255',
166 'description' => 'string|max:1000',
169 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
170 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
171 Activity::add($shelf, 'bookshelf_update');
173 return redirect($shelf->getUrl());
178 * Shows the page to confirm deletion
180 * @return \Illuminate\View\View
181 * @throws \BookStack\Exceptions\NotFoundException
183 public function showDelete(string $slug)
185 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
186 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
188 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
189 return view('shelves.delete', ['shelf' => $bookshelf]);
193 * Remove the specified bookshelf from storage.
194 * @param string $slug
196 * @throws \BookStack\Exceptions\NotFoundException
199 public function destroy(string $slug)
201 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
202 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
203 Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
204 $this->entityRepo->destroyBookshelf($bookshelf);
205 return redirect('/shelves');
209 * Show the permissions view.
210 * @param string $slug
211 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
212 * @throws \BookStack\Exceptions\NotFoundException
214 public function showPermissions(string $slug)
216 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
217 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
219 $roles = $this->userRepo->getRestrictableRoles();
220 return view('shelves.permissions', [
221 'shelf' => $bookshelf,
227 * Set the permissions for this bookshelf.
228 * @param string $slug
229 * @param Request $request
230 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
231 * @throws \BookStack\Exceptions\NotFoundException
234 public function permissions(string $slug, Request $request)
236 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
237 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
239 $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
240 session()->flash('success', trans('entities.shelves_permissions_updated'));
241 return redirect($bookshelf->getUrl());
245 * Copy the permissions of a bookshelf to the child books.
246 * @param string $slug
247 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
248 * @throws \BookStack\Exceptions\NotFoundException
250 public function copyPermissions(string $slug)
252 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
253 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
255 $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
256 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
257 return redirect($bookshelf->getUrl());