]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Updated a bunch of book views
[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\Repos\EntityRepo;
7 use BookStack\Entities\ExportService;
8 use Illuminate\Http\Request;
9 use Illuminate\Http\Response;
10 use Views;
11
12 class BookController extends Controller
13 {
14
15     protected $entityRepo;
16     protected $userRepo;
17     protected $exportService;
18
19     /**
20      * BookController constructor.
21      * @param EntityRepo $entityRepo
22      * @param \BookStack\Auth\UserRepo $userRepo
23      * @param \BookStack\Entities\ExportService $exportService
24      */
25     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
26     {
27         $this->entityRepo = $entityRepo;
28         $this->userRepo = $userRepo;
29         $this->exportService = $exportService;
30         parent::__construct();
31     }
32
33     /**
34      * Display a listing of the book.
35      * @return Response
36      */
37     public function index()
38     {
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');
42         $sortOptions = [
43             'name' => trans('common.sort_name'),
44             'created_at' => trans('common.sort_created_at'),
45             'updated_at' => trans('common.sort_updated_at'),
46         ];
47
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);
52
53         $this->setPageTitle(trans('entities.books'));
54         return view('books/index', [
55             'books' => $books,
56             'recents' => $recents,
57             'popular' => $popular,
58             'new' => $new,
59             'view' => $view,
60             'sort' => $sort,
61             'order' => $order,
62             'sortOptions' => $sortOptions,
63         ]);
64     }
65
66     /**
67      * Show the form for creating a new book.
68      * @return Response
69      */
70     public function create()
71     {
72         $this->checkPermission('book-create-all');
73         $this->setPageTitle(trans('entities.books_create'));
74         return view('books/create');
75     }
76
77     /**
78      * Store a newly created book in storage.
79      *
80      * @param  Request $request
81      * @return Response
82      */
83     public function store(Request $request)
84     {
85         $this->checkPermission('book-create-all');
86         $this->validate($request, [
87             'name' => 'required|string|max:255',
88             'description' => 'string|max:1000'
89         ]);
90         $book = $this->entityRepo->createFromInput('book', $request->all());
91         Activity::add($book, 'book_create', $book->id);
92         return redirect($book->getUrl());
93     }
94
95     /**
96      * Display the specified book.
97      * @param $slug
98      * @return Response
99      */
100     public function show($slug)
101     {
102         $book = $this->entityRepo->getBySlug('book', $slug);
103         $this->checkOwnablePermission('book-view', $book);
104         $bookChildren = $this->entityRepo->getBookChildren($book);
105         Views::add($book);
106         $this->setPageTitle($book->getShortName());
107         return view('books/show', [
108             'book' => $book,
109             'current' => $book,
110             'bookChildren' => $bookChildren,
111             'activity' => Activity::entityActivity($book, 20, 0)
112         ]);
113     }
114
115     /**
116      * Show the form for editing the specified book.
117      * @param $slug
118      * @return Response
119      */
120     public function edit($slug)
121     {
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]);
126     }
127
128     /**
129      * Update the specified book in storage.
130      * @param  Request $request
131      * @param          $slug
132      * @return Response
133      */
134     public function update(Request $request, $slug)
135     {
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'
141         ]);
142          $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
143          Activity::add($book, 'book_update', $book->id);
144          return redirect($book->getUrl());
145     }
146
147     /**
148      * Shows the page to confirm deletion
149      * @param $bookSlug
150      * @return \Illuminate\View\View
151      */
152     public function showDelete($bookSlug)
153     {
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]);
158     }
159
160     /**
161      * Shows the view which allows pages to be re-ordered and sorted.
162      * @param string $bookSlug
163      * @return \Illuminate\View\View
164      */
165     public function sort($bookSlug)
166     {
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]);
173     }
174
175     /**
176      * Shows the sort box for a single book.
177      * Used via AJAX when loading in extra books to a sort.
178      * @param $bookSlug
179      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
180      */
181     public function getSortItem($bookSlug)
182     {
183         $book = $this->entityRepo->getBySlug('book', $bookSlug);
184         $bookChildren = $this->entityRepo->getBookChildren($book);
185         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
186     }
187
188     /**
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
193      */
194     public function saveSort($bookSlug, Request $request)
195     {
196         $book = $this->entityRepo->getBySlug('book', $bookSlug);
197         $this->checkOwnablePermission('book-update', $book);
198
199         // Return if no map sent
200         if (!$request->filled('sort-tree')) {
201             return redirect($book->getUrl());
202         }
203
204         // Sort pages and chapters
205         $sortMap = collect(json_decode($request->get('sort-tree')));
206         $bookIdsInvolved = collect([$book->id]);
207
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));
215         });
216
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();
223         }
224         // Check permissions of involved books
225         $booksInvolved->each(function (Book $book) {
226              $this->checkOwnablePermission('book-update', $book);
227         });
228
229         // Perform the sort
230         $sortMap->each(function ($mapItem) {
231             $model = $mapItem->model;
232
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;
236
237             if ($bookChanged) {
238                 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
239             }
240             if ($chapterChanged) {
241                 $model->chapter_id = intval($mapItem->parentChapter);
242                 $model->save();
243             }
244             if ($priorityChanged) {
245                 $model->priority = intval($mapItem->sort);
246                 $model->save();
247             }
248         });
249
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);
254         });
255
256         return redirect($book->getUrl());
257     }
258
259     /**
260      * Remove the specified book from storage.
261      * @param $bookSlug
262      * @return Response
263      */
264     public function destroy($bookSlug)
265     {
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');
271     }
272
273     /**
274      * Show the Restrictions view.
275      * @param $bookSlug
276      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
277      */
278     public function showPermissions($bookSlug)
279     {
280         $book = $this->entityRepo->getBySlug('book', $bookSlug);
281         $this->checkOwnablePermission('restrictions-manage', $book);
282         $roles = $this->userRepo->getRestrictableRoles();
283         return view('books.permissions', [
284             'book' => $book,
285             'roles' => $roles
286         ]);
287     }
288
289     /**
290      * Set the restrictions for this book.
291      * @param $bookSlug
292      * @param Request $request
293      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
294      * @throws \BookStack\Exceptions\NotFoundException
295      * @throws \Throwable
296      */
297     public function permissions($bookSlug, Request $request)
298     {
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());
304     }
305
306     /**
307      * Export a book as a PDF file.
308      * @param string $bookSlug
309      * @return mixed
310      */
311     public function exportPdf($bookSlug)
312     {
313         $book = $this->entityRepo->getBySlug('book', $bookSlug);
314         $pdfContent = $this->exportService->bookToPdf($book);
315         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
316     }
317
318     /**
319      * Export a book as a contained HTML file.
320      * @param string $bookSlug
321      * @return mixed
322      */
323     public function exportHtml($bookSlug)
324     {
325         $book = $this->entityRepo->getBySlug('book', $bookSlug);
326         $htmlContent = $this->exportService->bookToContainedHtml($book);
327         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
328     }
329
330     /**
331      * Export a book as a plain text file.
332      * @param $bookSlug
333      * @return mixed
334      */
335     public function exportPlainText($bookSlug)
336     {
337         $book = $this->entityRepo->getBySlug('book', $bookSlug);
338         $textContent = $this->exportService->bookToPlainText($book);
339         return $this->downloadResponse($textContent, $bookSlug . '.txt');
340     }
341 }