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\EntityRepo;
8 use BookStack\Entities\ExportService;
9 use BookStack\Uploads\ImageRepo;
10 use Illuminate\Http\Request;
11 use Illuminate\Http\Response;
14 class BookController extends Controller
17 protected $entityRepo;
19 protected $exportService;
20 protected $entityContextManager;
24 * BookController constructor.
25 * @param EntityRepo $entityRepo
26 * @param UserRepo $userRepo
27 * @param ExportService $exportService
28 * @param EntityContextManager $entityContextManager
29 * @param ImageRepo $imageRepo
31 public function __construct(
32 EntityRepo $entityRepo,
34 ExportService $exportService,
35 EntityContextManager $entityContextManager,
38 $this->entityRepo = $entityRepo;
39 $this->userRepo = $userRepo;
40 $this->exportService = $exportService;
41 $this->entityContextManager = $entityContextManager;
42 $this->imageRepo = $imageRepo;
43 parent::__construct();
47 * Display a listing of the book.
50 public function index()
52 $view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
53 $sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
54 $order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
56 'name' => trans('common.sort_name'),
57 'created_at' => trans('common.sort_created_at'),
58 'updated_at' => trans('common.sort_updated_at'),
61 $books = $this->entityRepo->getAllPaginated('book', 18, $sort, $order);
62 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
63 $popular = $this->entityRepo->getPopular('book', 4, 0);
64 $new = $this->entityRepo->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,
77 'sortOptions' => $sortOptions,
82 * Show the form for creating a new book.
83 * @param string $shelfSlug
85 * @throws \BookStack\Exceptions\NotFoundException
87 public function create(string $shelfSlug = null)
90 if ($shelfSlug !== null) {
91 $bookshelf = $this->entityRepo->getBySlug('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 \BookStack\Exceptions\NotFoundException
109 * @throws \BookStack\Exceptions\ImageUploadException
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 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
123 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
126 $book = $this->entityRepo->createFromInput('book', $request->all());
127 $this->bookUpdateActions($book, $request);
128 Activity::add($book, 'book_create', $book->id);
131 $this->entityRepo->appendBookToShelf($bookshelf, $book);
132 Activity::add($bookshelf, 'bookshelf_update');
135 return redirect($book->getUrl());
139 * Display the specified book.
141 * @param Request $request
143 * @throws \BookStack\Exceptions\NotFoundException
145 public function show($slug, Request $request)
147 $book = $this->entityRepo->getBySlug('book', $slug);
148 $this->checkOwnablePermission('book-view', $book);
150 $bookChildren = $this->entityRepo->getBookChildren($book);
153 if ($request->has('shelf')) {
154 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
157 $this->setPageTitle($book->getShortName());
158 return view('books.show', [
161 'bookChildren' => $bookChildren,
162 'activity' => Activity::entityActivity($book, 20, 1)
167 * Show the form for editing the specified book.
171 public function edit($slug)
173 $book = $this->entityRepo->getBySlug('book', $slug);
174 $this->checkOwnablePermission('book-update', $book);
175 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
176 return view('books.edit', ['book' => $book, 'current' => $book]);
180 * Update the specified book in storage.
181 * @param Request $request
184 * @throws \BookStack\Exceptions\ImageUploadException
185 * @throws \BookStack\Exceptions\NotFoundException
187 public function update(Request $request, string $slug)
189 $book = $this->entityRepo->getBySlug('book', $slug);
190 $this->checkOwnablePermission('book-update', $book);
191 $this->validate($request, [
192 'name' => 'required|string|max:255',
193 'description' => 'string|max:1000',
194 'image' => $this->imageRepo->getImageValidationRules(),
197 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
198 $this->bookUpdateActions($book, $request);
200 Activity::add($book, 'book_update', $book->id);
202 return redirect($book->getUrl());
206 * Shows the page to confirm deletion
208 * @return \Illuminate\View\View
210 public function showDelete($bookSlug)
212 $book = $this->entityRepo->getBySlug('book', $bookSlug);
213 $this->checkOwnablePermission('book-delete', $book);
214 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
215 return view('books.delete', ['book' => $book, 'current' => $book]);
219 * Shows the view which allows pages to be re-ordered and sorted.
220 * @param string $bookSlug
221 * @return \Illuminate\View\View
222 * @throws \BookStack\Exceptions\NotFoundException
224 public function sort($bookSlug)
226 $book = $this->entityRepo->getBySlug('book', $bookSlug);
227 $this->checkOwnablePermission('book-update', $book);
229 $bookChildren = $this->entityRepo->getBookChildren($book, true);
231 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
232 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
236 * Shows the sort box for a single book.
237 * Used via AJAX when loading in extra books to a sort.
239 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
241 public function getSortItem($bookSlug)
243 $book = $this->entityRepo->getBySlug('book', $bookSlug);
244 $bookChildren = $this->entityRepo->getBookChildren($book);
245 return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
249 * Saves an array of sort mapping to pages and chapters.
250 * @param string $bookSlug
251 * @param Request $request
252 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
254 public function saveSort($bookSlug, Request $request)
256 $book = $this->entityRepo->getBySlug('book', $bookSlug);
257 $this->checkOwnablePermission('book-update', $book);
259 // Return if no map sent
260 if (!$request->filled('sort-tree')) {
261 return redirect($book->getUrl());
264 // Sort pages and chapters
265 $sortMap = collect(json_decode($request->get('sort-tree')));
266 $bookIdsInvolved = collect([$book->id]);
268 // Load models into map
269 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
270 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
271 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
272 // Store source and target books
273 $bookIdsInvolved->push(intval($mapItem->model->book_id));
274 $bookIdsInvolved->push(intval($mapItem->book));
277 // Get the books involved in the sort
278 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
279 $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
280 // Throw permission error if invalid ids or inaccessible books given.
281 if (count($bookIdsInvolved) !== count($booksInvolved)) {
282 $this->showPermissionError();
284 // Check permissions of involved books
285 $booksInvolved->each(function (Book $book) {
286 $this->checkOwnablePermission('book-update', $book);
290 $sortMap->each(function ($mapItem) {
291 $model = $mapItem->model;
293 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
294 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
295 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
298 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
300 if ($chapterChanged) {
301 $model->chapter_id = intval($mapItem->parentChapter);
304 if ($priorityChanged) {
305 $model->priority = intval($mapItem->sort);
310 // Rebuild permissions and add activity for involved books.
311 $booksInvolved->each(function (Book $book) {
312 $this->entityRepo->buildJointPermissionsForBook($book);
313 Activity::add($book, 'book_sort', $book->id);
316 return redirect($book->getUrl());
320 * Remove the specified book from storage.
324 public function destroy($bookSlug)
326 $book = $this->entityRepo->getBySlug('book', $bookSlug);
327 $this->checkOwnablePermission('book-delete', $book);
328 Activity::addMessage('book_delete', 0, $book->name);
331 $this->imageRepo->destroyImage($book->cover);
333 $this->entityRepo->destroyBook($book);
335 return redirect('/books');
339 * Show the Restrictions view.
341 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
343 public function showPermissions($bookSlug)
345 $book = $this->entityRepo->getBySlug('book', $bookSlug);
346 $this->checkOwnablePermission('restrictions-manage', $book);
347 $roles = $this->userRepo->getRestrictableRoles();
348 return view('books.permissions', [
355 * Set the restrictions for this book.
357 * @param Request $request
358 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
359 * @throws \BookStack\Exceptions\NotFoundException
362 public function permissions($bookSlug, Request $request)
364 $book = $this->entityRepo->getBySlug('book', $bookSlug);
365 $this->checkOwnablePermission('restrictions-manage', $book);
366 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
367 session()->flash('success', trans('entities.books_permissions_updated'));
368 return redirect($book->getUrl());
372 * Export a book as a PDF file.
373 * @param string $bookSlug
376 public function exportPdf($bookSlug)
378 $book = $this->entityRepo->getBySlug('book', $bookSlug);
379 $pdfContent = $this->exportService->bookToPdf($book);
380 return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
384 * Export a book as a contained HTML file.
385 * @param string $bookSlug
388 public function exportHtml($bookSlug)
390 $book = $this->entityRepo->getBySlug('book', $bookSlug);
391 $htmlContent = $this->exportService->bookToContainedHtml($book);
392 return $this->downloadResponse($htmlContent, $bookSlug . '.html');
396 * Export a book as a plain text file.
400 public function exportPlainText($bookSlug)
402 $book = $this->entityRepo->getBySlug('book', $bookSlug);
403 $textContent = $this->exportService->bookToPlainText($book);
404 return $this->downloadResponse($textContent, $bookSlug . '.txt');
408 * Common actions to run on book update.
409 * Handles updating the cover image.
411 * @param Request $request
412 * @throws \BookStack\Exceptions\ImageUploadException
414 protected function bookUpdateActions(Book $book, Request $request)
416 // Update the cover image if in request
417 if ($request->has('image')) {
418 $newImage = $request->file('image');
419 $image = $this->imageRepo->saveNew($newImage, 'cover_book', $book->id, 512, 512, true);
420 $book->image_id = $image->id;
424 if ($request->has('image_reset')) {
425 $this->imageRepo->destroyImage($book->cover);