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', 16);
40 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
41 $popular = $this->entityRepo->getPopular('book', 3, 0);
42 $display = $this->currentUser->display;
43 $this->setPageTitle('Books');
44 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular, 'display' => $display]); //added displaly to access user display
48 * Show the form for creating a new book.
51 public function create()
53 $this->checkPermission('book-create-all');
54 $this->setPageTitle(trans('entities.books_create'));
55 return view('books/create');
59 * Store a newly created book in storage.
61 * @param Request $request
64 public function store(Request $request)
66 $this->checkPermission('book-create-all');
67 $this->validate($request, [
68 'name' => 'required|string|max:255',
69 'description' => 'string|max:1000'
71 $book = $this->entityRepo->createFromInput('book', $request->all());
72 Activity::add($book, 'book_create', $book->id);
73 return redirect($book->getUrl());
77 * Display the specified book.
81 public function show($slug)
83 $book = $this->entityRepo->getBySlug('book', $slug);
84 $this->checkOwnablePermission('book-view', $book);
85 $bookChildren = $this->entityRepo->getBookChildren($book);
87 $this->setPageTitle($book->getShortName());
88 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
92 * Show the form for editing the specified book.
96 public function edit($slug)
98 $book = $this->entityRepo->getBySlug('book', $slug);
99 $this->checkOwnablePermission('book-update', $book);
100 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
101 return view('books/edit', ['book' => $book, 'current' => $book]);
105 * Update the specified book in storage.
106 * @param Request $request
110 public function update(Request $request, $slug)
112 $book = $this->entityRepo->getBySlug('book', $slug);
113 $this->checkOwnablePermission('book-update', $book);
114 $this->validate($request, [
115 'name' => 'required|string|max:255',
116 'description' => 'string|max:1000'
118 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
119 Activity::add($book, 'book_update', $book->id);
120 return redirect($book->getUrl());
124 * Shows the page to confirm deletion
126 * @return \Illuminate\View\View
128 public function showDelete($bookSlug)
130 $book = $this->entityRepo->getBySlug('book', $bookSlug);
131 $this->checkOwnablePermission('book-delete', $book);
132 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
133 return view('books/delete', ['book' => $book, 'current' => $book]);
137 * Shows the view which allows pages to be re-ordered and sorted.
138 * @param string $bookSlug
139 * @return \Illuminate\View\View
141 public function sort($bookSlug)
143 $book = $this->entityRepo->getBySlug('book', $bookSlug);
144 $this->checkOwnablePermission('book-update', $book);
145 $bookChildren = $this->entityRepo->getBookChildren($book, true);
146 $books = $this->entityRepo->getAll('book', false);
147 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
148 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
152 * Shows the sort box for a single book.
153 * Used via AJAX when loading in extra books to a sort.
155 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
157 public function getSortItem($bookSlug)
159 $book = $this->entityRepo->getBySlug('book', $bookSlug);
160 $bookChildren = $this->entityRepo->getBookChildren($book);
161 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
165 * Saves an array of sort mapping to pages and chapters.
166 * @param string $bookSlug
167 * @param Request $request
168 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
170 public function saveSort($bookSlug, Request $request)
172 $book = $this->entityRepo->getBySlug('book', $bookSlug);
173 $this->checkOwnablePermission('book-update', $book);
175 // Return if no map sent
176 if (!$request->has('sort-tree')) {
177 return redirect($book->getUrl());
180 // Sort pages and chapters
182 $updatedModels = collect();
183 $sortMap = json_decode($request->get('sort-tree'));
184 $defaultBookId = $book->id;
186 // Loop through contents of provided map and update entities accordingly
187 foreach ($sortMap as $bookChild) {
188 $priority = $bookChild->sort;
189 $id = intval($bookChild->id);
190 $isPage = $bookChild->type == 'page';
191 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
192 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
193 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
195 // Update models only if there's a change in parent chain or ordering.
196 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
197 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
198 $model->priority = $priority;
199 if ($isPage) $model->chapter_id = $chapterId;
201 $updatedModels->push($model);
204 // Store involved books to be sorted later
205 if (!in_array($bookId, $sortedBooks)) {
206 $sortedBooks[] = $bookId;
210 // Add activity for books
211 foreach ($sortedBooks as $bookId) {
212 /** @var Book $updatedBook */
213 $updatedBook = $this->entityRepo->getById('book', $bookId);
214 $this->entityRepo->buildJointPermissionsForBook($updatedBook);
215 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
218 return redirect($book->getUrl());
222 * Remove the specified book from storage.
226 public function destroy($bookSlug)
228 $book = $this->entityRepo->getBySlug('book', $bookSlug);
229 $this->checkOwnablePermission('book-delete', $book);
230 Activity::addMessage('book_delete', 0, $book->name);
231 $this->entityRepo->destroyBook($book);
232 return redirect('/books');
236 * Show the Restrictions view.
238 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
240 public function showRestrict($bookSlug)
242 $book = $this->entityRepo->getBySlug('book', $bookSlug);
243 $this->checkOwnablePermission('restrictions-manage', $book);
244 $roles = $this->userRepo->getRestrictableRoles();
245 return view('books/restrictions', [
252 * Set the restrictions for this book.
255 * @param Request $request
256 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
258 public function restrict($bookSlug, Request $request)
260 $book = $this->entityRepo->getBySlug('book', $bookSlug);
261 $this->checkOwnablePermission('restrictions-manage', $book);
262 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
263 session()->flash('success', trans('entities.books_permissions_updated'));
264 return redirect($book->getUrl());
268 * Export a book as a PDF file.
269 * @param string $bookSlug
272 public function exportPdf($bookSlug)
274 $book = $this->entityRepo->getBySlug('book', $bookSlug);
275 $pdfContent = $this->exportService->bookToPdf($book);
276 return response()->make($pdfContent, 200, [
277 'Content-Type' => 'application/octet-stream',
278 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
283 * Export a book as a contained HTML file.
284 * @param string $bookSlug
287 public function exportHtml($bookSlug)
289 $book = $this->entityRepo->getBySlug('book', $bookSlug);
290 $htmlContent = $this->exportService->bookToContainedHtml($book);
291 return response()->make($htmlContent, 200, [
292 'Content-Type' => 'application/octet-stream',
293 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
298 * Export a book as a plain text file.
302 public function exportPlainText($bookSlug)
304 $book = $this->entityRepo->getBySlug('book', $bookSlug);
305 $htmlContent = $this->exportService->bookToPlainText($book);
306 return response()->make($htmlContent, 200, [
307 'Content-Type' => 'application/octet-stream',
308 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'