3 namespace BookStack\Entities\Controllers;
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;
24 class BookController extends Controller
26 protected BookRepo $bookRepo;
27 protected ShelfContext $shelfContext;
28 protected ReferenceFetcher $referenceFetcher;
30 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo, ReferenceFetcher $referenceFetcher)
32 $this->bookRepo = $bookRepo;
33 $this->shelfContext = $entityContextManager;
34 $this->referenceFetcher = $referenceFetcher;
38 * Display a listing of the book.
40 public function index(Request $request)
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'),
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);
54 $this->shelfContext->clearShelfContext();
56 $this->setPageTitle(trans('entities.books'));
58 return view('books.index', [
60 'recents' => $recents,
61 'popular' => $popular,
64 'listOptions' => $listOptions,
69 * Show the form for creating a new book.
71 public function create(string $shelfSlug = null)
73 $this->checkPermission('book-create-all');
76 if ($shelfSlug !== null) {
77 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
78 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
81 $this->setPageTitle(trans('entities.books_create'));
83 return view('books.create', [
84 'bookshelf' => $bookshelf,
89 * Store a newly created book in storage.
91 * @throws ImageUploadException
92 * @throws ValidationException
94 public function store(Request $request, string $shelfSlug = null)
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()),
105 if ($shelfSlug !== null) {
106 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
107 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
110 $book = $this->bookRepo->create($validated);
113 $bookshelf->appendBook($book);
114 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
117 return redirect($book->getUrl());
121 * Display the specified book.
123 public function show(Request $request, ActivityQueries $activities, string $slug)
125 $book = $this->bookRepo->getBySlug($slug);
126 $bookChildren = (new BookContents($book))->getTree(true);
127 $bookParentShelves = $book->shelves()->scopes('visible')->get();
129 View::incrementFor($book);
130 if ($request->has('shelf')) {
131 $this->shelfContext->setShelfContext(intval($request->get('shelf')));
134 $this->setPageTitle($book->getShortName());
136 return view('books.show', [
139 'bookChildren' => $bookChildren,
140 'bookParentShelves' => $bookParentShelves,
141 'activity' => $activities->entityActivity($book, 20, 1),
142 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($book),
147 * Show the form for editing the specified book.
149 public function edit(string $slug)
151 $book = $this->bookRepo->getBySlug($slug);
152 $this->checkOwnablePermission('book-update', $book);
153 $this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
155 return view('books.edit', ['book' => $book, 'current' => $book]);
159 * Update the specified book in storage.
161 * @throws ImageUploadException
162 * @throws ValidationException
165 public function update(Request $request, string $slug)
167 $book = $this->bookRepo->getBySlug($slug);
168 $this->checkOwnablePermission('book-update', $book);
170 $validated = $this->validate($request, [
171 'name' => ['required', 'string', 'max:255'],
172 'description' => ['string', 'max:1000'],
173 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
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']);
183 $book = $this->bookRepo->update($book, $validated);
185 return redirect($book->getUrl());
189 * Shows the page to confirm deletion.
191 public function showDelete(string $bookSlug)
193 $book = $this->bookRepo->getBySlug($bookSlug);
194 $this->checkOwnablePermission('book-delete', $book);
195 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
197 return view('books.delete', ['book' => $book, 'current' => $book]);
201 * Remove the specified book from the system.
205 public function destroy(string $bookSlug)
207 $book = $this->bookRepo->getBySlug($bookSlug);
208 $this->checkOwnablePermission('book-delete', $book);
210 $this->bookRepo->destroy($book);
212 return redirect('/books');
216 * Show the view to copy a book.
218 * @throws NotFoundException
220 public function showCopy(string $bookSlug)
222 $book = $this->bookRepo->getBySlug($bookSlug);
223 $this->checkOwnablePermission('book-view', $book);
225 session()->flashInput(['name' => $book->name]);
227 return view('books.copy', [
233 * Create a copy of a book within the requested target destination.
235 * @throws NotFoundException
237 public function copy(Request $request, Cloner $cloner, string $bookSlug)
239 $book = $this->bookRepo->getBySlug($bookSlug);
240 $this->checkOwnablePermission('book-view', $book);
241 $this->checkPermission('book-create-all');
243 $newName = $request->get('name') ?: $book->name;
244 $bookCopy = $cloner->cloneBook($book, $newName);
245 $this->showSuccessNotification(trans('entities.books_copy_success'));
247 return redirect($bookCopy->getUrl());
251 * Convert the chapter to a book.
253 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
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');
261 $shelf = $transformer->transformBookToShelf($book);
263 return redirect($shelf->getUrl());