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', 20);
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 $this->setPageTitle('Books');
44 return view('books/index', [
46 'recents' => $recents,
47 'popular' => $popular,
53 * Show the form for creating a new book.
56 public function create()
58 $this->checkPermission('book-create-all');
59 $this->setPageTitle(trans('entities.books_create'));
60 return view('books/create');
64 * Store a newly created book in storage.
66 * @param Request $request
69 public function store(Request $request)
71 $this->checkPermission('book-create-all');
72 $this->validate($request, [
73 'name' => 'required|string|max:255',
74 'description' => 'string|max:1000'
76 $book = $this->entityRepo->createFromInput('book', $request->all());
77 Activity::add($book, 'book_create', $book->id);
78 return redirect($book->getUrl());
82 * Display the specified book.
86 public function show($slug)
88 $book = $this->entityRepo->getBySlug('book', $slug);
89 $this->checkOwnablePermission('book-view', $book);
90 $bookChildren = $this->entityRepo->getBookChildren($book);
92 $this->setPageTitle($book->getShortName());
93 return view('books/show', [
96 'bookChildren' => $bookChildren,
97 'activity' => Activity::entityActivity($book, 20, 0)
102 * Show the form for editing the specified book.
106 public function edit($slug)
108 $book = $this->entityRepo->getBySlug('book', $slug);
109 $this->checkOwnablePermission('book-update', $book);
110 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
111 return view('books/edit', ['book' => $book, 'current' => $book]);
115 * Update the specified book in storage.
116 * @param Request $request
120 public function update(Request $request, $slug)
122 $book = $this->entityRepo->getBySlug('book', $slug);
123 $this->checkOwnablePermission('book-update', $book);
124 $this->validate($request, [
125 'name' => 'required|string|max:255',
126 'description' => 'string|max:1000'
128 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
129 Activity::add($book, 'book_update', $book->id);
130 return redirect($book->getUrl());
134 * Shows the page to confirm deletion
136 * @return \Illuminate\View\View
138 public function showDelete($bookSlug)
140 $book = $this->entityRepo->getBySlug('book', $bookSlug);
141 $this->checkOwnablePermission('book-delete', $book);
142 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
143 return view('books/delete', ['book' => $book, 'current' => $book]);
147 * Shows the view which allows pages to be re-ordered and sorted.
148 * @param string $bookSlug
149 * @return \Illuminate\View\View
151 public function sort($bookSlug)
153 $book = $this->entityRepo->getBySlug('book', $bookSlug);
154 $this->checkOwnablePermission('book-update', $book);
155 $bookChildren = $this->entityRepo->getBookChildren($book, true);
156 $books = $this->entityRepo->getAll('book', false);
157 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
158 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
162 * Shows the sort box for a single book.
163 * Used via AJAX when loading in extra books to a sort.
165 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
167 public function getSortItem($bookSlug)
169 $book = $this->entityRepo->getBySlug('book', $bookSlug);
170 $bookChildren = $this->entityRepo->getBookChildren($book);
171 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
175 * Saves an array of sort mapping to pages and chapters.
176 * @param string $bookSlug
177 * @param Request $request
178 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
180 public function saveSort($bookSlug, Request $request)
182 $book = $this->entityRepo->getBySlug('book', $bookSlug);
183 $this->checkOwnablePermission('book-update', $book);
185 // Return if no map sent
186 if (!$request->has('sort-tree')) {
187 return redirect($book->getUrl());
190 // Sort pages and chapters
192 $updatedModels = collect();
193 $sortMap = json_decode($request->get('sort-tree'));
194 $defaultBookId = $book->id;
196 // Loop through contents of provided map and update entities accordingly
197 foreach ($sortMap as $bookChild) {
198 $priority = $bookChild->sort;
199 $id = intval($bookChild->id);
200 $isPage = $bookChild->type == 'page';
201 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
202 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
203 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
205 // Update models only if there's a change in parent chain or ordering.
206 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
207 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
208 $model->priority = $priority;
209 if ($isPage) $model->chapter_id = $chapterId;
211 $updatedModels->push($model);
214 // Store involved books to be sorted later
215 if (!in_array($bookId, $sortedBooks)) {
216 $sortedBooks[] = $bookId;
220 // Add activity for books
221 foreach ($sortedBooks as $bookId) {
222 /** @var Book $updatedBook */
223 $updatedBook = $this->entityRepo->getById('book', $bookId);
224 $this->entityRepo->buildJointPermissionsForBook($updatedBook);
225 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
228 return redirect($book->getUrl());
232 * Remove the specified book from storage.
236 public function destroy($bookSlug)
238 $book = $this->entityRepo->getBySlug('book', $bookSlug);
239 $this->checkOwnablePermission('book-delete', $book);
240 Activity::addMessage('book_delete', 0, $book->name);
241 $this->entityRepo->destroyBook($book);
242 return redirect('/books');
246 * Show the Restrictions view.
248 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
250 public function showRestrict($bookSlug)
252 $book = $this->entityRepo->getBySlug('book', $bookSlug);
253 $this->checkOwnablePermission('restrictions-manage', $book);
254 $roles = $this->userRepo->getRestrictableRoles();
255 return view('books/restrictions', [
262 * Set the restrictions for this book.
265 * @param Request $request
266 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
268 public function restrict($bookSlug, Request $request)
270 $book = $this->entityRepo->getBySlug('book', $bookSlug);
271 $this->checkOwnablePermission('restrictions-manage', $book);
272 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
273 session()->flash('success', trans('entities.books_permissions_updated'));
274 return redirect($book->getUrl());
278 * Export a book as a PDF file.
279 * @param string $bookSlug
282 public function exportPdf($bookSlug)
284 $book = $this->entityRepo->getBySlug('book', $bookSlug);
285 $pdfContent = $this->exportService->bookToPdf($book);
286 return response()->make($pdfContent, 200, [
287 'Content-Type' => 'application/octet-stream',
288 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
293 * Export a book as a contained HTML file.
294 * @param string $bookSlug
297 public function exportHtml($bookSlug)
299 $book = $this->entityRepo->getBySlug('book', $bookSlug);
300 $htmlContent = $this->exportService->bookToContainedHtml($book);
301 return response()->make($htmlContent, 200, [
302 'Content-Type' => 'application/octet-stream',
303 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
308 * Export a book as a plain text file.
312 public function exportPlainText($bookSlug)
314 $book = $this->entityRepo->getBySlug('book', $bookSlug);
315 $htmlContent = $this->exportService->bookToPlainText($book);
316 return response()->make($htmlContent, 200, [
317 'Content-Type' => 'application/octet-stream',
318 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'