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\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 public function __construct(
22 protected BookshelfRepo $shelfRepo,
23 protected ShelfContext $shelfContext,
24 protected ReferenceFetcher $referenceFetcher
29 * Display a listing of bookshelves.
31 public function index(Request $request)
33 $view = setting()->getForCurrentUser('bookshelves_view_type');
34 $listOptions = SimpleListOptions::fromRequest($request, 'bookshelves')->withSortOptions([
35 'name' => trans('common.sort_name'),
36 'created_at' => trans('common.sort_created_at'),
37 'updated_at' => trans('common.sort_updated_at'),
40 $shelves = $this->shelfRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
41 $recents = $this->isSignedIn() ? $this->shelfRepo->getRecentlyViewed(4) : false;
42 $popular = $this->shelfRepo->getPopular(4);
43 $new = $this->shelfRepo->getRecentlyCreated(4);
45 $this->shelfContext->clearShelfContext();
46 $this->setPageTitle(trans('entities.shelves'));
48 return view('shelves.index', [
49 'shelves' => $shelves,
50 'recents' => $recents,
51 'popular' => $popular,
54 'listOptions' => $listOptions,
59 * Show the form for creating a new bookshelf.
61 public function create()
63 $this->checkPermission('bookshelf-create-all');
64 $books = Book::visible()->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
65 $this->setPageTitle(trans('entities.shelves_create'));
67 return view('shelves.create', ['books' => $books]);
71 * Store a newly created bookshelf in storage.
73 * @throws ValidationException
74 * @throws ImageUploadException
76 public function store(Request $request)
78 $this->checkPermission('bookshelf-create-all');
79 $validated = $this->validate($request, [
80 'name' => ['required', 'string', 'max:255'],
81 'description_html' => ['string', 'max:2000'],
82 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
86 $bookIds = explode(',', $request->get('books', ''));
87 $shelf = $this->shelfRepo->create($validated, $bookIds);
89 return redirect($shelf->getUrl());
93 * Display the bookshelf of the given slug.
95 * @throws NotFoundException
97 public function show(Request $request, ActivityQueries $activities, string $slug)
99 $shelf = $this->shelfRepo->getBySlug($slug);
100 $this->checkOwnablePermission('bookshelf-view', $shelf);
102 $listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([
103 'default' => trans('common.sort_default'),
104 'name' => trans('common.sort_name'),
105 'created_at' => trans('common.sort_created_at'),
106 'updated_at' => trans('common.sort_updated_at'),
109 $sort = $listOptions->getSort();
110 $sortedVisibleShelfBooks = $shelf->visibleBooks()
111 ->reorder($sort === 'default' ? 'order' : $sort, $listOptions->getOrder())
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),
127 'listOptions' => $listOptions,
128 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($shelf),
133 * Show the form for editing the specified bookshelf.
135 public function edit(string $slug)
137 $shelf = $this->shelfRepo->getBySlug($slug);
138 $this->checkOwnablePermission('bookshelf-update', $shelf);
140 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
141 $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
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->shelfRepo->getBySlug($slug);
161 $this->checkOwnablePermission('bookshelf-update', $shelf);
162 $validated = $this->validate($request, [
163 'name' => ['required', 'string', 'max:255'],
164 'description_html' => ['string', 'max:2000'],
165 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
169 if ($request->has('image_reset')) {
170 $validated['image'] = null;
171 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
172 unset($validated['image']);
175 $bookIds = explode(',', $request->get('books', ''));
176 $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
178 return redirect($shelf->getUrl());
182 * Shows the page to confirm deletion.
184 public function showDelete(string $slug)
186 $shelf = $this->shelfRepo->getBySlug($slug);
187 $this->checkOwnablePermission('bookshelf-delete', $shelf);
189 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
191 return view('shelves.delete', ['shelf' => $shelf]);
195 * Remove the specified bookshelf from storage.
199 public function destroy(string $slug)
201 $shelf = $this->shelfRepo->getBySlug($slug);
202 $this->checkOwnablePermission('bookshelf-delete', $shelf);
204 $this->shelfRepo->destroy($shelf);
206 return redirect('/shelves');