]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Add APP_LOGGING
[bookstack] / app / Http / Controllers / BookController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Repos\EntityRepo;
5 use BookStack\Repos\UserRepo;
6 use BookStack\Services\ExportService;
7 use Illuminate\Http\Request;
8 use Illuminate\Http\Response;
9 use Views;
10
11 class BookController extends Controller
12 {
13
14     protected $entityRepo;
15     protected $userRepo;
16     protected $exportService;
17
18     /**
19      * BookController constructor.
20      * @param EntityRepo $entityRepo
21      * @param UserRepo $userRepo
22      * @param ExportService $exportService
23      */
24     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
25     {
26         $this->entityRepo = $entityRepo;
27         $this->userRepo = $userRepo;
28         $this->exportService = $exportService;
29         parent::__construct();
30     }
31
32     /**
33      * Display a listing of the book.
34      * @return Response
35      */
36     public function index()
37     {
38         $books = $this->entityRepo->getAllPaginated('book', 10);
39         $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
40         $popular = $this->entityRepo->getPopular('book', 4, 0);
41         $this->setPageTitle('Books');
42         return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
43     }
44
45     /**
46      * Show the form for creating a new book.
47      * @return Response
48      */
49     public function create()
50     {
51         $this->checkPermission('book-create-all');
52         $this->setPageTitle(trans('entities.books_create'));
53         return view('books/create');
54     }
55
56     /**
57      * Store a newly created book in storage.
58      *
59      * @param  Request $request
60      * @return Response
61      */
62     public function store(Request $request)
63     {
64         $this->checkPermission('book-create-all');
65         $this->validate($request, [
66             'name' => 'required|string|max:255',
67             'description' => 'string|max:1000'
68         ]);
69         $book = $this->entityRepo->createFromInput('book', $request->all());
70         Activity::add($book, 'book_create', $book->id);
71         return redirect($book->getUrl());
72     }
73
74     /**
75      * Display the specified book.
76      * @param $slug
77      * @return Response
78      */
79     public function show($slug)
80     {
81         $book = $this->entityRepo->getBySlug('book', $slug);
82         $this->checkOwnablePermission('book-view', $book);
83         $bookChildren = $this->entityRepo->getBookChildren($book);
84         Views::add($book);
85         $this->setPageTitle($book->getShortName());
86         return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
87     }
88
89     /**
90      * Show the form for editing the specified book.
91      * @param $slug
92      * @return Response
93      */
94     public function edit($slug)
95     {
96         $book = $this->entityRepo->getBySlug('book', $slug);
97         $this->checkOwnablePermission('book-update', $book);
98         $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
99         return view('books/edit', ['book' => $book, 'current' => $book]);
100     }
101
102     /**
103      * Update the specified book in storage.
104      * @param  Request $request
105      * @param          $slug
106      * @return Response
107      */
108     public function update(Request $request, $slug)
109     {
110         $book = $this->entityRepo->getBySlug('book', $slug);
111         $this->checkOwnablePermission('book-update', $book);
112         $this->validate($request, [
113             'name' => 'required|string|max:255',
114             'description' => 'string|max:1000'
115         ]);
116         $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
117         Activity::add($book, 'book_update', $book->id);
118         return redirect($book->getUrl());
119     }
120
121     /**
122      * Shows the page to confirm deletion
123      * @param $bookSlug
124      * @return \Illuminate\View\View
125      */
126     public function showDelete($bookSlug)
127     {
128         $book = $this->entityRepo->getBySlug('book', $bookSlug);
129         $this->checkOwnablePermission('book-delete', $book);
130         $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
131         return view('books/delete', ['book' => $book, 'current' => $book]);
132     }
133
134     /**
135      * Shows the view which allows pages to be re-ordered and sorted.
136      * @param string $bookSlug
137      * @return \Illuminate\View\View
138      */
139     public function sort($bookSlug)
140     {
141         $book = $this->entityRepo->getBySlug('book', $bookSlug);
142         $this->checkOwnablePermission('book-update', $book);
143         $bookChildren = $this->entityRepo->getBookChildren($book, true);
144         $books = $this->entityRepo->getAll('book', false);
145         $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
146         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
147     }
148
149     /**
150      * Shows the sort box for a single book.
151      * Used via AJAX when loading in extra books to a sort.
152      * @param $bookSlug
153      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
154      */
155     public function getSortItem($bookSlug)
156     {
157         $book = $this->entityRepo->getBySlug('book', $bookSlug);
158         $bookChildren = $this->entityRepo->getBookChildren($book);
159         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
160     }
161
162     /**
163      * Saves an array of sort mapping to pages and chapters.
164      * @param  string $bookSlug
165      * @param Request $request
166      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
167      */
168     public function saveSort($bookSlug, Request $request)
169     {
170         $book = $this->entityRepo->getBySlug('book', $bookSlug);
171         $this->checkOwnablePermission('book-update', $book);
172
173         // Return if no map sent
174         if (!$request->has('sort-tree')) {
175             return redirect($book->getUrl());
176         }
177
178         // Sort pages and chapters
179         $sortedBooks = [];
180         $updatedModels = collect();
181         $sortMap = json_decode($request->get('sort-tree'));
182         $defaultBookId = $book->id;
183
184         // Loop through contents of provided map and update entities accordingly
185         foreach ($sortMap as $bookChild) {
186             $priority = $bookChild->sort;
187             $id = intval($bookChild->id);
188             $isPage = $bookChild->type == 'page';
189             $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
190             $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
191             $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
192
193             // Update models only if there's a change in parent chain or ordering.
194             if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
195                 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
196                 $model->priority = $priority;
197                 if ($isPage) $model->chapter_id = $chapterId;
198                 $model->save();
199                 $updatedModels->push($model);
200             }
201
202             // Store involved books to be sorted later
203             if (!in_array($bookId, $sortedBooks)) {
204                 $sortedBooks[] = $bookId;
205             }
206         }
207
208         // Add activity for books
209         foreach ($sortedBooks as $bookId) {
210             $updatedBook = $this->entityRepo->getById('book', $bookId);
211             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
212         }
213
214         // Update permissions on changed models
215         if (count($updatedModels) === 0) $this->entityRepo->buildJointPermissions($updatedModels);
216
217         return redirect($book->getUrl());
218     }
219
220     /**
221      * Remove the specified book from storage.
222      * @param $bookSlug
223      * @return Response
224      */
225     public function destroy($bookSlug)
226     {
227         $book = $this->entityRepo->getBySlug('book', $bookSlug);
228         $this->checkOwnablePermission('book-delete', $book);
229         Activity::addMessage('book_delete', 0, $book->name);
230         $this->entityRepo->destroyBook($book);
231         return redirect('/books');
232     }
233
234     /**
235      * Show the Restrictions view.
236      * @param $bookSlug
237      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
238      */
239     public function showRestrict($bookSlug)
240     {
241         $book = $this->entityRepo->getBySlug('book', $bookSlug);
242         $this->checkOwnablePermission('restrictions-manage', $book);
243         $roles = $this->userRepo->getRestrictableRoles();
244         return view('books/restrictions', [
245             'book' => $book,
246             'roles' => $roles
247         ]);
248     }
249
250     /**
251      * Set the restrictions for this book.
252      * @param $bookSlug
253      * @param $bookSlug
254      * @param Request $request
255      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
256      */
257     public function restrict($bookSlug, Request $request)
258     {
259         $book = $this->entityRepo->getBySlug('book', $bookSlug);
260         $this->checkOwnablePermission('restrictions-manage', $book);
261         $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
262         session()->flash('success', trans('entities.books_permissions_updated'));
263         return redirect($book->getUrl());
264     }
265
266     /**
267      * Export a book as a PDF file.
268      * @param string $bookSlug
269      * @return mixed
270      */
271     public function exportPdf($bookSlug)
272     {
273         $book = $this->entityRepo->getBySlug('book', $bookSlug);
274         $pdfContent = $this->exportService->bookToPdf($book);
275         return response()->make($pdfContent, 200, [
276             'Content-Type'        => 'application/octet-stream',
277             'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
278         ]);
279     }
280
281     /**
282      * Export a book as a contained HTML file.
283      * @param string $bookSlug
284      * @return mixed
285      */
286     public function exportHtml($bookSlug)
287     {
288         $book = $this->entityRepo->getBySlug('book', $bookSlug);
289         $htmlContent = $this->exportService->bookToContainedHtml($book);
290         return response()->make($htmlContent, 200, [
291             'Content-Type'        => 'application/octet-stream',
292             'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
293         ]);
294     }
295
296     /**
297      * Export a book as a plain text file.
298      * @param $bookSlug
299      * @return mixed
300      */
301     public function exportPlainText($bookSlug)
302     {
303         $book = $this->entityRepo->getBySlug('book', $bookSlug);
304         $htmlContent = $this->exportService->bookToPlainText($book);
305         return response()->make($htmlContent, 200, [
306             'Content-Type'        => 'application/octet-stream',
307             'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'
308         ]);
309     }
310 }