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