3 namespace BookStack\Http\Controllers;
6 use BookStack\Actions\ActivityQueries;
7 use BookStack\Actions\ActivityType;
8 use BookStack\Actions\View;
9 use BookStack\Entities\Models\Bookshelf;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Entities\Tools\BookContents;
12 use BookStack\Entities\Tools\PermissionsUpdater;
13 use BookStack\Entities\Tools\ShelfContext;
14 use BookStack\Exceptions\ImageUploadException;
15 use Illuminate\Http\Request;
16 use Illuminate\Validation\ValidationException;
19 class BookController extends Controller
22 protected $entityContextManager;
24 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
26 $this->bookRepo = $bookRepo;
27 $this->entityContextManager = $entityContextManager;
31 * Display a listing of the book.
33 public function index()
35 $view = setting()->getForCurrentUser('books_view_type');
36 $sort = setting()->getForCurrentUser('books_sort', 'name');
37 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
39 $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
40 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
41 $popular = $this->bookRepo->getPopular(4);
42 $new = $this->bookRepo->getRecentlyCreated(4);
44 $this->entityContextManager->clearShelfContext();
46 $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'));
74 return view('books.create', [
75 'bookshelf' => $bookshelf,
80 * Store a newly created book in storage.
82 * @throws ImageUploadException
83 * @throws ValidationException
85 public function store(Request $request, string $shelfSlug = null)
87 $this->checkPermission('book-create-all');
88 $this->validate($request, [
89 'name' => ['required', 'string', 'max:255'],
90 'description' => ['string', 'max:1000'],
91 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
95 if ($shelfSlug !== null) {
96 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
97 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
100 $book = $this->bookRepo->create($request->all());
101 $this->bookRepo->updateCoverImage($book, $request->file('image', null));
104 $bookshelf->appendBook($book);
105 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
108 return redirect($book->getUrl());
112 * Display the specified book.
114 public function show(Request $request, ActivityQueries $activities, string $slug)
116 $book = $this->bookRepo->getBySlug($slug);
117 $bookChildren = (new BookContents($book))->getTree(true);
118 $bookParentShelves = $book->shelves()->scopes('visible')->get();
120 View::incrementFor($book);
121 if ($request->has('shelf')) {
122 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
125 $this->setPageTitle($book->getShortName());
127 return view('books.show', [
130 'bookChildren' => $bookChildren,
131 'bookParentShelves' => $bookParentShelves,
132 'activity' => $activities->entityActivity($book, 20, 1),
137 * Show the form for editing the specified book.
139 public function edit(string $slug)
141 $book = $this->bookRepo->getBySlug($slug);
142 $this->checkOwnablePermission('book-update', $book);
143 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
145 return view('books.edit', ['book' => $book, 'current' => $book]);
149 * Update the specified book in storage.
151 * @throws ImageUploadException
152 * @throws ValidationException
155 public function update(Request $request, string $slug)
157 $book = $this->bookRepo->getBySlug($slug);
158 $this->checkOwnablePermission('book-update', $book);
159 $this->validate($request, [
160 'name' => ['required', 'string', 'max:255'],
161 'description' => ['string', 'max:1000'],
162 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
165 $book = $this->bookRepo->update($book, $request->all());
166 $resetCover = $request->has('image_reset');
167 $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
169 return redirect($book->getUrl());
173 * Shows the page to confirm deletion.
175 public function showDelete(string $bookSlug)
177 $book = $this->bookRepo->getBySlug($bookSlug);
178 $this->checkOwnablePermission('book-delete', $book);
179 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
181 return view('books.delete', ['book' => $book, 'current' => $book]);
185 * Remove the specified book from the system.
189 public function destroy(string $bookSlug)
191 $book = $this->bookRepo->getBySlug($bookSlug);
192 $this->checkOwnablePermission('book-delete', $book);
194 $this->bookRepo->destroy($book);
196 return redirect('/books');
200 * Show the permissions view.
202 public function showPermissions(string $bookSlug)
204 $book = $this->bookRepo->getBySlug($bookSlug);
205 $this->checkOwnablePermission('restrictions-manage', $book);
207 return view('books.permissions', [
213 * Set the restrictions for this book.
217 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
219 $book = $this->bookRepo->getBySlug($bookSlug);
220 $this->checkOwnablePermission('restrictions-manage', $book);
222 $permissionsUpdater->updateFromPermissionsForm($book, $request);
224 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
226 return redirect($book->getUrl());