1 <?php namespace BookStack\Http\Controllers;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Repos\UserRepo;
7 use BookStack\Services\ExportService;
8 use Illuminate\Http\Request;
9 use Illuminate\Http\Response;
12 class BookController extends Controller
15 protected $entityRepo;
17 protected $exportService;
20 * BookController constructor.
21 * @param EntityRepo $entityRepo
22 * @param UserRepo $userRepo
23 * @param ExportService $exportService
25 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
27 $this->entityRepo = $entityRepo;
28 $this->userRepo = $userRepo;
29 $this->exportService = $exportService;
30 parent::__construct();
34 * Display a listing of the book.
37 public function index()
39 $books = $this->entityRepo->getAllPaginated('book', 18);
40 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
41 $popular = $this->entityRepo->getPopular('book', 4, 0);
42 $new = $this->entityRepo->getRecentlyCreated('book', 4, 0);
43 $booksViewType = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books', 'list'));
44 $this->setPageTitle(trans('entities.books'));
45 return view('books/index', [
47 'recents' => $recents,
48 'popular' => $popular,
50 'booksViewType' => $booksViewType
55 * Show the form for creating a new book.
58 public function create()
60 $this->checkPermission('book-create-all');
61 $this->setPageTitle(trans('entities.books_create'));
62 return view('books/create');
66 * Store a newly created book in storage.
68 * @param Request $request
71 public function store(Request $request)
73 $this->checkPermission('book-create-all');
74 $this->validate($request, [
75 'name' => 'required|string|max:255',
76 'description' => 'string|max:1000'
78 $book = $this->entityRepo->createFromInput('book', $request->all());
79 Activity::add($book, 'book_create', $book->id);
80 return redirect($book->getUrl());
84 * Display the specified book.
88 public function show($slug)
90 $book = $this->entityRepo->getBySlug('book', $slug);
91 $this->checkOwnablePermission('book-view', $book);
92 $bookChildren = $this->entityRepo->getBookChildren($book);
94 $this->setPageTitle($book->getShortName());
95 return view('books/show', [
98 'bookChildren' => $bookChildren,
99 'activity' => Activity::entityActivity($book, 20, 0)
104 * Show the form for editing the specified book.
108 public function edit($slug)
110 $book = $this->entityRepo->getBySlug('book', $slug);
111 $this->checkOwnablePermission('book-update', $book);
112 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
113 return view('books/edit', ['book' => $book, 'current' => $book]);
117 * Update the specified book in storage.
118 * @param Request $request
122 public function update(Request $request, $slug)
124 $book = $this->entityRepo->getBySlug('book', $slug);
125 $this->checkOwnablePermission('book-update', $book);
126 $this->validate($request, [
127 'name' => 'required|string|max:255',
128 'description' => 'string|max:1000'
130 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
131 Activity::add($book, 'book_update', $book->id);
132 return redirect($book->getUrl());
136 * Shows the page to confirm deletion
138 * @return \Illuminate\View\View
140 public function showDelete($bookSlug)
142 $book = $this->entityRepo->getBySlug('book', $bookSlug);
143 $this->checkOwnablePermission('book-delete', $book);
144 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
145 return view('books/delete', ['book' => $book, 'current' => $book]);
149 * Shows the view which allows pages to be re-ordered and sorted.
150 * @param string $bookSlug
151 * @return \Illuminate\View\View
153 public function sort($bookSlug)
155 $book = $this->entityRepo->getBySlug('book', $bookSlug);
156 $this->checkOwnablePermission('book-update', $book);
157 $bookChildren = $this->entityRepo->getBookChildren($book, true);
158 $books = $this->entityRepo->getAll('book', false, 'update');
159 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
160 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
164 * Shows the sort box for a single book.
165 * Used via AJAX when loading in extra books to a sort.
167 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
169 public function getSortItem($bookSlug)
171 $book = $this->entityRepo->getBySlug('book', $bookSlug);
172 $bookChildren = $this->entityRepo->getBookChildren($book);
173 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
177 * Saves an array of sort mapping to pages and chapters.
178 * @param string $bookSlug
179 * @param Request $request
180 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
182 public function saveSort($bookSlug, Request $request)
184 $book = $this->entityRepo->getBySlug('book', $bookSlug);
185 $this->checkOwnablePermission('book-update', $book);
187 // Return if no map sent
188 if (!$request->filled('sort-tree')) {
189 return redirect($book->getUrl());
192 // Sort pages and chapters
193 $sortMap = collect(json_decode($request->get('sort-tree')));
194 $bookIdsInvolved = collect([$book->id]);
196 // Load models into map
197 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
198 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
199 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
200 // Store source and target books
201 $bookIdsInvolved->push(intval($mapItem->model->book_id));
202 $bookIdsInvolved->push(intval($mapItem->book));
205 // Get the books involved in the sort
206 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
207 $booksInvolved = $this->entityRepo->book->newQuery()->whereIn('id', $bookIdsInvolved)->get();
208 // Throw permission error if invalid ids or inaccessible books given.
209 if (count($bookIdsInvolved) !== count($booksInvolved)) {
210 $this->showPermissionError();
212 // Check permissions of involved books
213 $booksInvolved->each(function (Book $book) {
214 $this->checkOwnablePermission('book-update', $book);
218 $sortMap->each(function ($mapItem) {
219 $model = $mapItem->model;
221 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
222 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
223 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
226 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
228 if ($chapterChanged) {
229 $model->chapter_id = intval($mapItem->parentChapter);
232 if ($priorityChanged) {
233 $model->priority = intval($mapItem->sort);
238 // Rebuild permissions and add activity for involved books.
239 $booksInvolved->each(function (Book $book) {
240 $this->entityRepo->buildJointPermissionsForBook($book);
241 Activity::add($book, 'book_sort', $book->id);
244 return redirect($book->getUrl());
248 * Remove the specified book from storage.
252 public function destroy($bookSlug)
254 $book = $this->entityRepo->getBySlug('book', $bookSlug);
255 $this->checkOwnablePermission('book-delete', $book);
256 Activity::addMessage('book_delete', 0, $book->name);
257 $this->entityRepo->destroyBook($book);
258 return redirect('/books');
262 * Show the Restrictions view.
264 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
266 public function showRestrict($bookSlug)
268 $book = $this->entityRepo->getBySlug('book', $bookSlug);
269 $this->checkOwnablePermission('restrictions-manage', $book);
270 $roles = $this->userRepo->getRestrictableRoles();
271 return view('books/restrictions', [
278 * Set the restrictions for this book.
281 * @param Request $request
282 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
284 public function restrict($bookSlug, Request $request)
286 $book = $this->entityRepo->getBySlug('book', $bookSlug);
287 $this->checkOwnablePermission('restrictions-manage', $book);
288 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
289 session()->flash('success', trans('entities.books_permissions_updated'));
290 return redirect($book->getUrl());
294 * Export a book as a PDF file.
295 * @param string $bookSlug
298 public function exportPdf($bookSlug)
300 $book = $this->entityRepo->getBySlug('book', $bookSlug);
301 $pdfContent = $this->exportService->bookToPdf($book);
302 return response()->make($pdfContent, 200, [
303 'Content-Type' => 'application/octet-stream',
304 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
309 * Export a book as a contained HTML file.
310 * @param string $bookSlug
313 public function exportHtml($bookSlug)
315 $book = $this->entityRepo->getBySlug('book', $bookSlug);
316 $htmlContent = $this->exportService->bookToContainedHtml($book);
317 return response()->make($htmlContent, 200, [
318 'Content-Type' => 'application/octet-stream',
319 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
324 * Export a book as a plain text file.
328 public function exportPlainText($bookSlug)
330 $book = $this->entityRepo->getBySlug('book', $bookSlug);
331 $htmlContent = $this->exportService->bookToPlainText($book);
332 return response()->make($htmlContent, 200, [
333 'Content-Type' => 'application/octet-stream',
334 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'