1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\EntityRepo;
5 use BookStack\Repos\UserRepo;
6 use BookStack\Services\ExportService;
7 use Illuminate\Http\Request;
8 use Illuminate\Http\Response;
11 class BookController extends Controller
14 protected $entityRepo;
16 protected $exportService;
19 * BookController constructor.
20 * @param EntityRepo $entityRepo
21 * @param UserRepo $userRepo
22 * @param ExportService $exportService
24 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
26 $this->entityRepo = $entityRepo;
27 $this->userRepo = $userRepo;
28 $this->exportService = $exportService;
29 parent::__construct();
33 * Display a listing of the book.
36 public function index()
38 $books = $this->entityRepo->getAllPaginated('book', 10);
39 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
40 $popular = $this->entityRepo->getPopular('book', 4, 0);
41 $this->setPageTitle('Books');
42 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
46 * Show the form for creating a new book.
49 public function create()
51 $this->checkPermission('book-create-all');
52 $this->setPageTitle(trans('entities.books_create'));
53 return view('books/create');
57 * Store a newly created book in storage.
59 * @param Request $request
62 public function store(Request $request)
64 $this->checkPermission('book-create-all');
65 $this->validate($request, [
66 'name' => 'required|string|max:255',
67 'description' => 'string|max:1000'
69 $book = $this->entityRepo->createFromInput('book', $request->all());
70 Activity::add($book, 'book_create', $book->id);
71 return redirect($book->getUrl());
75 * Display the specified book.
79 public function show($slug)
81 $book = $this->entityRepo->getBySlug('book', $slug);
82 $this->checkOwnablePermission('book-view', $book);
83 $bookChildren = $this->entityRepo->getBookChildren($book);
85 $this->setPageTitle($book->getShortName());
86 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
90 * Show the form for editing the specified book.
94 public function edit($slug)
96 $book = $this->entityRepo->getBySlug('book', $slug);
97 $this->checkOwnablePermission('book-update', $book);
98 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
99 return view('books/edit', ['book' => $book, 'current' => $book]);
103 * Update the specified book in storage.
104 * @param Request $request
108 public function update(Request $request, $slug)
110 $book = $this->entityRepo->getBySlug('book', $slug);
111 $this->checkOwnablePermission('book-update', $book);
112 $this->validate($request, [
113 'name' => 'required|string|max:255',
114 'description' => 'string|max:1000'
116 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
117 Activity::add($book, 'book_update', $book->id);
118 return redirect($book->getUrl());
122 * Shows the page to confirm deletion
124 * @return \Illuminate\View\View
126 public function showDelete($bookSlug)
128 $book = $this->entityRepo->getBySlug('book', $bookSlug);
129 $this->checkOwnablePermission('book-delete', $book);
130 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
131 return view('books/delete', ['book' => $book, 'current' => $book]);
135 * Shows the view which allows pages to be re-ordered and sorted.
136 * @param string $bookSlug
137 * @return \Illuminate\View\View
139 public function sort($bookSlug)
141 $book = $this->entityRepo->getBySlug('book', $bookSlug);
142 $this->checkOwnablePermission('book-update', $book);
143 $bookChildren = $this->entityRepo->getBookChildren($book, true);
144 $books = $this->entityRepo->getAll('book', false);
145 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
146 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
150 * Shows the sort box for a single book.
151 * Used via AJAX when loading in extra books to a sort.
153 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
155 public function getSortItem($bookSlug)
157 $book = $this->entityRepo->getBySlug('book', $bookSlug);
158 $bookChildren = $this->entityRepo->getBookChildren($book);
159 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
163 * Saves an array of sort mapping to pages and chapters.
164 * @param string $bookSlug
165 * @param Request $request
166 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
168 public function saveSort($bookSlug, Request $request)
170 $book = $this->entityRepo->getBySlug('book', $bookSlug);
171 $this->checkOwnablePermission('book-update', $book);
173 // Return if no map sent
174 if (!$request->has('sort-tree')) {
175 return redirect($book->getUrl());
178 // Sort pages and chapters
180 $updatedModels = collect();
181 $sortMap = json_decode($request->get('sort-tree'));
182 $defaultBookId = $book->id;
184 // Loop through contents of provided map and update entities accordingly
185 foreach ($sortMap as $bookChild) {
186 $priority = $bookChild->sort;
187 $id = intval($bookChild->id);
188 $isPage = $bookChild->type == 'page';
189 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
190 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
191 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
193 // Update models only if there's a change in parent chain or ordering.
194 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
195 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
196 $model->priority = $priority;
197 if ($isPage) $model->chapter_id = $chapterId;
199 $updatedModels->push($model);
202 // Store involved books to be sorted later
203 if (!in_array($bookId, $sortedBooks)) {
204 $sortedBooks[] = $bookId;
208 // Add activity for books
209 foreach ($sortedBooks as $bookId) {
210 $updatedBook = $this->entityRepo->getById('book', $bookId);
211 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
214 // Update permissions on changed models
215 if (count($updatedModels) === 0) $this->entityRepo->buildJointPermissions($updatedModels);
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'