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 protected BookRepo $bookRepo;
28 protected ShelfContext $shelfContext;
29 protected ReferenceFetcher $referenceFetcher;
31 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo, ReferenceFetcher $referenceFetcher)
33 $this->bookRepo = $bookRepo;
34 $this->shelfContext = $entityContextManager;
35 $this->referenceFetcher = $referenceFetcher;
39 * Display a listing of the book.
41 public function index(Request $request)
43 $view = setting()->getForCurrentUser('books_view_type');
44 $listOptions = SimpleListOptions::fromRequest($request, 'books')->withSortOptions([
45 'name' => trans('common.sort_name'),
46 'created_at' => trans('common.sort_created_at'),
47 'updated_at' => trans('common.sort_updated_at'),
50 $books = $this->bookRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
51 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
52 $popular = $this->bookRepo->getPopular(4);
53 $new = $this->bookRepo->getRecentlyCreated(4);
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' => ['string', 'max:1000'],
101 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
106 if ($shelfSlug !== null) {
107 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
108 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
111 $book = $this->bookRepo->create($validated);
114 $bookshelf->appendBook($book);
115 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
118 return redirect($book->getUrl());
122 * Display the specified book.
124 public function show(Request $request, ActivityQueries $activities, string $slug)
126 $book = $this->bookRepo->getBySlug($slug);
127 $bookChildren = (new BookContents($book))->getTree(true);
128 $bookParentShelves = $book->shelves()->scopes('visible')->get();
130 View::incrementFor($book);
131 if ($request->has('shelf')) {
132 $this->shelfContext->setShelfContext(intval($request->get('shelf')));
135 $this->setPageTitle($book->getShortName());
137 return view('books.show', [
140 'bookChildren' => $bookChildren,
141 'bookParentShelves' => $bookParentShelves,
142 'watchOptions' => new UserEntityWatchOptions(user(), $book),
143 'activity' => $activities->entityActivity($book, 20, 1),
144 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($book),
149 * Show the form for editing the specified book.
151 public function edit(string $slug)
153 $book = $this->bookRepo->getBySlug($slug);
154 $this->checkOwnablePermission('book-update', $book);
155 $this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
157 return view('books.edit', ['book' => $book, 'current' => $book]);
161 * Update the specified book in storage.
163 * @throws ImageUploadException
164 * @throws ValidationException
167 public function update(Request $request, string $slug)
169 $book = $this->bookRepo->getBySlug($slug);
170 $this->checkOwnablePermission('book-update', $book);
172 $validated = $this->validate($request, [
173 'name' => ['required', 'string', 'max:255'],
174 'description' => ['string', 'max:1000'],
175 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
179 if ($request->has('image_reset')) {
180 $validated['image'] = null;
181 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
182 unset($validated['image']);
185 $book = $this->bookRepo->update($book, $validated);
187 return redirect($book->getUrl());
191 * Shows the page to confirm deletion.
193 public function showDelete(string $bookSlug)
195 $book = $this->bookRepo->getBySlug($bookSlug);
196 $this->checkOwnablePermission('book-delete', $book);
197 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
199 return view('books.delete', ['book' => $book, 'current' => $book]);
203 * Remove the specified book from the system.
207 public function destroy(string $bookSlug)
209 $book = $this->bookRepo->getBySlug($bookSlug);
210 $this->checkOwnablePermission('book-delete', $book);
212 $this->bookRepo->destroy($book);
214 return redirect('/books');
218 * Show the view to copy a book.
220 * @throws NotFoundException
222 public function showCopy(string $bookSlug)
224 $book = $this->bookRepo->getBySlug($bookSlug);
225 $this->checkOwnablePermission('book-view', $book);
227 session()->flashInput(['name' => $book->name]);
229 return view('books.copy', [
235 * Create a copy of a book within the requested target destination.
237 * @throws NotFoundException
239 public function copy(Request $request, Cloner $cloner, string $bookSlug)
241 $book = $this->bookRepo->getBySlug($bookSlug);
242 $this->checkOwnablePermission('book-view', $book);
243 $this->checkPermission('book-create-all');
245 $newName = $request->get('name') ?: $book->name;
246 $bookCopy = $cloner->cloneBook($book, $newName);
247 $this->showSuccessNotification(trans('entities.books_copy_success'));
249 return redirect($bookCopy->getUrl());
253 * Convert the chapter to a book.
255 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
257 $book = $this->bookRepo->getBySlug($bookSlug);
258 $this->checkOwnablePermission('book-update', $book);
259 $this->checkOwnablePermission('book-delete', $book);
260 $this->checkPermission('bookshelf-create-all');
261 $this->checkPermission('book-create-all');
263 $shelf = $transformer->transformBookToShelf($book);
265 return redirect($shelf->getUrl());