X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/47b08888ba053375541d503682d503d2fd62ecee..refs/pull/1392/head:/app/Http/Controllers/BookshelfController.php diff --git a/app/Http/Controllers/BookshelfController.php b/app/Http/Controllers/BookshelfController.php index 02b6299ce..b86bc2e38 100644 --- a/app/Http/Controllers/BookshelfController.php +++ b/app/Http/Controllers/BookshelfController.php @@ -1,11 +1,10 @@ entityRepo = $entityRepo; $this->userRepo = $userRepo; - $this->exportService = $exportService; + $this->entityContextManager = $entityContextManager; parent::__construct(); } @@ -37,19 +36,35 @@ class BookshelfController extends Controller */ public function index() { - $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18); + $view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid')); + $sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name'); + $order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc'); + $sortOptions = [ + 'name' => trans('common.sort_name'), + 'created_at' => trans('common.sort_created_at'), + 'updated_at' => trans('common.sort_updated_at'), + ]; + + $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order); + foreach ($shelves as $shelf) { + $shelf->books = $this->entityRepo->getBookshelfChildren($shelf); + } + $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false; $popular = $this->entityRepo->getPopular('bookshelf', 4, 0); $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0); - $shelvesViewType = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid')); + $this->entityContextManager->clearShelfContext(); $this->setPageTitle(trans('entities.shelves')); - return view('shelves/index', [ + return view('shelves.index', [ 'shelves' => $shelves, 'recents' => $recents, 'popular' => $popular, 'new' => $new, - 'shelvesViewType' => $shelvesViewType + 'view' => $view, + 'sort' => $sort, + 'order' => $order, + 'sortOptions' => $sortOptions, ]); } @@ -62,7 +77,7 @@ class BookshelfController extends Controller $this->checkPermission('bookshelf-create-all'); $books = $this->entityRepo->getAll('book', false, 'update'); $this->setPageTitle(trans('entities.shelves_create')); - return view('shelves/create', ['books' => $books]); + return view('shelves.create', ['books' => $books]); } /** @@ -94,17 +109,19 @@ class BookshelfController extends Controller */ public function show(string $slug) { - $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */ + /** @var Bookshelf $bookshelf */ + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); $this->checkOwnablePermission('book-view', $bookshelf); $books = $this->entityRepo->getBookshelfChildren($bookshelf); Views::add($bookshelf); + $this->entityContextManager->setShelfContext($bookshelf->id); $this->setPageTitle($bookshelf->getShortName()); - return view('shelves/show', [ + return view('shelves.show', [ 'shelf' => $bookshelf, 'books' => $books, - 'activity' => Activity::entityActivity($bookshelf, 20, 0) + 'activity' => Activity::entityActivity($bookshelf, 20, 1) ]); } @@ -127,7 +144,7 @@ class BookshelfController extends Controller }); $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()])); - return view('shelves/edit', [ + return view('shelves.edit', [ 'shelf' => $bookshelf, 'books' => $books, 'shelfBooks' => $shelfBooks, @@ -171,7 +188,7 @@ class BookshelfController extends Controller $this->checkOwnablePermission('bookshelf-delete', $bookshelf); $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()])); - return view('shelves/delete', ['shelf' => $bookshelf]); + return view('shelves.delete', ['shelf' => $bookshelf]); } /** @@ -189,37 +206,56 @@ class BookshelfController extends Controller $this->entityRepo->destroyBookshelf($bookshelf); return redirect('/shelves'); } -// -// /** -// * Show the Restrictions view. -// * @param $bookSlug -// * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View -// */ -// public function showRestrict($bookSlug) -// { -// $book = $this->entityRepo->getBySlug('book', $bookSlug); -// $this->checkOwnablePermission('restrictions-manage', $book); -// $roles = $this->userRepo->getRestrictableRoles(); -// return view('books/restrictions', [ -// 'book' => $book, -// 'roles' => $roles -// ]); -// } -// -// /** -// * Set the restrictions for this book. -// * @param $bookSlug -// * @param $bookSlug -// * @param Request $request -// * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector -// */ -// public function restrict($bookSlug, Request $request) -// { -// $book = $this->entityRepo->getBySlug('book', $bookSlug); -// $this->checkOwnablePermission('restrictions-manage', $book); -// $this->entityRepo->updateEntityPermissionsFromRequest($request, $book); -// session()->flash('success', trans('entities.books_permissions_updated')); -// return redirect($book->getUrl()); -// } + /** + * Show the permissions view. + * @param string $slug + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + * @throws \BookStack\Exceptions\NotFoundException + */ + public function showPermissions(string $slug) + { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); + $this->checkOwnablePermission('restrictions-manage', $bookshelf); + + $roles = $this->userRepo->getRestrictableRoles(); + return view('shelves.permissions', [ + 'shelf' => $bookshelf, + 'roles' => $roles + ]); + } + + /** + * Set the permissions for this bookshelf. + * @param string $slug + * @param Request $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \BookStack\Exceptions\NotFoundException + * @throws \Throwable + */ + public function permissions(string $slug, Request $request) + { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); + $this->checkOwnablePermission('restrictions-manage', $bookshelf); + + $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf); + session()->flash('success', trans('entities.shelves_permissions_updated')); + return redirect($bookshelf->getUrl()); + } + + /** + * Copy the permissions of a bookshelf to the child books. + * @param string $slug + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \BookStack\Exceptions\NotFoundException + */ + public function copyPermissions(string $slug) + { + $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); + $this->checkOwnablePermission('restrictions-manage', $bookshelf); + + $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf); + session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount])); + return redirect($bookshelf->getUrl()); + } }