1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Actions\View;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Tools\PermissionsUpdater;
7 use BookStack\Entities\Tools\ShelfContext;
8 use BookStack\Entities\Repos\BookshelfRepo;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Exceptions\NotFoundException;
11 use BookStack\Uploads\ImageRepo;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\ValidationException;
17 class BookshelfController extends Controller
20 protected $bookshelfRepo;
21 protected $entityContextManager;
24 public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
26 $this->bookshelfRepo = $bookshelfRepo;
27 $this->entityContextManager = $entityContextManager;
28 $this->imageRepo = $imageRepo;
32 * Display a listing of the book.
34 public function index()
36 $view = setting()->getForCurrentUser('bookshelves_view_type');
37 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
38 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
40 'name' => trans('common.sort_name'),
41 'created_at' => trans('common.sort_created_at'),
42 'updated_at' => trans('common.sort_updated_at'),
45 $shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
46 $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
47 $popular = $this->bookshelfRepo->getPopular(4);
48 $new = $this->bookshelfRepo->getRecentlyCreated(4);
50 $this->entityContextManager->clearShelfContext();
51 $this->setPageTitle(trans('entities.shelves'));
52 return view('shelves.index', [
53 'shelves' => $shelves,
54 'recents' => $recents,
55 'popular' => $popular,
60 'sortOptions' => $sortOptions,
65 * Show the form for creating a new bookshelf.
67 public function create()
69 $this->checkPermission('bookshelf-create-all');
70 $books = Book::hasPermission('update')->get();
71 $this->setPageTitle(trans('entities.shelves_create'));
72 return view('shelves.create', ['books' => $books]);
76 * 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 $this->validate($request, [
84 'name' => 'required|string|max:255',
85 'description' => 'string|max:1000',
86 'image' => 'nullable|' . $this->getImageValidationRules(),
89 $bookIds = explode(',', $request->get('books', ''));
90 $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
91 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
93 return redirect($shelf->getUrl());
97 * Display the bookshelf of the given slug.
98 * @throws NotFoundException
100 public function show(string $slug)
102 $shelf = $this->bookshelfRepo->getBySlug($slug);
103 $this->checkOwnablePermission('book-view', $shelf);
105 $sort = setting()->getForCurrentUser('shelf_books_sort', 'default');
106 $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
108 $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
109 ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
113 View::incrementFor($shelf);
114 $this->entityContextManager->setShelfContext($shelf->id);
115 $view = setting()->getForCurrentUser('bookshelf_view_type');
117 $this->setPageTitle($shelf->getShortName());
118 return view('shelves.show', [
120 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
122 'activity' => Activity::entityActivity($shelf, 20, 1),
129 * Show the form for editing the specified bookshelf.
131 public function edit(string $slug)
133 $shelf = $this->bookshelfRepo->getBySlug($slug);
134 $this->checkOwnablePermission('bookshelf-update', $shelf);
136 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
137 $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
139 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
140 return view('shelves.edit', [
147 * Update the specified bookshelf in storage.
148 * @throws ValidationException
149 * @throws ImageUploadException
150 * @throws NotFoundException
152 public function update(Request $request, string $slug)
154 $shelf = $this->bookshelfRepo->getBySlug($slug);
155 $this->checkOwnablePermission('bookshelf-update', $shelf);
156 $this->validate($request, [
157 'name' => 'required|string|max:255',
158 'description' => 'string|max:1000',
159 'image' => 'nullable|' . $this->getImageValidationRules(),
163 $bookIds = explode(',', $request->get('books', ''));
164 $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
165 $resetCover = $request->has('image_reset');
166 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
168 return redirect($shelf->getUrl());
172 * Shows the page to confirm deletion
174 public function showDelete(string $slug)
176 $shelf = $this->bookshelfRepo->getBySlug($slug);
177 $this->checkOwnablePermission('bookshelf-delete', $shelf);
179 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
180 return view('shelves.delete', ['shelf' => $shelf]);
184 * Remove the specified bookshelf from storage.
187 public function destroy(string $slug)
189 $shelf = $this->bookshelfRepo->getBySlug($slug);
190 $this->checkOwnablePermission('bookshelf-delete', $shelf);
192 $this->bookshelfRepo->destroy($shelf);
194 return redirect('/shelves');
198 * Show the permissions view.
200 public function showPermissions(string $slug)
202 $shelf = $this->bookshelfRepo->getBySlug($slug);
203 $this->checkOwnablePermission('restrictions-manage', $shelf);
205 return view('shelves.permissions', [
211 * Set the permissions for this bookshelf.
213 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
215 $shelf = $this->bookshelfRepo->getBySlug($slug);
216 $this->checkOwnablePermission('restrictions-manage', $shelf);
218 $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
220 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
221 return redirect($shelf->getUrl());
225 * Copy the permissions of a bookshelf to the child books.
227 public function copyPermissions(string $slug)
229 $shelf = $this->bookshelfRepo->getBySlug($slug);
230 $this->checkOwnablePermission('restrictions-manage', $shelf);
232 $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
233 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
234 return redirect($shelf->getUrl());