Skip to content

Commit 7093daa

Browse files
committed
Sorting: Connected up default sort setting for books
1 parent b897af2 commit 7093daa

7 files changed

Lines changed: 77 additions & 14 deletions

File tree

app/Entities/Models/Book.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Entities\Models;
44

5+
use BookStack\Sorting\SortSet;
56
use BookStack\Uploads\Image;
67
use Exception;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -16,12 +17,14 @@
1617
* @property string $description
1718
* @property int $image_id
1819
* @property ?int $default_template_id
20+
* @property ?int $sort_set_id
1921
* @property Image|null $cover
2022
* @property \Illuminate\Database\Eloquent\Collection $chapters
2123
* @property \Illuminate\Database\Eloquent\Collection $pages
2224
* @property \Illuminate\Database\Eloquent\Collection $directPages
2325
* @property \Illuminate\Database\Eloquent\Collection $shelves
2426
* @property ?Page $defaultTemplate
27+
* @property ?SortSet $sortSet
2528
*/
2629
class Book extends Entity implements HasCoverImage
2730
{
@@ -82,6 +85,14 @@ public function defaultTemplate(): BelongsTo
8285
return $this->belongsTo(Page::class, 'default_template_id');
8386
}
8487

88+
/**
89+
* Get the sort set assigned to this book, if existing.
90+
*/
91+
public function sortSet(): BelongsTo
92+
{
93+
return $this->belongsTo(SortSet::class);
94+
}
95+
8596
/**
8697
* Get all pages within this book.
8798
*/

app/Entities/Repos/BookRepo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use BookStack\Entities\Tools\TrashCan;
99
use BookStack\Exceptions\ImageUploadException;
1010
use BookStack\Facades\Activity;
11+
use BookStack\Sorting\SortSet;
1112
use BookStack\Uploads\ImageRepo;
1213
use Exception;
1314
use Illuminate\Http\UploadedFile;
@@ -33,6 +34,12 @@ public function create(array $input): Book
3334
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
3435
Activity::add(ActivityType::BOOK_CREATE, $book);
3536

37+
$defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
38+
if ($defaultBookSortSetting && SortSet::query()->find($defaultBookSortSetting)) {
39+
$book->sort_set_id = $defaultBookSortSetting;
40+
$book->save();
41+
}
42+
3643
return $book;
3744
}
3845

app/Sorting/SortSet.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace BookStack\Sorting;
44

55
use BookStack\Activity\Models\Loggable;
6+
use BookStack\Entities\Models\Book;
67
use Carbon\Carbon;
78
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Relations\HasMany;
810

911
/**
1012
* @property int $id
@@ -41,4 +43,9 @@ public function getUrl(): string
4143
{
4244
return url("/settings/sorting/sets/{$this->id}");
4345
}
46+
47+
public function books(): HasMany
48+
{
49+
return $this->hasMany(Book::class);
50+
}
4451
}

app/Sorting/SortSetController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function update(string $id, Request $request)
6262
$set = SortSet::query()->findOrFail($id);
6363
$operations = SortSetOperation::fromSequence($request->input('sequence'));
6464
if (count($operations) === 0) {
65-
return redirect()->withInput()->withErrors(['sequence' => 'No operations set.']);
65+
return redirect($set->getUrl())->withInput()->withErrors(['sequence' => 'No operations set.']);
6666
}
6767

6868
$set->name = $request->input('name');
@@ -78,7 +78,16 @@ public function destroy(string $id)
7878
{
7979
$set = SortSet::query()->findOrFail($id);
8080

81-
// TODO - Check if it's in use
81+
if ($set->books()->count() > 0) {
82+
$this->showErrorNotification(trans('settings.sort_set_delete_fail_books'));
83+
return redirect($set->getUrl());
84+
}
85+
86+
$defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
87+
if ($defaultBookSortSetting === intval($id)) {
88+
$this->showErrorNotification(trans('settings.sort_set_delete_fail_default'));
89+
return redirect($set->getUrl());
90+
}
8291

8392
$set->delete();
8493
$this->logActivity(ActivityType::SORT_SET_DELETE, $set);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('books', function (Blueprint $table) {
15+
$table->unsignedInteger('sort_set_id')->nullable()->default(null);
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('books', function (Blueprint $table) {
25+
$table->dropColumn('sort_set_id');
26+
});
27+
}
28+
};

lang/en/settings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
'sort_set_edit' => 'Edit Sort Set',
8585
'sort_set_delete' => 'Delete Sort Set',
8686
'sort_set_delete_desc' => 'Remove this sort set from the system. Deletion will only go ahead if the sort is not in active use.',
87+
'sort_set_delete_fail_books' => 'Unable to delete this sort set since it has books assigned.',
88+
'sort_set_delete_fail_default' => 'Unable to delete this sort set since it\'s used as the default book sort.',
8789
'sort_set_details' => 'Sort Set Details',
8890
'sort_set_details_desc' => 'Set a name for this sort set, which will appear in lists when users are selecting a sort.',
8991
'sort_set_operations' => 'Sort Operations',

resources/views/settings/categories/sorting.blade.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
@extends('settings.layout')
22

3+
@php
4+
$sortSets = \BookStack\Sorting\SortSet::query()->orderBy('name', 'asc')->get();
5+
@endphp
6+
37
@section('card')
48
<h1 id="sorting" class="list-heading">{{ trans('settings.sorting') }}</h1>
59
<form action="{{ url("/settings/sorting") }}" method="POST">
@@ -19,15 +23,13 @@ class="setting-list-label">{{ trans('settings.sorting_book_default') }}</label>
1923
<option value="0" @if(intval(setting('sorting-book-default', '0')) === 0) selected @endif>
2024
-- {{ trans('common.none') }} --
2125
</option>
22-
{{-- TODO--}}
23-
{{-- @foreach(\BookStack\Users\Models\Role::all() as $role)--}}
24-
{{-- <option value="{{$role->id}}"--}}
25-
{{-- data-system-role-name="{{ $role->system_name ?? '' }}"--}}
26-
{{-- @if(intval(setting('registration-role', '0')) === $role->id) selected @endif--}}
27-
{{-- >--}}
28-
{{-- {{ $role->display_name }}--}}
29-
{{-- </option>--}}
30-
{{-- @endforeach--}}
26+
@foreach($sortSets as $set)
27+
<option value="{{$set->id}}"
28+
@if(intval(setting('sorting-book-default', '0')) === $set->id) selected @endif
29+
>
30+
{{ $set->name }}
31+
</option>
32+
@endforeach
3133
</select>
3234
</div>
3335
</div>
@@ -52,9 +54,6 @@ class="setting-list-label">{{ trans('settings.sorting_book_default') }}</label>
5254
</div>
5355
</div>
5456

55-
@php
56-
$sortSets = \BookStack\Sorting\SortSet::query()->orderBy('name', 'asc')->get();
57-
@endphp
5857
@if(empty($sortSets))
5958
<p class="italic text-muted">{{ trans('common.no_items') }}</p>
6059
@else

0 commit comments

Comments
 (0)