1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Entities\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 \BookStack\Auth\UserRepo $userRepo
23 * @param \BookStack\Entities\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 $view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
40 $sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
41 $order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
43 'name' => trans('common.sort_name'),
44 'created_at' => trans('common.sort_created_at'),
45 'updated_at' => trans('common.sort_updated_at'),
48 $books = $this->entityRepo->getAllPaginated('book', 18, $sort, $order);
49 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
50 $popular = $this->entityRepo->getPopular('book', 4, 0);
51 $new = $this->entityRepo->getRecentlyCreated('book', 4, 0);
53 $this->setPageTitle(trans('entities.books'));
54 return view('books/index', [
56 'recents' => $recents,
57 'popular' => $popular,
62 'sortOptions' => $sortOptions,
67 * Show the form for creating a new book.
70 public function create()
72 $this->checkPermission('book-create-all');
73 $this->setPageTitle(trans('entities.books_create'));
74 return view('books/create');
78 * Store a newly created book in storage.
80 * @param Request $request
83 public function store(Request $request)
85 $this->checkPermission('book-create-all');
86 $this->validate($request, [
87 'name' => 'required|string|max:255',
88 'description' => 'string|max:1000'
90 $book = $this->entityRepo->createFromInput('book', $request->all());
91 Activity::add($book, 'book_create', $book->id);
92 return redirect($book->getUrl());
96 * Display the specified book.
100 public function show($slug)
102 $book = $this->entityRepo->getBySlug('book', $slug);
103 $this->checkOwnablePermission('book-view', $book);
104 $bookChildren = $this->entityRepo->getBookChildren($book);
106 $this->setPageTitle($book->getShortName());
107 return view('books/show', [
110 'bookChildren' => $bookChildren,
111 'activity' => Activity::entityActivity($book, 20, 1)
116 * Show the form for editing the specified book.
120 public function edit($slug)
122 $book = $this->entityRepo->getBySlug('book', $slug);
123 $this->checkOwnablePermission('book-update', $book);
124 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
125 return view('books/edit', ['book' => $book, 'current' => $book]);
129 * Update the specified book in storage.
130 * @param Request $request
134 public function update(Request $request, $slug)
136 $book = $this->entityRepo->getBySlug('book', $slug);
137 $this->checkOwnablePermission('book-update', $book);
138 $this->validate($request, [
139 'name' => 'required|string|max:255',
140 'description' => 'string|max:1000'
142 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
143 Activity::add($book, 'book_update', $book->id);
144 return redirect($book->getUrl());
148 * Shows the page to confirm deletion
150 * @return \Illuminate\View\View
152 public function showDelete($bookSlug)
154 $book = $this->entityRepo->getBySlug('book', $bookSlug);
155 $this->checkOwnablePermission('book-delete', $book);
156 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
157 return view('books/delete', ['book' => $book, 'current' => $book]);
161 * Shows the view which allows pages to be re-ordered and sorted.
162 * @param string $bookSlug
163 * @return \Illuminate\View\View
164 * @throws \BookStack\Exceptions\NotFoundException
166 public function sort($bookSlug)
168 $book = $this->entityRepo->getBySlug('book', $bookSlug);
169 $this->checkOwnablePermission('book-update', $book);
171 $bookChildren = $this->entityRepo->getBookChildren($book, true);
173 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
174 return view('books/sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
178 * Shows the sort box for a single book.
179 * Used via AJAX when loading in extra books to a sort.
181 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
183 public function getSortItem($bookSlug)
185 $book = $this->entityRepo->getBySlug('book', $bookSlug);
186 $bookChildren = $this->entityRepo->getBookChildren($book);
187 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
191 * Saves an array of sort mapping to pages and chapters.
192 * @param string $bookSlug
193 * @param Request $request
194 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
196 public function saveSort($bookSlug, Request $request)
198 $book = $this->entityRepo->getBySlug('book', $bookSlug);
199 $this->checkOwnablePermission('book-update', $book);
201 // Return if no map sent
202 if (!$request->filled('sort-tree')) {
203 return redirect($book->getUrl());
206 // Sort pages and chapters
207 $sortMap = collect(json_decode($request->get('sort-tree')));
208 $bookIdsInvolved = collect([$book->id]);
210 // Load models into map
211 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
212 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
213 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
214 // Store source and target books
215 $bookIdsInvolved->push(intval($mapItem->model->book_id));
216 $bookIdsInvolved->push(intval($mapItem->book));
219 // Get the books involved in the sort
220 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
221 $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
222 // Throw permission error if invalid ids or inaccessible books given.
223 if (count($bookIdsInvolved) !== count($booksInvolved)) {
224 $this->showPermissionError();
226 // Check permissions of involved books
227 $booksInvolved->each(function (Book $book) {
228 $this->checkOwnablePermission('book-update', $book);
232 $sortMap->each(function ($mapItem) {
233 $model = $mapItem->model;
235 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
236 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
237 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
240 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
242 if ($chapterChanged) {
243 $model->chapter_id = intval($mapItem->parentChapter);
246 if ($priorityChanged) {
247 $model->priority = intval($mapItem->sort);
252 // Rebuild permissions and add activity for involved books.
253 $booksInvolved->each(function (Book $book) {
254 $this->entityRepo->buildJointPermissionsForBook($book);
255 Activity::add($book, 'book_sort', $book->id);
258 return redirect($book->getUrl());
262 * Remove the specified book from storage.
266 public function destroy($bookSlug)
268 $book = $this->entityRepo->getBySlug('book', $bookSlug);
269 $this->checkOwnablePermission('book-delete', $book);
270 Activity::addMessage('book_delete', 0, $book->name);
271 $this->entityRepo->destroyBook($book);
272 return redirect('/books');
276 * Show the Restrictions view.
278 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
280 public function showPermissions($bookSlug)
282 $book = $this->entityRepo->getBySlug('book', $bookSlug);
283 $this->checkOwnablePermission('restrictions-manage', $book);
284 $roles = $this->userRepo->getRestrictableRoles();
285 return view('books.permissions', [
292 * Set the restrictions for this book.
294 * @param Request $request
295 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
296 * @throws \BookStack\Exceptions\NotFoundException
299 public function permissions($bookSlug, Request $request)
301 $book = $this->entityRepo->getBySlug('book', $bookSlug);
302 $this->checkOwnablePermission('restrictions-manage', $book);
303 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
304 session()->flash('success', trans('entities.books_permissions_updated'));
305 return redirect($book->getUrl());
309 * Export a book as a PDF file.
310 * @param string $bookSlug
313 public function exportPdf($bookSlug)
315 $book = $this->entityRepo->getBySlug('book', $bookSlug);
316 $pdfContent = $this->exportService->bookToPdf($book);
317 return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
321 * Export a book as a contained HTML file.
322 * @param string $bookSlug
325 public function exportHtml($bookSlug)
327 $book = $this->entityRepo->getBySlug('book', $bookSlug);
328 $htmlContent = $this->exportService->bookToContainedHtml($book);
329 return $this->downloadResponse($htmlContent, $bookSlug . '.html');
333 * Export a book as a plain text file.
337 public function exportPlainText($bookSlug)
339 $book = $this->entityRepo->getBySlug('book', $bookSlug);
340 $textContent = $this->exportService->bookToPlainText($book);
341 return $this->downloadResponse($textContent, $bookSlug . '.txt');