]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Merge pull request #1153 from BookStackApp/2019-design
[bookstack] / app / Http / Controllers / BookController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
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;
11 use Views;
12
13 class BookController extends Controller
14 {
15
16     protected $entityRepo;
17     protected $userRepo;
18     protected $exportService;
19     protected $entityContextManager;
20
21     /**
22      * BookController constructor.
23      * @param EntityRepo $entityRepo
24      * @param UserRepo $userRepo
25      * @param ExportService $exportService
26      * @param EntityContextManager $entityContextManager
27      */
28     public function __construct(
29         EntityRepo $entityRepo,
30         UserRepo $userRepo,
31         ExportService $exportService,
32         EntityContextManager $entityContextManager
33     ) {
34         $this->entityRepo = $entityRepo;
35         $this->userRepo = $userRepo;
36         $this->exportService = $exportService;
37         $this->entityContextManager = $entityContextManager;
38         parent::__construct();
39     }
40
41     /**
42      * Display a listing of the book.
43      * @return Response
44      */
45     public function index()
46     {
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');
50         $sortOptions = [
51             'name' => trans('common.sort_name'),
52             'created_at' => trans('common.sort_created_at'),
53             'updated_at' => trans('common.sort_updated_at'),
54         ];
55
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);
60
61         $this->entityContextManager->clearShelfContext();
62
63         $this->setPageTitle(trans('entities.books'));
64         return view('books.index', [
65             'books' => $books,
66             'recents' => $recents,
67             'popular' => $popular,
68             'new' => $new,
69             'view' => $view,
70             'sort' => $sort,
71             'order' => $order,
72             'sortOptions' => $sortOptions,
73         ]);
74     }
75
76     /**
77      * Show the form for creating a new book.
78      * @return Response
79      */
80     public function create()
81     {
82         $this->checkPermission('book-create-all');
83         $this->setPageTitle(trans('entities.books_create'));
84         return view('books.create');
85     }
86
87     /**
88      * Store a newly created book in storage.
89      *
90      * @param  Request $request
91      * @return Response
92      */
93     public function store(Request $request)
94     {
95         $this->checkPermission('book-create-all');
96         $this->validate($request, [
97             'name' => 'required|string|max:255',
98             'description' => 'string|max:1000'
99         ]);
100         $book = $this->entityRepo->createFromInput('book', $request->all());
101         Activity::add($book, 'book_create', $book->id);
102         return redirect($book->getUrl());
103     }
104
105     /**
106      * Display the specified book.
107      * @param $slug
108      * @param Request $request
109      * @return Response
110      * @throws \BookStack\Exceptions\NotFoundException
111      */
112     public function show($slug, Request $request)
113     {
114         $book = $this->entityRepo->getBySlug('book', $slug);
115         $this->checkOwnablePermission('book-view', $book);
116
117         $bookChildren = $this->entityRepo->getBookChildren($book);
118
119         Views::add($book);
120         if ($request->has('shelf')) {
121             $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
122         }
123
124         $this->setPageTitle($book->getShortName());
125         return view('books.show', [
126             'book' => $book,
127             'current' => $book,
128             'bookChildren' => $bookChildren,
129             'activity' => Activity::entityActivity($book, 20, 1)
130         ]);
131     }
132
133     /**
134      * Show the form for editing the specified book.
135      * @param $slug
136      * @return Response
137      */
138     public function edit($slug)
139     {
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]);
144     }
145
146     /**
147      * Update the specified book in storage.
148      * @param  Request $request
149      * @param          $slug
150      * @return Response
151      */
152     public function update(Request $request, $slug)
153     {
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'
159         ]);
160          $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
161          Activity::add($book, 'book_update', $book->id);
162          return redirect($book->getUrl());
163     }
164
165     /**
166      * Shows the page to confirm deletion
167      * @param $bookSlug
168      * @return \Illuminate\View\View
169      */
170     public function showDelete($bookSlug)
171     {
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]);
176     }
177
178     /**
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
183      */
184     public function sort($bookSlug)
185     {
186         $book = $this->entityRepo->getBySlug('book', $bookSlug);
187         $this->checkOwnablePermission('book-update', $book);
188
189         $bookChildren = $this->entityRepo->getBookChildren($book, true);
190
191         $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
192         return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
193     }
194
195     /**
196      * Shows the sort box for a single book.
197      * Used via AJAX when loading in extra books to a sort.
198      * @param $bookSlug
199      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
200      */
201     public function getSortItem($bookSlug)
202     {
203         $book = $this->entityRepo->getBySlug('book', $bookSlug);
204         $bookChildren = $this->entityRepo->getBookChildren($book);
205         return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
206     }
207
208     /**
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
213      */
214     public function saveSort($bookSlug, Request $request)
215     {
216         $book = $this->entityRepo->getBySlug('book', $bookSlug);
217         $this->checkOwnablePermission('book-update', $book);
218
219         // Return if no map sent
220         if (!$request->filled('sort-tree')) {
221             return redirect($book->getUrl());
222         }
223
224         // Sort pages and chapters
225         $sortMap = collect(json_decode($request->get('sort-tree')));
226         $bookIdsInvolved = collect([$book->id]);
227
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));
235         });
236
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();
243         }
244         // Check permissions of involved books
245         $booksInvolved->each(function (Book $book) {
246              $this->checkOwnablePermission('book-update', $book);
247         });
248
249         // Perform the sort
250         $sortMap->each(function ($mapItem) {
251             $model = $mapItem->model;
252
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;
256
257             if ($bookChanged) {
258                 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
259             }
260             if ($chapterChanged) {
261                 $model->chapter_id = intval($mapItem->parentChapter);
262                 $model->save();
263             }
264             if ($priorityChanged) {
265                 $model->priority = intval($mapItem->sort);
266                 $model->save();
267             }
268         });
269
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);
274         });
275
276         return redirect($book->getUrl());
277     }
278
279     /**
280      * Remove the specified book from storage.
281      * @param $bookSlug
282      * @return Response
283      */
284     public function destroy($bookSlug)
285     {
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');
291     }
292
293     /**
294      * Show the Restrictions view.
295      * @param $bookSlug
296      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
297      */
298     public function showPermissions($bookSlug)
299     {
300         $book = $this->entityRepo->getBySlug('book', $bookSlug);
301         $this->checkOwnablePermission('restrictions-manage', $book);
302         $roles = $this->userRepo->getRestrictableRoles();
303         return view('books.permissions', [
304             'book' => $book,
305             'roles' => $roles
306         ]);
307     }
308
309     /**
310      * Set the restrictions for this book.
311      * @param $bookSlug
312      * @param Request $request
313      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
314      * @throws \BookStack\Exceptions\NotFoundException
315      * @throws \Throwable
316      */
317     public function permissions($bookSlug, Request $request)
318     {
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());
324     }
325
326     /**
327      * Export a book as a PDF file.
328      * @param string $bookSlug
329      * @return mixed
330      */
331     public function exportPdf($bookSlug)
332     {
333         $book = $this->entityRepo->getBySlug('book', $bookSlug);
334         $pdfContent = $this->exportService->bookToPdf($book);
335         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
336     }
337
338     /**
339      * Export a book as a contained HTML file.
340      * @param string $bookSlug
341      * @return mixed
342      */
343     public function exportHtml($bookSlug)
344     {
345         $book = $this->entityRepo->getBySlug('book', $bookSlug);
346         $htmlContent = $this->exportService->bookToContainedHtml($book);
347         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
348     }
349
350     /**
351      * Export a book as a plain text file.
352      * @param $bookSlug
353      * @return mixed
354      */
355     public function exportPlainText($bookSlug)
356     {
357         $book = $this->entityRepo->getBySlug('book', $bookSlug);
358         $textContent = $this->exportService->bookToPlainText($book);
359         return $this->downloadResponse($textContent, $bookSlug . '.txt');
360     }
361 }