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 $this->setPageTitle('Books');
43 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
47 * Show the form for creating a new book.
50 public function create()
52 $this->checkPermission('book-create-all');
53 $this->setPageTitle(trans('entities.books_create'));
54 return view('books/create');
58 * Store a newly created book in storage.
60 * @param Request $request
63 public function store(Request $request)
65 $this->checkPermission('book-create-all');
66 $this->validate($request, [
67 'name' => 'required|string|max:255',
68 'description' => 'string|max:1000'
70 $book = $this->entityRepo->createFromInput('book', $request->all());
71 Activity::add($book, 'book_create', $book->id);
72 return redirect($book->getUrl());
76 * Display the specified book.
80 public function show($slug)
82 $book = $this->entityRepo->getBySlug('book', $slug);
83 $this->checkOwnablePermission('book-view', $book);
84 $bookChildren = $this->entityRepo->getBookChildren($book);
86 $this->setPageTitle($book->getShortName());
87 return view('books/show', [
90 'bookChildren' => $bookChildren,
91 'activity' => Activity::entityActivity($book, 20, 0)
96 * Show the form for editing the specified book.
100 public function edit($slug)
102 $book = $this->entityRepo->getBySlug('book', $slug);
103 $this->checkOwnablePermission('book-update', $book);
104 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
105 return view('books/edit', ['book' => $book, 'current' => $book]);
109 * Update the specified book in storage.
110 * @param Request $request
114 public function update(Request $request, $slug)
116 $book = $this->entityRepo->getBySlug('book', $slug);
117 $this->checkOwnablePermission('book-update', $book);
118 $this->validate($request, [
119 'name' => 'required|string|max:255',
120 'description' => 'string|max:1000'
122 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
123 Activity::add($book, 'book_update', $book->id);
124 return redirect($book->getUrl());
128 * Shows the page to confirm deletion
130 * @return \Illuminate\View\View
132 public function showDelete($bookSlug)
134 $book = $this->entityRepo->getBySlug('book', $bookSlug);
135 $this->checkOwnablePermission('book-delete', $book);
136 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
137 return view('books/delete', ['book' => $book, 'current' => $book]);
141 * Shows the view which allows pages to be re-ordered and sorted.
142 * @param string $bookSlug
143 * @return \Illuminate\View\View
145 public function sort($bookSlug)
147 $book = $this->entityRepo->getBySlug('book', $bookSlug);
148 $this->checkOwnablePermission('book-update', $book);
149 $bookChildren = $this->entityRepo->getBookChildren($book, true);
150 $books = $this->entityRepo->getAll('book', false);
151 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
152 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
156 * Shows the sort box for a single book.
157 * Used via AJAX when loading in extra books to a sort.
159 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
161 public function getSortItem($bookSlug)
163 $book = $this->entityRepo->getBySlug('book', $bookSlug);
164 $bookChildren = $this->entityRepo->getBookChildren($book);
165 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
169 * Saves an array of sort mapping to pages and chapters.
170 * @param string $bookSlug
171 * @param Request $request
172 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
174 public function saveSort($bookSlug, Request $request)
176 $book = $this->entityRepo->getBySlug('book', $bookSlug);
177 $this->checkOwnablePermission('book-update', $book);
179 // Return if no map sent
180 if (!$request->has('sort-tree')) {
181 return redirect($book->getUrl());
184 // Sort pages and chapters
186 $updatedModels = collect();
187 $sortMap = json_decode($request->get('sort-tree'));
188 $defaultBookId = $book->id;
190 // Loop through contents of provided map and update entities accordingly
191 foreach ($sortMap as $bookChild) {
192 $priority = $bookChild->sort;
193 $id = intval($bookChild->id);
194 $isPage = $bookChild->type == 'page';
195 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
196 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
197 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
199 // Update models only if there's a change in parent chain or ordering.
200 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
201 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
202 $model->priority = $priority;
203 if ($isPage) $model->chapter_id = $chapterId;
205 $updatedModels->push($model);
208 // Store involved books to be sorted later
209 if (!in_array($bookId, $sortedBooks)) {
210 $sortedBooks[] = $bookId;
214 // Add activity for books
215 foreach ($sortedBooks as $bookId) {
216 /** @var Book $updatedBook */
217 $updatedBook = $this->entityRepo->getById('book', $bookId);
218 $this->entityRepo->buildJointPermissionsForBook($updatedBook);
219 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
222 return redirect($book->getUrl());
226 * Remove the specified book from storage.
230 public function destroy($bookSlug)
232 $book = $this->entityRepo->getBySlug('book', $bookSlug);
233 $this->checkOwnablePermission('book-delete', $book);
234 Activity::addMessage('book_delete', 0, $book->name);
235 $this->entityRepo->destroyBook($book);
236 return redirect('/books');
240 * Show the Restrictions view.
242 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
244 public function showRestrict($bookSlug)
246 $book = $this->entityRepo->getBySlug('book', $bookSlug);
247 $this->checkOwnablePermission('restrictions-manage', $book);
248 $roles = $this->userRepo->getRestrictableRoles();
249 return view('books/restrictions', [
256 * Set the restrictions for this book.
259 * @param Request $request
260 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
262 public function restrict($bookSlug, Request $request)
264 $book = $this->entityRepo->getBySlug('book', $bookSlug);
265 $this->checkOwnablePermission('restrictions-manage', $book);
266 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
267 session()->flash('success', trans('entities.books_permissions_updated'));
268 return redirect($book->getUrl());
272 * Export a book as a PDF file.
273 * @param string $bookSlug
276 public function exportPdf($bookSlug)
278 $book = $this->entityRepo->getBySlug('book', $bookSlug);
279 $pdfContent = $this->exportService->bookToPdf($book);
280 return response()->make($pdfContent, 200, [
281 'Content-Type' => 'application/octet-stream',
282 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
287 * Export a book as a contained HTML file.
288 * @param string $bookSlug
291 public function exportHtml($bookSlug)
293 $book = $this->entityRepo->getBySlug('book', $bookSlug);
294 $htmlContent = $this->exportService->bookToContainedHtml($book);
295 return response()->make($htmlContent, 200, [
296 'Content-Type' => 'application/octet-stream',
297 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
302 * Export a book as a plain text file.
306 public function exportPlainText($bookSlug)
308 $book = $this->entityRepo->getBySlug('book', $bookSlug);
309 $htmlContent = $this->exportService->bookToPlainText($book);
310 return response()->make($htmlContent, 200, [
311 'Content-Type' => 'application/octet-stream',
312 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'