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