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 $entityContextManager;
23 * BookController constructor.
24 * @param EntityRepo $entityRepo
25 * @param UserRepo $userRepo
26 * @param EntityContextManager $entityContextManager
27 * @param ImageRepo $imageRepo
29 public function __construct(
30 EntityRepo $entityRepo,
32 EntityContextManager $entityContextManager,
35 $this->entityRepo = $entityRepo;
36 $this->userRepo = $userRepo;
37 $this->entityContextManager = $entityContextManager;
38 $this->imageRepo = $imageRepo;
39 parent::__construct();
43 * Display a listing of the book.
46 public function index()
48 $view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
49 $sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
50 $order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
52 'name' => trans('common.sort_name'),
53 'created_at' => trans('common.sort_created_at'),
54 'updated_at' => trans('common.sort_updated_at'),
57 $books = $this->entityRepo->getAllPaginated('book', 18, $sort, $order);
58 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
59 $popular = $this->entityRepo->getPopular('book', 4, 0);
60 $new = $this->entityRepo->getRecentlyCreated('book', 4, 0);
62 $this->entityContextManager->clearShelfContext();
64 $this->setPageTitle(trans('entities.books'));
65 return view('books.index', [
67 'recents' => $recents,
68 'popular' => $popular,
73 'sortOptions' => $sortOptions,
78 * Show the form for creating a new book.
79 * @param string $shelfSlug
81 * @throws \BookStack\Exceptions\NotFoundException
83 public function create(string $shelfSlug = null)
86 if ($shelfSlug !== null) {
87 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
88 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
91 $this->checkPermission('book-create-all');
92 $this->setPageTitle(trans('entities.books_create'));
93 return view('books.create', [
94 'bookshelf' => $bookshelf
99 * Store a newly created book in storage.
101 * @param Request $request
102 * @param string $shelfSlug
104 * @throws \BookStack\Exceptions\NotFoundException
105 * @throws \BookStack\Exceptions\ImageUploadException
107 public function store(Request $request, string $shelfSlug = null)
109 $this->checkPermission('book-create-all');
110 $this->validate($request, [
111 'name' => 'required|string|max:255',
112 'description' => 'string|max:1000',
113 'image' => $this->imageRepo->getImageValidationRules(),
117 if ($shelfSlug !== null) {
118 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
119 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
122 $book = $this->entityRepo->createFromInput('book', $request->all());
123 $this->bookUpdateActions($book, $request);
124 Activity::add($book, 'book_create', $book->id);
127 $this->entityRepo->appendBookToShelf($bookshelf, $book);
128 Activity::add($bookshelf, 'bookshelf_update');
131 return redirect($book->getUrl());
135 * Display the specified book.
136 * @param Request $request
137 * @param string $slug
139 * @throws \BookStack\Exceptions\NotFoundException
141 public function show(Request $request, string $slug)
143 $book = $this->entityRepo->getBySlug('book', $slug);
144 $this->checkOwnablePermission('book-view', $book);
146 $bookChildren = $this->entityRepo->getBookChildren($book);
149 if ($request->has('shelf')) {
150 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
153 $this->setPageTitle($book->getShortName());
154 return view('books.show', [
157 'bookChildren' => $bookChildren,
158 'activity' => Activity::entityActivity($book, 20, 1)
163 * Show the form for editing the specified book.
167 public function edit($slug)
169 $book = $this->entityRepo->getBySlug('book', $slug);
170 $this->checkOwnablePermission('book-update', $book);
171 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
172 return view('books.edit', ['book' => $book, 'current' => $book]);
176 * Update the specified book in storage.
177 * @param Request $request
180 * @throws \BookStack\Exceptions\ImageUploadException
181 * @throws \BookStack\Exceptions\NotFoundException
183 public function update(Request $request, string $slug)
185 $book = $this->entityRepo->getBySlug('book', $slug);
186 $this->checkOwnablePermission('book-update', $book);
187 $this->validate($request, [
188 'name' => 'required|string|max:255',
189 'description' => 'string|max:1000',
190 'image' => $this->imageRepo->getImageValidationRules(),
193 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
194 $this->bookUpdateActions($book, $request);
196 Activity::add($book, 'book_update', $book->id);
198 return redirect($book->getUrl());
202 * Shows the page to confirm deletion
204 * @return \Illuminate\View\View
206 public function showDelete($bookSlug)
208 $book = $this->entityRepo->getBySlug('book', $bookSlug);
209 $this->checkOwnablePermission('book-delete', $book);
210 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
211 return view('books.delete', ['book' => $book, 'current' => $book]);
215 * Shows the view which allows pages to be re-ordered and sorted.
216 * @param string $bookSlug
217 * @return \Illuminate\View\View
218 * @throws \BookStack\Exceptions\NotFoundException
220 public function sort($bookSlug)
222 $book = $this->entityRepo->getBySlug('book', $bookSlug);
223 $this->checkOwnablePermission('book-update', $book);
225 $bookChildren = $this->entityRepo->getBookChildren($book, true);
227 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
228 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
232 * Shows the sort box for a single book.
233 * Used via AJAX when loading in extra books to a sort.
235 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
237 public function getSortItem($bookSlug)
239 $book = $this->entityRepo->getBySlug('book', $bookSlug);
240 $bookChildren = $this->entityRepo->getBookChildren($book);
241 return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
245 * Saves an array of sort mapping to pages and chapters.
246 * @param Request $request
247 * @param string $bookSlug
248 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
249 * @throws \BookStack\Exceptions\NotFoundException
251 public function saveSort(Request $request, string $bookSlug)
253 $book = $this->entityRepo->getBySlug('book', $bookSlug);
254 $this->checkOwnablePermission('book-update', $book);
256 // Return if no map sent
257 if (!$request->filled('sort-tree')) {
258 return redirect($book->getUrl());
261 // Sort pages and chapters
262 $sortMap = collect(json_decode($request->get('sort-tree')));
263 $bookIdsInvolved = collect([$book->id]);
265 // Load models into map
266 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
267 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
268 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
269 // Store source and target books
270 $bookIdsInvolved->push(intval($mapItem->model->book_id));
271 $bookIdsInvolved->push(intval($mapItem->book));
274 // Get the books involved in the sort
275 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
276 $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
277 // Throw permission error if invalid ids or inaccessible books given.
278 if (count($bookIdsInvolved) !== count($booksInvolved)) {
279 $this->showPermissionError();
281 // Check permissions of involved books
282 $booksInvolved->each(function (Book $book) {
283 $this->checkOwnablePermission('book-update', $book);
287 $sortMap->each(function ($mapItem) {
288 $model = $mapItem->model;
290 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
291 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
292 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
295 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
297 if ($chapterChanged) {
298 $model->chapter_id = intval($mapItem->parentChapter);
301 if ($priorityChanged) {
302 $model->priority = intval($mapItem->sort);
307 // Rebuild permissions and add activity for involved books.
308 $booksInvolved->each(function (Book $book) {
309 $this->entityRepo->buildJointPermissionsForBook($book);
310 Activity::add($book, 'book_sort', $book->id);
313 return redirect($book->getUrl());
317 * Remove the specified book from storage.
321 public function destroy($bookSlug)
323 $book = $this->entityRepo->getBySlug('book', $bookSlug);
324 $this->checkOwnablePermission('book-delete', $book);
325 Activity::addMessage('book_delete', 0, $book->name);
328 $this->imageRepo->destroyImage($book->cover);
330 $this->entityRepo->destroyBook($book);
332 return redirect('/books');
336 * Show the Restrictions view.
338 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
340 public function showPermissions($bookSlug)
342 $book = $this->entityRepo->getBySlug('book', $bookSlug);
343 $this->checkOwnablePermission('restrictions-manage', $book);
344 $roles = $this->userRepo->getRestrictableRoles();
345 return view('books.permissions', [
352 * Set the restrictions for this book.
353 * @param Request $request
354 * @param string $bookSlug
355 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
356 * @throws \BookStack\Exceptions\NotFoundException
359 public function permissions(Request $request, string $bookSlug)
361 $book = $this->entityRepo->getBySlug('book', $bookSlug);
362 $this->checkOwnablePermission('restrictions-manage', $book);
363 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
364 session()->flash('success', trans('entities.books_permissions_updated'));
365 return redirect($book->getUrl());
369 * Common actions to run on book update.
370 * Handles updating the cover image.
372 * @param Request $request
373 * @throws \BookStack\Exceptions\ImageUploadException
375 protected function bookUpdateActions(Book $book, Request $request)
377 // Update the cover image if in request
378 if ($request->has('image')) {
379 $this->imageRepo->destroyImage($book->cover);
380 $newImage = $request->file('image');
381 $image = $this->imageRepo->saveNew($newImage, 'cover_book', $book->id, 512, 512, true);
382 $book->image_id = $image->id;
386 if ($request->has('image_reset')) {
387 $this->imageRepo->destroyImage($book->cover);