]> BookStack Code Mirror - bookstack/commitdiff
Sorting: Extracted URL sort helper to own class
authorDan Brown <redacted>
Wed, 29 Jan 2025 17:02:34 +0000 (17:02 +0000)
committerDan Brown <redacted>
Wed, 29 Jan 2025 17:02:34 +0000 (17:02 +0000)
Was only used in one place, so didn't make sense to have extra global
helper clutter.

app/Activity/Controllers/AuditLogController.php
app/App/helpers.php
app/Sorting/SortUrl.php [new file with mode: 0644]
resources/views/settings/audit.blade.php

index 641106d7f450697b1c189b128264feecc46c9b06..66ca301977ce35300cc26e0d196a5586e5975e5f 100644 (file)
@@ -5,6 +5,7 @@ namespace BookStack\Activity\Controllers;
 use BookStack\Activity\ActivityType;
 use BookStack\Activity\Models\Activity;
 use BookStack\Http\Controller;
+use BookStack\Sorting\SortUrl;
 use BookStack\Util\SimpleListOptions;
 use Illuminate\Http\Request;
 
@@ -65,6 +66,7 @@ class AuditLogController extends Controller
             'filters'       => $filters,
             'listOptions'   => $listOptions,
             'activityTypes' => $types,
+            'filterSortUrl' => new SortUrl('settings/audit', array_filter($request->except('page')))
         ]);
     }
 }
index 941c267d6cd1950c183f65303915996a35917c89..204b3f06a72494433cbfea8cb9a8c7ffed6126e4 100644 (file)
@@ -96,35 +96,3 @@ function theme_path(string $path = ''): ?string
 
     return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
 }
-
-/**
- * Generate a URL with multiple parameters for sorting purposes.
- * Works out the logic to set the correct sorting direction
- * Discards empty parameters and allows overriding.
- */
-function sortUrl(string $path, array $data, array $overrideData = []): string
-{
-    $queryStringSections = [];
-    $queryData = array_merge($data, $overrideData);
-
-    // Change sorting direction is already sorted on current attribute
-    if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
-        $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
-    } elseif (isset($overrideData['sort'])) {
-        $queryData['order'] = 'asc';
-    }
-
-    foreach ($queryData as $name => $value) {
-        $trimmedVal = trim($value);
-        if ($trimmedVal === '') {
-            continue;
-        }
-        $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
-    }
-
-    if (count($queryStringSections) === 0) {
-        return url($path);
-    }
-
-    return url($path . '?' . implode('&', $queryStringSections));
-}
diff --git a/app/Sorting/SortUrl.php b/app/Sorting/SortUrl.php
new file mode 100644 (file)
index 0000000..f01df2c
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+namespace BookStack\Sorting;
+
+/**
+ * Generate a URL with multiple parameters for sorting purposes.
+ * Works out the logic to set the correct sorting direction
+ * Discards empty parameters and allows overriding.
+ */
+class SortUrl
+{
+    public function __construct(
+        protected string $path,
+        protected array $data,
+        protected array $overrideData = []
+    ) {
+    }
+
+    public function withOverrideData(array $overrideData = []): self
+    {
+        return new self($this->path, $this->data, $overrideData);
+    }
+
+    public function build(): string
+    {
+        $queryStringSections = [];
+        $queryData = array_merge($this->data, $this->overrideData);
+
+        // Change sorting direction if already sorted on current attribute
+        if (isset($this->overrideData['sort']) && $this->overrideData['sort'] === $this->data['sort']) {
+            $queryData['order'] = ($this->data['order'] === 'asc') ? 'desc' : 'asc';
+        } elseif (isset($this->overrideData['sort'])) {
+            $queryData['order'] = 'asc';
+        }
+
+        foreach ($queryData as $name => $value) {
+            $trimmedVal = trim($value);
+            if ($trimmedVal !== '') {
+                $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
+            }
+        }
+
+        if (count($queryStringSections) === 0) {
+            return url($this->path);
+        }
+
+        return url($this->path . '?' . implode('&', $queryStringSections));
+    }
+}
index 28cdeb8a5a7e5e8a5892b6fffc96bf9b4bb0888a..8e47766804094086d2dfb67a25747c3c86292aec 100644 (file)
                             class="input-base text-left">{{ $filters['event'] ?: trans('settings.audit_event_filter_no_filter') }}</button>
                     <ul refs="dropdown@menu" class="dropdown-menu">
                         <li @if($filters['event'] === '') class="active" @endif><a
-                                    href="{{ sortUrl('/settings/audit', array_filter(request()->except('page')), ['event' => '']) }}"
+                                    href="{{ $filterSortUrl->withOverrideData(['event' => ''])->build() }}"
                                     class="text-item">{{ trans('settings.audit_event_filter_no_filter') }}</a></li>
                         @foreach($activityTypes as $type)
                             <li @if($type === $filters['event']) class="active" @endif><a
-                                        href="{{ sortUrl('/settings/audit', array_filter(request()->except('page')), ['event' => $type]) }}"
+                                        href="{{ $filterSortUrl->withOverrideData(['event' => $type])->build() }}"
                                         class="text-item">{{ $type }}</a></li>
                         @endforeach
                     </ul>