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', 10);
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', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
91 * Show the form for editing the specified book.
95 public function edit($slug)
97 $book = $this->entityRepo->getBySlug('book', $slug);
98 $this->checkOwnablePermission('book-update', $book);
99 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
100 return view('books/edit', ['book' => $book, 'current' => $book]);
104 * Update the specified book in storage.
105 * @param Request $request
109 public function update(Request $request, $slug)
111 $book = $this->entityRepo->getBySlug('book', $slug);
112 $this->checkOwnablePermission('book-update', $book);
113 $this->validate($request, [
114 'name' => 'required|string|max:255',
115 'description' => 'string|max:1000'
117 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
118 Activity::add($book, 'book_update', $book->id);
119 return redirect($book->getUrl());
123 * Shows the page to confirm deletion
125 * @return \Illuminate\View\View
127 public function showDelete($bookSlug)
129 $book = $this->entityRepo->getBySlug('book', $bookSlug);
130 $this->checkOwnablePermission('book-delete', $book);
131 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
132 return view('books/delete', ['book' => $book, 'current' => $book]);
136 * Shows the view which allows pages to be re-ordered and sorted.
137 * @param string $bookSlug
138 * @return \Illuminate\View\View
140 public function sort($bookSlug)
142 $book = $this->entityRepo->getBySlug('book', $bookSlug);
143 $this->checkOwnablePermission('book-update', $book);
144 $bookChildren = $this->entityRepo->getBookChildren($book, true);
145 $books = $this->entityRepo->getAll('book', false);
146 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
147 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
151 * Shows the sort box for a single book.
152 * Used via AJAX when loading in extra books to a sort.
154 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
156 public function getSortItem($bookSlug)
158 $book = $this->entityRepo->getBySlug('book', $bookSlug);
159 $bookChildren = $this->entityRepo->getBookChildren($book);
160 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
164 * Saves an array of sort mapping to pages and chapters.
165 * @param string $bookSlug
166 * @param Request $request
167 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
169 public function saveSort($bookSlug, Request $request)
171 $book = $this->entityRepo->getBySlug('book', $bookSlug);
172 $this->checkOwnablePermission('book-update', $book);
174 // Return if no map sent
175 if (!$request->has('sort-tree')) {
176 return redirect($book->getUrl());
179 // Sort pages and chapters
181 $updatedModels = collect();
182 $sortMap = json_decode($request->get('sort-tree'));
183 $defaultBookId = $book->id;
185 // Loop through contents of provided map and update entities accordingly
186 foreach ($sortMap as $bookChild) {
187 $priority = $bookChild->sort;
188 $id = intval($bookChild->id);
189 $isPage = $bookChild->type == 'page';
190 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
191 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
192 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
194 // Update models only if there's a change in parent chain or ordering.
195 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
196 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
197 $model->priority = $priority;
198 if ($isPage) $model->chapter_id = $chapterId;
200 $updatedModels->push($model);
203 // Store involved books to be sorted later
204 if (!in_array($bookId, $sortedBooks)) {
205 $sortedBooks[] = $bookId;
209 // Add activity for books
210 foreach ($sortedBooks as $bookId) {
211 /** @var Book $updatedBook */
212 $updatedBook = $this->entityRepo->getById('book', $bookId);
213 $this->entityRepo->buildJointPermissionsForBook($updatedBook);
214 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
217 return redirect($book->getUrl());
221 * Remove the specified book from storage.
225 public function destroy($bookSlug)
227 $book = $this->entityRepo->getBySlug('book', $bookSlug);
228 $this->checkOwnablePermission('book-delete', $book);
229 Activity::addMessage('book_delete', 0, $book->name);
230 $this->entityRepo->destroyBook($book);
231 return redirect('/books');
235 * Show the Restrictions view.
237 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
239 public function showRestrict($bookSlug)
241 $book = $this->entityRepo->getBySlug('book', $bookSlug);
242 $this->checkOwnablePermission('restrictions-manage', $book);
243 $roles = $this->userRepo->getRestrictableRoles();
244 return view('books/restrictions', [
251 * Set the restrictions for this book.
254 * @param Request $request
255 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
257 public function restrict($bookSlug, Request $request)
259 $book = $this->entityRepo->getBySlug('book', $bookSlug);
260 $this->checkOwnablePermission('restrictions-manage', $book);
261 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
262 session()->flash('success', trans('entities.books_permissions_updated'));
263 return redirect($book->getUrl());
267 * Export a book as a PDF file.
268 * @param string $bookSlug
271 public function exportPdf($bookSlug)
273 $book = $this->entityRepo->getBySlug('book', $bookSlug);
274 $pdfContent = $this->exportService->bookToPdf($book);
275 return response()->make($pdfContent, 200, [
276 'Content-Type' => 'application/octet-stream',
277 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
282 * Export a book as a contained HTML file.
283 * @param string $bookSlug
286 public function exportHtml($bookSlug)
288 $book = $this->entityRepo->getBySlug('book', $bookSlug);
289 $htmlContent = $this->exportService->bookToContainedHtml($book);
290 return response()->make($htmlContent, 200, [
291 'Content-Type' => 'application/octet-stream',
292 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
297 * Export a book as a plain text file.
301 public function exportPlainText($bookSlug)
303 $book = $this->entityRepo->getBySlug('book', $bookSlug);
304 $htmlContent = $this->exportService->bookToPlainText($book);
305 return response()->make($htmlContent, 200, [
306 'Content-Type' => 'application/octet-stream',
307 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'