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 $this->validate($request, [
92 'name' => ['required', 'string', 'max:255'],
93 'description' => ['string', 'max:1000'],
94 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
98 if ($shelfSlug !== null) {
99 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
100 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
103 $book = $this->bookRepo->create($request->all());
106 $bookshelf->appendBook($book);
107 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
110 return redirect($book->getUrl());
114 * Display the specified book.
116 public function show(Request $request, ActivityQueries $activities, string $slug)
118 $book = $this->bookRepo->getBySlug($slug);
119 $bookChildren = (new BookContents($book))->getTree(true);
120 $bookParentShelves = $book->shelves()->scopes('visible')->get();
122 View::incrementFor($book);
123 if ($request->has('shelf')) {
124 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
127 $this->setPageTitle($book->getShortName());
129 return view('books.show', [
132 'bookChildren' => $bookChildren,
133 'bookParentShelves' => $bookParentShelves,
134 'activity' => $activities->entityActivity($book, 20, 1),
139 * Show the form for editing the specified book.
141 public function edit(string $slug)
143 $book = $this->bookRepo->getBySlug($slug);
144 $this->checkOwnablePermission('book-update', $book);
145 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
147 return view('books.edit', ['book' => $book, 'current' => $book]);
151 * Update the specified book in storage.
153 * @throws ImageUploadException
154 * @throws ValidationException
157 public function update(Request $request, string $slug)
159 $book = $this->bookRepo->getBySlug($slug);
160 $this->checkOwnablePermission('book-update', $book);
162 $validated = $this->validate($request, [
163 'name' => ['required', 'string', 'max:255'],
164 'description' => ['string', 'max:1000'],
165 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
168 if ($request->has('image_reset')) {
169 $validated['image'] = null;
170 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
171 unset($validated['image']);
174 $book = $this->bookRepo->update($book, $validated);
176 return redirect($book->getUrl());
180 * Shows the page to confirm deletion.
182 public function showDelete(string $bookSlug)
184 $book = $this->bookRepo->getBySlug($bookSlug);
185 $this->checkOwnablePermission('book-delete', $book);
186 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
188 return view('books.delete', ['book' => $book, 'current' => $book]);
192 * Remove the specified book from the system.
196 public function destroy(string $bookSlug)
198 $book = $this->bookRepo->getBySlug($bookSlug);
199 $this->checkOwnablePermission('book-delete', $book);
201 $this->bookRepo->destroy($book);
203 return redirect('/books');
207 * Show the permissions view.
209 public function showPermissions(string $bookSlug)
211 $book = $this->bookRepo->getBySlug($bookSlug);
212 $this->checkOwnablePermission('restrictions-manage', $book);
214 return view('books.permissions', [
220 * Set the restrictions for this book.
224 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
226 $book = $this->bookRepo->getBySlug($bookSlug);
227 $this->checkOwnablePermission('restrictions-manage', $book);
229 $permissionsUpdater->updateFromPermissionsForm($book, $request);
231 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
233 return redirect($book->getUrl());
237 * Show the view to copy a book.
239 * @throws NotFoundException
241 public function showCopy(string $bookSlug)
243 $book = $this->bookRepo->getBySlug($bookSlug);
244 $this->checkOwnablePermission('book-view', $book);
246 session()->flashInput(['name' => $book->name]);
248 return view('books.copy', [
254 * Create a copy of a book within the requested target destination.
256 * @throws NotFoundException
258 public function copy(Request $request, Cloner $cloner, string $bookSlug)
260 $book = $this->bookRepo->getBySlug($bookSlug);
261 $this->checkOwnablePermission('book-view', $book);
262 $this->checkPermission('book-create-all');
264 $newName = $request->get('name') ?: $book->name;
265 $bookCopy = $cloner->cloneBook($book, $newName);
266 $this->showSuccessNotification(trans('entities.books_copy_success'));
268 return redirect($bookCopy->getUrl());
272 * Convert the chapter to a book.
274 public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
276 $book = $this->bookRepo->getBySlug($bookSlug);
277 $this->checkOwnablePermission('book-update', $book);
278 $this->checkOwnablePermission('book-delete', $book);
279 $this->checkPermission('bookshelf-create-all');
280 $this->checkPermission('book-create-all');
282 $shelf = $transformer->transformBookToShelf($book);
284 return redirect($shelf->getUrl());