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());
105 $bookshelf->appendBook($book);
106 Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf);
109 return redirect($book->getUrl());
113 * Display the specified book.
115 public function show(Request $request, ActivityQueries $activities, string $slug)
117 $book = $this->bookRepo->getBySlug($slug);
118 $bookChildren = (new BookContents($book))->getTree(true);
119 $bookParentShelves = $book->shelves()->scopes('visible')->get();
121 View::incrementFor($book);
122 if ($request->has('shelf')) {
123 $this->entityContextManager->setShelfContext(intval($request->get('shelf')));
126 $this->setPageTitle($book->getShortName());
128 return view('books.show', [
131 'bookChildren' => $bookChildren,
132 'bookParentShelves' => $bookParentShelves,
133 'activity' => $activities->entityActivity($book, 20, 1),
138 * Show the form for editing the specified book.
140 public function edit(string $slug)
142 $book = $this->bookRepo->getBySlug($slug);
143 $this->checkOwnablePermission('book-update', $book);
144 $this->setPageTitle(trans('entities.books_edit_named', ['bookName'=>$book->getShortName()]));
146 return view('books.edit', ['book' => $book, 'current' => $book]);
150 * Update the specified book in storage.
152 * @throws ImageUploadException
153 * @throws ValidationException
156 public function update(Request $request, string $slug)
158 $book = $this->bookRepo->getBySlug($slug);
159 $this->checkOwnablePermission('book-update', $book);
161 $validated = $this->validate($request, [
162 'name' => ['required', 'string', 'max:255'],
163 'description' => ['string', 'max:1000'],
164 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
167 if ($request->has('image_reset')) {
168 $validated['image'] = null;
169 } else if (is_null($validated['image'])) {
170 unset($validated['image']);
173 $book = $this->bookRepo->update($book, $validated);
175 return redirect($book->getUrl());
179 * Shows the page to confirm deletion.
181 public function showDelete(string $bookSlug)
183 $book = $this->bookRepo->getBySlug($bookSlug);
184 $this->checkOwnablePermission('book-delete', $book);
185 $this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
187 return view('books.delete', ['book' => $book, 'current' => $book]);
191 * Remove the specified book from the system.
195 public function destroy(string $bookSlug)
197 $book = $this->bookRepo->getBySlug($bookSlug);
198 $this->checkOwnablePermission('book-delete', $book);
200 $this->bookRepo->destroy($book);
202 return redirect('/books');
206 * Show the permissions view.
208 public function showPermissions(string $bookSlug)
210 $book = $this->bookRepo->getBySlug($bookSlug);
211 $this->checkOwnablePermission('restrictions-manage', $book);
213 return view('books.permissions', [
219 * Set the restrictions for this book.
223 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
225 $book = $this->bookRepo->getBySlug($bookSlug);
226 $this->checkOwnablePermission('restrictions-manage', $book);
228 $permissionsUpdater->updateFromPermissionsForm($book, $request);
230 $this->showSuccessNotification(trans('entities.books_permissions_updated'));
232 return redirect($book->getUrl());
236 * Show the view to copy a book.
238 * @throws NotFoundException
240 public function showCopy(string $bookSlug)
242 $book = $this->bookRepo->getBySlug($bookSlug);
243 $this->checkOwnablePermission('book-view', $book);
245 session()->flashInput(['name' => $book->name]);
247 return view('books.copy', [
253 * Create a copy of a book within the requested target destination.
255 * @throws NotFoundException
257 public function copy(Request $request, Cloner $cloner, string $bookSlug)
259 $book = $this->bookRepo->getBySlug($bookSlug);
260 $this->checkOwnablePermission('book-view', $book);
261 $this->checkPermission('book-create-all');
263 $newName = $request->get('name') ?: $book->name;
264 $bookCopy = $cloner->cloneBook($book, $newName);
265 $this->showSuccessNotification(trans('entities.books_copy_success'));
267 return redirect($bookCopy->getUrl());