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()
39 $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18);
40 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
41 $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
42 $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
43 $shelvesViewType = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
45 $this->setPageTitle(trans('entities.shelves'));
46 return view('shelves/index', [
47 'shelves' => $shelves,
48 'recents' => $recents,
49 'popular' => $popular,
51 'shelvesViewType' => $shelvesViewType
56 * Show the form for creating a new bookshelf.
59 public function create()
61 $this->checkPermission('bookshelf-create-all');
62 $books = $this->entityRepo->getAll('book', false, 'update');
63 $this->setPageTitle(trans('entities.shelves_create'));
64 return view('shelves/create', ['books' => $books]);
68 * Store a newly created bookshelf in storage.
69 * @param Request $request
72 public function store(Request $request)
74 $this->checkPermission('bookshelf-create-all');
75 $this->validate($request, [
76 'name' => 'required|string|max:255',
77 'description' => 'string|max:1000',
80 $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
81 $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
82 Activity::add($bookshelf, 'bookshelf_create');
84 return redirect($bookshelf->getUrl());
89 * Display the specified bookshelf.
92 * @throws \BookStack\Exceptions\NotFoundException
94 public function show(string $slug)
96 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
97 $this->checkOwnablePermission('book-view', $bookshelf);
99 $books = $this->entityRepo->getBookshelfChildren($bookshelf);
100 Views::add($bookshelf);
102 $this->setPageTitle($bookshelf->getShortName());
103 return view('shelves/show', [
104 'shelf' => $bookshelf,
106 'activity' => Activity::entityActivity($bookshelf, 20, 0)
111 * Show the form for editing the specified bookshelf.
114 * @throws \BookStack\Exceptions\NotFoundException
116 public function edit(string $slug)
118 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
119 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
121 $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
122 $shelfBookIds = $shelfBooks->pluck('id');
123 $books = $this->entityRepo->getAll('book', false, 'update');
124 $books = $books->filter(function ($book) use ($shelfBookIds) {
125 return !$shelfBookIds->contains($book->id);
128 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
129 return view('shelves/edit', [
130 'shelf' => $bookshelf,
132 'shelfBooks' => $shelfBooks,
138 * Update the specified bookshelf in storage.
139 * @param Request $request
140 * @param string $slug
142 * @throws \BookStack\Exceptions\NotFoundException
144 public function update(Request $request, string $slug)
146 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
147 $this->checkOwnablePermission('bookshelf-update', $shelf);
148 $this->validate($request, [
149 'name' => 'required|string|max:255',
150 'description' => 'string|max:1000',
153 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
154 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
155 Activity::add($shelf, 'bookshelf_update');
157 return redirect($shelf->getUrl());
162 * Shows the page to confirm deletion
164 * @return \Illuminate\View\View
165 * @throws \BookStack\Exceptions\NotFoundException
167 public function showDelete(string $slug)
169 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
170 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
172 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
173 return view('shelves/delete', ['shelf' => $bookshelf]);
177 * Remove the specified bookshelf from storage.
178 * @param string $slug
180 * @throws \BookStack\Exceptions\NotFoundException
183 public function destroy(string $slug)
185 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
186 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
187 Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
188 $this->entityRepo->destroyBookshelf($bookshelf);
189 return redirect('/shelves');
193 * Show the Restrictions view.
195 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
196 * @throws \BookStack\Exceptions\NotFoundException
198 public function showRestrict(string $slug)
200 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
201 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
203 $roles = $this->userRepo->getRestrictableRoles();
204 return view('shelves.restrictions', [
205 'shelf' => $bookshelf,
211 * Set the restrictions for this bookshelf.
213 * @param Request $request
214 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
215 * @throws \BookStack\Exceptions\NotFoundException
217 public function restrict(string $slug, Request $request)
219 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
220 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
222 $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
223 session()->flash('success', trans('entities.shelves_permissions_updated'));
224 return redirect($bookshelf->getUrl());
228 * Copy the permissions of a bookshelf to the child books.
229 * @param string $slug
230 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
231 * @throws \BookStack\Exceptions\NotFoundException
233 public function copyPermissions(string $slug)
235 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
236 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
238 $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
239 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
240 return redirect($bookshelf->getUrl());