]> BookStack Code Mirror - bookstack/blob - app/Sorting/SortRuleController.php
Copying: Fixed issue with non-page links to page permalinks
[bookstack] / app / Sorting / SortRuleController.php
1 <?php
2
3 namespace BookStack\Sorting;
4
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;
10
11 class SortRuleController extends Controller
12 {
13     public function __construct()
14     {
15         $this->middleware(Permission::SettingsManage->middleware());
16     }
17
18     public function create()
19     {
20         $this->setPageTitle(trans('settings.sort_rule_create'));
21
22         return view('settings.sort-rules.create');
23     }
24
25     public function store(Request $request)
26     {
27         $this->validate($request, [
28             'name' => ['required', 'string', 'min:1', 'max:200'],
29             'sequence' => ['required', 'string', 'min:1'],
30         ]);
31
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.']);
35         }
36
37         $rule = new SortRule();
38         $rule->name = $request->input('name');
39         $rule->setOperations($operations);
40         $rule->save();
41
42         $this->logActivity(ActivityType::SORT_RULE_CREATE, $rule);
43
44         return redirect('/settings/sorting');
45     }
46
47     public function edit(string $id)
48     {
49         $rule = SortRule::query()->findOrFail($id);
50
51         $this->setPageTitle(trans('settings.sort_rule_edit'));
52
53         return view('settings.sort-rules.edit', ['rule' => $rule]);
54     }
55
56     public function update(string $id, Request $request, BookSorter $bookSorter)
57     {
58         $this->validate($request, [
59             'name' => ['required', 'string', 'min:1', 'max:200'],
60             'sequence' => ['required', 'string', 'min:1'],
61         ]);
62
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.']);
67         }
68
69         $rule->name = $request->input('name');
70         $rule->setOperations($operations);
71         $changedSequence = $rule->isDirty('sequence');
72         $rule->save();
73
74         $this->logActivity(ActivityType::SORT_RULE_UPDATE, $rule);
75
76         if ($changedSequence) {
77             $bookSorter->runBookAutoSortForAllWithSet($rule);
78         }
79
80         return redirect('/settings/sorting');
81     }
82
83     public function destroy(string $id, Request $request)
84     {
85         $rule = SortRule::query()->findOrFail($id);
86         $confirmed = $request->input('confirm') === 'true';
87         $booksAssigned = $rule->books()->count();
88         $warnings = [];
89
90         if ($booksAssigned > 0) {
91             if ($confirmed) {
92                 EntityContainerData::query()
93                     ->where('sort_rule_id', $rule->id)
94                     ->update(['sort_rule_id' => null]);
95             } else {
96                 $warnings[] = trans('settings.sort_rule_delete_warn_books', ['count' => $booksAssigned]);
97             }
98         }
99
100         $defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
101         if ($defaultBookSortSetting === intval($id)) {
102             if ($confirmed) {
103                 setting()->remove('sorting-book-default');
104             } else {
105                 $warnings[] = trans('settings.sort_rule_delete_warn_default');
106             }
107         }
108
109         if (count($warnings) > 0) {
110             return redirect($rule->getUrl() . '#delete')->withErrors(['delete' => $warnings]);
111         }
112
113         $rule->delete();
114         $this->logActivity(ActivityType::SORT_RULE_DELETE, $rule);
115
116         return redirect('/settings/sorting');
117     }
118 }