1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\Bookshelf;
7 use BookStack\Entities\EntityContextManager;
8 use BookStack\Entities\Repos\BookRepo;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Exceptions\NotFoundException;
11 use BookStack\Exceptions\NotifyException;
12 use BookStack\Uploads\ImageRepo;
13 use Illuminate\Contracts\View\Factory;
14 use Illuminate\Http\RedirectResponse;
15 use Illuminate\Http\Request;
16 use Illuminate\Http\Response;
17 use Illuminate\Routing\Redirector;
18 use Illuminate\Validation\ValidationException;
19 use Illuminate\View\View;
23 class BookController extends Controller
28 protected $entityContextManager;
32 * BookController constructor.
33 * @param BookRepo $bookRepo
34 * @param UserRepo $userRepo
35 * @param EntityContextManager $entityContextManager
36 * @param ImageRepo $imageRepo
38 public function __construct(
41 EntityContextManager $entityContextManager,
44 $this->bookRepo = $bookRepo;
45 $this->userRepo = $userRepo;
46 $this->entityContextManager = $entityContextManager;
47 $this->imageRepo = $imageRepo;
48 parent::__construct();
52 * Display a listing of the book.
55 public function index()
57 $view = setting()->getForCurrentUser('books_view_type', config('app.views.books'));
58 $sort = setting()->getForCurrentUser('books_sort', 'name');
59 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
61 $books = $this->bookRepo->getAllPaginated('book', 18, $sort, $order);
62 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false;
63 $popular = $this->bookRepo->getPopular('book', 4, 0);
64 $new = $this->bookRepo->getRecentlyCreated('book', 4, 0);
66 $this->entityContextManager->clearShelfContext();
68 $this->setPageTitle(trans('entities.books'));
69 return view('books.index', [
71 'recents' => $recents,
72 'popular' => $popular,
81 * Show the form for creating a new book.
82 * @param string $shelfSlug
84 * @throws NotFoundException
86 public function create(string $shelfSlug = null)
89 if ($shelfSlug !== null) {
90 $bookshelf = $this->bookRepo->getEntityBySlug('bookshelf', $shelfSlug);
91 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
94 $this->checkPermission('book-create-all');
95 $this->setPageTitle(trans('entities.books_create'));
96 return view('books.create', [
97 'bookshelf' => $bookshelf
102 * Store a newly created book in storage.
104 * @param Request $request
105 * @param string $shelfSlug
107 * @throws NotFoundException
108 * @throws ImageUploadException
109 * @throws ValidationException
111 public function store(Request $request, string $shelfSlug = null)
113 $this->checkPermission('book-create-all');
114 $this->validate($request, [
115 'name' => 'required|string|max:255',
116 'description' => 'string|max:1000',
117 'image' => $this->imageRepo->getImageValidationRules(),
121 if ($shelfSlug !== null) {
122 /** @var Bookshelf $bookshelf */
123 $bookshelf = $this->bookRepo->getEntityBySlug('bookshelf', $shelfSlug);
124 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
127 /** @var Book $book */
128 $book = $this->bookRepo->createFromInput('book', $request->all());
129 $this->bookUpdateActions($book, $request);
130 Activity::add($book, 'book_create', $book->id);
133 $bookshelf->appendBook($book);
134 Activity::add($bookshelf, 'bookshelf_update');
137 return redirect($book->getUrl());
141 * Display the specified book.
142 * @param Request $request
143 * @param string $slug
145 * @throws NotFoundException
147 public function show(Request $request, string $slug)
149 $book = $this->bookRepo->getBySlug($slug);
150 $this->checkOwnablePermission('book-view', $book);
152 $bookChildren = $this->bookRepo->getBookChildren($book);
155 if ($request->has('shelf')) {
156 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
159 $this->setPageTitle($book->getShortName());
160 return view('books.show', [
163 'bookChildren' => $bookChildren,
164 'activity' => Activity::entityActivity($book, 20, 1)
169 * Show the form for editing the specified book.
170 * @param string $slug
172 * @throws NotFoundException
174 public function edit(string $slug)
176 $book = $this->bookRepo->getBySlug($slug);
177 $this->checkOwnablePermission('book-update', $book);
178 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
179 return view('books.edit', ['book' => $book, 'current' => $book]);
183 * Update the specified book in storage.
184 * @param Request $request
185 * @param string $slug
187 * @throws ImageUploadException
188 * @throws NotFoundException
189 * @throws ValidationException
192 public function update(Request $request, string $slug)
194 $book = $this->bookRepo->getBySlug($slug);
195 $this->checkOwnablePermission('book-update', $book);
196 $this->validate($request, [
197 'name' => 'required|string|max:255',
198 'description' => 'string|max:1000',
199 'image' => $this->imageRepo->getImageValidationRules(),
202 $book = $this->bookRepo->updateFromInput($book, $request->all());
203 $this->bookUpdateActions($book, $request);
205 Activity::add($book, 'book_update', $book->id);
207 return redirect($book->getUrl());
211 * Shows the page to confirm deletion
212 * @param string $bookSlug
214 * @throws NotFoundException
216 public function showDelete(string $bookSlug)
218 $book = $this->bookRepo->getBySlug($bookSlug);
219 $this->checkOwnablePermission('book-delete', $book);
220 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
221 return view('books.delete', ['book' => $book, 'current' => $book]);
225 * Shows the view which allows pages to be re-ordered and sorted.
226 * @param string $bookSlug
228 * @throws NotFoundException
230 public function sort(string $bookSlug)
232 $book = $this->bookRepo->getBySlug($bookSlug);
233 $this->checkOwnablePermission('book-update', $book);
235 $bookChildren = $this->bookRepo->getBookChildren($book, true);
237 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
238 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
242 * Shows the sort box for a single book.
243 * Used via AJAX when loading in extra books to a sort.
244 * @param string $bookSlug
245 * @return Factory|View
246 * @throws NotFoundException
248 public function sortItem(string $bookSlug)
250 $book = $this->bookRepo->getBySlug($bookSlug);
251 $bookChildren = $this->bookRepo->getBookChildren($book);
252 return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
256 * Saves an array of sort mapping to pages and chapters.
257 * @param Request $request
258 * @param string $bookSlug
259 * @return RedirectResponse|Redirector
260 * @throws NotFoundException
262 public function saveSort(Request $request, string $bookSlug)
264 $book = $this->bookRepo->getBySlug($bookSlug);
265 $this->checkOwnablePermission('book-update', $book);
267 // Return if no map sent
268 if (!$request->filled('sort-tree')) {
269 return redirect($book->getUrl());
272 // Sort pages and chapters
273 $sortMap = collect(json_decode($request->get('sort-tree')));
274 $bookIdsInvolved = collect([$book->id]);
276 // Load models into map
277 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
278 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
279 $mapItem->model = $this->bookRepo->getById($mapItem->type, $mapItem->id);
280 // Store source and target books
281 $bookIdsInvolved->push(intval($mapItem->model->book_id));
282 $bookIdsInvolved->push(intval($mapItem->book));
285 // Get the books involved in the sort
286 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
287 $booksInvolved = $this->bookRepo->getManyById('book', $bookIdsInvolved, false, true);
289 // Throw permission error if invalid ids or inaccessible books given.
290 if (count($bookIdsInvolved) !== count($booksInvolved)) {
291 $this->showPermissionError();
294 // Check permissions of involved books
295 $booksInvolved->each(function (Book $book) {
296 $this->checkOwnablePermission('book-update', $book);
300 $sortMap->each(function ($mapItem) {
301 $model = $mapItem->model;
303 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
304 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
305 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
308 $this->bookRepo->changeBook($model, $mapItem->book);
310 if ($chapterChanged) {
311 $model->chapter_id = intval($mapItem->parentChapter);
314 if ($priorityChanged) {
315 $model->priority = intval($mapItem->sort);
320 // Rebuild permissions and add activity for involved books.
321 $booksInvolved->each(function (Book $book) {
322 $book->rebuildPermissions();
323 Activity::add($book, 'book_sort', $book->id);
326 return redirect($book->getUrl());
330 * Remove the specified book from storage.
331 * @param string $bookSlug
333 * @throws NotFoundException
335 * @throws NotifyException
337 public function destroy(string $bookSlug)
339 $book = $this->bookRepo->getBySlug($bookSlug);
340 $this->checkOwnablePermission('book-delete', $book);
341 Activity::addMessage('book_delete', $book->name);
344 $this->imageRepo->destroyImage($book->cover);
346 $this->bookRepo->destroyBook($book);
348 return redirect('/books');
352 * Show the Restrictions view.
353 * @param string $bookSlug
354 * @return Factory|View
355 * @throws NotFoundException
357 public function showPermissions(string $bookSlug)
359 $book = $this->bookRepo->getBySlug($bookSlug);
360 $this->checkOwnablePermission('restrictions-manage', $book);
361 $roles = $this->userRepo->getRestrictableRoles();
362 return view('books.permissions', [
369 * Set the restrictions for this book.
370 * @param Request $request
371 * @param string $bookSlug
372 * @return RedirectResponse|Redirector
373 * @throws NotFoundException
376 public function permissions(Request $request, string $bookSlug)
378 $book = $this->bookRepo->getBySlug($bookSlug);
379 $this->checkOwnablePermission('restrictions-manage', $book);
380 $this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
381 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
382 return redirect($book->getUrl());
386 * Common actions to run on book update.
387 * Handles updating the cover image.
389 * @param Request $request
390 * @throws ImageUploadException
392 protected function bookUpdateActions(Book $book, Request $request)
394 // Update the cover image if in request
395 if ($request->has('image')) {
396 $this->imageRepo->destroyImage($book->cover);
397 $newImage = $request->file('image');
398 $image = $this->imageRepo->saveNew($newImage, 'cover_book', $book->id, 512, 512, true);
399 $book->image_id = $image->id;
403 if ($request->has('image_reset')) {
404 $this->imageRepo->destroyImage($book->cover);