]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Standardised view referencing to dot-notation
[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, 1)
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      * @throws \BookStack\Exceptions\NotFoundException
165      */
166     public function sort($bookSlug)
167     {
168         $book = $this->entityRepo->getBySlug('book', $bookSlug);
169         $this->checkOwnablePermission('book-update', $book);
170
171         $bookChildren = $this->entityRepo->getBookChildren($book, true);
172
173         $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
174         return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
175     }
176
177     /**
178      * Shows the sort box for a single book.
179      * Used via AJAX when loading in extra books to a sort.
180      * @param $bookSlug
181      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
182      */
183     public function getSortItem($bookSlug)
184     {
185         $book = $this->entityRepo->getBySlug('book', $bookSlug);
186         $bookChildren = $this->entityRepo->getBookChildren($book);
187         return view('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
188     }
189
190     /**
191      * Saves an array of sort mapping to pages and chapters.
192      * @param  string $bookSlug
193      * @param Request $request
194      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
195      */
196     public function saveSort($bookSlug, Request $request)
197     {
198         $book = $this->entityRepo->getBySlug('book', $bookSlug);
199         $this->checkOwnablePermission('book-update', $book);
200
201         // Return if no map sent
202         if (!$request->filled('sort-tree')) {
203             return redirect($book->getUrl());
204         }
205
206         // Sort pages and chapters
207         $sortMap = collect(json_decode($request->get('sort-tree')));
208         $bookIdsInvolved = collect([$book->id]);
209
210         // Load models into map
211         $sortMap->each(function ($mapItem) use ($bookIdsInvolved) {
212             $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
213             $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
214             // Store source and target books
215             $bookIdsInvolved->push(intval($mapItem->model->book_id));
216             $bookIdsInvolved->push(intval($mapItem->book));
217         });
218
219         // Get the books involved in the sort
220         $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
221         $booksInvolved = $this->entityRepo->getManyById('book', $bookIdsInvolved, false, true);
222         // Throw permission error if invalid ids or inaccessible books given.
223         if (count($bookIdsInvolved) !== count($booksInvolved)) {
224             $this->showPermissionError();
225         }
226         // Check permissions of involved books
227         $booksInvolved->each(function (Book $book) {
228              $this->checkOwnablePermission('book-update', $book);
229         });
230
231         // Perform the sort
232         $sortMap->each(function ($mapItem) {
233             $model = $mapItem->model;
234
235             $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
236             $bookChanged = intval($model->book_id) !== intval($mapItem->book);
237             $chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
238
239             if ($bookChanged) {
240                 $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
241             }
242             if ($chapterChanged) {
243                 $model->chapter_id = intval($mapItem->parentChapter);
244                 $model->save();
245             }
246             if ($priorityChanged) {
247                 $model->priority = intval($mapItem->sort);
248                 $model->save();
249             }
250         });
251
252         // Rebuild permissions and add activity for involved books.
253         $booksInvolved->each(function (Book $book) {
254             $this->entityRepo->buildJointPermissionsForBook($book);
255             Activity::add($book, 'book_sort', $book->id);
256         });
257
258         return redirect($book->getUrl());
259     }
260
261     /**
262      * Remove the specified book from storage.
263      * @param $bookSlug
264      * @return Response
265      */
266     public function destroy($bookSlug)
267     {
268         $book = $this->entityRepo->getBySlug('book', $bookSlug);
269         $this->checkOwnablePermission('book-delete', $book);
270         Activity::addMessage('book_delete', 0, $book->name);
271         $this->entityRepo->destroyBook($book);
272         return redirect('/books');
273     }
274
275     /**
276      * Show the Restrictions view.
277      * @param $bookSlug
278      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
279      */
280     public function showPermissions($bookSlug)
281     {
282         $book = $this->entityRepo->getBySlug('book', $bookSlug);
283         $this->checkOwnablePermission('restrictions-manage', $book);
284         $roles = $this->userRepo->getRestrictableRoles();
285         return view('books.permissions', [
286             'book' => $book,
287             'roles' => $roles
288         ]);
289     }
290
291     /**
292      * Set the restrictions for this book.
293      * @param $bookSlug
294      * @param Request $request
295      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
296      * @throws \BookStack\Exceptions\NotFoundException
297      * @throws \Throwable
298      */
299     public function permissions($bookSlug, Request $request)
300     {
301         $book = $this->entityRepo->getBySlug('book', $bookSlug);
302         $this->checkOwnablePermission('restrictions-manage', $book);
303         $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
304         session()->flash('success', trans('entities.books_permissions_updated'));
305         return redirect($book->getUrl());
306     }
307
308     /**
309      * Export a book as a PDF file.
310      * @param string $bookSlug
311      * @return mixed
312      */
313     public function exportPdf($bookSlug)
314     {
315         $book = $this->entityRepo->getBySlug('book', $bookSlug);
316         $pdfContent = $this->exportService->bookToPdf($book);
317         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
318     }
319
320     /**
321      * Export a book as a contained HTML file.
322      * @param string $bookSlug
323      * @return mixed
324      */
325     public function exportHtml($bookSlug)
326     {
327         $book = $this->entityRepo->getBySlug('book', $bookSlug);
328         $htmlContent = $this->exportService->bookToContainedHtml($book);
329         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
330     }
331
332     /**
333      * Export a book as a plain text file.
334      * @param $bookSlug
335      * @return mixed
336      */
337     public function exportPlainText($bookSlug)
338     {
339         $book = $this->entityRepo->getBySlug('book', $bookSlug);
340         $textContent = $this->exportService->bookToPlainText($book);
341         return $this->downloadResponse($textContent, $bookSlug . '.txt');
342     }
343 }