3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\ActivityQueries;
6 use BookStack\Activity\Models\View;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Queries\BookshelfQueries;
9 use BookStack\Entities\Repos\BookshelfRepo;
10 use BookStack\Entities\Tools\ShelfContext;
11 use BookStack\Exceptions\ImageUploadException;
12 use BookStack\Exceptions\NotFoundException;
13 use BookStack\Http\Controller;
14 use BookStack\References\ReferenceFetcher;
15 use BookStack\Util\SimpleListOptions;
17 use Illuminate\Http\Request;
18 use Illuminate\Validation\ValidationException;
20 class BookshelfController extends Controller
22 public function __construct(
23 protected BookshelfRepo $shelfRepo,
24 protected BookshelfQueries $queries,
25 protected ShelfContext $shelfContext,
26 protected ReferenceFetcher $referenceFetcher,
31 * Display a listing of bookshelves.
33 public function index(Request $request)
35 $view = setting()->getForCurrentUser('bookshelves_view_type');
36 $listOptions = SimpleListOptions::fromRequest($request, 'bookshelves')->withSortOptions([
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->queries->visibleForListWithCover()
43 ->orderBy($listOptions->getSort(), $listOptions->getOrder())
45 $recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->get() : false;
46 $popular = $this->queries->popularForList()->get();
47 $new = $this->queries->visibleForList()
48 ->orderBy('created_at', 'desc')
52 $this->shelfContext->clearShelfContext();
53 $this->setPageTitle(trans('entities.shelves'));
55 return view('shelves.index', [
56 'shelves' => $shelves,
57 'recents' => $recents,
58 'popular' => $popular,
61 'listOptions' => $listOptions,
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', 'created_at', 'updated_at']);
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_html' => ['string', 'max:2000'],
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(Request $request, ActivityQueries $activities, string $slug)
106 $shelf = $this->queries->findVisibleBySlug($slug);
107 $this->checkOwnablePermission('bookshelf-view', $shelf);
109 $listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([
110 'default' => trans('common.sort_default'),
111 'name' => trans('common.sort_name'),
112 'created_at' => trans('common.sort_created_at'),
113 'updated_at' => trans('common.sort_updated_at'),
116 $sort = $listOptions->getSort();
117 $sortedVisibleShelfBooks = $shelf->visibleBooks()
118 ->reorder($sort === 'default' ? 'order' : $sort, $listOptions->getOrder())
123 View::incrementFor($shelf);
124 $this->shelfContext->setShelfContext($shelf->id);
125 $view = setting()->getForCurrentUser('bookshelf_view_type');
127 $this->setPageTitle($shelf->getShortName());
129 return view('shelves.show', [
131 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
133 'activity' => $activities->entityActivity($shelf, 20, 1),
134 'listOptions' => $listOptions,
135 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($shelf),
140 * Show the form for editing the specified bookshelf.
142 public function edit(string $slug)
144 $shelf = $this->queries->findVisibleBySlug($slug);
145 $this->checkOwnablePermission('bookshelf-update', $shelf);
147 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
148 $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
150 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
152 return view('shelves.edit', [
159 * Update the specified bookshelf in storage.
161 * @throws ValidationException
162 * @throws ImageUploadException
163 * @throws NotFoundException
165 public function update(Request $request, string $slug)
167 $shelf = $this->queries->findVisibleBySlug($slug);
168 $this->checkOwnablePermission('bookshelf-update', $shelf);
169 $validated = $this->validate($request, [
170 'name' => ['required', 'string', 'max:255'],
171 'description_html' => ['string', 'max:2000'],
172 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
176 if ($request->has('image_reset')) {
177 $validated['image'] = null;
178 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
179 unset($validated['image']);
182 $bookIds = explode(',', $request->get('books', ''));
183 $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
185 return redirect($shelf->getUrl());
189 * Shows the page to confirm deletion.
191 public function showDelete(string $slug)
193 $shelf = $this->queries->findVisibleBySlug($slug);
194 $this->checkOwnablePermission('bookshelf-delete', $shelf);
196 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
198 return view('shelves.delete', ['shelf' => $shelf]);
202 * Remove the specified bookshelf from storage.
206 public function destroy(string $slug)
208 $shelf = $this->queries->findVisibleBySlug($slug);
209 $this->checkOwnablePermission('bookshelf-delete', $shelf);
211 $this->shelfRepo->destroy($shelf);
213 return redirect('/shelves');