1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\EntityContextManager;
7 use BookStack\Entities\Repos\EntityRepo;
8 use BookStack\Entities\ExportService;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
13 class BookController extends Controller
16 protected $entityRepo;
18 protected $exportService;
19 protected $entityContextManager;
22 * BookController constructor.
23 * @param EntityRepo $entityRepo
24 * @param UserRepo $userRepo
25 * @param ExportService $exportService
26 * @param EntityContextManager $entityContextManager
28 public function __construct(
29 EntityRepo $entityRepo,
31 ExportService $exportService,
32 EntityContextManager $entityContextManager
34 $this->entityRepo = $entityRepo;
35 $this->userRepo = $userRepo;
36 $this->exportService = $exportService;
37 $this->entityContextManager = $entityContextManager;
38 parent::__construct();
42 * Display a listing of the book.
45 public function index()
47 $view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
48 $sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
49 $order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
51 'name' => trans('common.sort_name'),
52 'created_at' => trans('common.sort_created_at'),
53 'updated_at' => trans('common.sort_updated_at'),
56 $books = $this->entityRepo->getAllPaginated('book', 18, $sort, $order);
57 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
58 $popular = $this->entityRepo->getPopular('book', 4, 0);
59 $new = $this->entityRepo->getRecentlyCreated('book', 4, 0);
61 $this->entityContextManager->clearShelfContext();
63 $this->setPageTitle(trans('entities.books'));
64 return view('books.index', [
66 'recents' => $recents,
67 'popular' => $popular,
72 'sortOptions' => $sortOptions,
77 * Show the form for creating a new book.
78 * @param string $shelfSlug
80 * @throws \BookStack\Exceptions\NotFoundException
82 public function create(string $shelfSlug = null)
85 if ($shelfSlug !== null) {
86 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
87 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
90 $this->checkPermission('book-create-all');
91 $this->setPageTitle(trans('entities.books_create'));
92 return view('books.create', [
93 'bookshelf' => $bookshelf
98 * Store a newly created book in storage.
100 * @param Request $request
101 * @param string $shelfSlug
103 * @throws \BookStack\Exceptions\NotFoundException
105 public function store(Request $request, string $shelfSlug = null)
107 $this->checkPermission('book-create-all');
108 $this->validate($request, [
109 'name' => 'required|string|max:255',
110 'description' => 'string|max:1000'
114 if ($shelfSlug !== null) {
115 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
116 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
119 $book = $this->entityRepo->createFromInput('book', $request->all());
120 Activity::add($book, 'book_create', $book->id);
123 $this->entityRepo->appendBookToShelf($bookshelf, $book);
124 Activity::add($bookshelf, 'bookshelf_update');
127 return redirect($book->getUrl());
131 * Display the specified book.
133 * @param Request $request
135 * @throws \BookStack\Exceptions\NotFoundException
137 public function show($slug, Request $request)
139 $book = $this->entityRepo->getBySlug('book', $slug);
140 $this->checkOwnablePermission('book-view', $book);
142 $bookChildren = $this->entityRepo->getBookChildren($book);
145 if ($request->has('shelf')) {
146 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
149 $this->setPageTitle($book->getShortName());
150 return view('books.show', [
153 'bookChildren' => $bookChildren,
154 'activity' => Activity::entityActivity($book, 20, 1)
159 * Show the form for editing the specified book.
163 public function edit($slug)
165 $book = $this->entityRepo->getBySlug('book', $slug);
166 $this->checkOwnablePermission('book-update', $book);
167 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
168 return view('books.edit', ['book' => $book, 'current' => $book]);
172 * Update the specified book in storage.
173 * @param Request $request
177 public function update(Request $request, $slug)
179 $book = $this->entityRepo->getBySlug('book', $slug);
180 $this->checkOwnablePermission('book-update', $book);
181 $this->validate($request, [
182 'name' => 'required|string|max:255',
183 'description' => 'string|max:1000'
185 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
186 Activity::add($book, 'book_update', $book->id);
187 return redirect($book->getUrl());
191 * Shows the page to confirm deletion
193 * @return \Illuminate\View\View
195 public function showDelete($bookSlug)
197 $book = $this->entityRepo->getBySlug('book', $bookSlug);
198 $this->checkOwnablePermission('book-delete', $book);
199 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
200 return view('books.delete', ['book' => $book, 'current' => $book]);
204 * Shows the view which allows pages to be re-ordered and sorted.
205 * @param string $bookSlug
206 * @return \Illuminate\View\View
207 * @throws \BookStack\Exceptions\NotFoundException
209 public function sort($bookSlug)
211 $book = $this->entityRepo->getBySlug('book', $bookSlug);
212 $this->checkOwnablePermission('book-update', $book);
214 $bookChildren = $this->entityRepo->getBookChildren($book, true);
216 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
217 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
221 * Shows the sort box for a single book.
222 * Used via AJAX when loading in extra books to a sort.
224 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
226 public function getSortItem($bookSlug)
228 $book = $this->entityRepo->getBySlug('book', $bookSlug);
229 $bookChildren = $this->entityRepo->getBookChildren($book);
230 return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
234 * Saves an array of sort mapping to pages and chapters.
235 * @param string $bookSlug
236 * @param Request $request
237 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
239 public function saveSort($bookSlug, Request $request)
241 $book = $this->entityRepo->getBySlug('book', $bookSlug);
242 $this->checkOwnablePermission('book-update', $book);
244 // Return if no map sent
245 if (!$request->filled('sort-tree')) {
246 return redirect($book->getUrl());
249 // Sort pages and chapters
250 $sortMap = collect(json_decode($request->get('sort-tree')));
251 $bookIdsInvolved = collect([$book->id]);
253 // Load models into map
254 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
255 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
256 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
257 // Store source and target books
258 $bookIdsInvolved->push(intval($mapItem->model->book_id));
259 $bookIdsInvolved->push(intval($mapItem->book));
262 // Get the books involved in the sort
263 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
264 $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
265 // Throw permission error if invalid ids or inaccessible books given.
266 if (count($bookIdsInvolved) !== count($booksInvolved)) {
267 $this->showPermissionError();
269 // Check permissions of involved books
270 $booksInvolved->each(function (Book $book) {
271 $this->checkOwnablePermission('book-update', $book);
275 $sortMap->each(function ($mapItem) {
276 $model = $mapItem->model;
278 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
279 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
280 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
283 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
285 if ($chapterChanged) {
286 $model->chapter_id = intval($mapItem->parentChapter);
289 if ($priorityChanged) {
290 $model->priority = intval($mapItem->sort);
295 // Rebuild permissions and add activity for involved books.
296 $booksInvolved->each(function (Book $book) {
297 $this->entityRepo->buildJointPermissionsForBook($book);
298 Activity::add($book, 'book_sort', $book->id);
301 return redirect($book->getUrl());
305 * Remove the specified book from storage.
309 public function destroy($bookSlug)
311 $book = $this->entityRepo->getBySlug('book', $bookSlug);
312 $this->checkOwnablePermission('book-delete', $book);
313 Activity::addMessage('book_delete', 0, $book->name);
314 $this->entityRepo->destroyBook($book);
315 return redirect('/books');
319 * Show the Restrictions view.
321 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
323 public function showPermissions($bookSlug)
325 $book = $this->entityRepo->getBySlug('book', $bookSlug);
326 $this->checkOwnablePermission('restrictions-manage', $book);
327 $roles = $this->userRepo->getRestrictableRoles();
328 return view('books.permissions', [
335 * Set the restrictions for this book.
337 * @param Request $request
338 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
339 * @throws \BookStack\Exceptions\NotFoundException
342 public function permissions($bookSlug, Request $request)
344 $book = $this->entityRepo->getBySlug('book', $bookSlug);
345 $this->checkOwnablePermission('restrictions-manage', $book);
346 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
347 session()->flash('success', trans('entities.books_permissions_updated'));
348 return redirect($book->getUrl());
352 * Export a book as a PDF file.
353 * @param string $bookSlug
356 public function exportPdf($bookSlug)
358 $book = $this->entityRepo->getBySlug('book', $bookSlug);
359 $pdfContent = $this->exportService->bookToPdf($book);
360 return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
364 * Export a book as a contained HTML file.
365 * @param string $bookSlug
368 public function exportHtml($bookSlug)
370 $book = $this->entityRepo->getBySlug('book', $bookSlug);
371 $htmlContent = $this->exportService->bookToContainedHtml($book);
372 return $this->downloadResponse($htmlContent, $bookSlug . '.html');
376 * Export a book as a plain text file.
380 public function exportPlainText($bookSlug)
382 $book = $this->entityRepo->getBySlug('book', $bookSlug);
383 $textContent = $this->exportService->bookToPlainText($book);
384 return $this->downloadResponse($textContent, $bookSlug . '.txt');