1 <?php namespace BookStack\Http\Controllers;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Repos\UserRepo;
7 use BookStack\Services\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 UserRepo $userRepo
23 * @param 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 $books = $this->entityRepo->getAllPaginated('book', 10);
40 $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
41 $popular = $this->entityRepo->getPopular('book', 4, 0);
42 $this->setPageTitle('Books');
43 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular, 'display' => $display]); //added displaly to access user display
47 * Show the form for creating a new book.
50 public function create()
52 $this->checkPermission('book-create-all');
53 $this->setPageTitle(trans('entities.books_create'));
54 return view('books/create');
58 * Store a newly created book in storage.
60 * @param Request $request
63 public function store(Request $request)
65 $this->checkPermission('book-create-all');
66 $this->validate($request, [
67 'name' => 'required|string|max:255',
68 'description' => 'string|max:1000'
70 $image = $request->file('image');
71 $input = time().'-'.$image->getClientOriginalName();
72 $destinationPath = public_path('uploads/book/');
73 $image->move($destinationPath, $input);
74 $path = baseUrl('/uploads/book/').'/'.$input;
75 $book = $this->entityRepo->createFromInput('book', $request->all());
78 Activity::add($book, 'book_create', $book->id);
79 return redirect($book->getUrl());
83 * Display the specified book.
87 public function show($slug)
89 $book = $this->entityRepo->getBySlug('book', $slug);
90 $this->checkOwnablePermission('book-view', $book);
91 $bookChildren = $this->entityRepo->getBookChildren($book);
93 $this->setPageTitle($book->getShortName());
94 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
98 * Show the form for editing the specified book.
102 public function edit($slug)
104 $book = $this->entityRepo->getBySlug('book', $slug);
105 $this->checkOwnablePermission('book-update', $book);
106 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
107 return view('books/edit', ['book' => $book, 'current' => $book]);
111 * Update the specified book in storage.
112 * @param Request $request
116 public function update(Request $request, $slug)
118 $book = $this->entityRepo->getBySlug('book', $slug);
119 $this->checkOwnablePermission('book-update', $book);
120 $this->validate($request, [
121 'name' => 'required|string|max:255',
122 'description' => 'string|max:1000'
125 $input = $request->file('image')->getClientOriginalName();
127 $destinationPath = public_path('uploads/book/');
128 $request->file('image')->move($destinationPath, $input);
129 $path = baseUrl('/uploads/book/').'/'.$input;
130 $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
131 $book->image = $path;
133 Activity::add($book, 'book_update', $book->id);
134 return redirect($book->getUrl());
138 * Shows the page to confirm deletion
140 * @return \Illuminate\View\View
142 public function showDelete($bookSlug)
144 $book = $this->entityRepo->getBySlug('book', $bookSlug);
145 $this->checkOwnablePermission('book-delete', $book);
146 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
147 return view('books/delete', ['book' => $book, 'current' => $book]);
151 * Shows the view which allows pages to be re-ordered and sorted.
152 * @param string $bookSlug
153 * @return \Illuminate\View\View
155 public function sort($bookSlug)
157 $book = $this->entityRepo->getBySlug('book', $bookSlug);
158 $this->checkOwnablePermission('book-update', $book);
159 $bookChildren = $this->entityRepo->getBookChildren($book, true);
160 $books = $this->entityRepo->getAll('book', false);
161 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
162 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
166 * Shows the sort box for a single book.
167 * Used via AJAX when loading in extra books to a sort.
169 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
171 public function getSortItem($bookSlug)
173 $book = $this->entityRepo->getBySlug('book', $bookSlug);
174 $bookChildren = $this->entityRepo->getBookChildren($book);
175 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
179 * Saves an array of sort mapping to pages and chapters.
180 * @param string $bookSlug
181 * @param Request $request
182 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
184 public function saveSort($bookSlug, Request $request)
186 $book = $this->entityRepo->getBySlug('book', $bookSlug);
187 $this->checkOwnablePermission('book-update', $book);
189 // Return if no map sent
190 if (!$request->has('sort-tree')) {
191 return redirect($book->getUrl());
194 // Sort pages and chapters
196 $updatedModels = collect();
197 $sortMap = json_decode($request->get('sort-tree'));
198 $defaultBookId = $book->id;
200 // Loop through contents of provided map and update entities accordingly
201 foreach ($sortMap as $bookChild) {
202 $priority = $bookChild->sort;
203 $id = intval($bookChild->id);
204 $isPage = $bookChild->type == 'page';
205 $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
206 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
207 $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
209 // Update models only if there's a change in parent chain or ordering.
210 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
211 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
212 $model->priority = $priority;
213 if ($isPage) $model->chapter_id = $chapterId;
215 $updatedModels->push($model);
218 // Store involved books to be sorted later
219 if (!in_array($bookId, $sortedBooks)) {
220 $sortedBooks[] = $bookId;
224 // Add activity for books
225 foreach ($sortedBooks as $bookId) {
226 /** @var Book $updatedBook */
227 $updatedBook = $this->entityRepo->getById('book', $bookId);
228 $this->entityRepo->buildJointPermissionsForBook($updatedBook);
229 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
232 return redirect($book->getUrl());
236 * Remove the specified book from storage.
240 public function destroy($bookSlug)
242 $book = $this->entityRepo->getBySlug('book', $bookSlug);
243 $this->checkOwnablePermission('book-delete', $book);
244 Activity::addMessage('book_delete', 0, $book->name);
245 $this->entityRepo->destroyBook($book);
246 return redirect('/books');
250 * Show the Restrictions view.
252 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
254 public function showRestrict($bookSlug)
256 $book = $this->entityRepo->getBySlug('book', $bookSlug);
257 $this->checkOwnablePermission('restrictions-manage', $book);
258 $roles = $this->userRepo->getRestrictableRoles();
259 return view('books/restrictions', [
266 * Set the restrictions for this book.
269 * @param Request $request
270 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
272 public function restrict($bookSlug, Request $request)
274 $book = $this->entityRepo->getBySlug('book', $bookSlug);
275 $this->checkOwnablePermission('restrictions-manage', $book);
276 $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
277 session()->flash('success', trans('entities.books_permissions_updated'));
278 return redirect($book->getUrl());
282 * Export a book as a PDF file.
283 * @param string $bookSlug
286 public function exportPdf($bookSlug)
288 $book = $this->entityRepo->getBySlug('book', $bookSlug);
289 $pdfContent = $this->exportService->bookToPdf($book);
290 return response()->make($pdfContent, 200, [
291 'Content-Type' => 'application/octet-stream',
292 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
297 * Export a book as a contained HTML file.
298 * @param string $bookSlug
301 public function exportHtml($bookSlug)
303 $book = $this->entityRepo->getBySlug('book', $bookSlug);
304 $htmlContent = $this->exportService->bookToContainedHtml($book);
305 return response()->make($htmlContent, 200, [
306 'Content-Type' => 'application/octet-stream',
307 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
312 * Export a book as a plain text file.
316 public function exportPlainText($bookSlug)
318 $book = $this->entityRepo->getBySlug('book', $bookSlug);
319 $htmlContent = $this->exportService->bookToPlainText($book);
320 return response()->make($htmlContent, 200, [
321 'Content-Type' => 'application/octet-stream',
322 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'