Improve sorting Shelf Books
This commit is contained in:
parent
26ba056302
commit
a7848b916b
|
@ -1,7 +1,6 @@
|
||||||
<?php namespace BookStack\Entities\Models;
|
<?php namespace BookStack\Entities\Models;
|
||||||
|
|
||||||
use BookStack\Uploads\Image;
|
use BookStack\Uploads\Image;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
|
@ -35,20 +34,6 @@ class Bookshelf extends Entity implements HasCoverImage
|
||||||
return $this->books()->visible();
|
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.
|
* Get the url for this bookshelf.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -101,8 +101,14 @@ class BookshelfController extends Controller
|
||||||
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
||||||
$this->checkOwnablePermission('book-view', $shelf);
|
$this->checkOwnablePermission('book-view', $shelf);
|
||||||
|
|
||||||
$sort = setting()->getForCurrentUser('books_sort', 'name');
|
$sort = setting()->getForCurrentUser('shelf_books_sort', 'name');
|
||||||
$order = setting()->getForCurrentUser('books_sort_order', 'asc');
|
$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);
|
Views::add($shelf);
|
||||||
$this->entityContextManager->setShelfContext($shelf->id);
|
$this->entityContextManager->setShelfContext($shelf->id);
|
||||||
|
@ -111,6 +117,7 @@ class BookshelfController extends Controller
|
||||||
$this->setPageTitle($shelf->getShortName());
|
$this->setPageTitle($shelf->getShortName());
|
||||||
return view('shelves.show', [
|
return view('shelves.show', [
|
||||||
'shelf' => $shelf,
|
'shelf' => $shelf,
|
||||||
|
'sortedVisibleShelfBooks' => $sortedVisibleShelfBooks,
|
||||||
'view' => $view,
|
'view' => $view,
|
||||||
'activity' => Activity::entityActivity($shelf, 20, 1),
|
'activity' => Activity::entityActivity($shelf, 20, 1),
|
||||||
'order' => $order,
|
'order' => $order,
|
||||||
|
|
|
@ -310,7 +310,7 @@ class UserController extends Controller
|
||||||
*/
|
*/
|
||||||
public function changeSort(Request $request, string $id, string $type)
|
public function changeSort(Request $request, string $id, string $type)
|
||||||
{
|
{
|
||||||
$validSortTypes = ['books', 'bookshelves'];
|
$validSortTypes = ['books', 'bookshelves', 'shelf_books'];
|
||||||
if (!in_array($type, $validSortTypes)) {
|
if (!in_array($type, $validSortTypes)) {
|
||||||
return redirect()->back(500);
|
return redirect()->back(500);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,22 +17,22 @@
|
||||||
'name' => trans('common.sort_name'),
|
'name' => trans('common.sort_name'),
|
||||||
'created_at' => trans('common.sort_created_at'),
|
'created_at' => trans('common.sort_created_at'),
|
||||||
'updated_at' => trans('common.sort_updated_at'),
|
'updated_at' => trans('common.sort_updated_at'),
|
||||||
], 'order' => $order, 'sort' => $sort, 'type' => 'books'])
|
], 'order' => $order, 'sort' => $sort, 'type' => 'shelf_books'])
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="book-content">
|
<div class="book-content">
|
||||||
<p class="text-muted">{!! nl2br(e($shelf->description)) !!}</p>
|
<p class="text-muted">{!! nl2br(e($shelf->description)) !!}</p>
|
||||||
@if(count($shelf->visibleBooksByCustomSorting($sort, $order)) > 0)
|
@if(count($sortedVisibleShelfBooks) > 0)
|
||||||
@if($view === 'list')
|
@if($view === 'list')
|
||||||
<div class="entity-list">
|
<div class="entity-list">
|
||||||
@foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $book)
|
@foreach($sortedVisibleShelfBooks as $book)
|
||||||
@include('books.list-item', ['book' => $book])
|
@include('books.list-item', ['book' => $book])
|
||||||
@endforeach
|
@endforeach
|
||||||
</div>
|
</div>
|
||||||
@else
|
@else
|
||||||
<div class="grid third">
|
<div class="grid third">
|
||||||
@foreach($shelf->visibleBooksByCustomSorting($sort, $order) as $key => $book)
|
@foreach($sortedVisibleShelfBooks as $key => $book)
|
||||||
@include('partials.entity-grid-item', ['entity' => $book])
|
@include('partials.entity-grid-item', ['entity' => $book])
|
||||||
@endforeach
|
@endforeach
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue