mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-07 23:03:00 +03:00
Sorting: Finished main sort set CRUD work
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use BookStack\Activity\Models\Loggable;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -12,16 +13,14 @@ use Illuminate\Database\Eloquent\Model;
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
*/
|
||||
class SortSet extends Model
|
||||
class SortSet extends Model implements Loggable
|
||||
{
|
||||
/**
|
||||
* @return SortSetOperation[]
|
||||
*/
|
||||
public function getOperations(): array
|
||||
{
|
||||
$strOptions = explode(',', $this->sequence);
|
||||
$options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
|
||||
return array_filter($options);
|
||||
return SortSetOperation::fromSequence($this->sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,4 +31,14 @@ class SortSet extends Model
|
||||
$values = array_map(fn (SortSetOperation $opt) => $opt->value, $options);
|
||||
$this->sequence = implode(',', $values);
|
||||
}
|
||||
|
||||
public function logDescriptor(): string
|
||||
{
|
||||
return "({$this->id}) {$this->name}";
|
||||
}
|
||||
|
||||
public function getUrl(): string
|
||||
{
|
||||
return url("/settings/sorting/sets/{$this->id}");
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace BookStack\Sorting;
|
||||
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Http\Controller;
|
||||
use BookStack\Http\Request;
|
||||
|
||||
class SortSetController extends Controller
|
||||
{
|
||||
@@ -14,6 +16,73 @@ class SortSetController extends Controller
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->setPageTitle(trans('settings.sort_set_create'));
|
||||
|
||||
return view('settings.sort-sets.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'name' => ['required', 'string', 'min:1', 'max:200'],
|
||||
'sequence' => ['required', 'string', 'min:1'],
|
||||
]);
|
||||
|
||||
$operations = SortSetOperation::fromSequence($request->input('sequence'));
|
||||
if (count($operations) === 0) {
|
||||
return redirect()->withInput()->withErrors(['sequence' => 'No operations set.']);
|
||||
}
|
||||
|
||||
$set = new SortSet();
|
||||
$set->name = $request->input('name');
|
||||
$set->setOperations($operations);
|
||||
$set->save();
|
||||
|
||||
$this->logActivity(ActivityType::SORT_SET_CREATE, $set);
|
||||
|
||||
return redirect('/settings/sorting');
|
||||
}
|
||||
|
||||
public function edit(string $id)
|
||||
{
|
||||
$set = SortSet::query()->findOrFail($id);
|
||||
|
||||
$this->setPageTitle(trans('settings.sort_set_edit'));
|
||||
|
||||
return view('settings.sort-sets.edit', ['set' => $set]);
|
||||
}
|
||||
|
||||
public function update(string $id, Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'name' => ['required', 'string', 'min:1', 'max:200'],
|
||||
'sequence' => ['required', 'string', 'min:1'],
|
||||
]);
|
||||
|
||||
$set = SortSet::query()->findOrFail($id);
|
||||
$operations = SortSetOperation::fromSequence($request->input('sequence'));
|
||||
if (count($operations) === 0) {
|
||||
return redirect()->withInput()->withErrors(['sequence' => 'No operations set.']);
|
||||
}
|
||||
|
||||
$set->name = $request->input('name');
|
||||
$set->setOperations($operations);
|
||||
$set->save();
|
||||
|
||||
$this->logActivity(ActivityType::SORT_SET_UPDATE, $set);
|
||||
|
||||
return redirect('/settings/sorting');
|
||||
}
|
||||
|
||||
public function destroy(string $id)
|
||||
{
|
||||
$set = SortSet::query()->findOrFail($id);
|
||||
|
||||
// TODO - Check if it's in use
|
||||
|
||||
$set->delete();
|
||||
$this->logActivity(ActivityType::SORT_SET_DELETE, $set);
|
||||
|
||||
return redirect('/settings/sorting');
|
||||
}
|
||||
}
|
||||
|
@@ -39,6 +39,21 @@ enum SortSetOperation: string
|
||||
public static function allExcluding(array $operations): array
|
||||
{
|
||||
$all = SortSetOperation::cases();
|
||||
return array_diff($all, $operations);
|
||||
$filtered = array_filter($all, function (SortSetOperation $operation) use ($operations) {
|
||||
return !in_array($operation, $operations);
|
||||
});
|
||||
return array_values($filtered);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a set of operations from a string sequence representation.
|
||||
* (values seperated by commas).
|
||||
* @return SortSetOperation[]
|
||||
*/
|
||||
public static function fromSequence(string $sequence): array
|
||||
{
|
||||
$strOptions = explode(',', $sequence);
|
||||
$options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
|
||||
return array_filter($options);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user