3 namespace BookStack\Http\Controllers;
6 use BookStack\Actions\ActivityType;
7 use BookStack\Actions\View;
8 use BookStack\Entities\Models\Bookshelf;
9 use BookStack\Entities\Repos\BookRepo;
10 use BookStack\Entities\Tools\BookContents;
11 use BookStack\Entities\Tools\PermissionsUpdater;
12 use BookStack\Entities\Tools\ShelfContext;
13 use BookStack\Exceptions\ImageUploadException;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
18 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'));
47 return view('books.index', [
49 'recents' => $recents,
50 'popular' => $popular,
59 * Show the form for creating a new book.
61 public function create(string $shelfSlug = null)
63 $this->checkPermission('book-create-all');
66 if ($shelfSlug !== null) {
67 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
68 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
71 $this->setPageTitle(trans('entities.books_create'));
73 return view('books.create', [
74 'bookshelf' => $bookshelf,
79 * Store a newly created book in storage.
81 * @throws ImageUploadException
82 * @throws ValidationException
84 public function store(Request $request, string $shelfSlug = null)
86 $this->checkPermission('book-create-all');
87 $this->validate($request, [
88 'name' => ['required', 'string', 'max:255'],
89 'description' => ['string', 'max:1000'],
90 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
94 if ($shelfSlug !== null) {
95 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
96 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
99 $book = $this->bookRepo->create($request->all());
100 $this->bookRepo->updateCoverImage($book, $request->file('image', null));
103 $bookshelf->appendBook($book);
104 Activity::addForEntity($bookshelf, ActivityType::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()->scopes('visible')->get();
119 View::incrementFor($book);
120 if ($request->has('shelf')) {
121 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
124 $this->setPageTitle($book->getShortName());
126 return view('books.show', [
129 'bookChildren' => $bookChildren,
130 'bookParentShelves' => $bookParentShelves,
131 'activity' => Activity::entityActivity($book, 20, 1),
136 * Show the form for editing the specified book.
138 public function edit(string $slug)
140 $book = $this->bookRepo->getBySlug($slug);
141 $this->checkOwnablePermission('book-update', $book);
142 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
144 return view('books.edit', ['book' => $book, 'current' => $book]);
148 * Update the specified book in storage.
150 * @throws ImageUploadException
151 * @throws ValidationException
154 public function update(Request $request, string $slug)
156 $book = $this->bookRepo->getBySlug($slug);
157 $this->checkOwnablePermission('book-update', $book);
158 $this->validate($request, [
159 'name' => ['required', 'string', 'max:255'],
160 'description' => ['string', 'max:1000'],
161 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
164 $book = $this->bookRepo->update($book, $request->all());
165 $resetCover = $request->has('image_reset');
166 $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
168 return redirect($book->getUrl());
172 * Shows the page to confirm deletion.
174 public function showDelete(string $bookSlug)
176 $book = $this->bookRepo->getBySlug($bookSlug);
177 $this->checkOwnablePermission('book-delete', $book);
178 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
180 return view('books.delete', ['book' => $book, 'current' => $book]);
184 * Remove the specified book from the system.
188 public function destroy(string $bookSlug)
190 $book = $this->bookRepo->getBySlug($bookSlug);
191 $this->checkOwnablePermission('book-delete', $book);
193 $this->bookRepo->destroy($book);
195 return redirect('/books');
199 * Show the permissions view.
201 public function showPermissions(string $bookSlug)
203 $book = $this->bookRepo->getBySlug($bookSlug);
204 $this->checkOwnablePermission('restrictions-manage', $book);
206 return view('books.permissions', [
212 * Set the restrictions for this book.
216 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
218 $book = $this->bookRepo->getBySlug($bookSlug);
219 $this->checkOwnablePermission('restrictions-manage', $book);
221 $permissionsUpdater->updateFromPermissionsForm($book, $request);
223 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
225 return redirect($book->getUrl());