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.
80 public function create($shelfSlug = null)
82 if ($shelfSlug !== null) {
83 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
84 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
89 $this->checkPermission('book-create-all');
90 $this->setPageTitle(trans('entities.books_create'));
91 return view('books.create', [
92 'bookshelf' => $bookshelf
97 * Store a newly created book in storage.
99 * @param Request $request
103 public function store(Request $request, $shelfSlug = null)
105 if ($shelfSlug !== null) {
106 $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
107 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
109 $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
110 $shelfBookIds = $shelfBooks->pluck('id');
116 $this->checkPermission('book-create-all');
117 $this->validate($request, [
118 'name' => 'required|string|max:255',
119 'description' => 'string|max:1000'
121 $book = $this->entityRepo->createFromInput('book', $request->all());
122 Activity::add($book, 'book_create', $book->id);
125 $shelfBookIds = $shelfBookIds->toArray();
126 array_unshift($shelfBookIds, $book->id);
128 $shelfBookIds = implode(',', $shelfBookIds);
130 $this->entityRepo->updateShelfBooks($bookshelf, $shelfBookIds);
131 Activity::add($bookshelf, 'bookshelf_update');
133 return redirect($bookshelf->getUrl());
136 return redirect($book->getUrl());
140 * Display the specified book.
142 * @param Request $request
144 * @throws \BookStack\Exceptions\NotFoundException
146 public function show($slug, Request $request)
148 $book = $this->entityRepo->getBySlug('book', $slug);
149 $this->checkOwnablePermission('book-view', $book);
151 $bookChildren = $this->entityRepo->getBookChildren($book);
154 if ($request->has('shelf')) {
155 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
158 $this->setPageTitle($book->getShortName());
159 return view('books.show', [
162 'bookChildren' => $bookChildren,
163 'activity' => Activity::entityActivity($book, 20, 1)
168 * Show the form for editing the specified book.
172 public function edit($slug)
174 $book = $this->entityRepo->getBySlug('book', $slug);
175 $this->checkOwnablePermission('book-update', $book);
176 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
177 return view('books.edit', ['book' => $book, 'current' => $book]);
181 * Update the specified book in storage.
182 * @param Request $request
186 public function update(Request $request, $slug)
188 $book = $this->entityRepo->getBySlug('book', $slug);
189 $this->checkOwnablePermission('book-update', $book);
190 $this->validate($request, [
191 'name' => 'required|string|max:255',
192 'description' => 'string|max:1000'
194 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
195 Activity::add($book, 'book_update', $book->id);
196 return redirect($book->getUrl());
200 * Shows the page to confirm deletion
202 * @return \Illuminate\View\View
204 public function showDelete($bookSlug)
206 $book = $this->entityRepo->getBySlug('book', $bookSlug);
207 $this->checkOwnablePermission('book-delete', $book);
208 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
209 return view('books.delete', ['book' => $book, 'current' => $book]);
213 * Shows the view which allows pages to be re-ordered and sorted.
214 * @param string $bookSlug
215 * @return \Illuminate\View\View
216 * @throws \BookStack\Exceptions\NotFoundException
218 public function sort($bookSlug)
220 $book = $this->entityRepo->getBySlug('book', $bookSlug);
221 $this->checkOwnablePermission('book-update', $book);
223 $bookChildren = $this->entityRepo->getBookChildren($book, true);
225 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
226 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
230 * Shows the sort box for a single book.
231 * Used via AJAX when loading in extra books to a sort.
233 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
235 public function getSortItem($bookSlug)
237 $book = $this->entityRepo->getBySlug('book', $bookSlug);
238 $bookChildren = $this->entityRepo->getBookChildren($book);
239 return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
243 * Saves an array of sort mapping to pages and chapters.
244 * @param string $bookSlug
245 * @param Request $request
246 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
248 public function saveSort($bookSlug, Request $request)
250 $book = $this->entityRepo->getBySlug('book', $bookSlug);
251 $this->checkOwnablePermission('book-update', $book);
253 // Return if no map sent
254 if (!$request->filled('sort-tree')) {
255 return redirect($book->getUrl());
258 // Sort pages and chapters
259 $sortMap = collect(json_decode($request->get('sort-tree')));
260 $bookIdsInvolved = collect([$book->id]);
262 // Load models into map
263 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
264 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
265 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
266 // Store source and target books
267 $bookIdsInvolved->push(intval($mapItem->model->book_id));
268 $bookIdsInvolved->push(intval($mapItem->book));
271 // Get the books involved in the sort
272 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
273 $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
274 // Throw permission error if invalid ids or inaccessible books given.
275 if (count($bookIdsInvolved) !== count($booksInvolved)) {
276 $this->showPermissionError();
278 // Check permissions of involved books
279 $booksInvolved->each(function (Book $book) {
280 $this->checkOwnablePermission('book-update', $book);
284 $sortMap->each(function ($mapItem) {
285 $model = $mapItem->model;
287 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
288 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
289 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
292 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
294 if ($chapterChanged) {
295 $model->chapter_id = intval($mapItem->parentChapter);
298 if ($priorityChanged) {
299 $model->priority = intval($mapItem->sort);
304 // Rebuild permissions and add activity for involved books.
305 $booksInvolved->each(function (Book $book) {
306 $this->entityRepo->buildJointPermissionsForBook($book);
307 Activity::add($book, 'book_sort', $book->id);
310 return redirect($book->getUrl());
314 * Remove the specified book from storage.
318 public function destroy($bookSlug)
320 $book = $this->entityRepo->getBySlug('book', $bookSlug);
321 $this->checkOwnablePermission('book-delete', $book);
322 Activity::addMessage('book_delete', 0, $book->name);
323 $this->entityRepo->destroyBook($book);
324 return redirect('/books');
328 * Show the Restrictions view.
330 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
332 public function showPermissions($bookSlug)
334 $book = $this->entityRepo->getBySlug('book', $bookSlug);
335 $this->checkOwnablePermission('restrictions-manage', $book);
336 $roles = $this->userRepo->getRestrictableRoles();
337 return view('books.permissions', [
344 * Set the restrictions for this book.
346 * @param Request $request
347 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
348 * @throws \BookStack\Exceptions\NotFoundException
351 public function permissions($bookSlug, Request $request)
353 $book = $this->entityRepo->getBySlug('book', $bookSlug);
354 $this->checkOwnablePermission('restrictions-manage', $book);
355 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
356 session()->flash('success', trans('entities.books_permissions_updated'));
357 return redirect($book->getUrl());
361 * Export a book as a PDF file.
362 * @param string $bookSlug
365 public function exportPdf($bookSlug)
367 $book = $this->entityRepo->getBySlug('book', $bookSlug);
368 $pdfContent = $this->exportService->bookToPdf($book);
369 return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
373 * Export a book as a contained HTML file.
374 * @param string $bookSlug
377 public function exportHtml($bookSlug)
379 $book = $this->entityRepo->getBySlug('book', $bookSlug);
380 $htmlContent = $this->exportService->bookToContainedHtml($book);
381 return $this->downloadResponse($htmlContent, $bookSlug . '.html');
385 * Export a book as a plain text file.
389 public function exportPlainText($bookSlug)
391 $book = $this->entityRepo->getBySlug('book', $bookSlug);
392 $textContent = $this->exportService->bookToPlainText($book);
393 return $this->downloadResponse($textContent, $bookSlug . '.txt');