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