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\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;
25 class BookController extends Controller
27 public function __construct(
28 protected ShelfContext $shelfContext,
29 protected BookRepo $bookRepo,
30 protected ReferenceFetcher $referenceFetcher
35 * Display a listing of the book.
37 public function index(Request $request)
39 $view = setting()->getForCurrentUser('books_view_type');
40 $listOptions = SimpleListOptions::fromRequest($request, 'books')->withSortOptions([
41 'name' => trans('common.sort_name'),
42 'created_at' => trans('common.sort_created_at'),
43 'updated_at' => trans('common.sort_updated_at'),
46 $books = $this->bookRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
47 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
48 $popular = $this->bookRepo->getPopular(4);
49 $new = $this->bookRepo->getRecentlyCreated(4);
51 $this->shelfContext->clearShelfContext();
53 $this->setPageTitle(trans('entities.books'));
55 return view('books.index', [
57 'recents' => $recents,
58 'popular' => $popular,
61 'listOptions' => $listOptions,
66 * Show the form for creating a new book.
68 public function create(string $shelfSlug = null)
70 $this->checkPermission('book-create-all');
73 if ($shelfSlug !== null) {
74 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
75 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
78 $this->setPageTitle(trans('entities.books_create'));
80 return view('books.create', [
81 'bookshelf' => $bookshelf,
86 * Store a newly created book in storage.
88 * @throws ImageUploadException
89 * @throws ValidationException
91 public function store(Request $request, string $shelfSlug = null)
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()),
99 'default_template' => ['nullable', 'integer'],
103 if ($shelfSlug !== null) {
104 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
105 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
108 $book = $this->bookRepo->create($validated);
111 $bookshelf->appendBook($book);
112 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
115 return redirect($book->getUrl());
119 * Display the specified book.
121 public function show(Request $request, ActivityQueries $activities, string $slug)
123 $book = $this->bookRepo->getBySlug($slug);
124 $bookChildren = (new BookContents($book))->getTree(true);
125 $bookParentShelves = $book->shelves()->scopes('visible')->get();
127 View::incrementFor($book);
128 if ($request->has('shelf')) {
129 $this->shelfContext->setShelfContext(intval($request->get('shelf')));
132 $this->setPageTitle($book->getShortName());
134 return view('books.show', [
137 'bookChildren' => $bookChildren,
138 'bookParentShelves' => $bookParentShelves,
139 'watchOptions' => new UserEntityWatchOptions(user(), $book),
140 'activity' => $activities->entityActivity($book, 20, 1),
141 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($book),
146 * Show the form for editing the specified book.
148 public function edit(string $slug)
150 $book = $this->bookRepo->getBySlug($slug);
151 $this->checkOwnablePermission('book-update', $book);
152 $this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
154 return view('books.edit', ['book' => $book, 'current' => $book]);
158 * Update the specified book in storage.
160 * @throws ImageUploadException
161 * @throws ValidationException
164 public function update(Request $request, string $slug)
166 $book = $this->bookRepo->getBySlug($slug);
167 $this->checkOwnablePermission('book-update', $book);
169 $validated = $this->validate($request, [
170 'name' => ['required', 'string', 'max:255'],
171 'description' => ['string', 'max:1000'],
172 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
174 'default_template' => ['nullable', 'integer'],
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());