]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Add button to add a book directly from a shelf view
[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($shelfSlug = null)
81     {
82         if ($shelfSlug !== null) {
83             $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
84             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
85         } else {
86             $bookshelf = null;
87         }
88
89         $this->checkPermission('book-create-all');
90         $this->setPageTitle(trans('entities.books_create'));
91         return view('books.create', [
92             'bookshelf' => $bookshelf
93         ]);
94     }
95
96     /**
97      * Store a newly created book in storage.
98      *
99      * @param Request $request
100      * @param $shelfSlug
101      * @return Response
102      */
103     public function store(Request $request, $shelfSlug = null)
104     {
105         if ($shelfSlug !== null) {
106             $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
107             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
108
109             $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
110             $shelfBookIds = $shelfBooks->pluck('id');
111             
112         } else {
113             $bookshelf = null;
114         }
115
116         $this->checkPermission('book-create-all');
117         $this->validate($request, [
118             'name' => 'required|string|max:255',
119             'description' => 'string|max:1000'
120         ]);
121         $book = $this->entityRepo->createFromInput('book', $request->all());
122         Activity::add($book, 'book_create', $book->id);
123
124         if ($bookshelf) {
125             $shelfBookIds = $shelfBookIds->toArray();
126             array_unshift($shelfBookIds, $book->id);
127
128             $shelfBookIds = implode(',', $shelfBookIds);
129
130             $this->entityRepo->updateShelfBooks($bookshelf, $shelfBookIds);
131             Activity::add($bookshelf, 'bookshelf_update');
132
133             return redirect($bookshelf->getUrl());
134         }
135
136         return redirect($book->getUrl());
137     }
138
139     /**
140      * Display the specified book.
141      * @param $slug
142      * @param Request $request
143      * @return Response
144      * @throws \BookStack\Exceptions\NotFoundException
145      */
146     public function show($slug, Request $request)
147     {
148         $book = $this->entityRepo->getBySlug('book', $slug);
149         $this->checkOwnablePermission('book-view', $book);
150
151         $bookChildren = $this->entityRepo->getBookChildren($book);
152
153         Views::add($book);
154         if ($request->has('shelf')) {
155             $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
156         }
157
158         $this->setPageTitle($book->getShortName());
159         return view('books.show', [
160             'book' => $book,
161             'current' => $book,
162             'bookChildren' => $bookChildren,
163             'activity' => Activity::entityActivity($book, 20, 1)
164         ]);
165     }
166
167     /**
168      * Show the form for editing the specified book.
169      * @param $slug
170      * @return Response
171      */
172     public function edit($slug)
173     {
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]);
178     }
179
180     /**
181      * Update the specified book in storage.
182      * @param  Request $request
183      * @param          $slug
184      * @return Response
185      */
186     public function update(Request $request, $slug)
187     {
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'
193         ]);
194          $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
195          Activity::add($book, 'book_update', $book->id);
196          return redirect($book->getUrl());
197     }
198
199     /**
200      * Shows the page to confirm deletion
201      * @param $bookSlug
202      * @return \Illuminate\View\View
203      */
204     public function showDelete($bookSlug)
205     {
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]);
210     }
211
212     /**
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
217      */
218     public function sort($bookSlug)
219     {
220         $book = $this->entityRepo->getBySlug('book', $bookSlug);
221         $this->checkOwnablePermission('book-update', $book);
222
223         $bookChildren = $this->entityRepo->getBookChildren($book, true);
224
225         $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
226         return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
227     }
228
229     /**
230      * Shows the sort box for a single book.
231      * Used via AJAX when loading in extra books to a sort.
232      * @param $bookSlug
233      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
234      */
235     public function getSortItem($bookSlug)
236     {
237         $book = $this->entityRepo->getBySlug('book', $bookSlug);
238         $bookChildren = $this->entityRepo->getBookChildren($book);
239         return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
240     }
241
242     /**
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
247      */
248     public function saveSort($bookSlug, Request $request)
249     {
250         $book = $this->entityRepo->getBySlug('book', $bookSlug);
251         $this->checkOwnablePermission('book-update', $book);
252
253         // Return if no map sent
254         if (!$request->filled('sort-tree')) {
255             return redirect($book->getUrl());
256         }
257
258         // Sort pages and chapters
259         $sortMap = collect(json_decode($request->get('sort-tree')));
260         $bookIdsInvolved = collect([$book->id]);
261
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));
269         });
270
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();
277         }
278         // Check permissions of involved books
279         $booksInvolved->each(function (Book $book) {
280              $this->checkOwnablePermission('book-update', $book);
281         });
282
283         // Perform the sort
284         $sortMap->each(function ($mapItem) {
285             $model = $mapItem->model;
286
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;
290
291             if ($bookChanged) {
292                 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
293             }
294             if ($chapterChanged) {
295                 $model->chapter_id = intval($mapItem->parentChapter);
296                 $model->save();
297             }
298             if ($priorityChanged) {
299                 $model->priority = intval($mapItem->sort);
300                 $model->save();
301             }
302         });
303
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);
308         });
309
310         return redirect($book->getUrl());
311     }
312
313     /**
314      * Remove the specified book from storage.
315      * @param $bookSlug
316      * @return Response
317      */
318     public function destroy($bookSlug)
319     {
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');
325     }
326
327     /**
328      * Show the Restrictions view.
329      * @param $bookSlug
330      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
331      */
332     public function showPermissions($bookSlug)
333     {
334         $book = $this->entityRepo->getBySlug('book', $bookSlug);
335         $this->checkOwnablePermission('restrictions-manage', $book);
336         $roles = $this->userRepo->getRestrictableRoles();
337         return view('books.permissions', [
338             'book' => $book,
339             'roles' => $roles
340         ]);
341     }
342
343     /**
344      * Set the restrictions for this book.
345      * @param $bookSlug
346      * @param Request $request
347      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
348      * @throws \BookStack\Exceptions\NotFoundException
349      * @throws \Throwable
350      */
351     public function permissions($bookSlug, Request $request)
352     {
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());
358     }
359
360     /**
361      * Export a book as a PDF file.
362      * @param string $bookSlug
363      * @return mixed
364      */
365     public function exportPdf($bookSlug)
366     {
367         $book = $this->entityRepo->getBySlug('book', $bookSlug);
368         $pdfContent = $this->exportService->bookToPdf($book);
369         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
370     }
371
372     /**
373      * Export a book as a contained HTML file.
374      * @param string $bookSlug
375      * @return mixed
376      */
377     public function exportHtml($bookSlug)
378     {
379         $book = $this->entityRepo->getBySlug('book', $bookSlug);
380         $htmlContent = $this->exportService->bookToContainedHtml($book);
381         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
382     }
383
384     /**
385      * Export a book as a plain text file.
386      * @param $bookSlug
387      * @return mixed
388      */
389     public function exportPlainText($bookSlug)
390     {
391         $book = $this->entityRepo->getBySlug('book', $bookSlug);
392         $textContent = $this->exportService->bookToPlainText($book);
393         return $this->downloadResponse($textContent, $bookSlug . '.txt');
394     }
395 }