3 namespace BookStack\Entities\Controllers;
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\Queries\BookQueries;
10 use BookStack\Entities\Queries\BookshelfQueries;
11 use BookStack\Entities\Repos\BookRepo;
12 use BookStack\Entities\Tools\BookContents;
13 use BookStack\Entities\Tools\Cloner;
14 use BookStack\Entities\Tools\HierarchyTransformer;
15 use BookStack\Entities\Tools\ShelfContext;
16 use BookStack\Exceptions\ImageUploadException;
17 use BookStack\Exceptions\NotFoundException;
18 use BookStack\Facades\Activity;
19 use BookStack\Http\Controller;
20 use BookStack\References\ReferenceFetcher;
21 use BookStack\Util\SimpleListOptions;
22 use Illuminate\Http\Request;
23 use Illuminate\Validation\ValidationException;
26 class BookController extends Controller
28 public function __construct(
29 protected ShelfContext $shelfContext,
30 protected BookRepo $bookRepo,
31 protected BookQueries $queries,
32 protected BookshelfQueries $shelfQueries,
33 protected 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->queries->visibleForListWithCover()
50 ->orderBy($listOptions->getSort(), $listOptions->getOrder())
52 $recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->take(4)->get() : false;
53 $popular = $this->queries->popularForList()->take(4)->get();
54 $new = $this->queries->visibleForList()->orderBy('created_at', 'desc')->take(4)->get();
56 $this->shelfContext->clearShelfContext();
58 $this->setPageTitle(trans('entities.books'));
60 return view('books.index', [
62 'recents' => $recents,
63 'popular' => $popular,
66 'listOptions' => $listOptions,
71 * Show the form for creating a new book.
73 public function create(string $shelfSlug = null)
75 $this->checkPermission('book-create-all');
78 if ($shelfSlug !== null) {
79 $bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
80 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
83 $this->setPageTitle(trans('entities.books_create'));
85 return view('books.create', [
86 'bookshelf' => $bookshelf,
91 * Store a newly created book in storage.
93 * @throws ImageUploadException
94 * @throws ValidationException
96 public function store(Request $request, string $shelfSlug = null)
98 $this->checkPermission('book-create-all');
99 $validated = $this->validate($request, [
100 'name' => ['required', 'string', 'max:255'],
101 'description_html' => ['string', 'max:2000'],
102 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
104 'default_template_id' => ['nullable', 'integer'],
108 if ($shelfSlug !== null) {
109 $bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
110 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
113 $book = $this->bookRepo->create($validated);
116 $bookshelf->appendBook($book);
117 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
120 return redirect($book->getUrl());
124 * Display the specified book.
126 public function show(Request $request, ActivityQueries $activities, string $slug)
128 $book = $this->queries->findVisibleBySlugOrFail($slug);
129 $bookChildren = (new BookContents($book))->getTree(true);
130 $bookParentShelves = $book->shelves()->scopes('visible')->get();
132 View::incrementFor($book);
133 if ($request->has('shelf')) {
134 $this->shelfContext->setShelfContext(intval($request->get('shelf')));
137 $this->setPageTitle($book->getShortName());
139 return view('books.show', [
142 'bookChildren' => $bookChildren,
143 'bookParentShelves' => $bookParentShelves,
144 'watchOptions' => new UserEntityWatchOptions(user(), $book),
145 'activity' => $activities->entityActivity($book, 20, 1),
146 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($book),
151 * Show the form for editing the specified book.
153 public function edit(string $slug)
155 $book = $this->queries->findVisibleBySlugOrFail($slug);
156 $this->checkOwnablePermission('book-update', $book);
157 $this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
159 return view('books.edit', ['book' => $book, 'current' => $book]);
163 * Update the specified book in storage.
165 * @throws ImageUploadException
166 * @throws ValidationException
169 public function update(Request $request, string $slug)
171 $book = $this->queries->findVisibleBySlugOrFail($slug);
172 $this->checkOwnablePermission('book-update', $book);
174 $validated = $this->validate($request, [
175 'name' => ['required', 'string', 'max:255'],
176 'description_html' => ['string', 'max:2000'],
177 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
179 'default_template_id' => ['nullable', 'integer'],
182 if ($request->has('image_reset')) {
183 $validated['image'] = null;
184 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
185 unset($validated['image']);
188 $book = $this->bookRepo->update($book, $validated);
190 return redirect($book->getUrl());
194 * Shows the page to confirm deletion.
196 public function showDelete(string $bookSlug)
198 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
199 $this->checkOwnablePermission('book-delete', $book);
200 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
202 return view('books.delete', ['book' => $book, 'current' => $book]);
206 * Remove the specified book from the system.
210 public function destroy(string $bookSlug)
212 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
213 $this->checkOwnablePermission('book-delete', $book);
215 $this->bookRepo->destroy($book);
217 return redirect('/books');
221 * Show the view to copy a book.
223 * @throws NotFoundException
225 public function showCopy(string $bookSlug)
227 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
228 $this->checkOwnablePermission('book-view', $book);
230 session()->flashInput(['name' => $book->name]);
232 return view('books.copy', [
238 * Create a copy of a book within the requested target destination.
240 * @throws NotFoundException
242 public function copy(Request $request, Cloner $cloner, string $bookSlug)
244 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
245 $this->checkOwnablePermission('book-view', $book);
246 $this->checkPermission('book-create-all');
248 $newName = $request->get('name') ?: $book->name;
249 $bookCopy = $cloner->cloneBook($book, $newName);
250 $this->showSuccessNotification(trans('entities.books_copy_success'));
252 return redirect($bookCopy->getUrl());
256 * Convert the chapter to a book.
258 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
260 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
261 $this->checkOwnablePermission('book-update', $book);
262 $this->checkOwnablePermission('book-delete', $book);
263 $this->checkPermission('bookshelf-create-all');
264 $this->checkPermission('book-create-all');
266 $shelf = $transformer->transformBookToShelf($book);
268 return redirect($shelf->getUrl());