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\PermissionsUpdater;
14 use BookStack\Entities\Tools\ShelfContext;
15 use BookStack\Exceptions\ImageUploadException;
16 use BookStack\Exceptions\NotFoundException;
17 use BookStack\Facades\Activity;
18 use Illuminate\Http\Request;
19 use Illuminate\Validation\ValidationException;
22 class BookController extends Controller
25 protected $entityContextManager;
27 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
29 $this->bookRepo = $bookRepo;
30 $this->entityContextManager = $entityContextManager;
34 * Display a listing of the book.
36 public function index()
38 $view = setting()->getForCurrentUser('books_view_type');
39 $sort = setting()->getForCurrentUser('books_sort', 'name');
40 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
42 $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
43 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
44 $popular = $this->bookRepo->getPopular(4);
45 $new = $this->bookRepo->getRecentlyCreated(4);
47 $this->entityContextManager->clearShelfContext();
49 $this->setPageTitle(trans('entities.books'));
51 return view('books.index', [
53 'recents' => $recents,
54 'popular' => $popular,
63 * Show the form for creating a new book.
65 public function create(string $shelfSlug = null)
67 $this->checkPermission('book-create-all');
70 if ($shelfSlug !== null) {
71 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
72 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
75 $this->setPageTitle(trans('entities.books_create'));
77 return view('books.create', [
78 'bookshelf' => $bookshelf,
83 * Store a newly created book in storage.
85 * @throws ImageUploadException
86 * @throws ValidationException
88 public function store(Request $request, string $shelfSlug = null)
90 $this->checkPermission('book-create-all');
91 $validated = $this->validate($request, [
92 'name' => ['required', 'string', 'max:255'],
93 'description' => ['string', 'max:1000'],
94 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
99 if ($shelfSlug !== null) {
100 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
101 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
104 $book = $this->bookRepo->create($validated);
107 $bookshelf->appendBook($book);
108 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
111 return redirect($book->getUrl());
115 * Display the specified book.
117 public function show(Request $request, ActivityQueries $activities, string $slug)
119 $book = $this->bookRepo->getBySlug($slug);
120 $bookChildren = (new BookContents($book))->getTree(true);
121 $bookParentShelves = $book->shelves()->scopes('visible')->get();
123 View::incrementFor($book);
124 if ($request->has('shelf')) {
125 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
128 $this->setPageTitle($book->getShortName());
130 return view('books.show', [
133 'bookChildren' => $bookChildren,
134 'bookParentShelves' => $bookParentShelves,
135 'activity' => $activities->entityActivity($book, 20, 1),
140 * Show the form for editing the specified book.
142 public function edit(string $slug)
144 $book = $this->bookRepo->getBySlug($slug);
145 $this->checkOwnablePermission('book-update', $book);
146 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
148 return view('books.edit', ['book' => $book, 'current' => $book]);
152 * Update the specified book in storage.
154 * @throws ImageUploadException
155 * @throws ValidationException
158 public function update(Request $request, string $slug)
160 $book = $this->bookRepo->getBySlug($slug);
161 $this->checkOwnablePermission('book-update', $book);
163 $validated = $this->validate($request, [
164 'name' => ['required', 'string', 'max:255'],
165 'description' => ['string', 'max:1000'],
166 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
170 if ($request->has('image_reset')) {
171 $validated['image'] = null;
172 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
173 unset($validated['image']);
176 $book = $this->bookRepo->update($book, $validated);
178 return redirect($book->getUrl());
182 * Shows the page to confirm deletion.
184 public function showDelete(string $bookSlug)
186 $book = $this->bookRepo->getBySlug($bookSlug);
187 $this->checkOwnablePermission('book-delete', $book);
188 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
190 return view('books.delete', ['book' => $book, 'current' => $book]);
194 * Remove the specified book from the system.
198 public function destroy(string $bookSlug)
200 $book = $this->bookRepo->getBySlug($bookSlug);
201 $this->checkOwnablePermission('book-delete', $book);
203 $this->bookRepo->destroy($book);
205 return redirect('/books');
209 * Show the permissions view.
211 public function showPermissions(string $bookSlug)
213 $book = $this->bookRepo->getBySlug($bookSlug);
214 $this->checkOwnablePermission('restrictions-manage', $book);
216 return view('books.permissions', [
222 * Set the restrictions for this book.
226 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
228 $book = $this->bookRepo->getBySlug($bookSlug);
229 $this->checkOwnablePermission('restrictions-manage', $book);
231 $permissionsUpdater->updateFromPermissionsForm($book, $request);
233 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
235 return redirect($book->getUrl());
239 * Show the view to copy a book.
241 * @throws NotFoundException
243 public function showCopy(string $bookSlug)
245 $book = $this->bookRepo->getBySlug($bookSlug);
246 $this->checkOwnablePermission('book-view', $book);
248 session()->flashInput(['name' => $book->name]);
250 return view('books.copy', [
256 * Create a copy of a book within the requested target destination.
258 * @throws NotFoundException
260 public function copy(Request $request, Cloner $cloner, string $bookSlug)
262 $book = $this->bookRepo->getBySlug($bookSlug);
263 $this->checkOwnablePermission('book-view', $book);
264 $this->checkPermission('book-create-all');
266 $newName = $request->get('name') ?: $book->name;
267 $bookCopy = $cloner->cloneBook($book, $newName);
268 $this->showSuccessNotification(trans('entities.books_copy_success'));
270 return redirect($bookCopy->getUrl());
274 * Convert the chapter to a book.
276 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
278 $book = $this->bookRepo->getBySlug($bookSlug);
279 $this->checkOwnablePermission('book-update', $book);
280 $this->checkOwnablePermission('book-delete', $book);
281 $this->checkPermission('bookshelf-create-all');
282 $this->checkPermission('book-create-all');
284 $shelf = $transformer->transformBookToShelf($book);
286 return redirect($shelf->getUrl());