1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Actions\ActivityType;
5 use BookStack\Actions\View;
6 use BookStack\Entities\Tools\BookContents;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Tools\PermissionsUpdater;
9 use BookStack\Entities\Tools\ShelfContext;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Exceptions\ImageUploadException;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
17 class BookController extends Controller
21 protected $entityContextManager;
23 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
25 $this->bookRepo = $bookRepo;
26 $this->entityContextManager = $entityContextManager;
30 * Display a listing of the book.
32 public function index()
34 $view = setting()->getForCurrentUser('books_view_type');
35 $sort = setting()->getForCurrentUser('books_sort', 'name');
36 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
38 $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
39 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
40 $popular = $this->bookRepo->getPopular(4);
41 $new = $this->bookRepo->getRecentlyCreated(4);
43 $this->entityContextManager->clearShelfContext();
45 $this->setPageTitle(trans('entities.books'));
46 return view('books.index', [
48 'recents' => $recents,
49 'popular' => $popular,
58 * Show the form for creating a new book.
60 public function create(string $shelfSlug = null)
62 $this->checkPermission('book-create-all');
65 if ($shelfSlug !== null) {
66 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
67 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
70 $this->setPageTitle(trans('entities.books_create'));
71 return view('books.create', [
72 'bookshelf' => $bookshelf
77 * Store a newly created book in storage.
78 * @throws ImageUploadException
79 * @throws ValidationException
81 public function store(Request $request, string $shelfSlug = null)
83 $this->checkPermission('book-create-all');
84 $this->validate($request, [
85 'name' => 'required|string|max:255',
86 'description' => 'string|max:1000',
87 'image' => 'nullable|' . $this->getImageValidationRules(),
91 if ($shelfSlug !== null) {
92 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
93 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
96 $book = $this->bookRepo->create($request->all());
97 $this->bookRepo->updateCoverImage($book, $request->file('image', null));
100 $bookshelf->appendBook($book);
101 Activity::addForEntity($bookshelf, ActivityType::BOOKSHELF_UPDATE);
104 return redirect($book->getUrl());
108 * Display the specified book.
110 public function show(Request $request, string $slug)
112 $book = $this->bookRepo->getBySlug($slug);
113 $bookChildren = (new BookContents($book))->getTree(true);
114 $bookParentShelves = $book->shelves()->visible()->get();
116 View::incrementFor($book);
117 if ($request->has('shelf')) {
118 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
121 $this->setPageTitle($book->getShortName());
122 return view('books.show', [
125 'bookChildren' => $bookChildren,
126 'bookParentShelves' => $bookParentShelves,
127 'activity' => Activity::entityActivity($book, 20, 1)
132 * Show the form for editing the specified book.
134 public function edit(string $slug)
136 $book = $this->bookRepo->getBySlug($slug);
137 $this->checkOwnablePermission('book-update', $book);
138 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
139 return view('books.edit', ['book' => $book, 'current' => $book]);
143 * Update the specified book in storage.
144 * @throws ImageUploadException
145 * @throws ValidationException
148 public function update(Request $request, string $slug)
150 $book = $this->bookRepo->getBySlug($slug);
151 $this->checkOwnablePermission('book-update', $book);
152 $this->validate($request, [
153 'name' => 'required|string|max:255',
154 'description' => 'string|max:1000',
155 'image' => 'nullable|' . $this->getImageValidationRules(),
158 $book = $this->bookRepo->update($book, $request->all());
159 $resetCover = $request->has('image_reset');
160 $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
162 return redirect($book->getUrl());
166 * Shows the page to confirm deletion.
168 public function showDelete(string $bookSlug)
170 $book = $this->bookRepo->getBySlug($bookSlug);
171 $this->checkOwnablePermission('book-delete', $book);
172 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
173 return view('books.delete', ['book' => $book, 'current' => $book]);
177 * Remove the specified book from the system.
180 public function destroy(string $bookSlug)
182 $book = $this->bookRepo->getBySlug($bookSlug);
183 $this->checkOwnablePermission('book-delete', $book);
185 $this->bookRepo->destroy($book);
187 return redirect('/books');
191 * Show the permissions view.
193 public function showPermissions(string $bookSlug)
195 $book = $this->bookRepo->getBySlug($bookSlug);
196 $this->checkOwnablePermission('restrictions-manage', $book);
198 return view('books.permissions', [
204 * Set the restrictions for this book.
207 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
209 $book = $this->bookRepo->getBySlug($bookSlug);
210 $this->checkOwnablePermission('restrictions-manage', $book);
212 $permissionsUpdater->updateFromPermissionsForm($book, $request);
214 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
215 return redirect($book->getUrl());