Skip to content

Commit b897af2

Browse files
committed
Sorting: Finished main sort set CRUD work
1 parent d28278b commit b897af2

File tree

10 files changed

+187
-11
lines changed

10 files changed

+187
-11
lines changed

app/Activity/ActivityType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class ActivityType
7171
const IMPORT_RUN = 'import_run';
7272
const IMPORT_DELETE = 'import_delete';
7373

74+
const SORT_SET_CREATE = 'sort_set_create';
75+
const SORT_SET_UPDATE = 'sort_set_update';
76+
const SORT_SET_DELETE = 'sort_set_delete';
77+
7478
/**
7579
* Get all the possible values.
7680
*/

app/Sorting/SortSet.php

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

33
namespace BookStack\Sorting;
44

5+
use BookStack\Activity\Models\Loggable;
56
use Carbon\Carbon;
67
use Illuminate\Database\Eloquent\Model;
78

@@ -12,16 +13,14 @@
1213
* @property Carbon $created_at
1314
* @property Carbon $updated_at
1415
*/
15-
class SortSet extends Model
16+
class SortSet extends Model implements Loggable
1617
{
1718
/**
1819
* @return SortSetOperation[]
1920
*/
2021
public function getOperations(): array
2122
{
22-
$strOptions = explode(',', $this->sequence);
23-
$options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
24-
return array_filter($options);
23+
return SortSetOperation::fromSequence($this->sequence);
2524
}
2625

2726
/**
@@ -32,4 +31,14 @@ public function setOperations(array $options): void
3231
$values = array_map(fn (SortSetOperation $opt) => $opt->value, $options);
3332
$this->sequence = implode(',', $values);
3433
}
34+
35+
public function logDescriptor(): string
36+
{
37+
return "({$this->id}) {$this->name}";
38+
}
39+
40+
public function getUrl(): string
41+
{
42+
return url("/settings/sorting/sets/{$this->id}");
43+
}
3544
}

app/Sorting/SortSetController.php

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

33
namespace BookStack\Sorting;
44

5+
use BookStack\Activity\ActivityType;
56
use BookStack\Http\Controller;
7+
use BookStack\Http\Request;
68

79
class SortSetController extends Controller
810
{
@@ -14,6 +16,73 @@ public function __construct()
1416

1517
public function create()
1618
{
19+
$this->setPageTitle(trans('settings.sort_set_create'));
20+
1721
return view('settings.sort-sets.create');
1822
}
23+
24+
public function store(Request $request)
25+
{
26+
$this->validate($request, [
27+
'name' => ['required', 'string', 'min:1', 'max:200'],
28+
'sequence' => ['required', 'string', 'min:1'],
29+
]);
30+
31+
$operations = SortSetOperation::fromSequence($request->input('sequence'));
32+
if (count($operations) === 0) {
33+
return redirect()->withInput()->withErrors(['sequence' => 'No operations set.']);
34+
}
35+
36+
$set = new SortSet();
37+
$set->name = $request->input('name');
38+
$set->setOperations($operations);
39+
$set->save();
40+
41+
$this->logActivity(ActivityType::SORT_SET_CREATE, $set);
42+
43+
return redirect('/settings/sorting');
44+
}
45+
46+
public function edit(string $id)
47+
{
48+
$set = SortSet::query()->findOrFail($id);
49+
50+
$this->setPageTitle(trans('settings.sort_set_edit'));
51+
52+
return view('settings.sort-sets.edit', ['set' => $set]);
53+
}
54+
55+
public function update(string $id, Request $request)
56+
{
57+
$this->validate($request, [
58+
'name' => ['required', 'string', 'min:1', 'max:200'],
59+
'sequence' => ['required', 'string', 'min:1'],
60+
]);
61+
62+
$set = SortSet::query()->findOrFail($id);
63+
$operations = SortSetOperation::fromSequence($request->input('sequence'));
64+
if (count($operations) === 0) {
65+
return redirect()->withInput()->withErrors(['sequence' => 'No operations set.']);
66+
}
67+
68+
$set->name = $request->input('name');
69+
$set->setOperations($operations);
70+
$set->save();
71+
72+
$this->logActivity(ActivityType::SORT_SET_UPDATE, $set);
73+
74+
return redirect('/settings/sorting');
75+
}
76+
77+
public function destroy(string $id)
78+
{
79+
$set = SortSet::query()->findOrFail($id);
80+
81+
// TODO - Check if it's in use
82+
83+
$set->delete();
84+
$this->logActivity(ActivityType::SORT_SET_DELETE, $set);
85+
86+
return redirect('/settings/sorting');
87+
}
1988
}

app/Sorting/SortSetOperation.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public function getLabel(): string
3939
public static function allExcluding(array $operations): array
4040
{
4141
$all = SortSetOperation::cases();
42-
return array_diff($all, $operations);
42+
$filtered = array_filter($all, function (SortSetOperation $operation) use ($operations) {
43+
return !in_array($operation, $operations);
44+
});
45+
return array_values($filtered);
46+
}
47+
48+
/**
49+
* Create a set of operations from a string sequence representation.
50+
* (values seperated by commas).
51+
* @return SortSetOperation[]
52+
*/
53+
public static function fromSequence(string $sequence): array
54+
{
55+
$strOptions = explode(',', $sequence);
56+
$options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
57+
return array_filter($options);
4358
}
4459
}

lang/en/activities.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@
127127
'comment_update' => 'updated comment',
128128
'comment_delete' => 'deleted comment',
129129

130+
// Sort Sets
131+
'sort_set_create' => 'created sort set',
132+
'sort_set_create_notification' => 'Sort set successfully created',
133+
'sort_set_update' => 'updated sort set',
134+
'sort_set_update_notification' => 'Sort set successfully update',
135+
'sort_set_delete' => 'deleted sort set',
136+
'sort_set_delete_notification' => 'Sort set successfully deleted',
137+
130138
// Other
131139
'permissions_update' => 'updated permissions',
132140
];

lang/en/settings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
'sorting_sets_desc' => 'These are predefined sorting operations which can be applied to content in the system.',
8383
'sort_set_create' => 'Create Sort Set',
8484
'sort_set_edit' => 'Edit Sort Set',
85+
'sort_set_delete' => 'Delete Sort Set',
86+
'sort_set_delete_desc' => 'Remove this sort set from the system. Deletion will only go ahead if the sort is not in active use.',
8587
'sort_set_details' => 'Sort Set Details',
8688
'sort_set_details_desc' => 'Set a name for this sort set, which will appear in lists when users are selecting a sort.',
8789
'sort_set_operations' => 'Sort Operations',

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ class="setting-list-label">{{ trans('settings.sorting_book_default') }}</label>
5252
</div>
5353
</div>
5454

55-
{{-- TODO--}}
55+
@php
56+
$sortSets = \BookStack\Sorting\SortSet::query()->orderBy('name', 'asc')->get();
57+
@endphp
58+
@if(empty($sortSets))
59+
<p class="italic text-muted">{{ trans('common.no_items') }}</p>
60+
@else
61+
<div class="item-list">
62+
@foreach($sortSets as $set)
63+
@include('settings.sort-sets.parts.sort-set-list-item', ['set' => $set])
64+
@endforeach
65+
</div>
66+
@endif
5667
</div>
5768
@endsection
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@extends('layouts.simple')
2+
3+
@section('body')
4+
5+
<div class="container small">
6+
7+
@include('settings.parts.navbar', ['selected' => 'settings'])
8+
9+
<div class="card content-wrap auto-height">
10+
<h1 class="list-heading">{{ trans('settings.sort_set_edit') }}</h1>
11+
12+
<form action="{{ $set->getUrl() }}" method="POST">
13+
{{ method_field('PUT') }}
14+
{{ csrf_field() }}
15+
16+
@include('settings.sort-sets.parts.form', ['model' => $set])
17+
18+
<div class="form-group text-right">
19+
<a href="{{ url("/settings/sorting") }}" class="button outline">{{ trans('common.cancel') }}</a>
20+
<button type="submit" class="button">{{ trans('common.save') }}</button>
21+
</div>
22+
</form>
23+
</div>
24+
25+
<div class="card content-wrap auto-height">
26+
<div class="flex-container-row items-center gap-l">
27+
<div>
28+
<h2 class="list-heading">{{ trans('settings.sort_set_delete') }}</h2>
29+
<p class="text-muted">{{ trans('settings.sort_set_delete_desc') }}</p>
30+
</div>
31+
<div class="flex">
32+
<form action="{{ $set->getUrl() }}" method="POST">
33+
{{ method_field('DELETE') }}
34+
{{ csrf_field() }}
35+
<div class="text-right">
36+
<button type="submit" class="button outline">{{ trans('common.delete') }}</button>
37+
</div>
38+
</form>
39+
</div>
40+
</div>
41+
</div>
42+
</div>
43+
44+
@stop

resources/views/settings/sort-sets/parts/form.blade.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
<div component="sort-set-manager">
1616
<label class="setting-list-label">{{ trans('settings.sort_set_operations') }}</label>
1717
<p class="text-muted text-small">{{ trans('settings.sort_set_operations_desc') }}</p>
18+
@include('form.errors', ['name' => 'sequence'])
1819

19-
<input refs="sort-set-manager@input" type="hidden" name="books"
20-
value="{{ $model?->sequence ?? '' }}">
20+
<input refs="sort-set-manager@input" type="hidden" name="sequence"
21+
value="{{ old('sequence') ?? $model?->sequence ?? '' }}">
22+
23+
@php
24+
$configuredOps = old('sequence') ? \BookStack\Sorting\SortSetOperation::fromSequence(old('sequence')) : ($model?->getOperations() ?? []);
25+
@endphp
2126

2227
<div class="grid half">
2328
<div class="form-group">
@@ -27,8 +32,9 @@
2732
aria-labelledby="sort-set-configured-operations"
2833
class="scroll-box configured-option-list">
2934
<li class="text-muted empty-state px-m py-s italic text-small">{{ trans('settings.sort_set_configured_operations_empty') }}</li>
30-
@foreach(($model?->getOperations() ?? []) as $option)
31-
@include('settings.sort-sets.parts.operation')
35+
36+
@foreach($configuredOps as $operation)
37+
@include('settings.sort-sets.parts.operation', ['operation' => $operation])
3238
@endforeach
3339
</ul>
3440
</div>
@@ -40,7 +46,7 @@ class="scroll-box configured-option-list">
4046
aria-labelledby="sort-set-available-operations"
4147
class="scroll-box available-option-list">
4248
<li class="text-muted empty-state px-m py-s italic text-small">{{ trans('settings.sort_set_available_operations_empty') }}</li>
43-
@foreach(\BookStack\Sorting\SortSetOperation::allExcluding($model?->getOperations() ?? []) as $operation)
49+
@foreach(\BookStack\Sorting\SortSetOperation::allExcluding($configuredOps) as $operation)
4450
@include('settings.sort-sets.parts.operation', ['operation' => $operation])
4551
@endforeach
4652
</ul>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="item-list-row flex-container-row py-xs items-center">
2+
<div class="py-xs px-m flex-2">
3+
<a href="{{ $set->getUrl() }}">{{ $set->name }}</a>
4+
</div>
5+
<div class="px-m text-small text-muted">
6+
{{ implode(', ', array_map(fn ($op) => $op->getLabel(), $set->getOperations())) }}
7+
</div>
8+
</div>

0 commit comments

Comments
 (0)