Skip to content

Commit 5b0cb3d

Browse files
committed
Sorting: Extracted URL sort helper to own class
Was only used in one place, so didn't make sense to have extra global helper clutter.
1 parent ac0cd99 commit 5b0cb3d

4 files changed

Lines changed: 53 additions & 34 deletions

File tree

app/Activity/Controllers/AuditLogController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Activity\ActivityType;
66
use BookStack\Activity\Models\Activity;
77
use BookStack\Http\Controller;
8+
use BookStack\Sorting\SortUrl;
89
use BookStack\Util\SimpleListOptions;
910
use Illuminate\Http\Request;
1011

@@ -65,6 +66,7 @@ public function index(Request $request)
6566
'filters' => $filters,
6667
'listOptions' => $listOptions,
6768
'activityTypes' => $types,
69+
'filterSortUrl' => new SortUrl('settings/audit', array_filter($request->except('page')))
6870
]);
6971
}
7072
}

app/App/helpers.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,3 @@ function theme_path(string $path = ''): ?string
9696

9797
return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
9898
}
99-
100-
/**
101-
* Generate a URL with multiple parameters for sorting purposes.
102-
* Works out the logic to set the correct sorting direction
103-
* Discards empty parameters and allows overriding.
104-
*/
105-
function sortUrl(string $path, array $data, array $overrideData = []): string
106-
{
107-
$queryStringSections = [];
108-
$queryData = array_merge($data, $overrideData);
109-
110-
// Change sorting direction is already sorted on current attribute
111-
if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
112-
$queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
113-
} elseif (isset($overrideData['sort'])) {
114-
$queryData['order'] = 'asc';
115-
}
116-
117-
foreach ($queryData as $name => $value) {
118-
$trimmedVal = trim($value);
119-
if ($trimmedVal === '') {
120-
continue;
121-
}
122-
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
123-
}
124-
125-
if (count($queryStringSections) === 0) {
126-
return url($path);
127-
}
128-
129-
return url($path . '?' . implode('&', $queryStringSections));
130-
}

app/Sorting/SortUrl.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace BookStack\Sorting;
4+
5+
/**
6+
* Generate a URL with multiple parameters for sorting purposes.
7+
* Works out the logic to set the correct sorting direction
8+
* Discards empty parameters and allows overriding.
9+
*/
10+
class SortUrl
11+
{
12+
public function __construct(
13+
protected string $path,
14+
protected array $data,
15+
protected array $overrideData = []
16+
) {
17+
}
18+
19+
public function withOverrideData(array $overrideData = []): self
20+
{
21+
return new self($this->path, $this->data, $overrideData);
22+
}
23+
24+
public function build(): string
25+
{
26+
$queryStringSections = [];
27+
$queryData = array_merge($this->data, $this->overrideData);
28+
29+
// Change sorting direction if already sorted on current attribute
30+
if (isset($this->overrideData['sort']) && $this->overrideData['sort'] === $this->data['sort']) {
31+
$queryData['order'] = ($this->data['order'] === 'asc') ? 'desc' : 'asc';
32+
} elseif (isset($this->overrideData['sort'])) {
33+
$queryData['order'] = 'asc';
34+
}
35+
36+
foreach ($queryData as $name => $value) {
37+
$trimmedVal = trim($value);
38+
if ($trimmedVal !== '') {
39+
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
40+
}
41+
}
42+
43+
if (count($queryStringSections) === 0) {
44+
return url($this->path);
45+
}
46+
47+
return url($this->path . '?' . implode('&', $queryStringSections));
48+
}
49+
}

resources/views/settings/audit.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class="flex-container-row wrap justify-flex-start gap-x-m gap-y-xs">
2626
class="input-base text-left">{{ $filters['event'] ?: trans('settings.audit_event_filter_no_filter') }}</button>
2727
<ul refs="dropdown@menu" class="dropdown-menu">
2828
<li @if($filters['event'] === '') class="active" @endif><a
29-
href="{{ sortUrl('/settings/audit', array_filter(request()->except('page')), ['event' => '']) }}"
29+
href="{{ $filterSortUrl->withOverrideData(['event' => ''])->build() }}"
3030
class="text-item">{{ trans('settings.audit_event_filter_no_filter') }}</a></li>
3131
@foreach($activityTypes as $type)
3232
<li @if($type === $filters['event']) class="active" @endif><a
33-
href="{{ sortUrl('/settings/audit', array_filter(request()->except('page')), ['event' => $type]) }}"
33+
href="{{ $filterSortUrl->withOverrideData(['event' => $type])->build() }}"
3434
class="text-item">{{ $type }}</a></li>
3535
@endforeach
3636
</ul>

0 commit comments

Comments
 (0)