1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Tools\PermissionsUpdater;
6 use BookStack\Entities\Tools\ShelfContext;
7 use BookStack\Entities\Repos\BookshelfRepo;
8 use BookStack\Exceptions\ImageUploadException;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Uploads\ImageRepo;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
16 class BookshelfController extends Controller
19 protected $bookshelfRepo;
20 protected $entityContextManager;
23 public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
25 $this->bookshelfRepo = $bookshelfRepo;
26 $this->entityContextManager = $entityContextManager;
27 $this->imageRepo = $imageRepo;
31 * Display a listing of the book.
33 public function index()
35 $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
36 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
37 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
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->bookshelfRepo->getAllPaginated(18, $sort, $order);
45 $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
46 $popular = $this->bookshelfRepo->getPopular(4);
47 $new = $this->bookshelfRepo->getRecentlyCreated(4);
49 $this->entityContextManager->clearShelfContext();
50 $this->setPageTitle(trans('entities.shelves'));
51 return view('shelves.index', [
52 'shelves' => $shelves,
53 'recents' => $recents,
54 'popular' => $popular,
59 'sortOptions' => $sortOptions,
64 * Show the form for creating a new bookshelf.
66 public function create()
68 $this->checkPermission('bookshelf-create-all');
69 $books = Book::hasPermission('update')->get();
70 $this->setPageTitle(trans('entities.shelves_create'));
71 return view('shelves.create', ['books' => $books]);
75 * 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 $this->validate($request, [
83 'name' => 'required|string|max:255',
84 'description' => 'string|max:1000',
85 'image' => 'nullable|' . $this->getImageValidationRules(),
88 $bookIds = explode(',', $request->get('books', ''));
89 $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
90 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
92 return redirect($shelf->getUrl());
96 * Display the bookshelf of the given slug.
97 * @throws NotFoundException
99 public function show(string $slug)
101 $shelf = $this->bookshelfRepo->getBySlug($slug);
102 $this->checkOwnablePermission('book-view', $shelf);
104 $sort = setting()->getForCurrentUser('books_sort', 'name');
105 $order = setting()->getForCurrentUser('books_sort_order', 'asc');
108 $this->entityContextManager->setShelfContext($shelf->id);
109 $view = setting()->getForCurrentUser('bookshelf_view_type', config('app.views.books'));
111 $this->setPageTitle($shelf->getShortName());
112 return view('shelves.show', [
115 'activity' => Activity::entityActivity($shelf, 20, 1),
122 * Show the form for editing the specified bookshelf.
124 public function edit(string $slug)
126 $shelf = $this->bookshelfRepo->getBySlug($slug);
127 $this->checkOwnablePermission('bookshelf-update', $shelf);
129 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
130 $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
132 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
133 return view('shelves.edit', [
140 * Update the specified bookshelf in storage.
141 * @throws ValidationException
142 * @throws ImageUploadException
143 * @throws NotFoundException
145 public function update(Request $request, string $slug)
147 $shelf = $this->bookshelfRepo->getBySlug($slug);
148 $this->checkOwnablePermission('bookshelf-update', $shelf);
149 $this->validate($request, [
150 'name' => 'required|string|max:255',
151 'description' => 'string|max:1000',
152 'image' => 'nullable|' . $this->getImageValidationRules(),
156 $bookIds = explode(',', $request->get('books', ''));
157 $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
158 $resetCover = $request->has('image_reset');
159 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
161 return redirect($shelf->getUrl());
165 * Shows the page to confirm deletion
167 public function showDelete(string $slug)
169 $shelf = $this->bookshelfRepo->getBySlug($slug);
170 $this->checkOwnablePermission('bookshelf-delete', $shelf);
172 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
173 return view('shelves.delete', ['shelf' => $shelf]);
177 * Remove the specified bookshelf from storage.
180 public function destroy(string $slug)
182 $shelf = $this->bookshelfRepo->getBySlug($slug);
183 $this->checkOwnablePermission('bookshelf-delete', $shelf);
185 $this->bookshelfRepo->destroy($shelf);
187 return redirect('/shelves');
191 * Show the permissions view.
193 public function showPermissions(string $slug)
195 $shelf = $this->bookshelfRepo->getBySlug($slug);
196 $this->checkOwnablePermission('restrictions-manage', $shelf);
198 return view('shelves.permissions', [
204 * Set the permissions for this bookshelf.
206 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
208 $shelf = $this->bookshelfRepo->getBySlug($slug);
209 $this->checkOwnablePermission('restrictions-manage', $shelf);
211 $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
213 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
214 return redirect($shelf->getUrl());
218 * Copy the permissions of a bookshelf to the child books.
220 public function copyPermissions(string $slug)
222 $shelf = $this->bookshelfRepo->getBySlug($slug);
223 $this->checkOwnablePermission('restrictions-manage', $shelf);
225 $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
226 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
227 return redirect($shelf->getUrl());