]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Added code editor changes mobile design handling
[bookstack] / app / Http / Controllers / BookController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityQueries;
6 use BookStack\Actions\ActivityType;
7 use BookStack\Actions\View;
8 use BookStack\Entities\Models\Bookshelf;
9 use BookStack\Entities\Repos\BookRepo;
10 use BookStack\Entities\Tools\BookContents;
11 use BookStack\Entities\Tools\Cloner;
12 use BookStack\Entities\Tools\HierarchyTransformer;
13 use BookStack\Entities\Tools\PermissionsUpdater;
14 use BookStack\Entities\Tools\ShelfContext;
15 use BookStack\Exceptions\ImageUploadException;
16 use BookStack\Exceptions\NotFoundException;
17 use BookStack\Facades\Activity;
18 use Illuminate\Http\Request;
19 use Illuminate\Validation\ValidationException;
20 use Throwable;
21
22 class BookController extends Controller
23 {
24     protected $bookRepo;
25     protected $entityContextManager;
26
27     public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
28     {
29         $this->bookRepo = $bookRepo;
30         $this->entityContextManager = $entityContextManager;
31     }
32
33     /**
34      * Display a listing of the book.
35      */
36     public function index()
37     {
38         $view = setting()->getForCurrentUser('books_view_type');
39         $sort = setting()->getForCurrentUser('books_sort', 'name');
40         $order = setting()->getForCurrentUser('books_sort_order', 'asc');
41
42         $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
43         $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
44         $popular = $this->bookRepo->getPopular(4);
45         $new = $this->bookRepo->getRecentlyCreated(4);
46
47         $this->entityContextManager->clearShelfContext();
48
49         $this->setPageTitle(trans('entities.books'));
50
51         return view('books.index', [
52             'books'   => $books,
53             'recents' => $recents,
54             'popular' => $popular,
55             'new'     => $new,
56             'view'    => $view,
57             'sort'    => $sort,
58             'order'   => $order,
59         ]);
60     }
61
62     /**
63      * Show the form for creating a new book.
64      */
65     public function create(string $shelfSlug = null)
66     {
67         $this->checkPermission('book-create-all');
68
69         $bookshelf = null;
70         if ($shelfSlug !== null) {
71             $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
72             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
73         }
74
75         $this->setPageTitle(trans('entities.books_create'));
76
77         return view('books.create', [
78             'bookshelf' => $bookshelf,
79         ]);
80     }
81
82     /**
83      * Store a newly created book in storage.
84      *
85      * @throws ImageUploadException
86      * @throws ValidationException
87      */
88     public function store(Request $request, string $shelfSlug = null)
89     {
90         $this->checkPermission('book-create-all');
91         $this->validate($request, [
92             'name'        => ['required', 'string', 'max:255'],
93             'description' => ['string', 'max:1000'],
94             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
95         ]);
96
97         $bookshelf = null;
98         if ($shelfSlug !== null) {
99             $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
100             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
101         }
102
103         $book = $this->bookRepo->create($request->all());
104
105         if ($bookshelf) {
106             $bookshelf->appendBook($book);
107             Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
108         }
109
110         return redirect($book->getUrl());
111     }
112
113     /**
114      * Display the specified book.
115      */
116     public function show(Request $request, ActivityQueries $activities, string $slug)
117     {
118         $book = $this->bookRepo->getBySlug($slug);
119         $bookChildren = (new BookContents($book))->getTree(true);
120         $bookParentShelves = $book->shelves()->scopes('visible')->get();
121
122         View::incrementFor($book);
123         if ($request->has('shelf')) {
124             $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
125         }
126
127         $this->setPageTitle($book->getShortName());
128
129         return view('books.show', [
130             'book'              => $book,
131             'current'           => $book,
132             'bookChildren'      => $bookChildren,
133             'bookParentShelves' => $bookParentShelves,
134             'activity'          => $activities->entityActivity($book, 20, 1),
135         ]);
136     }
137
138     /**
139      * Show the form for editing the specified book.
140      */
141     public function edit(string $slug)
142     {
143         $book = $this->bookRepo->getBySlug($slug);
144         $this->checkOwnablePermission('book-update', $book);
145         $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
146
147         return view('books.edit', ['book' => $book, 'current' => $book]);
148     }
149
150     /**
151      * Update the specified book in storage.
152      *
153      * @throws ImageUploadException
154      * @throws ValidationException
155      * @throws Throwable
156      */
157     public function update(Request $request, string $slug)
158     {
159         $book = $this->bookRepo->getBySlug($slug);
160         $this->checkOwnablePermission('book-update', $book);
161
162         $validated = $this->validate($request, [
163             'name'        => ['required', 'string', 'max:255'],
164             'description' => ['string', 'max:1000'],
165             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
166         ]);
167
168         if ($request->has('image_reset')) {
169             $validated['image'] = null;
170         } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
171             unset($validated['image']);
172         }
173
174         $book = $this->bookRepo->update($book, $validated);
175
176         return redirect($book->getUrl());
177     }
178
179     /**
180      * Shows the page to confirm deletion.
181      */
182     public function showDelete(string $bookSlug)
183     {
184         $book = $this->bookRepo->getBySlug($bookSlug);
185         $this->checkOwnablePermission('book-delete', $book);
186         $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
187
188         return view('books.delete', ['book' => $book, 'current' => $book]);
189     }
190
191     /**
192      * Remove the specified book from the system.
193      *
194      * @throws Throwable
195      */
196     public function destroy(string $bookSlug)
197     {
198         $book = $this->bookRepo->getBySlug($bookSlug);
199         $this->checkOwnablePermission('book-delete', $book);
200
201         $this->bookRepo->destroy($book);
202
203         return redirect('/books');
204     }
205
206     /**
207      * Show the permissions view.
208      */
209     public function showPermissions(string $bookSlug)
210     {
211         $book = $this->bookRepo->getBySlug($bookSlug);
212         $this->checkOwnablePermission('restrictions-manage', $book);
213
214         return view('books.permissions', [
215             'book' => $book,
216         ]);
217     }
218
219     /**
220      * Set the restrictions for this book.
221      *
222      * @throws Throwable
223      */
224     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
225     {
226         $book = $this->bookRepo->getBySlug($bookSlug);
227         $this->checkOwnablePermission('restrictions-manage', $book);
228
229         $permissionsUpdater->updateFromPermissionsForm($book, $request);
230
231         $this->showSuccessNotification(trans('entities.books_permissions_updated'));
232
233         return redirect($book->getUrl());
234     }
235
236     /**
237      * Show the view to copy a book.
238      *
239      * @throws NotFoundException
240      */
241     public function showCopy(string $bookSlug)
242     {
243         $book = $this->bookRepo->getBySlug($bookSlug);
244         $this->checkOwnablePermission('book-view', $book);
245
246         session()->flashInput(['name' => $book->name]);
247
248         return view('books.copy', [
249             'book' => $book,
250         ]);
251     }
252
253     /**
254      * Create a copy of a book within the requested target destination.
255      *
256      * @throws NotFoundException
257      */
258     public function copy(Request $request, Cloner $cloner, string $bookSlug)
259     {
260         $book = $this->bookRepo->getBySlug($bookSlug);
261         $this->checkOwnablePermission('book-view', $book);
262         $this->checkPermission('book-create-all');
263
264         $newName = $request->get('name') ?: $book->name;
265         $bookCopy = $cloner->cloneBook($book, $newName);
266         $this->showSuccessNotification(trans('entities.books_copy_success'));
267
268         return redirect($bookCopy->getUrl());
269     }
270
271     /**
272      * Convert the chapter to a book.
273      */
274     public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
275     {
276         $book = $this->bookRepo->getBySlug($bookSlug);
277         $this->checkOwnablePermission('book-update', $book);
278         $this->checkOwnablePermission('book-delete', $book);
279         $this->checkPermission('bookshelf-create-all');
280         $this->checkPermission('book-create-all');
281
282         $shelf = $transformer->transformBookToShelf($book);
283
284         return redirect($shelf->getUrl());
285     }
286 }