]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
#47 - Fixes the issues with the test case.
[bookstack] / app / Http / Controllers / BookController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Book;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Repos\UserRepo;
7 use BookStack\Services\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 UserRepo $userRepo
23      * @param 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         $books = $this->entityRepo->getAllPaginated('book', 10);
40         $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
41         $popular = $this->entityRepo->getPopular('book', 4, 0);
42         $this->setPageTitle('Books');
43         return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
44     }
45
46     /**
47      * Show the form for creating a new book.
48      * @return Response
49      */
50     public function create()
51     {
52         $this->checkPermission('book-create-all');
53         $this->setPageTitle(trans('entities.books_create'));
54         return view('books/create');
55     }
56
57     /**
58      * Store a newly created book in storage.
59      *
60      * @param  Request $request
61      * @return Response
62      */
63     public function store(Request $request)
64     {
65         $this->checkPermission('book-create-all');
66         $this->validate($request, [
67             'name' => 'required|string|max:255',
68             'description' => 'string|max:1000'
69         ]);
70         $book = $this->entityRepo->createFromInput('book', $request->all());
71         Activity::add($book, 'book_create', $book->id);
72         return redirect($book->getUrl());
73     }
74
75     /**
76      * Display the specified book.
77      * @param $slug
78      * @return Response
79      */
80     public function show($slug)
81     {
82         $book = $this->entityRepo->getBySlug('book', $slug);
83         $this->checkOwnablePermission('book-view', $book);
84         $bookChildren = $this->entityRepo->getBookChildren($book);
85         Views::add($book);
86         $this->setPageTitle($book->getShortName());
87         return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
88     }
89
90     /**
91      * Show the form for editing the specified book.
92      * @param $slug
93      * @return Response
94      */
95     public function edit($slug)
96     {
97         $book = $this->entityRepo->getBySlug('book', $slug);
98         $this->checkOwnablePermission('book-update', $book);
99         $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
100         return view('books/edit', ['book' => $book, 'current' => $book]);
101     }
102
103     /**
104      * Update the specified book in storage.
105      * @param  Request $request
106      * @param          $slug
107      * @return Response
108      */
109     public function update(Request $request, $slug)
110     {
111         $book = $this->entityRepo->getBySlug('book', $slug);
112         $this->checkOwnablePermission('book-update', $book);
113         $this->validate($request, [
114             'name' => 'required|string|max:255',
115             'description' => 'string|max:1000'
116         ]);
117         $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
118         Activity::add($book, 'book_update', $book->id);
119         return redirect($book->getUrl());
120     }
121
122     /**
123      * Shows the page to confirm deletion
124      * @param $bookSlug
125      * @return \Illuminate\View\View
126      */
127     public function showDelete($bookSlug)
128     {
129         $book = $this->entityRepo->getBySlug('book', $bookSlug);
130         $this->checkOwnablePermission('book-delete', $book);
131         $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
132         return view('books/delete', ['book' => $book, 'current' => $book]);
133     }
134
135     /**
136      * Shows the view which allows pages to be re-ordered and sorted.
137      * @param string $bookSlug
138      * @return \Illuminate\View\View
139      */
140     public function sort($bookSlug)
141     {
142         $book = $this->entityRepo->getBySlug('book', $bookSlug);
143         $this->checkOwnablePermission('book-update', $book);
144         $bookChildren = $this->entityRepo->getBookChildren($book, true);
145         $books = $this->entityRepo->getAll('book', false);
146         $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
147         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
148     }
149
150     /**
151      * Shows the sort box for a single book.
152      * Used via AJAX when loading in extra books to a sort.
153      * @param $bookSlug
154      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
155      */
156     public function getSortItem($bookSlug)
157     {
158         $book = $this->entityRepo->getBySlug('book', $bookSlug);
159         $bookChildren = $this->entityRepo->getBookChildren($book);
160         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
161     }
162
163     /**
164      * Saves an array of sort mapping to pages and chapters.
165      * @param  string $bookSlug
166      * @param Request $request
167      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
168      */
169     public function saveSort($bookSlug, Request $request)
170     {
171         $book = $this->entityRepo->getBySlug('book', $bookSlug);
172         $this->checkOwnablePermission('book-update', $book);
173
174         // Return if no map sent
175         if (!$request->has('sort-tree')) {
176             return redirect($book->getUrl());
177         }
178
179         // Sort pages and chapters
180         $sortedBooks = [];
181         $updatedModels = collect();
182         $sortMap = json_decode($request->get('sort-tree'));
183         $defaultBookId = $book->id;
184
185         // Loop through contents of provided map and update entities accordingly
186         foreach ($sortMap as $bookChild) {
187             $priority = $bookChild->sort;
188             $id = intval($bookChild->id);
189             $isPage = $bookChild->type == 'page';
190             $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
191             $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
192             $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
193
194             // Update models only if there's a change in parent chain or ordering.
195             if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
196                 $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
197                 $model->priority = $priority;
198                 if ($isPage) $model->chapter_id = $chapterId;
199                 $model->save();
200                 $updatedModels->push($model);
201             }
202
203             // Store involved books to be sorted later
204             if (!in_array($bookId, $sortedBooks)) {
205                 $sortedBooks[] = $bookId;
206             }
207         }
208
209         // Add activity for books
210         foreach ($sortedBooks as $bookId) {
211             /** @var Book $updatedBook */
212             $updatedBook = $this->entityRepo->getById('book', $bookId);
213             $this->entityRepo->buildJointPermissionsForBook($updatedBook);
214             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
215         }
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 }