]> BookStack Code Mirror - bookstack/commitdiff
Improve sorting Shelf Books 2515/head
authorGuillaume Hanotel <redacted>
Sun, 31 Jan 2021 03:25:31 +0000 (04:25 +0100)
committerGuillaume Hanotel <redacted>
Sun, 31 Jan 2021 03:28:25 +0000 (04:28 +0100)
app/Entities/Models/Bookshelf.php
app/Http/Controllers/BookshelfController.php
app/Http/Controllers/UserController.php
resources/views/shelves/show.blade.php

index d2b8879198fcee4f3218e280c3b67419b49e2508..8ffd06d2e2f1b9d22dee6cabb23318976c85ddac 100644 (file)
@@ -1,7 +1,6 @@
 <?php namespace BookStack\Entities\Models;
 
 use BookStack\Uploads\Image;
-use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 
@@ -35,20 +34,6 @@ class Bookshelf extends Entity implements HasCoverImage
         return $this->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.
      */
index 1137f6481224af0067a07112a3f53b31ff2e7ff2..6c090a26da999cecbe12f7482b1e872d548d7421 100644 (file)
@@ -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,
index 92e1cd8b7766864527f71641efd78cf4e1c6ec39..f3ee0315615cf07bb536b9d8ef0584d7c7899dfa 100644 (file)
@@ -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);
         }
index 3f87ad25299ef44a53ecac59c315c21a8cb7f41c..a7a55a4270bddd0ca91f69a66845e2a6a5b88472 100644 (file)
                     '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'])
             </div>
         </div>
 
         <div class="book-content">
             <p class="text-muted">{!! nl2br(e($shelf->description)) !!}</p>
-            @if(count($shelf->visibleBooksByCustomSorting($sort, $order)) > 0)
+            @if(count($sortedVisibleShelfBooks) > 0)
                 @if($view === 'list')
                     <div class="entity-list">
-                        @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $book)
+                        @foreach($sortedVisibleShelfBooks as $book)
                             @include('books.list-item', ['book' => $book])
                         @endforeach
                     </div>
                 @else
                     <div class="grid third">
-                        @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $key => $book)
+                        @foreach($sortedVisibleShelfBooks as $key => $book)
                             @include('partials.entity-grid-item', ['entity' => $book])
                         @endforeach
                     </div>