]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Merge branch 'patch-1' of git://github.com/XVilka/BookStack into XVilka-patch-1
[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      * @param string $shelfSlug
79      * @return Response
80      * @throws \BookStack\Exceptions\NotFoundException
81      */
82     public function create(string $shelfSlug = null)
83     {
84         $bookshelf = null;
85         if ($shelfSlug !== null) {
86             $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
87             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
88         }
89
90         $this->checkPermission('book-create-all');
91         $this->setPageTitle(trans('entities.books_create'));
92         return view('books.create', [
93             'bookshelf' => $bookshelf
94         ]);
95     }
96
97     /**
98      * Store a newly created book in storage.
99      *
100      * @param Request $request
101      * @param string $shelfSlug
102      * @return Response
103      * @throws \BookStack\Exceptions\NotFoundException
104      */
105     public function store(Request $request, string $shelfSlug = null)
106     {
107         $this->checkPermission('book-create-all');
108         $this->validate($request, [
109             'name' => 'required|string|max:255',
110             'description' => 'string|max:1000'
111         ]);
112
113         $bookshelf = null;
114         if ($shelfSlug !== null) {
115             $bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
116             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
117         }
118
119         $book = $this->entityRepo->createFromInput('book', $request->all());
120         Activity::add($book, 'book_create', $book->id);
121
122         if ($bookshelf) {
123             $this->entityRepo->appendBookToShelf($bookshelf, $book);
124             Activity::add($bookshelf, 'bookshelf_update');
125         }
126
127         return redirect($book->getUrl());
128     }
129
130     /**
131      * Display the specified book.
132      * @param $slug
133      * @param Request $request
134      * @return Response
135      * @throws \BookStack\Exceptions\NotFoundException
136      */
137     public function show($slug, Request $request)
138     {
139         $book = $this->entityRepo->getBySlug('book', $slug);
140         $this->checkOwnablePermission('book-view', $book);
141
142         $bookChildren = $this->entityRepo->getBookChildren($book);
143
144         Views::add($book);
145         if ($request->has('shelf')) {
146             $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
147         }
148
149         $this->setPageTitle($book->getShortName());
150         return view('books.show', [
151             'book' => $book,
152             'current' => $book,
153             'bookChildren' => $bookChildren,
154             'activity' => Activity::entityActivity($book, 20, 1)
155         ]);
156     }
157
158     /**
159      * Show the form for editing the specified book.
160      * @param $slug
161      * @return Response
162      */
163     public function edit($slug)
164     {
165         $book = $this->entityRepo->getBySlug('book', $slug);
166         $this->checkOwnablePermission('book-update', $book);
167         $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
168         return view('books.edit', ['book' => $book, 'current' => $book]);
169     }
170
171     /**
172      * Update the specified book in storage.
173      * @param  Request $request
174      * @param          $slug
175      * @return Response
176      */
177     public function update(Request $request, $slug)
178     {
179         $book = $this->entityRepo->getBySlug('book', $slug);
180         $this->checkOwnablePermission('book-update', $book);
181         $this->validate($request, [
182             'name' => 'required|string|max:255',
183             'description' => 'string|max:1000'
184         ]);
185          $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
186          Activity::add($book, 'book_update', $book->id);
187          return redirect($book->getUrl());
188     }
189
190     /**
191      * Shows the page to confirm deletion
192      * @param $bookSlug
193      * @return \Illuminate\View\View
194      */
195     public function showDelete($bookSlug)
196     {
197         $book = $this->entityRepo->getBySlug('book', $bookSlug);
198         $this->checkOwnablePermission('book-delete', $book);
199         $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
200         return view('books.delete', ['book' => $book, 'current' => $book]);
201     }
202
203     /**
204      * Shows the view which allows pages to be re-ordered and sorted.
205      * @param string $bookSlug
206      * @return \Illuminate\View\View
207      * @throws \BookStack\Exceptions\NotFoundException
208      */
209     public function sort($bookSlug)
210     {
211         $book = $this->entityRepo->getBySlug('book', $bookSlug);
212         $this->checkOwnablePermission('book-update', $book);
213
214         $bookChildren = $this->entityRepo->getBookChildren($book, true);
215
216         $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
217         return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
218     }
219
220     /**
221      * Shows the sort box for a single book.
222      * Used via AJAX when loading in extra books to a sort.
223      * @param $bookSlug
224      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
225      */
226     public function getSortItem($bookSlug)
227     {
228         $book = $this->entityRepo->getBySlug('book', $bookSlug);
229         $bookChildren = $this->entityRepo->getBookChildren($book);
230         return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
231     }
232
233     /**
234      * Saves an array of sort mapping to pages and chapters.
235      * @param  string $bookSlug
236      * @param Request $request
237      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
238      */
239     public function saveSort($bookSlug, Request $request)
240     {
241         $book = $this->entityRepo->getBySlug('book', $bookSlug);
242         $this->checkOwnablePermission('book-update', $book);
243
244         // Return if no map sent
245         if (!$request->filled('sort-tree')) {
246             return redirect($book->getUrl());
247         }
248
249         // Sort pages and chapters
250         $sortMap = collect(json_decode($request->get('sort-tree')));
251         $bookIdsInvolved = collect([$book->id]);
252
253         // Load models into map
254         $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
255             $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
256             $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
257             // Store source and target books
258             $bookIdsInvolved->push(intval($mapItem->model->book_id));
259             $bookIdsInvolved->push(intval($mapItem->book));
260         });
261
262         // Get the books involved in the sort
263         $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
264         $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
265         // Throw permission error if invalid ids or inaccessible books given.
266         if (count($bookIdsInvolved) !== count($booksInvolved)) {
267             $this->showPermissionError();
268         }
269         // Check permissions of involved books
270         $booksInvolved->each(function (Book $book) {
271              $this->checkOwnablePermission('book-update', $book);
272         });
273
274         // Perform the sort
275         $sortMap->each(function ($mapItem) {
276             $model = $mapItem->model;
277
278             $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
279             $bookChanged = intval($model->book_id) !== intval($mapItem->book);
280             $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
281
282             if ($bookChanged) {
283                 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
284             }
285             if ($chapterChanged) {
286                 $model->chapter_id = intval($mapItem->parentChapter);
287                 $model->save();
288             }
289             if ($priorityChanged) {
290                 $model->priority = intval($mapItem->sort);
291                 $model->save();
292             }
293         });
294
295         // Rebuild permissions and add activity for involved books.
296         $booksInvolved->each(function (Book $book) {
297             $this->entityRepo->buildJointPermissionsForBook($book);
298             Activity::add($book, 'book_sort', $book->id);
299         });
300
301         return redirect($book->getUrl());
302     }
303
304     /**
305      * Remove the specified book from storage.
306      * @param $bookSlug
307      * @return Response
308      */
309     public function destroy($bookSlug)
310     {
311         $book = $this->entityRepo->getBySlug('book', $bookSlug);
312         $this->checkOwnablePermission('book-delete', $book);
313         Activity::addMessage('book_delete', 0, $book->name);
314         $this->entityRepo->destroyBook($book);
315         return redirect('/books');
316     }
317
318     /**
319      * Show the Restrictions view.
320      * @param $bookSlug
321      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
322      */
323     public function showPermissions($bookSlug)
324     {
325         $book = $this->entityRepo->getBySlug('book', $bookSlug);
326         $this->checkOwnablePermission('restrictions-manage', $book);
327         $roles = $this->userRepo->getRestrictableRoles();
328         return view('books.permissions', [
329             'book' => $book,
330             'roles' => $roles
331         ]);
332     }
333
334     /**
335      * Set the restrictions for this book.
336      * @param $bookSlug
337      * @param Request $request
338      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
339      * @throws \BookStack\Exceptions\NotFoundException
340      * @throws \Throwable
341      */
342     public function permissions($bookSlug, Request $request)
343     {
344         $book = $this->entityRepo->getBySlug('book', $bookSlug);
345         $this->checkOwnablePermission('restrictions-manage', $book);
346         $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
347         session()->flash('success', trans('entities.books_permissions_updated'));
348         return redirect($book->getUrl());
349     }
350
351     /**
352      * Export a book as a PDF file.
353      * @param string $bookSlug
354      * @return mixed
355      */
356     public function exportPdf($bookSlug)
357     {
358         $book = $this->entityRepo->getBySlug('book', $bookSlug);
359         $pdfContent = $this->exportService->bookToPdf($book);
360         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
361     }
362
363     /**
364      * Export a book as a contained HTML file.
365      * @param string $bookSlug
366      * @return mixed
367      */
368     public function exportHtml($bookSlug)
369     {
370         $book = $this->entityRepo->getBySlug('book', $bookSlug);
371         $htmlContent = $this->exportService->bookToContainedHtml($book);
372         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
373     }
374
375     /**
376      * Export a book as a plain text file.
377      * @param $bookSlug
378      * @return mixed
379      */
380     public function exportPlainText($bookSlug)
381     {
382         $book = $this->entityRepo->getBySlug('book', $bookSlug);
383         $textContent = $this->exportService->bookToPlainText($book);
384         return $this->downloadResponse($textContent, $bookSlug . '.txt');
385     }
386 }