3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityQueries;
6 use BookStack\Actions\View;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Repos\BookshelfRepo;
9 use BookStack\Entities\Tools\PermissionsUpdater;
10 use BookStack\Entities\Tools\ShelfContext;
11 use BookStack\Exceptions\ImageUploadException;
12 use BookStack\Exceptions\NotFoundException;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
17 class BookshelfController extends Controller
19 protected BookshelfRepo $shelfRepo;
20 protected ShelfContext $shelfContext;
22 public function __construct(BookshelfRepo $shelfRepo, ShelfContext $shelfContext)
24 $this->shelfRepo = $shelfRepo;
25 $this->shelfContext = $shelfContext;
29 * Display a listing of the book.
31 public function index()
33 $view = setting()->getForCurrentUser('bookshelves_view_type');
34 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
35 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
37 'name' => trans('common.sort_name'),
38 'created_at' => trans('common.sort_created_at'),
39 'updated_at' => trans('common.sort_updated_at'),
42 $shelves = $this->shelfRepo->getAllPaginated(18, $sort, $order);
43 $recents = $this->isSignedIn() ? $this->shelfRepo->getRecentlyViewed(4) : false;
44 $popular = $this->shelfRepo->getPopular(4);
45 $new = $this->shelfRepo->getRecentlyCreated(4);
47 $this->shelfContext->clearShelfContext();
48 $this->setPageTitle(trans('entities.shelves'));
50 return view('shelves.index', [
51 'shelves' => $shelves,
52 'recents' => $recents,
53 'popular' => $popular,
58 'sortOptions' => $sortOptions,
63 * Show the form for creating a new bookshelf.
65 public function create()
67 $this->checkPermission('bookshelf-create-all');
68 $books = Book::visible()->orderBy('name')->get(['name', 'id', 'slug']);
69 $this->setPageTitle(trans('entities.shelves_create'));
71 return view('shelves.create', ['books' => $books]);
75 * Store a newly created bookshelf in storage.
77 * @throws ValidationException
78 * @throws ImageUploadException
80 public function store(Request $request)
82 $this->checkPermission('bookshelf-create-all');
83 $validated = $this->validate($request, [
84 'name' => ['required', 'string', 'max:255'],
85 'description' => ['string', 'max:1000'],
86 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
90 $bookIds = explode(',', $request->get('books', ''));
91 $shelf = $this->shelfRepo->create($validated, $bookIds);
93 return redirect($shelf->getUrl());
97 * Display the bookshelf of the given slug.
99 * @throws NotFoundException
101 public function show(ActivityQueries $activities, string $slug)
103 $shelf = $this->shelfRepo->getBySlug($slug);
104 $this->checkOwnablePermission('bookshelf-view', $shelf);
106 $sort = setting()->getForCurrentUser('shelf_books_sort', 'default');
107 $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
109 $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
110 ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
114 View::incrementFor($shelf);
115 $this->shelfContext->setShelfContext($shelf->id);
116 $view = setting()->getForCurrentUser('bookshelf_view_type');
118 $this->setPageTitle($shelf->getShortName());
120 return view('shelves.show', [
122 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
124 'activity' => $activities->entityActivity($shelf, 20, 1),
131 * Show the form for editing the specified bookshelf.
133 public function edit(string $slug)
135 $shelf = $this->shelfRepo->getBySlug($slug);
136 $this->checkOwnablePermission('bookshelf-update', $shelf);
138 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
139 $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug']);
141 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
143 return view('shelves.edit', [
150 * Update the specified bookshelf in storage.
152 * @throws ValidationException
153 * @throws ImageUploadException
154 * @throws NotFoundException
156 public function update(Request $request, string $slug)
158 $shelf = $this->shelfRepo->getBySlug($slug);
159 $this->checkOwnablePermission('bookshelf-update', $shelf);
160 $validated = $this->validate($request, [
161 'name' => ['required', 'string', 'max:255'],
162 'description' => ['string', 'max:1000'],
163 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
167 if ($request->has('image_reset')) {
168 $validated['image'] = null;
169 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
170 unset($validated['image']);
173 $bookIds = explode(',', $request->get('books', ''));
174 $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
176 return redirect($shelf->getUrl());
180 * Shows the page to confirm deletion.
182 public function showDelete(string $slug)
184 $shelf = $this->shelfRepo->getBySlug($slug);
185 $this->checkOwnablePermission('bookshelf-delete', $shelf);
187 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
189 return view('shelves.delete', ['shelf' => $shelf]);
193 * Remove the specified bookshelf from storage.
197 public function destroy(string $slug)
199 $shelf = $this->shelfRepo->getBySlug($slug);
200 $this->checkOwnablePermission('bookshelf-delete', $shelf);
202 $this->shelfRepo->destroy($shelf);
204 return redirect('/shelves');
208 * Show the permissions view.
210 public function showPermissions(string $slug)
212 $shelf = $this->shelfRepo->getBySlug($slug);
213 $this->checkOwnablePermission('restrictions-manage', $shelf);
215 return view('shelves.permissions', [
221 * Set the permissions for this bookshelf.
223 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
225 $shelf = $this->shelfRepo->getBySlug($slug);
226 $this->checkOwnablePermission('restrictions-manage', $shelf);
228 $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
230 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
232 return redirect($shelf->getUrl());
236 * Copy the permissions of a bookshelf to the child books.
238 public function copyPermissions(string $slug)
240 $shelf = $this->shelfRepo->getBySlug($slug);
241 $this->checkOwnablePermission('restrictions-manage', $shelf);
243 $updateCount = $this->shelfRepo->copyDownPermissions($shelf);
244 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
246 return redirect($shelf->getUrl());