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