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