3 namespace BookStack\Sorting;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\EntityContainerData;
7 use BookStack\Http\Controller;
8 use BookStack\Permissions\Permission;
9 use Illuminate\Http\Request;
11 class SortRuleController extends Controller
13 public function __construct()
15 $this->middleware(Permission::SettingsManage->middleware());
18 public function create()
20 $this->setPageTitle(trans('settings.sort_rule_create'));
22 return view('settings.sort-rules.create');
25 public function store(Request $request)
27 $this->validate($request, [
28 'name' => ['required', 'string', 'min:1', 'max:200'],
29 'sequence' => ['required', 'string', 'min:1'],
32 $operations = SortRuleOperation::fromSequence($request->input('sequence'));
33 if (count($operations) === 0) {
34 return redirect('/settings/sorting/rules/new')->withInput()->withErrors(['sequence' => 'No operations set.']);
37 $rule = new SortRule();
38 $rule->name = $request->input('name');
39 $rule->setOperations($operations);
42 $this->logActivity(ActivityType::SORT_RULE_CREATE, $rule);
44 return redirect('/settings/sorting');
47 public function edit(string $id)
49 $rule = SortRule::query()->findOrFail($id);
51 $this->setPageTitle(trans('settings.sort_rule_edit'));
53 return view('settings.sort-rules.edit', ['rule' => $rule]);
56 public function update(string $id, Request $request, BookSorter $bookSorter)
58 $this->validate($request, [
59 'name' => ['required', 'string', 'min:1', 'max:200'],
60 'sequence' => ['required', 'string', 'min:1'],
63 $rule = SortRule::query()->findOrFail($id);
64 $operations = SortRuleOperation::fromSequence($request->input('sequence'));
65 if (count($operations) === 0) {
66 return redirect($rule->getUrl())->withInput()->withErrors(['sequence' => 'No operations set.']);
69 $rule->name = $request->input('name');
70 $rule->setOperations($operations);
71 $changedSequence = $rule->isDirty('sequence');
74 $this->logActivity(ActivityType::SORT_RULE_UPDATE, $rule);
76 if ($changedSequence) {
77 $bookSorter->runBookAutoSortForAllWithSet($rule);
80 return redirect('/settings/sorting');
83 public function destroy(string $id, Request $request)
85 $rule = SortRule::query()->findOrFail($id);
86 $confirmed = $request->input('confirm') === 'true';
87 $booksAssigned = $rule->books()->count();
90 if ($booksAssigned > 0) {
92 EntityContainerData::query()
93 ->where('sort_rule_id', $rule->id)
94 ->update(['sort_rule_id' => null]);
96 $warnings[] = trans('settings.sort_rule_delete_warn_books', ['count' => $booksAssigned]);
100 $defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
101 if ($defaultBookSortSetting === intval($id)) {
103 setting()->remove('sorting-book-default');
105 $warnings[] = trans('settings.sort_rule_delete_warn_default');
109 if (count($warnings) > 0) {
110 return redirect($rule->getUrl() . '#delete')->withErrors(['delete' => $warnings]);
114 $this->logActivity(ActivityType::SORT_RULE_DELETE, $rule);
116 return redirect('/settings/sorting');