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;
16 class BookController extends Controller
20 protected $entityContextManager;
22 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
24 $this->bookRepo = $bookRepo;
25 $this->entityContextManager = $entityContextManager;
29 * Display a listing of the book.
31 public function index()
33 $view = setting()->getForCurrentUser('books_view_type');
34 $sort = setting()->getForCurrentUser('books_sort', 'name');
35 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
37 $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
38 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
39 $popular = $this->bookRepo->getPopular(4);
40 $new = $this->bookRepo->getRecentlyCreated(4);
42 $this->entityContextManager->clearShelfContext();
44 $this->setPageTitle(trans('entities.books'));
45 return view('books.index', [
47 'recents' => $recents,
48 'popular' => $popular,
57 * Show the form for creating a new book.
59 public function create(string $shelfSlug = null)
61 $this->checkPermission('book-create-all');
64 if ($shelfSlug !== null) {
65 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
66 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
69 $this->setPageTitle(trans('entities.books_create'));
70 return view('books.create', [
71 'bookshelf' => $bookshelf
76 * Store a newly created book in storage.
77 * @throws ImageUploadException
78 * @throws ValidationException
80 public function store(Request $request, string $shelfSlug = null)
82 $this->checkPermission('book-create-all');
83 $this->validate($request, [
84 'name' => 'required|string|max:255',
85 'description' => 'string|max:1000',
86 'image' => 'nullable|' . $this->getImageValidationRules(),
90 if ($shelfSlug !== null) {
91 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
92 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
95 $book = $this->bookRepo->create($request->all());
96 $this->bookRepo->updateCoverImage($book, $request->file('image', null));
99 $bookshelf->appendBook($book);
100 Activity::addForEntity($bookshelf, ActivityType::BOOKSHELF_UPDATE);
103 return redirect($book->getUrl());
107 * Display the specified book.
109 public function show(Request $request, string $slug)
111 $book = $this->bookRepo->getBySlug($slug);
112 $bookChildren = (new BookContents($book))->getTree(true);
113 $bookParentShelves = $book->shelves()->visible()->get();
115 View::incrementFor($book);
116 if ($request->has('shelf')) {
117 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
120 $this->setPageTitle($book->getShortName());
121 return view('books.show', [
124 'bookChildren' => $bookChildren,
125 'bookParentShelves' => $bookParentShelves,
126 'activity' => Activity::entityActivity($book, 20, 1)
131 * Show the form for editing the specified book.
133 public function edit(string $slug)
135 $book = $this->bookRepo->getBySlug($slug);
136 $this->checkOwnablePermission('book-update', $book);
137 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
138 return view('books.edit', ['book' => $book, 'current' => $book]);
142 * Update the specified book in storage.
143 * @throws ImageUploadException
144 * @throws ValidationException
147 public function update(Request $request, string $slug)
149 $book = $this->bookRepo->getBySlug($slug);
150 $this->checkOwnablePermission('book-update', $book);
151 $this->validate($request, [
152 'name' => 'required|string|max:255',
153 'description' => 'string|max:1000',
154 'image' => 'nullable|' . $this->getImageValidationRules(),
157 $book = $this->bookRepo->update($book, $request->all());
158 $resetCover = $request->has('image_reset');
159 $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
161 return redirect($book->getUrl());
165 * Shows the page to confirm deletion.
167 public function showDelete(string $bookSlug)
169 $book = $this->bookRepo->getBySlug($bookSlug);
170 $this->checkOwnablePermission('book-delete', $book);
171 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
172 return view('books.delete', ['book' => $book, 'current' => $book]);
176 * Remove the specified book from the system.
179 public function destroy(string $bookSlug)
181 $book = $this->bookRepo->getBySlug($bookSlug);
182 $this->checkOwnablePermission('book-delete', $book);
184 $this->bookRepo->destroy($book);
186 return redirect('/books');
190 * Show the permissions view.
192 public function showPermissions(string $bookSlug)
194 $book = $this->bookRepo->getBySlug($bookSlug);
195 $this->checkOwnablePermission('restrictions-manage', $book);
197 return view('books.permissions', [
203 * Set the restrictions for this book.
206 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
208 $book = $this->bookRepo->getBySlug($bookSlug);
209 $this->checkOwnablePermission('restrictions-manage', $book);
211 $permissionsUpdater->updateFromPermissionsForm($book, $request);
213 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
214 return redirect($book->getUrl());