1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\EntityContextManager;
7 use BookStack\Entities\Repos\BookRepo;
8 use BookStack\Entities\Repos\EntityRepo;
9 use BookStack\Entities\ExportService;
10 use BookStack\Exceptions\ImageUploadException;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Exceptions\NotifyException;
13 use BookStack\Uploads\ImageRepo;
14 use Illuminate\Contracts\View\Factory;
15 use Illuminate\Http\RedirectResponse;
16 use Illuminate\Http\Request;
17 use Illuminate\Http\Response;
18 use Illuminate\Routing\Redirector;
19 use Illuminate\Validation\ValidationException;
20 use Illuminate\View\View;
24 class BookController extends Controller
29 protected $entityContextManager;
33 * BookController constructor.
34 * @param BookRepo $bookRepo
35 * @param UserRepo $userRepo
36 * @param EntityContextManager $entityContextManager
37 * @param ImageRepo $imageRepo
39 public function __construct(
42 EntityContextManager $entityContextManager,
45 $this->bookRepo = $bookRepo;
46 $this->userRepo = $userRepo;
47 $this->entityContextManager = $entityContextManager;
48 $this->imageRepo = $imageRepo;
49 parent::__construct();
53 * Display a listing of the book.
56 public function index()
58 $view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
59 $sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
60 $order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
62 $books = $this->bookRepo->getAllPaginated('book', 18, $sort, $order);
63 $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false;
64 $popular = $this->bookRepo->getPopular('book', 4, 0);
65 $new = $this->bookRepo->getRecentlyCreated('book', 4, 0);
67 $this->entityContextManager->clearShelfContext();
69 $this->setPageTitle(trans('entities.books'));
70 return view('books.index', [
72 'recents' => $recents,
73 'popular' => $popular,
82 * Show the form for creating a new book.
83 * @param string $shelfSlug
85 * @throws NotFoundException
87 public function create(string $shelfSlug = null)
90 if ($shelfSlug !== null) {
91 $bookshelf = $this->bookRepo->getEntityBySlug('bookshelf', $shelfSlug);
92 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
95 $this->checkPermission('book-create-all');
96 $this->setPageTitle(trans('entities.books_create'));
97 return view('books.create', [
98 'bookshelf' => $bookshelf
103 * Store a newly created book in storage.
105 * @param Request $request
106 * @param string $shelfSlug
108 * @throws NotFoundException
109 * @throws ImageUploadException
110 * @throws ValidationException
113 public function store(Request $request, string $shelfSlug = null)
115 $this->checkPermission('book-create-all');
116 $this->validate($request, [
117 'name' => 'required|string|max:255',
118 'description' => 'string|max:1000',
119 'image' => $this->imageRepo->getImageValidationRules(),
123 if ($shelfSlug !== null) {
124 $bookshelf = $this->bookRepo->getEntityBySlug('bookshelf', $shelfSlug);
125 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
128 $book = $this->bookRepo->createFromInput('book', $request->all());
129 $this->bookUpdateActions($book, $request);
130 Activity::add($book, 'book_create', $book->id);
133 $this->bookRepo->appendBookToShelf($bookshelf, $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', $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 getSortItem(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);
288 // Throw permission error if invalid ids or inaccessible books given.
289 if (count($bookIdsInvolved) !== count($booksInvolved)) {
290 $this->showPermissionError();
292 // Check permissions of involved books
293 $booksInvolved->each(function (Book $book) {
294 $this->checkOwnablePermission('book-update', $book);
298 $sortMap->each(function ($mapItem) {
299 $model = $mapItem->model;
301 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
302 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
303 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
306 $this->bookRepo->changeBook($mapItem->type, $mapItem->book, $model);
308 if ($chapterChanged) {
309 $model->chapter_id = intval($mapItem->parentChapter);
312 if ($priorityChanged) {
313 $model->priority = intval($mapItem->sort);
318 // Rebuild permissions and add activity for involved books.
319 $booksInvolved->each(function (Book $book) {
320 $this->bookRepo->buildJointPermissionsForBook($book);
321 Activity::add($book, 'book_sort', $book->id);
324 return redirect($book->getUrl());
328 * Remove the specified book from storage.
329 * @param string $bookSlug
331 * @throws NotFoundException
333 * @throws NotifyException
335 public function destroy(string $bookSlug)
337 $book = $this->bookRepo->getBySlug($bookSlug);
338 $this->checkOwnablePermission('book-delete', $book);
339 Activity::addMessage('book_delete', $book->name);
342 $this->imageRepo->destroyImage($book->cover);
344 $this->bookRepo->destroyBook($book);
346 return redirect('/books');
350 * Show the Restrictions view.
351 * @param string $bookSlug
352 * @return Factory|View
353 * @throws NotFoundException
355 public function showPermissions(string $bookSlug)
357 $book = $this->bookRepo->getBySlug($bookSlug);
358 $this->checkOwnablePermission('restrictions-manage', $book);
359 $roles = $this->userRepo->getRestrictableRoles();
360 return view('books.permissions', [
367 * Set the restrictions for this book.
368 * @param Request $request
369 * @param string $bookSlug
370 * @return RedirectResponse|Redirector
371 * @throws NotFoundException
374 public function permissions(Request $request, string $bookSlug)
376 $book = $this->bookRepo->getBySlug($bookSlug);
377 $this->checkOwnablePermission('restrictions-manage', $book);
378 $this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
379 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
380 return redirect($book->getUrl());
384 * Common actions to run on book update.
385 * Handles updating the cover image.
387 * @param Request $request
388 * @throws ImageUploadException
390 protected function bookUpdateActions(Book $book, Request $request)
392 // Update the cover image if in request
393 if ($request->has('image')) {
394 $this->imageRepo->destroyImage($book->cover);
395 $newImage = $request->file('image');
396 $image = $this->imageRepo->saveNew($newImage, 'cover_book', $book->id, 512, 512, true);
397 $book->image_id = $image->id;
401 if ($request->has('image_reset')) {
402 $this->imageRepo->destroyImage($book->cover);