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\PermissionsUpdater;
13 use BookStack\Entities\Tools\ShelfContext;
14 use BookStack\Exceptions\ImageUploadException;
15 use BookStack\Exceptions\NotFoundException;
16 use BookStack\Facades\Activity;
17 use Illuminate\Http\Request;
18 use Illuminate\Validation\ValidationException;
21 class BookController extends Controller
24 protected $entityContextManager;
26 public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
28 $this->bookRepo = $bookRepo;
29 $this->entityContextManager = $entityContextManager;
33 * Display a listing of the book.
35 public function index()
37 $view = setting()->getForCurrentUser('books_view_type');
38 $sort = setting()->getForCurrentUser('books_sort', 'name');
39 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
41 $books = $this->bookRepo->getAllPaginated(18, $sort, $order);
42 $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
43 $popular = $this->bookRepo->getPopular(4);
44 $new = $this->bookRepo->getRecentlyCreated(4);
46 $this->entityContextManager->clearShelfContext();
48 $this->setPageTitle(trans('entities.books'));
50 return view('books.index', [
52 'recents' => $recents,
53 'popular' => $popular,
62 * Show the form for creating a new book.
64 public function create(string $shelfSlug = null)
66 $this->checkPermission('book-create-all');
69 if ($shelfSlug !== null) {
70 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
71 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
74 $this->setPageTitle(trans('entities.books_create'));
76 return view('books.create', [
77 'bookshelf' => $bookshelf,
82 * Store a newly created book in storage.
84 * @throws ImageUploadException
85 * @throws ValidationException
87 public function store(Request $request, string $shelfSlug = null)
89 $this->checkPermission('book-create-all');
90 $this->validate($request, [
91 'name' => ['required', 'string', 'max:255'],
92 'description' => ['string', 'max:1000'],
93 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
97 if ($shelfSlug !== null) {
98 $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
99 $this->checkOwnablePermission('bookshelf-update', $bookshelf);
102 $book = $this->bookRepo->create($request->all());
103 $this->bookRepo->updateCoverImage($book, $request->file('image', null));
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);
161 $this->validate($request, [
162 'name' => ['required', 'string', 'max:255'],
163 'description' => ['string', 'max:1000'],
164 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
167 $book = $this->bookRepo->update($book, $request->all());
168 $resetCover = $request->has('image_reset');
169 $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
171 return redirect($book->getUrl());
175 * Shows the page to confirm deletion.
177 public function showDelete(string $bookSlug)
179 $book = $this->bookRepo->getBySlug($bookSlug);
180 $this->checkOwnablePermission('book-delete', $book);
181 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
183 return view('books.delete', ['book' => $book, 'current' => $book]);
187 * Remove the specified book from the system.
191 public function destroy(string $bookSlug)
193 $book = $this->bookRepo->getBySlug($bookSlug);
194 $this->checkOwnablePermission('book-delete', $book);
196 $this->bookRepo->destroy($book);
198 return redirect('/books');
202 * Show the permissions view.
204 public function showPermissions(string $bookSlug)
206 $book = $this->bookRepo->getBySlug($bookSlug);
207 $this->checkOwnablePermission('restrictions-manage', $book);
209 return view('books.permissions', [
215 * Set the restrictions for this book.
219 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
221 $book = $this->bookRepo->getBySlug($bookSlug);
222 $this->checkOwnablePermission('restrictions-manage', $book);
224 $permissionsUpdater->updateFromPermissionsForm($book, $request);
226 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
228 return redirect($book->getUrl());
232 * Show the view to copy a book.
234 * @throws NotFoundException
236 public function showCopy(string $bookSlug)
238 $book = $this->bookRepo->getBySlug($bookSlug);
239 $this->checkOwnablePermission('book-view', $book);
241 session()->flashInput(['name' => $book->name]);
243 return view('books.copy', [
249 * Create a copy of a book within the requested target destination.
251 * @throws NotFoundException
253 public function copy(Request $request, Cloner $cloner, string $bookSlug)
255 $book = $this->bookRepo->getBySlug($bookSlug);
256 $this->checkOwnablePermission('book-view', $book);
257 $this->checkPermission('book-create-all');
259 $newName = $request->get('name') ?: $book->name;
260 $bookCopy = $cloner->cloneBook($book, $newName);
261 $this->showSuccessNotification(trans('entities.books_copy_success'));
263 return redirect($bookCopy->getUrl());