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