3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityQueries;
6 use BookStack\Actions\ActivityType;
7 use BookStack\Actions\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\References\ReferenceFetcher;
18 use BookStack\Util\SimpleListOptions;
19 use Illuminate\Http\Request;
20 use Illuminate\Validation\ValidationException;
23 class BookController extends Controller
25 protected BookRepo $bookRepo;
26 protected ShelfContext $shelfContext;
27 protected ReferenceFetcher $referenceFetcher;
29 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo, ReferenceFetcher $referenceFetcher)
31 $this->bookRepo = $bookRepo;
32 $this->shelfContext = $entityContextManager;
33 $this->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->bookRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
49 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
50 $popular = $this->bookRepo->getPopular(4);
51 $new = $this->bookRepo->getRecentlyCreated(4);
53 $this->shelfContext->clearShelfContext();
55 $this->setPageTitle(trans('entities.books'));
57 return view('books.index', [
59 'recents' => $recents,
60 'popular' => $popular,
63 'listOptions' => $listOptions,
68 * Show the form for creating a new book.
70 public function create(string $shelfSlug = null)
72 $this->checkPermission('book-create-all');
75 if ($shelfSlug !== null) {
76 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
77 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
80 $this->setPageTitle(trans('entities.books_create'));
82 return view('books.create', [
83 'bookshelf' => $bookshelf,
88 * Store a newly created book in storage.
90 * @throws ImageUploadException
91 * @throws ValidationException
93 public function store(Request $request, string $shelfSlug = null)
95 $this->checkPermission('book-create-all');
96 $validated = $this->validate($request, [
97 'name' => ['required', 'string', 'max:255'],
98 'description' => ['string', 'max:1000'],
99 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
104 if ($shelfSlug !== null) {
105 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
106 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
109 $book = $this->bookRepo->create($validated);
112 $bookshelf->appendBook($book);
113 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
116 return redirect($book->getUrl());
120 * Display the specified book.
122 public function show(Request $request, ActivityQueries $activities, string $slug)
124 $book = $this->bookRepo->getBySlug($slug);
125 $bookChildren = (new BookContents($book))->getTree(true);
126 $bookParentShelves = $book->shelves()->scopes('visible')->get();
128 View::incrementFor($book);
129 if ($request->has('shelf')) {
130 $this->shelfContext->setShelfContext(intval($request->get('shelf')));
133 $this->setPageTitle($book->getShortName());
135 return view('books.show', [
138 'bookChildren' => $bookChildren,
139 'bookParentShelves' => $bookParentShelves,
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()),
176 if ($request->has('image_reset')) {
177 $validated['image'] = null;
178 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
179 unset($validated['image']);
182 $book = $this->bookRepo->update($book, $validated);
184 return redirect($book->getUrl());
188 * Shows the page to confirm deletion.
190 public function showDelete(string $bookSlug)
192 $book = $this->bookRepo->getBySlug($bookSlug);
193 $this->checkOwnablePermission('book-delete', $book);
194 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
196 return view('books.delete', ['book' => $book, 'current' => $book]);
200 * Remove the specified book from the system.
204 public function destroy(string $bookSlug)
206 $book = $this->bookRepo->getBySlug($bookSlug);
207 $this->checkOwnablePermission('book-delete', $book);
209 $this->bookRepo->destroy($book);
211 return redirect('/books');
215 * Show the view to copy a book.
217 * @throws NotFoundException
219 public function showCopy(string $bookSlug)
221 $book = $this->bookRepo->getBySlug($bookSlug);
222 $this->checkOwnablePermission('book-view', $book);
224 session()->flashInput(['name' => $book->name]);
226 return view('books.copy', [
232 * Create a copy of a book within the requested target destination.
234 * @throws NotFoundException
236 public function copy(Request $request, Cloner $cloner, string $bookSlug)
238 $book = $this->bookRepo->getBySlug($bookSlug);
239 $this->checkOwnablePermission('book-view', $book);
240 $this->checkPermission('book-create-all');
242 $newName = $request->get('name') ?: $book->name;
243 $bookCopy = $cloner->cloneBook($book, $newName);
244 $this->showSuccessNotification(trans('entities.books_copy_success'));
246 return redirect($bookCopy->getUrl());
250 * Convert the chapter to a book.
252 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
254 $book = $this->bookRepo->getBySlug($bookSlug);
255 $this->checkOwnablePermission('book-update', $book);
256 $this->checkOwnablePermission('book-delete', $book);
257 $this->checkPermission('bookshelf-create-all');
258 $this->checkPermission('book-create-all');
260 $shelf = $transformer->transformBookToShelf($book);
262 return redirect($shelf->getUrl());