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\ShelfContext;
10 use BookStack\Exceptions\ImageUploadException;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\References\ReferenceFetcher;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
17 class BookshelfController extends Controller
19 protected BookshelfRepo $shelfRepo;
20 protected ShelfContext $shelfContext;
21 protected ReferenceFetcher $referenceFetcher;
23 public function __construct(BookshelfRepo $shelfRepo, ShelfContext $shelfContext, ReferenceFetcher $referenceFetcher)
25 $this->shelfRepo = $shelfRepo;
26 $this->shelfContext = $shelfContext;
27 $this->referenceFetcher = $referenceFetcher;
31 * Display a listing of the book.
33 public function index()
35 $view = setting()->getForCurrentUser('bookshelves_view_type');
36 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
37 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
39 'name' => trans('common.sort_name'),
40 'created_at' => trans('common.sort_created_at'),
41 'updated_at' => trans('common.sort_updated_at'),
44 $shelves = $this->shelfRepo->getAllPaginated(18, $sort, $order);
45 $recents = $this->isSignedIn() ? $this->shelfRepo->getRecentlyViewed(4) : false;
46 $popular = $this->shelfRepo->getPopular(4);
47 $new = $this->shelfRepo->getRecentlyCreated(4);
49 $this->shelfContext->clearShelfContext();
50 $this->setPageTitle(trans('entities.shelves'));
52 return view('shelves.index', [
53 'shelves' => $shelves,
54 'recents' => $recents,
55 'popular' => $popular,
60 'sortOptions' => $sortOptions,
65 * Show the form for creating a new bookshelf.
67 public function create()
69 $this->checkPermission('bookshelf-create-all');
70 $books = Book::visible()->orderBy('name')->get(['name', 'id', 'slug']);
71 $this->setPageTitle(trans('entities.shelves_create'));
73 return view('shelves.create', ['books' => $books]);
77 * Store a newly created bookshelf in storage.
79 * @throws ValidationException
80 * @throws ImageUploadException
82 public function store(Request $request)
84 $this->checkPermission('bookshelf-create-all');
85 $validated = $this->validate($request, [
86 'name' => ['required', 'string', 'max:255'],
87 'description' => ['string', 'max:1000'],
88 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
92 $bookIds = explode(',', $request->get('books', ''));
93 $shelf = $this->shelfRepo->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->shelfRepo->getBySlug($slug);
106 $this->checkOwnablePermission('bookshelf-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->shelfContext->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),
129 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($shelf),
134 * Show the form for editing the specified bookshelf.
136 public function edit(string $slug)
138 $shelf = $this->shelfRepo->getBySlug($slug);
139 $this->checkOwnablePermission('bookshelf-update', $shelf);
141 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
142 $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug']);
144 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
146 return view('shelves.edit', [
153 * Update the specified bookshelf in storage.
155 * @throws ValidationException
156 * @throws ImageUploadException
157 * @throws NotFoundException
159 public function update(Request $request, string $slug)
161 $shelf = $this->shelfRepo->getBySlug($slug);
162 $this->checkOwnablePermission('bookshelf-update', $shelf);
163 $validated = $this->validate($request, [
164 'name' => ['required', 'string', 'max:255'],
165 'description' => ['string', 'max:1000'],
166 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
170 if ($request->has('image_reset')) {
171 $validated['image'] = null;
172 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
173 unset($validated['image']);
176 $bookIds = explode(',', $request->get('books', ''));
177 $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
179 return redirect($shelf->getUrl());
183 * Shows the page to confirm deletion.
185 public function showDelete(string $slug)
187 $shelf = $this->shelfRepo->getBySlug($slug);
188 $this->checkOwnablePermission('bookshelf-delete', $shelf);
190 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
192 return view('shelves.delete', ['shelf' => $shelf]);
196 * Remove the specified bookshelf from storage.
200 public function destroy(string $slug)
202 $shelf = $this->shelfRepo->getBySlug($slug);
203 $this->checkOwnablePermission('bookshelf-delete', $shelf);
205 $this->shelfRepo->destroy($shelf);
207 return redirect('/shelves');