From 26ba0563026590b24ee0500a30ee46e1b2fbe442 Mon Sep 17 00:00:00 2001 From: Guillaume Hanotel Date: Fri, 29 Jan 2021 07:58:17 +0100 Subject: [PATCH 1/2] Sort Books within Shelves --- app/Entities/Models/Bookshelf.php | 15 +++++++++++++++ app/Http/Controllers/BookshelfController.php | 7 ++++++- resources/views/shelves/show.blade.php | 19 +++++++++++++++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/Entities/Models/Bookshelf.php b/app/Entities/Models/Bookshelf.php index 8ffd06d2e..d2b887919 100644 --- a/app/Entities/Models/Bookshelf.php +++ b/app/Entities/Models/Bookshelf.php @@ -1,6 +1,7 @@ books()->visible(); } + /** + * Get the books in this shelf that are visible to the current user with sorted by custom parameter + * @param string $sort - Chosen Column to be sorted + * @param string $order - Order of the sort + * @return Collection + */ + public function visibleBooksByCustomSorting(string $sort = 'name', string $order = 'asc'): Collection + { + return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id') + ->orderBy($sort, $order) + ->visible() + ->get(); + } + /** * Get the url for this bookshelf. */ diff --git a/app/Http/Controllers/BookshelfController.php b/app/Http/Controllers/BookshelfController.php index 32c22e185..1137f6481 100644 --- a/app/Http/Controllers/BookshelfController.php +++ b/app/Http/Controllers/BookshelfController.php @@ -101,6 +101,9 @@ class BookshelfController extends Controller $shelf = $this->bookshelfRepo->getBySlug($slug); $this->checkOwnablePermission('book-view', $shelf); + $sort = setting()->getForCurrentUser('books_sort', 'name'); + $order = setting()->getForCurrentUser('books_sort_order', 'asc'); + Views::add($shelf); $this->entityContextManager->setShelfContext($shelf->id); $view = setting()->getForCurrentUser('bookshelf_view_type', config('app.views.books')); @@ -109,7 +112,9 @@ class BookshelfController extends Controller return view('shelves.show', [ 'shelf' => $shelf, 'view' => $view, - 'activity' => Activity::entityActivity($shelf, 20, 1) + 'activity' => Activity::entityActivity($shelf, 20, 1), + 'order' => $order, + 'sort' => $sort ]); } diff --git a/resources/views/shelves/show.blade.php b/resources/views/shelves/show.blade.php index 46432c1b9..3f87ad252 100644 --- a/resources/views/shelves/show.blade.php +++ b/resources/views/shelves/show.blade.php @@ -9,19 +9,30 @@
-

{{$shelf->name}}

+ +
+

{{$shelf->name}}

+
+ @include('partials.sort', ['options' => [ + 'name' => trans('common.sort_name'), + 'created_at' => trans('common.sort_created_at'), + 'updated_at' => trans('common.sort_updated_at'), + ], 'order' => $order, 'sort' => $sort, 'type' => 'books']) +
+
+

{!! nl2br(e($shelf->description)) !!}

- @if(count($shelf->visibleBooks) > 0) + @if(count($shelf->visibleBooksByCustomSorting($sort, $order)) > 0) @if($view === 'list')
- @foreach($shelf->visibleBooks as $book) + @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $book) @include('books.list-item', ['book' => $book]) @endforeach
@else
- @foreach($shelf->visibleBooks as $key => $book) + @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $key => $book) @include('partials.entity-grid-item', ['entity' => $book]) @endforeach
From a7848b916b302832d0f0823f789443daaeb7786e Mon Sep 17 00:00:00 2001 From: Guillaume Hanotel Date: Sun, 31 Jan 2021 04:25:31 +0100 Subject: [PATCH 2/2] Improve sorting Shelf Books --- app/Entities/Models/Bookshelf.php | 15 --------------- app/Http/Controllers/BookshelfController.php | 11 +++++++++-- app/Http/Controllers/UserController.php | 2 +- resources/views/shelves/show.blade.php | 8 ++++---- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/app/Entities/Models/Bookshelf.php b/app/Entities/Models/Bookshelf.php index d2b887919..8ffd06d2e 100644 --- a/app/Entities/Models/Bookshelf.php +++ b/app/Entities/Models/Bookshelf.php @@ -1,7 +1,6 @@ books()->visible(); } - /** - * Get the books in this shelf that are visible to the current user with sorted by custom parameter - * @param string $sort - Chosen Column to be sorted - * @param string $order - Order of the sort - * @return Collection - */ - public function visibleBooksByCustomSorting(string $sort = 'name', string $order = 'asc'): Collection - { - return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id') - ->orderBy($sort, $order) - ->visible() - ->get(); - } - /** * Get the url for this bookshelf. */ diff --git a/app/Http/Controllers/BookshelfController.php b/app/Http/Controllers/BookshelfController.php index 1137f6481..6c090a26d 100644 --- a/app/Http/Controllers/BookshelfController.php +++ b/app/Http/Controllers/BookshelfController.php @@ -101,8 +101,14 @@ class BookshelfController extends Controller $shelf = $this->bookshelfRepo->getBySlug($slug); $this->checkOwnablePermission('book-view', $shelf); - $sort = setting()->getForCurrentUser('books_sort', 'name'); - $order = setting()->getForCurrentUser('books_sort_order', 'asc'); + $sort = setting()->getForCurrentUser('shelf_books_sort', 'name'); + $order = setting()->getForCurrentUser('shelf_books_sort_order', 'asc'); + + $visibleShelfBooks = $shelf->visibleBooks()->get(); + $sortedVisibleShelfBooks = $visibleShelfBooks + ->sortBy($sort, SORT_REGULAR, $order === 'desc') + ->values() + ->all(); Views::add($shelf); $this->entityContextManager->setShelfContext($shelf->id); @@ -111,6 +117,7 @@ class BookshelfController extends Controller $this->setPageTitle($shelf->getShortName()); return view('shelves.show', [ 'shelf' => $shelf, + 'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks, 'view' => $view, 'activity' => Activity::entityActivity($shelf, 20, 1), 'order' => $order, diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 92e1cd8b7..f3ee03156 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -310,7 +310,7 @@ class UserController extends Controller */ public function changeSort(Request $request, string $id, string $type) { - $validSortTypes = ['books', 'bookshelves']; + $validSortTypes = ['books', 'bookshelves', 'shelf_books']; if (!in_array($type, $validSortTypes)) { return redirect()->back(500); } diff --git a/resources/views/shelves/show.blade.php b/resources/views/shelves/show.blade.php index 3f87ad252..a7a55a427 100644 --- a/resources/views/shelves/show.blade.php +++ b/resources/views/shelves/show.blade.php @@ -17,22 +17,22 @@ 'name' => trans('common.sort_name'), 'created_at' => trans('common.sort_created_at'), 'updated_at' => trans('common.sort_updated_at'), - ], 'order' => $order, 'sort' => $sort, 'type' => 'books']) + ], 'order' => $order, 'sort' => $sort, 'type' => 'shelf_books'])

{!! nl2br(e($shelf->description)) !!}

- @if(count($shelf->visibleBooksByCustomSorting($sort, $order)) > 0) + @if(count($sortedVisibleShelfBooks) > 0) @if($view === 'list')
- @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $book) + @foreach($sortedVisibleShelfBooks as $book) @include('books.list-item', ['book' => $book]) @endforeach
@else
- @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $key => $book) + @foreach($sortedVisibleShelfBooks as $key => $book) @include('partials.entity-grid-item', ['entity' => $book]) @endforeach