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\Queries\BookQueries;
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 ReferenceFetcher $referenceFetcher,
37 * Display a listing of the book.
39 public function index(Request $request)
41 $view = setting()->getForCurrentUser('books_view_type');
42 $listOptions = SimpleListOptions::fromRequest($request, 'books')->withSortOptions([
43 'name' => trans('common.sort_name'),
44 'created_at' => trans('common.sort_created_at'),
45 'updated_at' => trans('common.sort_updated_at'),
48 $books = $this->queries->visibleForListWithCover()
49 ->orderBy($listOptions->getSort(), $listOptions->getOrder())
51 $recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->take(4)->get() : false;
52 $popular = $this->queries->popularForList()->take(4)->get();
53 $new = $this->queries->visibleForList()->orderBy('created_at', 'desc')->take(4)->get();
55 $this->shelfContext->clearShelfContext();
57 $this->setPageTitle(trans('entities.books'));
59 return view('books.index', [
61 'recents' => $recents,
62 'popular' => $popular,
65 'listOptions' => $listOptions,
70 * Show the form for creating a new book.
72 public function create(string $shelfSlug = null)
74 $this->checkPermission('book-create-all');
77 if ($shelfSlug !== null) {
78 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
79 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
82 $this->setPageTitle(trans('entities.books_create'));
84 return view('books.create', [
85 'bookshelf' => $bookshelf,
90 * Store a newly created book in storage.
92 * @throws ImageUploadException
93 * @throws ValidationException
95 public function store(Request $request, string $shelfSlug = null)
97 $this->checkPermission('book-create-all');
98 $validated = $this->validate($request, [
99 'name' => ['required', 'string', 'max:255'],
100 'description_html' => ['string', 'max:2000'],
101 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
103 'default_template_id' => ['nullable', 'integer'],
107 if ($shelfSlug !== null) {
108 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
109 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
112 $book = $this->bookRepo->create($validated);
115 $bookshelf->appendBook($book);
116 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
119 return redirect($book->getUrl());
123 * Display the specified book.
125 public function show(Request $request, ActivityQueries $activities, string $slug)
127 $book = $this->queries->findVisibleBySlugOrFail($slug);
128 $bookChildren = (new BookContents($book))->getTree(true);
129 $bookParentShelves = $book->shelves()->scopes('visible')->get();
131 View::incrementFor($book);
132 if ($request->has('shelf')) {
133 $this->shelfContext->setShelfContext(intval($request->get('shelf')));
136 $this->setPageTitle($book->getShortName());
138 return view('books.show', [
141 'bookChildren' => $bookChildren,
142 'bookParentShelves' => $bookParentShelves,
143 'watchOptions' => new UserEntityWatchOptions(user(), $book),
144 'activity' => $activities->entityActivity($book, 20, 1),
145 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($book),
150 * Show the form for editing the specified book.
152 public function edit(string $slug)
154 $book = $this->queries->findVisibleBySlugOrFail($slug);
155 $this->checkOwnablePermission('book-update', $book);
156 $this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
158 return view('books.edit', ['book' => $book, 'current' => $book]);
162 * Update the specified book in storage.
164 * @throws ImageUploadException
165 * @throws ValidationException
168 public function update(Request $request, string $slug)
170 $book = $this->queries->findVisibleBySlugOrFail($slug);
171 $this->checkOwnablePermission('book-update', $book);
173 $validated = $this->validate($request, [
174 'name' => ['required', 'string', 'max:255'],
175 'description_html' => ['string', 'max:2000'],
176 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
178 'default_template_id' => ['nullable', 'integer'],
181 if ($request->has('image_reset')) {
182 $validated['image'] = null;
183 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
184 unset($validated['image']);
187 $book = $this->bookRepo->update($book, $validated);
189 return redirect($book->getUrl());
193 * Shows the page to confirm deletion.
195 public function showDelete(string $bookSlug)
197 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
198 $this->checkOwnablePermission('book-delete', $book);
199 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
201 return view('books.delete', ['book' => $book, 'current' => $book]);
205 * Remove the specified book from the system.
209 public function destroy(string $bookSlug)
211 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
212 $this->checkOwnablePermission('book-delete', $book);
214 $this->bookRepo->destroy($book);
216 return redirect('/books');
220 * Show the view to copy a book.
222 * @throws NotFoundException
224 public function showCopy(string $bookSlug)
226 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
227 $this->checkOwnablePermission('book-view', $book);
229 session()->flashInput(['name' => $book->name]);
231 return view('books.copy', [
237 * Create a copy of a book within the requested target destination.
239 * @throws NotFoundException
241 public function copy(Request $request, Cloner $cloner, string $bookSlug)
243 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
244 $this->checkOwnablePermission('book-view', $book);
245 $this->checkPermission('book-create-all');
247 $newName = $request->get('name') ?: $book->name;
248 $bookCopy = $cloner->cloneBook($book, $newName);
249 $this->showSuccessNotification(trans('entities.books_copy_success'));
251 return redirect($bookCopy->getUrl());
255 * Convert the chapter to a book.
257 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
259 $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
260 $this->checkOwnablePermission('book-update', $book);
261 $this->checkOwnablePermission('book-delete', $book);
262 $this->checkPermission('bookshelf-create-all');
263 $this->checkPermission('book-create-all');
265 $shelf = $transformer->transformBookToShelf($book);
267 return redirect($shelf->getUrl());