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\Uploads\ImageRepo;
15 use Illuminate\Http\Request;
16 use Illuminate\Validation\ValidationException;
18 class BookshelfController extends Controller
20 protected $bookshelfRepo;
21 protected $entityContextManager;
24 public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
26 $this->bookshelfRepo = $bookshelfRepo;
27 $this->entityContextManager = $entityContextManager;
28 $this->imageRepo = $imageRepo;
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->bookshelfRepo->getAllPaginated(18, $sort, $order);
46 $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
47 $popular = $this->bookshelfRepo->getPopular(4);
48 $new = $this->bookshelfRepo->getRecentlyCreated(4);
50 $this->entityContextManager->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::hasPermission('update')->get();
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()),
92 $bookIds = explode(',', $request->get('books', ''));
93 $shelf = $this->bookshelfRepo->create($validated, $bookIds);
95 return redirect($shelf->getUrl());
99 * Display the bookshelf of the given slug.
101 * @throws NotFoundException
103 public function show(ActivityQueries $activities, string $slug)
105 $shelf = $this->bookshelfRepo->getBySlug($slug);
106 $this->checkOwnablePermission('book-view', $shelf);
108 $sort = setting()->getForCurrentUser('shelf_books_sort', 'default');
109 $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
111 $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
112 ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
116 View::incrementFor($shelf);
117 $this->entityContextManager->setShelfContext($shelf->id);
118 $view = setting()->getForCurrentUser('bookshelf_view_type');
120 $this->setPageTitle($shelf->getShortName());
122 return view('shelves.show', [
124 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
126 'activity' => $activities->entityActivity($shelf, 20, 1),
133 * Show the form for editing the specified bookshelf.
135 public function edit(string $slug)
137 $shelf = $this->bookshelfRepo->getBySlug($slug);
138 $this->checkOwnablePermission('bookshelf-update', $shelf);
140 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
141 $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
143 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
145 return view('shelves.edit', [
152 * Update the specified bookshelf in storage.
154 * @throws ValidationException
155 * @throws ImageUploadException
156 * @throws NotFoundException
158 public function update(Request $request, string $slug)
160 $shelf = $this->bookshelfRepo->getBySlug($slug);
161 $this->checkOwnablePermission('bookshelf-update', $shelf);
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 $bookIds = explode(',', $request->get('books', ''));
175 $shelf = $this->bookshelfRepo->update($shelf, $validated, $bookIds);
177 return redirect($shelf->getUrl());
181 * Shows the page to confirm deletion.
183 public function showDelete(string $slug)
185 $shelf = $this->bookshelfRepo->getBySlug($slug);
186 $this->checkOwnablePermission('bookshelf-delete', $shelf);
188 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
190 return view('shelves.delete', ['shelf' => $shelf]);
194 * Remove the specified bookshelf from storage.
198 public function destroy(string $slug)
200 $shelf = $this->bookshelfRepo->getBySlug($slug);
201 $this->checkOwnablePermission('bookshelf-delete', $shelf);
203 $this->bookshelfRepo->destroy($shelf);
205 return redirect('/shelves');
209 * Show the permissions view.
211 public function showPermissions(string $slug)
213 $shelf = $this->bookshelfRepo->getBySlug($slug);
214 $this->checkOwnablePermission('restrictions-manage', $shelf);
216 return view('shelves.permissions', [
222 * Set the permissions for this bookshelf.
224 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
226 $shelf = $this->bookshelfRepo->getBySlug($slug);
227 $this->checkOwnablePermission('restrictions-manage', $shelf);
229 $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
231 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
233 return redirect($shelf->getUrl());
237 * Copy the permissions of a bookshelf to the child books.
239 public function copyPermissions(string $slug)
241 $shelf = $this->bookshelfRepo->getBySlug($slug);
242 $this->checkOwnablePermission('restrictions-manage', $shelf);
244 $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
245 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
247 return redirect($shelf->getUrl());