]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Applied StyleCI changes
[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         $validated = $this->validate($request, [
92             'name'        => ['required', 'string', 'max:255'],
93             'description' => ['string', 'max:1000'],
94             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
95             'tags'        => ['array'],
96         ]);
97
98         $bookshelf = null;
99         if ($shelfSlug !== null) {
100             $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
101             $this->checkOwnablePermission('bookshelf-update', $bookshelf);
102         }
103
104         $book = $this->bookRepo->create($validated);
105
106         if ($bookshelf) {
107             $bookshelf->appendBook($book);
108             Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
109         }
110
111         return redirect($book->getUrl());
112     }
113
114     /**
115      * Display the specified book.
116      */
117     public function show(Request $request, ActivityQueries $activities, string $slug)
118     {
119         $book = $this->bookRepo->getBySlug($slug);
120         $bookChildren = (new BookContents($book))->getTree(true);
121         $bookParentShelves = $book->shelves()->scopes('visible')->get();
122
123         View::incrementFor($book);
124         if ($request->has('shelf')) {
125             $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
126         }
127
128         $this->setPageTitle($book->getShortName());
129
130         return view('books.show', [
131             'book'              => $book,
132             'current'           => $book,
133             'bookChildren'      => $bookChildren,
134             'bookParentShelves' => $bookParentShelves,
135             'activity'          => $activities->entityActivity($book, 20, 1),
136         ]);
137     }
138
139     /**
140      * Show the form for editing the specified book.
141      */
142     public function edit(string $slug)
143     {
144         $book = $this->bookRepo->getBySlug($slug);
145         $this->checkOwnablePermission('book-update', $book);
146         $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
147
148         return view('books.edit', ['book' => $book, 'current' => $book]);
149     }
150
151     /**
152      * Update the specified book in storage.
153      *
154      * @throws ImageUploadException
155      * @throws ValidationException
156      * @throws Throwable
157      */
158     public function update(Request $request, string $slug)
159     {
160         $book = $this->bookRepo->getBySlug($slug);
161         $this->checkOwnablePermission('book-update', $book);
162
163         $validated = $this->validate($request, [
164             'name'        => ['required', 'string', 'max:255'],
165             'description' => ['string', 'max:1000'],
166             'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
167             'tags'        => ['array'],
168         ]);
169
170         if ($request->has('image_reset')) {
171             $validated['image'] = null;
172         } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
173             unset($validated['image']);
174         }
175
176         $book = $this->bookRepo->update($book, $validated);
177
178         return redirect($book->getUrl());
179     }
180
181     /**
182      * Shows the page to confirm deletion.
183      */
184     public function showDelete(string $bookSlug)
185     {
186         $book = $this->bookRepo->getBySlug($bookSlug);
187         $this->checkOwnablePermission('book-delete', $book);
188         $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
189
190         return view('books.delete', ['book' => $book, 'current' => $book]);
191     }
192
193     /**
194      * Remove the specified book from the system.
195      *
196      * @throws Throwable
197      */
198     public function destroy(string $bookSlug)
199     {
200         $book = $this->bookRepo->getBySlug($bookSlug);
201         $this->checkOwnablePermission('book-delete', $book);
202
203         $this->bookRepo->destroy($book);
204
205         return redirect('/books');
206     }
207
208     /**
209      * Show the permissions view.
210      */
211     public function showPermissions(string $bookSlug)
212     {
213         $book = $this->bookRepo->getBySlug($bookSlug);
214         $this->checkOwnablePermission('restrictions-manage', $book);
215
216         return view('books.permissions', [
217             'book' => $book,
218         ]);
219     }
220
221     /**
222      * Set the restrictions for this book.
223      *
224      * @throws Throwable
225      */
226     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
227     {
228         $book = $this->bookRepo->getBySlug($bookSlug);
229         $this->checkOwnablePermission('restrictions-manage', $book);
230
231         $permissionsUpdater->updateFromPermissionsForm($book, $request);
232
233         $this->showSuccessNotification(trans('entities.books_permissions_updated'));
234
235         return redirect($book->getUrl());
236     }
237
238     /**
239      * Show the view to copy a book.
240      *
241      * @throws NotFoundException
242      */
243     public function showCopy(string $bookSlug)
244     {
245         $book = $this->bookRepo->getBySlug($bookSlug);
246         $this->checkOwnablePermission('book-view', $book);
247
248         session()->flashInput(['name' => $book->name]);
249
250         return view('books.copy', [
251             'book' => $book,
252         ]);
253     }
254
255     /**
256      * Create a copy of a book within the requested target destination.
257      *
258      * @throws NotFoundException
259      */
260     public function copy(Request $request, Cloner $cloner, string $bookSlug)
261     {
262         $book = $this->bookRepo->getBySlug($bookSlug);
263         $this->checkOwnablePermission('book-view', $book);
264         $this->checkPermission('book-create-all');
265
266         $newName = $request->get('name') ?: $book->name;
267         $bookCopy = $cloner->cloneBook($book, $newName);
268         $this->showSuccessNotification(trans('entities.books_copy_success'));
269
270         return redirect($bookCopy->getUrl());
271     }
272
273     /**
274      * Convert the chapter to a book.
275      */
276     public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
277     {
278         $book = $this->bookRepo->getBySlug($bookSlug);
279         $this->checkOwnablePermission('book-update', $book);
280         $this->checkOwnablePermission('book-delete', $book);
281         $this->checkPermission('bookshelf-create-all');
282         $this->checkPermission('book-create-all');
283
284         $shelf = $transformer->transformBookToShelf($book);
285
286         return redirect($shelf->getUrl());
287     }
288 }