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