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;
13 use BookStack\References\ReferenceFetcher;
15 use Illuminate\Http\Request;
16 use Illuminate\Validation\ValidationException;
18 class BookshelfController extends Controller
20 protected BookshelfRepo $shelfRepo;
21 protected ShelfContext $shelfContext;
22 protected ReferenceFetcher $referenceFetcher;
24 public function __construct(BookshelfRepo $shelfRepo, ShelfContext $shelfContext, ReferenceFetcher $referenceFetcher)
26 $this->shelfRepo = $shelfRepo;
27 $this->shelfContext = $shelfContext;
28 $this->referenceFetcher = $referenceFetcher;
32 * Display a listing of the book.
34 public function index()
36 $view = setting()->getForCurrentUser('bookshelves_view_type');
37 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
38 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
40 'name' => trans('common.sort_name'),
41 'created_at' => trans('common.sort_created_at'),
42 'updated_at' => trans('common.sort_updated_at'),
45 $shelves = $this->shelfRepo->getAllPaginated(18, $sort, $order);
46 $recents = $this->isSignedIn() ? $this->shelfRepo->getRecentlyViewed(4) : false;
47 $popular = $this->shelfRepo->getPopular(4);
48 $new = $this->shelfRepo->getRecentlyCreated(4);
50 $this->shelfContext->clearShelfContext();
51 $this->setPageTitle(trans('entities.shelves'));
53 return view('shelves.index', [
54 'shelves' => $shelves,
55 'recents' => $recents,
56 'popular' => $popular,
61 'sortOptions' => $sortOptions,
66 * Show the form for creating a new bookshelf.
68 public function create()
70 $this->checkPermission('bookshelf-create-all');
71 $books = Book::visible()->orderBy('name')->get(['name', 'id', 'slug']);
72 $this->setPageTitle(trans('entities.shelves_create'));
74 return view('shelves.create', ['books' => $books]);
78 * Store a newly created bookshelf in storage.
80 * @throws ValidationException
81 * @throws ImageUploadException
83 public function store(Request $request)
85 $this->checkPermission('bookshelf-create-all');
86 $validated = $this->validate($request, [
87 'name' => ['required', 'string', 'max:255'],
88 'description' => ['string', 'max:1000'],
89 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
93 $bookIds = explode(',', $request->get('books', ''));
94 $shelf = $this->shelfRepo->create($validated, $bookIds);
96 return redirect($shelf->getUrl());
100 * Display the bookshelf of the given slug.
102 * @throws NotFoundException
104 public function show(ActivityQueries $activities, string $slug)
106 $shelf = $this->shelfRepo->getBySlug($slug);
107 $this->checkOwnablePermission('bookshelf-view', $shelf);
109 $sort = setting()->getForCurrentUser('shelf_books_sort', 'default');
110 $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
112 $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
113 ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
117 View::incrementFor($shelf);
118 $this->shelfContext->setShelfContext($shelf->id);
119 $view = setting()->getForCurrentUser('bookshelf_view_type');
121 $this->setPageTitle($shelf->getShortName());
123 return view('shelves.show', [
125 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
127 'activity' => $activities->entityActivity($shelf, 20, 1),
130 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($shelf),
135 * Show the form for editing the specified bookshelf.
137 public function edit(string $slug)
139 $shelf = $this->shelfRepo->getBySlug($slug);
140 $this->checkOwnablePermission('bookshelf-update', $shelf);
142 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
143 $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug']);
145 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
147 return view('shelves.edit', [
154 * Update the specified bookshelf in storage.
156 * @throws ValidationException
157 * @throws ImageUploadException
158 * @throws NotFoundException
160 public function update(Request $request, string $slug)
162 $shelf = $this->shelfRepo->getBySlug($slug);
163 $this->checkOwnablePermission('bookshelf-update', $shelf);
164 $validated = $this->validate($request, [
165 'name' => ['required', 'string', 'max:255'],
166 'description' => ['string', 'max:1000'],
167 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
171 if ($request->has('image_reset')) {
172 $validated['image'] = null;
173 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
174 unset($validated['image']);
177 $bookIds = explode(',', $request->get('books', ''));
178 $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
180 return redirect($shelf->getUrl());
184 * Shows the page to confirm deletion.
186 public function showDelete(string $slug)
188 $shelf = $this->shelfRepo->getBySlug($slug);
189 $this->checkOwnablePermission('bookshelf-delete', $shelf);
191 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
193 return view('shelves.delete', ['shelf' => $shelf]);
197 * Remove the specified bookshelf from storage.
201 public function destroy(string $slug)
203 $shelf = $this->shelfRepo->getBySlug($slug);
204 $this->checkOwnablePermission('bookshelf-delete', $shelf);
206 $this->shelfRepo->destroy($shelf);
208 return redirect('/shelves');
212 * Show the permissions view.
214 public function showPermissions(string $slug)
216 $shelf = $this->shelfRepo->getBySlug($slug);
217 $this->checkOwnablePermission('restrictions-manage', $shelf);
219 return view('shelves.permissions', [
225 * Set the permissions for this bookshelf.
227 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
229 $shelf = $this->shelfRepo->getBySlug($slug);
230 $this->checkOwnablePermission('restrictions-manage', $shelf);
232 $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
234 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
236 return redirect($shelf->getUrl());
240 * Copy the permissions of a bookshelf to the child books.
242 public function copyPermissions(string $slug)
244 $shelf = $this->shelfRepo->getBySlug($slug);
245 $this->checkOwnablePermission('restrictions-manage', $shelf);
247 $updateCount = $this->shelfRepo->copyDownPermissions($shelf);
248 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
250 return redirect($shelf->getUrl());