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 Illuminate\Http\Request;
19 use Illuminate\Validation\ValidationException;
22 class BookController extends Controller
24 protected BookRepo $bookRepo;
25 protected ShelfContext $shelfContext;
26 protected ReferenceFetcher $referenceFetcher;
28 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo, ReferenceFetcher $referenceFetcher)
30 $this->bookRepo = $bookRepo;
31 $this->shelfContext = $entityContextManager;
32 $this->referenceFetcher = $referenceFetcher;
36 * Display a listing of the book.
38 public function index()
40 $view = setting()->getForCurrentUser('books_view_type');
41 $sort = setting()->getForCurrentUser('books_sort', 'name');
42 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
44 $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
45 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
46 $popular = $this->bookRepo->getPopular(4);
47 $new = $this->bookRepo->getRecentlyCreated(4);
49 $this->shelfContext->clearShelfContext();
51 $this->setPageTitle(trans('entities.books'));
53 return view('books.index', [
55 'recents' => $recents,
56 'popular' => $popular,
65 * Show the form for creating a new book.
67 public function create(string $shelfSlug = null)
69 $this->checkPermission('book-create-all');
72 if ($shelfSlug !== null) {
73 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
74 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
77 $this->setPageTitle(trans('entities.books_create'));
79 return view('books.create', [
80 'bookshelf' => $bookshelf,
85 * Store a newly created book in storage.
87 * @throws ImageUploadException
88 * @throws ValidationException
90 public function store(Request $request, string $shelfSlug = null)
92 $this->checkPermission('book-create-all');
93 $validated = $this->validate($request, [
94 'name' => ['required', 'string', 'max:255'],
95 'description' => ['string', 'max:1000'],
96 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
101 if ($shelfSlug !== null) {
102 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
103 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
106 $book = $this->bookRepo->create($validated);
109 $bookshelf->appendBook($book);
110 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
113 return redirect($book->getUrl());
117 * Display the specified book.
119 public function show(Request $request, ActivityQueries $activities, string $slug)
121 $book = $this->bookRepo->getBySlug($slug);
122 $bookChildren = (new BookContents($book))->getTree(true);
123 $bookParentShelves = $book->shelves()->scopes('visible')->get();
125 View::incrementFor($book);
126 if ($request->has('shelf')) {
127 $this->shelfContext->setShelfContext(intval($request->get('shelf')));
130 $this->setPageTitle($book->getShortName());
132 return view('books.show', [
135 'bookChildren' => $bookChildren,
136 'bookParentShelves' => $bookParentShelves,
137 'activity' => $activities->entityActivity($book, 20, 1),
138 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($book),
143 * Show the form for editing the specified book.
145 public function edit(string $slug)
147 $book = $this->bookRepo->getBySlug($slug);
148 $this->checkOwnablePermission('book-update', $book);
149 $this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
151 return view('books.edit', ['book' => $book, 'current' => $book]);
155 * Update the specified book in storage.
157 * @throws ImageUploadException
158 * @throws ValidationException
161 public function update(Request $request, string $slug)
163 $book = $this->bookRepo->getBySlug($slug);
164 $this->checkOwnablePermission('book-update', $book);
166 $validated = $this->validate($request, [
167 'name' => ['required', 'string', 'max:255'],
168 'description' => ['string', 'max:1000'],
169 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
173 if ($request->has('image_reset')) {
174 $validated['image'] = null;
175 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
176 unset($validated['image']);
179 $book = $this->bookRepo->update($book, $validated);
181 return redirect($book->getUrl());
185 * Shows the page to confirm deletion.
187 public function showDelete(string $bookSlug)
189 $book = $this->bookRepo->getBySlug($bookSlug);
190 $this->checkOwnablePermission('book-delete', $book);
191 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
193 return view('books.delete', ['book' => $book, 'current' => $book]);
197 * Remove the specified book from the system.
201 public function destroy(string $bookSlug)
203 $book = $this->bookRepo->getBySlug($bookSlug);
204 $this->checkOwnablePermission('book-delete', $book);
206 $this->bookRepo->destroy($book);
208 return redirect('/books');
212 * Show the view to copy a book.
214 * @throws NotFoundException
216 public function showCopy(string $bookSlug)
218 $book = $this->bookRepo->getBySlug($bookSlug);
219 $this->checkOwnablePermission('book-view', $book);
221 session()->flashInput(['name' => $book->name]);
223 return view('books.copy', [
229 * Create a copy of a book within the requested target destination.
231 * @throws NotFoundException
233 public function copy(Request $request, Cloner $cloner, string $bookSlug)
235 $book = $this->bookRepo->getBySlug($bookSlug);
236 $this->checkOwnablePermission('book-view', $book);
237 $this->checkPermission('book-create-all');
239 $newName = $request->get('name') ?: $book->name;
240 $bookCopy = $cloner->cloneBook($book, $newName);
241 $this->showSuccessNotification(trans('entities.books_copy_success'));
243 return redirect($bookCopy->getUrl());
247 * Convert the chapter to a book.
249 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
251 $book = $this->bookRepo->getBySlug($bookSlug);
252 $this->checkOwnablePermission('book-update', $book);
253 $this->checkOwnablePermission('book-delete', $book);
254 $this->checkPermission('bookshelf-create-all');
255 $this->checkPermission('book-create-all');
257 $shelf = $transformer->transformBookToShelf($book);
259 return redirect($shelf->getUrl());