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()
82 $this->checkPermission('book-create-all');
83 $this->setPageTitle(trans('entities.books_create'));
84 return view('books.create');
88 * Store a newly created book in storage.
90 * @param Request $request
93 public function store(Request $request)
95 $this->checkPermission('book-create-all');
96 $this->validate($request, [
97 'name' => 'required|string|max:255',
98 'description' => 'string|max:1000'
100 $book = $this->entityRepo->createFromInput('book', $request->all());
101 Activity::add($book, 'book_create', $book->id);
102 return redirect($book->getUrl());
106 * Display the specified book.
108 * @param Request $request
110 * @throws \BookStack\Exceptions\NotFoundException
112 public function show($slug, Request $request)
114 $book = $this->entityRepo->getBySlug('book', $slug);
115 $this->checkOwnablePermission('book-view', $book);
117 $bookChildren = $this->entityRepo->getBookChildren($book);
120 if ($request->has('shelf')) {
121 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
124 $this->setPageTitle($book->getShortName());
125 return view('books.show', [
128 'bookChildren' => $bookChildren,
129 'activity' => Activity::entityActivity($book, 20, 1)
134 * Show the form for editing the specified book.
138 public function edit($slug)
140 $book = $this->entityRepo->getBySlug('book', $slug);
141 $this->checkOwnablePermission('book-update', $book);
142 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
143 return view('books.edit', ['book' => $book, 'current' => $book]);
147 * Update the specified book in storage.
148 * @param Request $request
152 public function update(Request $request, $slug)
154 $book = $this->entityRepo->getBySlug('book', $slug);
155 $this->checkOwnablePermission('book-update', $book);
156 $this->validate($request, [
157 'name' => 'required|string|max:255',
158 'description' => 'string|max:1000'
160 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
161 Activity::add($book, 'book_update', $book->id);
162 return redirect($book->getUrl());
166 * Shows the page to confirm deletion
168 * @return \Illuminate\View\View
170 public function showDelete($bookSlug)
172 $book = $this->entityRepo->getBySlug('book', $bookSlug);
173 $this->checkOwnablePermission('book-delete', $book);
174 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
175 return view('books.delete', ['book' => $book, 'current' => $book]);
179 * Shows the view which allows pages to be re-ordered and sorted.
180 * @param string $bookSlug
181 * @return \Illuminate\View\View
182 * @throws \BookStack\Exceptions\NotFoundException
184 public function sort($bookSlug)
186 $book = $this->entityRepo->getBySlug('book', $bookSlug);
187 $this->checkOwnablePermission('book-update', $book);
189 $bookChildren = $this->entityRepo->getBookChildren($book, true);
191 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
192 return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
196 * Shows the sort box for a single book.
197 * Used via AJAX when loading in extra books to a sort.
199 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
201 public function getSortItem($bookSlug)
203 $book = $this->entityRepo->getBySlug('book', $bookSlug);
204 $bookChildren = $this->entityRepo->getBookChildren($book);
205 return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
209 * Saves an array of sort mapping to pages and chapters.
210 * @param string $bookSlug
211 * @param Request $request
212 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
214 public function saveSort($bookSlug, Request $request)
216 $book = $this->entityRepo->getBySlug('book', $bookSlug);
217 $this->checkOwnablePermission('book-update', $book);
219 // Return if no map sent
220 if (!$request->filled('sort-tree')) {
221 return redirect($book->getUrl());
224 // Sort pages and chapters
225 $sortMap = collect(json_decode($request->get('sort-tree')));
226 $bookIdsInvolved = collect([$book->id]);
228 // Load models into map
229 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
230 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
231 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
232 // Store source and target books
233 $bookIdsInvolved->push(intval($mapItem->model->book_id));
234 $bookIdsInvolved->push(intval($mapItem->book));
237 // Get the books involved in the sort
238 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
239 $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
240 // Throw permission error if invalid ids or inaccessible books given.
241 if (count($bookIdsInvolved) !== count($booksInvolved)) {
242 $this->showPermissionError();
244 // Check permissions of involved books
245 $booksInvolved->each(function (Book $book) {
246 $this->checkOwnablePermission('book-update', $book);
250 $sortMap->each(function ($mapItem) {
251 $model = $mapItem->model;
253 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
254 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
255 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
258 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
260 if ($chapterChanged) {
261 $model->chapter_id = intval($mapItem->parentChapter);
264 if ($priorityChanged) {
265 $model->priority = intval($mapItem->sort);
270 // Rebuild permissions and add activity for involved books.
271 $booksInvolved->each(function (Book $book) {
272 $this->entityRepo->buildJointPermissionsForBook($book);
273 Activity::add($book, 'book_sort', $book->id);
276 return redirect($book->getUrl());
280 * Remove the specified book from storage.
284 public function destroy($bookSlug)
286 $book = $this->entityRepo->getBySlug('book', $bookSlug);
287 $this->checkOwnablePermission('book-delete', $book);
288 Activity::addMessage('book_delete', 0, $book->name);
289 $this->entityRepo->destroyBook($book);
290 return redirect('/books');
294 * Show the Restrictions view.
296 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
298 public function showPermissions($bookSlug)
300 $book = $this->entityRepo->getBySlug('book', $bookSlug);
301 $this->checkOwnablePermission('restrictions-manage', $book);
302 $roles = $this->userRepo->getRestrictableRoles();
303 return view('books.permissions', [
310 * Set the restrictions for this book.
312 * @param Request $request
313 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
314 * @throws \BookStack\Exceptions\NotFoundException
317 public function permissions($bookSlug, Request $request)
319 $book = $this->entityRepo->getBySlug('book', $bookSlug);
320 $this->checkOwnablePermission('restrictions-manage', $book);
321 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
322 session()->flash('success', trans('entities.books_permissions_updated'));
323 return redirect($book->getUrl());
327 * Export a book as a PDF file.
328 * @param string $bookSlug
331 public function exportPdf($bookSlug)
333 $book = $this->entityRepo->getBySlug('book', $bookSlug);
334 $pdfContent = $this->exportService->bookToPdf($book);
335 return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
339 * Export a book as a contained HTML file.
340 * @param string $bookSlug
343 public function exportHtml($bookSlug)
345 $book = $this->entityRepo->getBySlug('book', $bookSlug);
346 $htmlContent = $this->exportService->bookToContainedHtml($book);
347 return $this->downloadResponse($htmlContent, $bookSlug . '.html');
351 * Export a book as a plain text file.
355 public function exportPlainText($bookSlug)
357 $book = $this->entityRepo->getBySlug('book', $bookSlug);
358 $textContent = $this->exportService->bookToPlainText($book);
359 return $this->downloadResponse($textContent, $bookSlug . '.txt');