1 <?php namespace BookStack\Http\Controllers;
5 use BookStack\Bookshelf;
6 use BookStack\Repos\EntityRepo;
7 use BookStack\Repos\UserRepo;
8 use BookStack\Services\ExportService;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
13 class BookshelfController extends Controller
16 protected $entityRepo;
18 protected $exportService;
21 * BookController constructor.
22 * @param EntityRepo $entityRepo
23 * @param UserRepo $userRepo
24 * @param ExportService $exportService
26 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
28 $this->entityRepo = $entityRepo;
29 $this->userRepo = $userRepo;
30 $this->exportService = $exportService;
31 parent::__construct();
35 * Display a listing of the book.
38 public function index()
40 $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18);
41 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
42 $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
43 $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
44 $shelvesViewType = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
46 $this->setPageTitle(trans('entities.shelves'));
47 return view('shelves/index', [
48 'shelves' => $shelves,
49 'recents' => $recents,
50 'popular' => $popular,
52 'shelvesViewType' => $shelvesViewType
57 * Show the form for creating a new bookshelf.
60 public function create()
62 $this->checkPermission('bookshelf-create-all');
63 $books = $this->entityRepo->getAll('book', false, 'update');
64 $this->setPageTitle(trans('entities.shelves_create'));
65 return view('shelves/create', ['books' => $books]);
69 * Store a newly created bookshelf in storage.
70 * @param Request $request
73 public function store(Request $request)
75 $this->checkPermission('bookshelf-create-all');
76 $this->validate($request, [
77 'name' => 'required|string|max:255',
78 'description' => 'string|max:1000',
81 $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
82 $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
83 Activity::add($bookshelf, 'bookshelf_create');
85 return redirect($bookshelf->getUrl());
90 * Display the specified bookshelf.
93 * @throws \BookStack\Exceptions\NotFoundException
95 public function show(string $slug)
97 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
98 $this->checkOwnablePermission('book-view', $bookshelf);
100 $books = $this->entityRepo->getBookshelfChildren($bookshelf);
101 Views::add($bookshelf);
103 $this->setPageTitle($bookshelf->getShortName());
104 return view('shelves/show', [
105 'shelf' => $bookshelf,
107 'activity' => Activity::entityActivity($bookshelf, 20, 0)
112 * Show the form for editing the specified bookshelf.
115 * @throws \BookStack\Exceptions\NotFoundException
117 public function edit(string $slug)
119 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
120 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
122 $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
123 $shelfBookIds = $shelfBooks->pluck('id');
124 $books = $this->entityRepo->getAll('book', false, 'update');
125 $books = $books->filter(function ($book) use ($shelfBookIds) {
126 return !$shelfBookIds->contains($book->id);
129 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
130 return view('shelves/edit', [
131 'shelf' => $bookshelf,
133 'shelfBooks' => $shelfBooks,
139 * Update the specified bookshelf in storage.
140 * @param Request $request
141 * @param string $slug
143 * @throws \BookStack\Exceptions\NotFoundException
145 public function update(Request $request, string $slug)
147 $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
148 $this->checkOwnablePermission('bookshelf-update', $shelf);
149 $this->validate($request, [
150 'name' => 'required|string|max:255',
151 'description' => 'string|max:1000',
154 $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
155 $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
156 Activity::add($shelf, 'bookshelf_update');
158 return redirect($shelf->getUrl());
163 * Shows the page to confirm deletion
165 * @return \Illuminate\View\View
166 * @throws \BookStack\Exceptions\NotFoundException
168 public function showDelete(string $slug)
170 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
171 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
173 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
174 return view('shelves/delete', ['shelf' => $bookshelf]);
178 * Remove the specified bookshelf from storage.
179 * @param string $slug
181 * @throws \BookStack\Exceptions\NotFoundException
184 public function destroy(string $slug)
186 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
187 $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
188 Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
189 $this->entityRepo->destroyBookshelf($bookshelf);
190 return redirect('/shelves');
194 * Show the Restrictions view.
196 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
197 * @throws \BookStack\Exceptions\NotFoundException
199 public function showRestrict(string $slug)
201 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
202 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
204 $roles = $this->userRepo->getRestrictableRoles();
205 return view('shelves.restrictions', [
206 'shelf' => $bookshelf,
212 * Set the restrictions for this bookshelf.
214 * @param Request $request
215 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
216 * @throws \BookStack\Exceptions\NotFoundException
218 public function restrict(string $slug, Request $request)
220 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
221 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
223 $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
224 session()->flash('success', trans('entities.shelves_permissions_updated'));
225 return redirect($bookshelf->getUrl());
229 * Copy the permissions of a bookshelf to the child books.
230 * @param string $slug
231 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
232 * @throws \BookStack\Exceptions\NotFoundException
234 public function copyPermissions(string $slug)
236 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
237 $this->checkOwnablePermission('restrictions-manage', $bookshelf);
239 $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
240 session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
241 return redirect($bookshelf->getUrl());