- $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
- $this->checkOwnablePermission('book-view', $bookshelf);
-
- $books = $this->entityRepo->getBookshelfChildren($bookshelf);
- Views::add($bookshelf);
-
- $this->setPageTitle($bookshelf->getShortName());
- return view('shelves/show', [
- 'shelf' => $bookshelf,
- 'books' => $books,
- 'activity' => Activity::entityActivity($bookshelf, 20, 0)
+ $shelf = $this->bookshelfRepo->getBySlug($slug);
+ $this->checkOwnablePermission('book-view', $shelf);
+
+ $sort = setting()->getForCurrentUser('shelf_books_sort', 'default');
+ $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc');
+
+ $sortedVisibleShelfBooks = $shelf->visibleBooks()->get()
+ ->sortBy($sort === 'default' ? 'pivot.order' : $sort, SORT_REGULAR, $order === 'desc')
+ ->values()
+ ->all();
+
+ Views::add($shelf);
+ $this->entityContextManager->setShelfContext($shelf->id);
+ $view = setting()->getForCurrentUser('bookshelf_view_type');
+
+ $this->setPageTitle($shelf->getShortName());
+ return view('shelves.show', [
+ 'shelf' => $shelf,
+ 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
+ 'view' => $view,
+ 'activity' => Activity::entityActivity($shelf, 20, 1),
+ 'order' => $order,
+ 'sort' => $sort