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
165 public function sort($bookSlug)
167 $book = $this->entityRepo->getBySlug('book', $bookSlug);
168 $this->checkOwnablePermission('book-update', $book);
169 $bookChildren = $this->entityRepo->getBookChildren($book, true);
170 $books = $this->entityRepo->getAll('book', false, 'update');
171 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
172 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
176 * Shows the sort box for a single book.
177 * Used via AJAX when loading in extra books to a sort.
179 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
181 public function getSortItem($bookSlug)
183 $book = $this->entityRepo->getBySlug('book', $bookSlug);
184 $bookChildren = $this->entityRepo->getBookChildren($book);
185 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
189 * Saves an array of sort mapping to pages and chapters.
190 * @param string $bookSlug
191 * @param Request $request
192 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
194 public function saveSort($bookSlug, Request $request)
196 $book = $this->entityRepo->getBySlug('book', $bookSlug);
197 $this->checkOwnablePermission('book-update', $book);
199 // Return if no map sent
200 if (!$request->filled('sort-tree')) {
201 return redirect($book->getUrl());
204 // Sort pages and chapters
205 $sortMap = collect(json_decode($request->get('sort-tree')));
206 $bookIdsInvolved = collect([$book->id]);
208 // Load models into map
209 $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
210 $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
211 $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
212 // Store source and target books
213 $bookIdsInvolved->push(intval($mapItem->model->book_id));
214 $bookIdsInvolved->push(intval($mapItem->book));
217 // Get the books involved in the sort
218 $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
219 $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
220 // Throw permission error if invalid ids or inaccessible books given.
221 if (count($bookIdsInvolved) !== count($booksInvolved)) {
222 $this->showPermissionError();
224 // Check permissions of involved books
225 $booksInvolved->each(function (Book $book) {
226 $this->checkOwnablePermission('book-update', $book);
230 $sortMap->each(function ($mapItem) {
231 $model = $mapItem->model;
233 $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
234 $bookChanged = intval($model->book_id) !== intval($mapItem->book);
235 $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
238 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
240 if ($chapterChanged) {
241 $model->chapter_id = intval($mapItem->parentChapter);
244 if ($priorityChanged) {
245 $model->priority = intval($mapItem->sort);
250 // Rebuild permissions and add activity for involved books.
251 $booksInvolved->each(function (Book $book) {
252 $this->entityRepo->buildJointPermissionsForBook($book);
253 Activity::add($book, 'book_sort', $book->id);
256 return redirect($book->getUrl());
260 * Remove the specified book from storage.
264 public function destroy($bookSlug)
266 $book = $this->entityRepo->getBySlug('book', $bookSlug);
267 $this->checkOwnablePermission('book-delete', $book);
268 Activity::addMessage('book_delete', 0, $book->name);
269 $this->entityRepo->destroyBook($book);
270 return redirect('/books');
274 * Show the Restrictions view.
276 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
278 public function showPermissions($bookSlug)
280 $book = $this->entityRepo->getBySlug('book', $bookSlug);
281 $this->checkOwnablePermission('restrictions-manage', $book);
282 $roles = $this->userRepo->getRestrictableRoles();
283 return view('books.permissions', [
290 * Set the restrictions for this book.
292 * @param Request $request
293 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
294 * @throws \BookStack\Exceptions\NotFoundException
297 public function permissions($bookSlug, Request $request)
299 $book = $this->entityRepo->getBySlug('book', $bookSlug);
300 $this->checkOwnablePermission('restrictions-manage', $book);
301 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
302 session()->flash('success', trans('entities.books_permissions_updated'));
303 return redirect($book->getUrl());
307 * Export a book as a PDF file.
308 * @param string $bookSlug
311 public function exportPdf($bookSlug)
313 $book = $this->entityRepo->getBySlug('book', $bookSlug);
314 $pdfContent = $this->exportService->bookToPdf($book);
315 return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
319 * Export a book as a contained HTML file.
320 * @param string $bookSlug
323 public function exportHtml($bookSlug)
325 $book = $this->entityRepo->getBySlug('book', $bookSlug);
326 $htmlContent = $this->exportService->bookToContainedHtml($book);
327 return $this->downloadResponse($htmlContent, $bookSlug . '.html');
331 * Export a book as a plain text file.
335 public function exportPlainText($bookSlug)
337 $book = $this->entityRepo->getBySlug('book', $bookSlug);
338 $textContent = $this->exportService->bookToPlainText($book);
339 return $this->downloadResponse($textContent, $bookSlug . '.txt');