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 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 bookshelves.
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()
115 ->reorder($sort === 'default' ? 'order' : $sort, $listOptions->getOrder())
120 View::incrementFor($shelf);
121 $this->shelfContext->setShelfContext($shelf->id);
122 $view = setting()->getForCurrentUser('bookshelf_view_type');
124 $this->setPageTitle($shelf->getShortName());
126 return view('shelves.show', [
128 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
130 'activity' => $activities->entityActivity($shelf, 20, 1),
131 'listOptions' => $listOptions,
132 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($shelf),
137 * Show the form for editing the specified bookshelf.
139 public function edit(string $slug)
141 $shelf = $this->shelfRepo->getBySlug($slug);
142 $this->checkOwnablePermission('bookshelf-update', $shelf);
144 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
145 $books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
147 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
149 return view('shelves.edit', [
156 * Update the specified bookshelf in storage.
158 * @throws ValidationException
159 * @throws ImageUploadException
160 * @throws NotFoundException
162 public function update(Request $request, string $slug)
164 $shelf = $this->shelfRepo->getBySlug($slug);
165 $this->checkOwnablePermission('bookshelf-update', $shelf);
166 $validated = $this->validate($request, [
167 'name' => ['required', 'string', 'max:255'],
168 'description' => ['string', 'max:1000'],
169 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
173 if ($request->has('image_reset')) {
174 $validated['image'] = null;
175 } elseif (array_key_exists('image', $validated) && is_null($validated['image'])) {
176 unset($validated['image']);
179 $bookIds = explode(',', $request->get('books', ''));
180 $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds);
182 return redirect($shelf->getUrl());
186 * Shows the page to confirm deletion.
188 public function showDelete(string $slug)
190 $shelf = $this->shelfRepo->getBySlug($slug);
191 $this->checkOwnablePermission('bookshelf-delete', $shelf);
193 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
195 return view('shelves.delete', ['shelf' => $shelf]);
199 * Remove the specified bookshelf from storage.
203 public function destroy(string $slug)
205 $shelf = $this->shelfRepo->getBySlug($slug);
206 $this->checkOwnablePermission('bookshelf-delete', $shelf);
208 $this->shelfRepo->destroy($shelf);
210 return redirect('/shelves');