1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Managers\EntityContext;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use BookStack\Exceptions\ImageUploadException;
8 use BookStack\Exceptions\NotFoundException;
9 use BookStack\Uploads\ImageRepo;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
15 class BookshelfController extends Controller
18 protected $bookshelfRepo;
19 protected $entityContextManager;
23 * BookController constructor.
25 public function __construct(BookshelfRepo $bookshelfRepo, EntityContext $entityContextManager, ImageRepo $imageRepo)
27 $this->bookshelfRepo = $bookshelfRepo;
28 $this->entityContextManager = $entityContextManager;
29 $this->imageRepo = $imageRepo;
30 parent::__construct();
34 * Display a listing of the book.
36 public function index()
38 $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid'));
39 $sort = setting()->getForCurrentUser('bookshelves_sort', 'name');
40 $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc');
42 'name' => trans('common.sort_name'),
43 'created_at' => trans('common.sort_created_at'),
44 'updated_at' => trans('common.sort_updated_at'),
47 $shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
48 $recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
49 $popular = $this->bookshelfRepo->getPopular(4);
50 $new = $this->bookshelfRepo->getRecentlyCreated(4);
52 $this->entityContextManager->clearShelfContext();
53 $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'));
74 return view('shelves.create', ['books' => $books]);
78 * Store a newly created bookshelf in storage.
79 * @throws ValidationException
80 * @throws ImageUploadException
82 public function store(Request $request)
84 $this->checkPermission('bookshelf-create-all');
85 $this->validate($request, [
86 'name' => 'required|string|max:255',
87 'description' => 'string|max:1000',
88 'image' => $this->getImageValidationRules(),
91 $bookIds = explode(',', $request->get('books', ''));
92 $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
93 $this->bookshelfRepo->updateCoverImage($shelf);
95 Activity::add($shelf, 'bookshelf_create');
96 return redirect($shelf->getUrl());
100 * Display the bookshelf of the given slug.
101 * @throws NotFoundException
103 public function show(string $slug)
105 $shelf = $this->bookshelfRepo->getBySlug($slug);
106 $this->checkOwnablePermission('book-view', $shelf);
109 $this->entityContextManager->setShelfContext($shelf->id);
111 $this->setPageTitle($shelf->getShortName());
112 return view('shelves.show', [
114 'activity' => Activity::entityActivity($shelf, 20, 1)
119 * Show the form for editing the specified bookshelf.
121 public function edit(string $slug)
123 $shelf = $this->bookshelfRepo->getBySlug($slug);
124 $this->checkOwnablePermission('bookshelf-update', $shelf);
126 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
127 $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
129 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
130 return view('shelves.edit', [
137 * Update the specified bookshelf in storage.
138 * @throws ValidationException
139 * @throws ImageUploadException
140 * @throws NotFoundException
142 public function update(Request $request, string $slug)
144 $shelf = $this->bookshelfRepo->getBySlug($slug);
145 $this->checkOwnablePermission('bookshelf-update', $shelf);
146 $this->validate($request, [
147 'name' => 'required|string|max:255',
148 'description' => 'string|max:1000',
149 'image' => $this->imageRepo->getImageValidationRules(),
153 $bookIds = explode(',', $request->get('books', ''));
154 $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
155 $resetCover = $request->has('image_reset');
156 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
157 Activity::add($shelf, 'bookshelf_update');
159 return redirect($shelf->getUrl());
163 * Shows the page to confirm deletion
165 public function showDelete(string $slug)
167 $shelf = $this->bookshelfRepo->getBySlug($slug);
168 $this->checkOwnablePermission('bookshelf-delete', $shelf);
170 $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
171 return view('shelves.delete', ['shelf' => $shelf]);
175 * Remove the specified bookshelf from storage.
178 public function destroy(string $slug)
180 $shelf = $this->bookshelfRepo->getBySlug($slug);
181 $this->checkOwnablePermission('bookshelf-delete', $shelf);
183 Activity::addMessage('bookshelf_delete', $shelf->name);
184 $this->bookshelfRepo->destroy($shelf);
186 return redirect('/shelves');
190 * Show the permissions view.
192 public function showPermissions(string $slug)
194 $shelf = $this->bookshelfRepo->getBySlug($slug);
195 $this->checkOwnablePermission('restrictions-manage', $shelf);
197 return view('shelves.permissions', [
203 * Set the permissions for this bookshelf.
205 public function permissions(Request $request, string $slug)
207 $shelf = $this->bookshelfRepo->getBySlug($slug);
208 $this->checkOwnablePermission('restrictions-manage', $shelf);
210 $restricted = $request->get('restricted') === 'true';
211 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
212 $this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
214 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
215 return redirect($shelf->getUrl());
219 * Copy the permissions of a bookshelf to the child books.
221 public function copyPermissions(string $slug)
223 $shelf = $this->bookshelfRepo->getBySlug($slug);
224 $this->checkOwnablePermission('restrictions-manage', $shelf);
226 $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
227 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
228 return redirect($shelf->getUrl());