1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Managers\BookContents;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\Managers\EntityContext;
7 use BookStack\Entities\Repos\BookRepo;
8 use BookStack\Exceptions\ImageUploadException;
9 use BookStack\Exceptions\NotifyException;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
15 class BookController extends Controller
19 protected $entityContextManager;
22 * BookController constructor.
24 public function __construct(EntityContext $entityContextManager, BookRepo $bookRepo)
26 $this->bookRepo = $bookRepo;
27 $this->entityContextManager = $entityContextManager;
28 parent::__construct();
32 * Display a listing of the book.
34 public function index()
36 $view = setting()->getForCurrentUser('books_view_type', config('app.views.books'));
37 $sort = setting()->getForCurrentUser('books_sort', 'name');
38 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
40 $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
41 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
42 $popular = $this->bookRepo->getPopular(4);
43 $new = $this->bookRepo->getRecentlyCreated(4);
45 $this->entityContextManager->clearShelfContext();
47 $this->setPageTitle(trans('entities.books'));
48 return view('books.index', [
50 'recents' => $recents,
51 'popular' => $popular,
60 * Show the form for creating a new book.
62 public function create(string $shelfSlug = null)
64 $this->checkPermission('book-create-all');
67 if ($shelfSlug !== null) {
68 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
69 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
72 $this->setPageTitle(trans('entities.books_create'));
73 return view('books.create', [
74 'bookshelf' => $bookshelf
79 * Store a newly created book in storage.
80 * @throws ImageUploadException
81 * @throws ValidationException
83 public function store(Request $request, string $shelfSlug = null)
85 $this->checkPermission('book-create-all');
86 $this->validate($request, [
87 'name' => 'required|string|max:255',
88 'description' => 'string|max:1000',
89 'image' => 'nullable|' . $this->getImageValidationRules(),
93 if ($shelfSlug !== null) {
94 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
95 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
98 $book = $this->bookRepo->create($request->all());
99 $this->bookRepo->updateCoverImage($book, $request->file('image', null));
100 Activity::add($book, 'book_create', $book->id);
103 $bookshelf->appendBook($book);
104 Activity::add($bookshelf, 'bookshelf_update');
107 return redirect($book->getUrl());
111 * Display the specified book.
113 public function show(Request $request, string $slug)
115 $book = $this->bookRepo->getBySlug($slug);
116 $bookChildren = (new BookContents($book))->getTree(true);
117 $bookParentShelves = $book->shelves()->visible()->get();
120 if ($request->has('shelf')) {
121 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
124 $this->setPageTitle($book->getShortName());
125 return view('books.show', [
128 'bookChildren' => $bookChildren,
129 'bookParentShelves' => $bookParentShelves,
130 'activity' => Activity::entityActivity($book, 20, 1)
135 * Show the form for editing the specified book.
137 public function edit(string $slug)
139 $book = $this->bookRepo->getBySlug($slug);
140 $this->checkOwnablePermission('book-update', $book);
141 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
142 return view('books.edit', ['book' => $book, 'current' => $book]);
146 * Update the specified book in storage.
147 * @throws ImageUploadException
148 * @throws ValidationException
151 public function update(Request $request, string $slug)
153 $book = $this->bookRepo->getBySlug($slug);
154 $this->checkOwnablePermission('book-update', $book);
155 $this->validate($request, [
156 'name' => 'required|string|max:255',
157 'description' => 'string|max:1000',
158 'image' => 'nullable|' . $this->getImageValidationRules(),
161 $book = $this->bookRepo->update($book, $request->all());
162 $resetCover = $request->has('image_reset');
163 $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
165 Activity::add($book, 'book_update', $book->id);
167 return redirect($book->getUrl());
171 * Shows the page to confirm deletion.
173 public function showDelete(string $bookSlug)
175 $book = $this->bookRepo->getBySlug($bookSlug);
176 $this->checkOwnablePermission('book-delete', $book);
177 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
178 return view('books.delete', ['book' => $book, 'current' => $book]);
182 * Remove the specified book from the system.
184 * @throws NotifyException
186 public function destroy(string $bookSlug)
188 $book = $this->bookRepo->getBySlug($bookSlug);
189 $this->checkOwnablePermission('book-delete', $book);
191 Activity::addMessage('book_delete', $book->name);
192 $this->bookRepo->destroy($book);
194 return redirect('/books');
198 * Show the permissions view.
200 public function showPermissions(string $bookSlug)
202 $book = $this->bookRepo->getBySlug($bookSlug);
203 $this->checkOwnablePermission('restrictions-manage', $book);
205 return view('books.permissions', [
211 * Set the restrictions for this book.
214 public function permissions(Request $request, string $bookSlug)
216 $book = $this->bookRepo->getBySlug($bookSlug);
217 $this->checkOwnablePermission('restrictions-manage', $book);
219 $restricted = $request->get('restricted') === 'true';
220 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
221 $this->bookRepo->updatePermissions($book, $restricted, $permissions);
223 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
224 return redirect($book->getUrl());