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\Repos\BookshelfRepo;
9 use BookStack\Entities\Tools\ShelfContext;
10 use BookStack\Exceptions\ImageUploadException;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Http\Controllers\Controller;
13 use BookStack\References\ReferenceFetcher;
14 use BookStack\Util\SimpleListOptions;
16 use Illuminate\Http\Request;
17 use Illuminate\Validation\ValidationException;
19 class BookshelfController extends Controller
21 protected BookshelfRepo $shelfRepo;
22 protected ShelfContext $shelfContext;
23 protected ReferenceFetcher $referenceFetcher;
25 public function __construct(BookshelfRepo $shelfRepo, ShelfContext $shelfContext, ReferenceFetcher $referenceFetcher)
27 $this->shelfRepo = $shelfRepo;
28 $this->shelfContext = $shelfContext;
29 $this->referenceFetcher = $referenceFetcher;
33 * Display a listing of the book.
35 public function index(Request $request)
37 $view = setting()->getForCurrentUser('bookshelves_view_type');
38 $listOptions = SimpleListOptions::fromRequest($request, 'bookshelves')->withSortOptions([
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, $listOptions->getSort(), $listOptions->getOrder());
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,
58 'listOptions' => $listOptions,
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', 'created_at', 'updated_at']);
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(Request $request, ActivityQueries $activities, string $slug)
103 $shelf = $this->shelfRepo->getBySlug($slug);
104 $this->checkOwnablePermission('bookshelf-view', $shelf);
106 $listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([
107 'default' => trans('common.sort_default'),
108 'name' => trans('common.sort_name'),
109 'created_at' => trans('common.sort_created_at'),
110 'updated_at' => trans('common.sort_updated_at'),
113 $sort = $listOptions->getSort();
114 $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
115 ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $listOptions->getOrder() === 'desc')
119 View::incrementFor($shelf);
120 $this->shelfContext->setShelfContext($shelf->id);
121 $view = setting()->getForCurrentUser('bookshelf_view_type');
123 $this->setPageTitle($shelf->getShortName());
125 return view('shelves.show', [
127 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
129 'activity' => $activities->entityActivity($shelf, 20, 1),
130 'listOptions' => $listOptions,
131 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($shelf),
136 * Show the form for editing the specified bookshelf.
138 public function edit(string $slug)
140 $shelf = $this->shelfRepo->getBySlug($slug);
141 $this->checkOwnablePermission('bookshelf-update', $shelf);
143 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
144 $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
146 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
148 return view('shelves.edit', [
155 * Update the specified bookshelf in storage.
157 * @throws ValidationException
158 * @throws ImageUploadException
159 * @throws NotFoundException
161 public function update(Request $request, string $slug)
163 $shelf = $this->shelfRepo->getBySlug($slug);
164 $this->checkOwnablePermission('bookshelf-update', $shelf);
165 $validated = $this->validate($request, [
166 'name' => ['required', 'string', 'max:255'],
167 'description' => ['string', 'max:1000'],
168 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
172 if ($request->has('image_reset')) {
173 $validated['image'] = null;
174 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
175 unset($validated['image']);
178 $bookIds = explode(',', $request->get('books', ''));
179 $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
181 return redirect($shelf->getUrl());
185 * Shows the page to confirm deletion.
187 public function showDelete(string $slug)
189 $shelf = $this->shelfRepo->getBySlug($slug);
190 $this->checkOwnablePermission('bookshelf-delete', $shelf);
192 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
194 return view('shelves.delete', ['shelf' => $shelf]);
198 * Remove the specified bookshelf from storage.
202 public function destroy(string $slug)
204 $shelf = $this->shelfRepo->getBySlug($slug);
205 $this->checkOwnablePermission('bookshelf-delete', $shelf);
207 $this->shelfRepo->destroy($shelf);
209 return redirect('/shelves');