Skip to content

Commit 60bcd5c

Browse files
authored
devices page handle legacy url filters (librenms#19757)
* devices handle legacy urls for now * style fixes * Only strings please * poller_group 0 is not none * Location could be a string
1 parent 2ebee3c commit 60bcd5c

1 file changed

Lines changed: 63 additions & 12 deletions

File tree

app/Http/Controllers/DevicesController.php

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\Device;
6+
use App\Models\Location;
67
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Http\Request;
89
use Illuminate\Pagination\LengthAwarePaginator;
@@ -41,18 +42,7 @@ public function index(Request $request, ?string $view = null, ?string $graph = n
4142
$hideFilter = $request->input('searchbar') === 'hide';
4243
$perPage = $request->integer('per_page', 50);
4344

44-
$legacyFormat = $request->string('format');
45-
if ($legacyFormat->startsWith('graph_')) {
46-
$view ??= 'graph';
47-
$graph ??= $legacyFormat->after('graph_')->toString();
48-
} else {
49-
$view ??= match ($legacyFormat->toString()) {
50-
'list_basic' => 'basic',
51-
default => 'detail',
52-
};
53-
$graph ??= '';
54-
}
55-
45+
[$view, $graph] = $this->parseLegacyUrls($view, $graph, $request);
5646
$view = in_array($view, ['basic', 'detail', 'graph']) ? $view : 'detail';
5747

5848
$graphTemplate = [
@@ -231,4 +221,65 @@ private function filterFields(): array
231221
],
232222
];
233223
}
224+
225+
/**
226+
* @param string|null $view
227+
* @param string|null $graph
228+
* @param Request $request
229+
* @return array{string, string}
230+
*/
231+
private function parseLegacyUrls(?string $view, ?string $graph, Request $request): array
232+
{
233+
$legacy = Url::parseLegacyPath($request->path());
234+
235+
// handle legacy format
236+
$legacyFormat = $legacy->getString('format');
237+
if (str_starts_with($legacyFormat, 'graph_')) {
238+
$view ??= 'graph';
239+
$graph ??= substr($legacyFormat, 6);
240+
} else {
241+
$view ??= match ($legacyFormat) {
242+
'list_basic' => 'basic',
243+
default => 'detail',
244+
};
245+
$graph ??= '';
246+
}
247+
248+
// handle legacy filters
249+
$filters = [];
250+
$fields = [
251+
'type',
252+
'state',
253+
'disable_notify',
254+
'disabled',
255+
'ignore',
256+
'poller_group',
257+
];
258+
259+
foreach ($fields as $field) {
260+
if ($legacy->has($field)) {
261+
$v = $legacy->get($field);
262+
$filters[$field] = ['eq' => is_numeric($v) ? (int) $v : $v];
263+
}
264+
}
265+
266+
if ($legacy->has('group')) {
267+
$v = $legacy->get('group');
268+
$filters['groups.id'] = $v === 'none' ? ['is_empty' => 1] : ['eq' => (int) $v];
269+
}
270+
271+
if ($legacy->has('location')) {
272+
$v = $legacy->get('location');
273+
$locationId = is_numeric($v) ? (int) $v : Location::where('location', $v)->value('id');
274+
if ($locationId) {
275+
$filters['location_id'] = ['eq' => $locationId];
276+
}
277+
}
278+
279+
if (! empty($filters)) {
280+
$request->merge(['filter' => array_merge($request->input('filter', []), $filters)]);
281+
}
282+
283+
return [$view, $graph];
284+
}
234285
}

0 commit comments

Comments
 (0)