]> BookStack Code Mirror - bookstack/commitdiff
Sort Books within Shelves
authorGuillaume Hanotel <redacted>
Fri, 29 Jan 2021 06:58:17 +0000 (07:58 +0100)
committerGuillaume Hanotel <redacted>
Fri, 29 Jan 2021 07:02:18 +0000 (08:02 +0100)
app/Entities/Models/Bookshelf.php
app/Http/Controllers/BookshelfController.php
resources/views/shelves/show.blade.php

index 8ffd06d2e2f1b9d22dee6cabb23318976c85ddac..d2b8879198fcee4f3218e280c3b67419b49e2508 100644 (file)
@@ -1,6 +1,7 @@
 <?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;
 
@@ -34,6 +35,20 @@ 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 32c22e185fa20116cabc0d3221d621c0e3f70d12..1137f6481224af0067a07112a3f53b31ff2e7ff2 100644 (file)
@@ -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
         ]);
     }
 
index 46432c1b92594372b196fdb35fec9951833fffa9..3f87ad25299ef44a53ecac59c315c21a8cb7f41c 100644 (file)
@@ -9,19 +9,30 @@
     </div>
 
     <main class="card content-wrap">
-        <h1 class="break-text">{{$shelf->name}}</h1>
+
+        <div class="grid half v-center">
+            <h1 class="break-text">{{$shelf->name}}</h1>
+            <div class="text-m-right my-m">
+                @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'])
+            </div>
+        </div>
+
         <div class="book-content">
             <p class="text-muted">{!! nl2br(e($shelf->description)) !!}</p>
-            @if(count($shelf->visibleBooks) > 0)
+            @if(count($shelf->visibleBooksByCustomSorting($sort, $order)) > 0)
                 @if($view === 'list')
                     <div class="entity-list">
-                        @foreach($shelf->visibleBooks as $book)
+                        @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $book)
                             @include('books.list-item', ['book' => $book])
                         @endforeach
                     </div>
                 @else
                     <div class="grid third">
-                        @foreach($shelf->visibleBooks as $key => $book)
+                        @foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $key => $book)
                             @include('partials.entity-grid-item', ['entity' => $book])
                         @endforeach
                     </div>