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;
13 use BookStack\Util\SimpleListOptions;
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(Request $request)
36 $view = setting()->getForCurrentUser('bookshelves_view_type');
37 $listOptions = SimpleListOptions::fromRequest($request, 'bookshelves')->withSortOptions([
38 'name' => trans('common.sort_name'),
39 'created_at' => trans('common.sort_created_at'),
40 'updated_at' => trans('common.sort_updated_at'),
43 $shelves = $this->shelfRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
44 $recents = $this->isSignedIn() ? $this->shelfRepo->getRecentlyViewed(4) : false;
45 $popular = $this->shelfRepo->getPopular(4);
46 $new = $this->shelfRepo->getRecentlyCreated(4);
48 $this->shelfContext->clearShelfContext();
49 $this->setPageTitle(trans('entities.shelves'));
51 return view('shelves.index', [
52 'shelves' => $shelves,
53 'recents' => $recents,
54 'popular' => $popular,
57 'listOptions' => $listOptions,
62 * Show the form for creating a new bookshelf.
64 public function create()
66 $this->checkPermission('bookshelf-create-all');
67 $books = Book::visible()->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
68 $this->setPageTitle(trans('entities.shelves_create'));
70 return view('shelves.create', ['books' => $books]);
74 * Store a newly created bookshelf in storage.
76 * @throws ValidationException
77 * @throws ImageUploadException
79 public function store(Request $request)
81 $this->checkPermission('bookshelf-create-all');
82 $validated = $this->validate($request, [
83 'name' => ['required', 'string', 'max:255'],
84 'description' => ['string', 'max:1000'],
85 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
89 $bookIds = explode(',', $request->get('books', ''));
90 $shelf = $this->shelfRepo->create($validated, $bookIds);
92 return redirect($shelf->getUrl());
96 * Display the bookshelf of the given slug.
98 * @throws NotFoundException
100 public function show(Request $request, ActivityQueries $activities, string $slug)
102 $shelf = $this->shelfRepo->getBySlug($slug);
103 $this->checkOwnablePermission('bookshelf-view', $shelf);
105 $listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([
106 'default' => trans('common.sort_default'),
107 'name' => trans('common.sort_name'),
108 'created_at' => trans('common.sort_created_at'),
109 'updated_at' => trans('common.sort_updated_at'),
112 $sort = $listOptions->getSort();
113 $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
114 ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $listOptions->getOrder() === 'desc')
118 View::incrementFor($shelf);
119 $this->shelfContext->setShelfContext($shelf->id);
120 $view = setting()->getForCurrentUser('bookshelf_view_type');
122 $this->setPageTitle($shelf->getShortName());
124 return view('shelves.show', [
126 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
128 'activity' => $activities->entityActivity($shelf, 20, 1),
129 'listOptions' => $listOptions,
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', 'created_at', 'updated_at']);
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');