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);
51 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
52 $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
53 $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
56 $this->setPageTitle(trans('entities.shelves'));
57 return view('shelves.index', [
58 'shelves' => $shelves,
59 'recents' => $recents,
60 'popular' => $popular,
65 'sortOptions' => $sortOptions,
70 * Show the form for creating a new bookshelf.
73 public function create()
75 $this->checkPermission('bookshelf-create-all');
76 $books = $this->entityRepo->getAll('book', false, 'update');
77 $this->setPageTitle(trans('entities.shelves_create'));
78 return view('shelves.create', ['books' => $books]);
82 * Store a newly created bookshelf in storage.
83 * @param Request $request
86 public function store(Request $request)
88 $this->checkPermission('bookshelf-create-all');
89 $this->validate($request, [
90 'name' => 'required|string|max:255',
91 'description' => 'string|max:1000',
94 $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
95 $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
96 Activity::add($bookshelf, 'bookshelf_create');
98 return redirect($bookshelf->getUrl());
103 * Display the specified bookshelf.
104 * @param String $slug
106 * @throws \BookStack\Exceptions\NotFoundException
108 public function show(string $slug)
110 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
111 $this->checkOwnablePermission('book-view', $bookshelf);
113 $books = $this->entityRepo->getBookshelfChildren($bookshelf);
114 Views::add($bookshelf);
116 $this->setPageTitle($bookshelf->getShortName());
117 return view('shelves.show', [
118 'shelf' => $bookshelf,
120 'activity' => Activity::entityActivity($bookshelf, 20, 1)
125 * Show the form for editing the specified bookshelf.
128 * @throws \BookStack\Exceptions\NotFoundException
130 public function edit(string $slug)
132 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
133 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
135 $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
136 $shelfBookIds = $shelfBooks->pluck('id');
137 $books = $this->entityRepo->getAll('book', false, 'update');
138 $books = $books->filter(function ($book) use ($shelfBookIds) {
139 return !$shelfBookIds->contains($book->id);
142 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
143 return view('shelves.edit', [
144 'shelf' => $bookshelf,
146 'shelfBooks' => $shelfBooks,
152 * Update the specified bookshelf in storage.
153 * @param Request $request
154 * @param string $slug
156 * @throws \BookStack\Exceptions\NotFoundException
158 public function update(Request $request, string $slug)
160 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
161 $this->checkOwnablePermission('bookshelf-update', $shelf);
162 $this->validate($request, [
163 'name' => 'required|string|max:255',
164 'description' => 'string|max:1000',
167 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
168 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
169 Activity::add($shelf, 'bookshelf_update');
171 return redirect($shelf->getUrl());
176 * Shows the page to confirm deletion
178 * @return \Illuminate\View\View
179 * @throws \BookStack\Exceptions\NotFoundException
181 public function showDelete(string $slug)
183 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
184 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
186 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
187 return view('shelves.delete', ['shelf' => $bookshelf]);
191 * Remove the specified bookshelf from storage.
192 * @param string $slug
194 * @throws \BookStack\Exceptions\NotFoundException
197 public function destroy(string $slug)
199 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
200 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
201 Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
202 $this->entityRepo->destroyBookshelf($bookshelf);
203 return redirect('/shelves');
207 * Show the permissions view.
208 * @param string $slug
209 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
210 * @throws \BookStack\Exceptions\NotFoundException
212 public function showPermissions(string $slug)
214 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
215 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
217 $roles = $this->userRepo->getRestrictableRoles();
218 return view('shelves.permissions', [
219 'shelf' => $bookshelf,
225 * Set the permissions for this bookshelf.
226 * @param string $slug
227 * @param Request $request
228 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
229 * @throws \BookStack\Exceptions\NotFoundException
232 public function permissions(string $slug, Request $request)
234 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
235 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
237 $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
238 session()->flash('success', trans('entities.shelves_permissions_updated'));
239 return redirect($bookshelf->getUrl());
243 * Copy the permissions of a bookshelf to the child books.
244 * @param string $slug
245 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
246 * @throws \BookStack\Exceptions\NotFoundException
248 public function copyPermissions(string $slug)
250 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
251 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
253 $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
254 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
255 return redirect($bookshelf->getUrl());