3 namespace BookStack\Http\Controllers;
6 use BookStack\Actions\ActivityQueries;
7 use BookStack\Actions\View;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Repos\BookshelfRepo;
10 use BookStack\Entities\Tools\PermissionsUpdater;
11 use BookStack\Entities\Tools\ShelfContext;
12 use BookStack\Exceptions\ImageUploadException;
13 use BookStack\Exceptions\NotFoundException;
14 use BookStack\Uploads\ImageRepo;
16 use Illuminate\Http\Request;
17 use Illuminate\Validation\ValidationException;
19 class BookshelfController extends Controller
21 protected $bookshelfRepo;
22 protected $entityContextManager;
25 public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
27 $this->bookshelfRepo = $bookshelfRepo;
28 $this->entityContextManager = $entityContextManager;
29 $this->imageRepo = $imageRepo;
33 * Display a listing of the book.
35 public function index()
37 $view = setting()->getForCurrentUser('bookshelves_view_type');
38 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
39 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
41 'name' => trans('common.sort_name'),
42 'created_at' => trans('common.sort_created_at'),
43 'updated_at' => trans('common.sort_updated_at'),
46 $shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
47 $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
48 $popular = $this->bookshelfRepo->getPopular(4);
49 $new = $this->bookshelfRepo->getRecentlyCreated(4);
51 $this->entityContextManager->clearShelfContext();
52 $this->setPageTitle(trans('entities.shelves'));
54 return view('shelves.index', [
55 'shelves' => $shelves,
56 'recents' => $recents,
57 'popular' => $popular,
62 'sortOptions' => $sortOptions,
67 * Show the form for creating a new bookshelf.
69 public function create()
71 $this->checkPermission('bookshelf-create-all');
72 $books = Book::hasPermission('update')->get();
73 $this->setPageTitle(trans('entities.shelves_create'));
75 return view('shelves.create', ['books' => $books]);
79 * Store a newly created bookshelf in storage.
81 * @throws ValidationException
82 * @throws ImageUploadException
84 public function store(Request $request)
86 $this->checkPermission('bookshelf-create-all');
87 $this->validate($request, [
88 'name' => ['required', 'string', 'max:255'],
89 'description' => ['string', 'max:1000'],
90 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
93 $bookIds = explode(',', $request->get('books', ''));
94 $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
95 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
97 return redirect($shelf->getUrl());
101 * Display the bookshelf of the given slug.
103 * @throws NotFoundException
105 public function show(ActivityQueries $activities, string $slug)
107 $shelf = $this->bookshelfRepo->getBySlug($slug);
108 $this->checkOwnablePermission('book-view', $shelf);
110 $sort = setting()->getForCurrentUser('shelf_books_sort', 'default');
111 $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
113 $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
114 ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
118 View::incrementFor($shelf);
119 $this->entityContextManager->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),
135 * Show the form for editing the specified bookshelf.
137 public function edit(string $slug)
139 $shelf = $this->bookshelfRepo->getBySlug($slug);
140 $this->checkOwnablePermission('bookshelf-update', $shelf);
142 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
143 $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
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->bookshelfRepo->getBySlug($slug);
163 $this->checkOwnablePermission('bookshelf-update', $shelf);
164 $this->validate($request, [
165 'name' => ['required', 'string', 'max:255'],
166 'description' => ['string', 'max:1000'],
167 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
170 $bookIds = explode(',', $request->get('books', ''));
171 $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
172 $resetCover = $request->has('image_reset');
173 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
175 return redirect($shelf->getUrl());
179 * Shows the page to confirm deletion.
181 public function showDelete(string $slug)
183 $shelf = $this->bookshelfRepo->getBySlug($slug);
184 $this->checkOwnablePermission('bookshelf-delete', $shelf);
186 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
188 return view('shelves.delete', ['shelf' => $shelf]);
192 * Remove the specified bookshelf from storage.
196 public function destroy(string $slug)
198 $shelf = $this->bookshelfRepo->getBySlug($slug);
199 $this->checkOwnablePermission('bookshelf-delete', $shelf);
201 $this->bookshelfRepo->destroy($shelf);
203 return redirect('/shelves');
207 * Show the permissions view.
209 public function showPermissions(string $slug)
211 $shelf = $this->bookshelfRepo->getBySlug($slug);
212 $this->checkOwnablePermission('restrictions-manage', $shelf);
214 return view('shelves.permissions', [
220 * Set the permissions for this bookshelf.
222 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
224 $shelf = $this->bookshelfRepo->getBySlug($slug);
225 $this->checkOwnablePermission('restrictions-manage', $shelf);
227 $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
229 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
231 return redirect($shelf->getUrl());
235 * Copy the permissions of a bookshelf to the child books.
237 public function copyPermissions(string $slug)
239 $shelf = $this->bookshelfRepo->getBySlug($slug);
240 $this->checkOwnablePermission('restrictions-manage', $shelf);
242 $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
243 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
245 return redirect($shelf->getUrl());