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);
119 if ($request->has('shelf')) {
120 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
123 $this->setPageTitle($book->getShortName());
124 return view('books.show', [
127 'bookChildren' => $bookChildren,
128 'activity' => Activity::entityActivity($book, 20, 1)
133 * Show the form for editing the specified book.
135 public function edit(string $slug)
137 $book = $this->bookRepo->getBySlug($slug);
138 $this->checkOwnablePermission('book-update', $book);
139 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
140 return view('books.edit', ['book' => $book, 'current' => $book]);
144 * Update the specified book in storage.
145 * @throws ImageUploadException
146 * @throws ValidationException
149 public function update(Request $request, string $slug)
151 $book = $this->bookRepo->getBySlug($slug);
152 $this->checkOwnablePermission('book-update', $book);
153 $this->validate($request, [
154 'name' => 'required|string|max:255',
155 'description' => 'string|max:1000',
156 'image' => 'nullable|' . $this->getImageValidationRules(),
159 $book = $this->bookRepo->update($book, $request->all());
160 $resetCover = $request->has('image_reset');
161 $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
163 Activity::add($book, 'book_update', $book->id);
165 return redirect($book->getUrl());
169 * Shows the page to confirm deletion.
171 public function showDelete(string $bookSlug)
173 $book = $this->bookRepo->getBySlug($bookSlug);
174 $this->checkOwnablePermission('book-delete', $book);
175 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
176 return view('books.delete', ['book' => $book, 'current' => $book]);
180 * Remove the specified book from the system.
182 * @throws NotifyException
184 public function destroy(string $bookSlug)
186 $book = $this->bookRepo->getBySlug($bookSlug);
187 $this->checkOwnablePermission('book-delete', $book);
189 Activity::addMessage('book_delete', $book->name);
190 $this->bookRepo->destroy($book);
192 return redirect('/books');
196 * Show the permissions view.
198 public function showPermissions(string $bookSlug)
200 $book = $this->bookRepo->getBySlug($bookSlug);
201 $this->checkOwnablePermission('restrictions-manage', $book);
203 return view('books.permissions', [
209 * Set the restrictions for this book.
212 public function permissions(Request $request, string $bookSlug)
214 $book = $this->bookRepo->getBySlug($bookSlug);
215 $this->checkOwnablePermission('restrictions-manage', $book);
217 $restricted = $request->get('restricted') === 'true';
218 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
219 $this->bookRepo->updatePermissions($book, $restricted, $permissions);
221 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
222 return redirect($book->getUrl());