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' => 'nullable|' . $this->getImageValidationRules(),
91 $bookIds = explode(',', $request->get('books', ''));
92 $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
93 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
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 $view = setting()->getForCurrentUser('books_view_type', config('app.views.books'));
107 $this->checkOwnablePermission('book-view', $shelf);
110 $this->entityContextManager->setShelfContext($shelf->id);
112 $this->setPageTitle($shelf->getShortName());
113 return view('shelves.show', [
116 'activity' => Activity::entityActivity($shelf, 20, 1)
121 * Show the form for editing the specified bookshelf.
123 public function edit(string $slug)
125 $shelf = $this->bookshelfRepo->getBySlug($slug);
126 $this->checkOwnablePermission('bookshelf-update', $shelf);
128 $shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
129 $books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
131 $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
132 return view('shelves.edit', [
139 * Update the specified bookshelf in storage.
140 * @throws ValidationException
141 * @throws ImageUploadException
142 * @throws NotFoundException
144 public function update(Request $request, string $slug)
146 $shelf = $this->bookshelfRepo->getBySlug($slug);
147 $this->checkOwnablePermission('bookshelf-update', $shelf);
148 $this->validate($request, [
149 'name' => 'required|string|max:255',
150 'description' => 'string|max:1000',
151 'image' => 'nullable|' . $this->getImageValidationRules(),
155 $bookIds = explode(',', $request->get('books', ''));
156 $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
157 $resetCover = $request->has('image_reset');
158 $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
159 Activity::add($shelf, 'bookshelf_update');
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 Activity::addMessage('bookshelf_delete', $shelf->name);
186 $this->bookshelfRepo->destroy($shelf);
188 return redirect('/shelves');
192 * Show the permissions view.
194 public function showPermissions(string $slug)
196 $shelf = $this->bookshelfRepo->getBySlug($slug);
197 $this->checkOwnablePermission('restrictions-manage', $shelf);
199 return view('shelves.permissions', [
205 * Set the permissions for this bookshelf.
207 public function permissions(Request $request, string $slug)
209 $shelf = $this->bookshelfRepo->getBySlug($slug);
210 $this->checkOwnablePermission('restrictions-manage', $shelf);
212 $restricted = $request->get('restricted') === 'true';
213 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
214 $this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
216 $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
217 return redirect($shelf->getUrl());
221 * Copy the permissions of a bookshelf to the child books.
223 public function copyPermissions(string $slug)
225 $shelf = $this->bookshelfRepo->getBySlug($slug);
226 $this->checkOwnablePermission('restrictions-manage', $shelf);
228 $updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
229 $this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
230 return redirect($shelf->getUrl());