|
3 | 3 | namespace App\Http\Controllers; |
4 | 4 |
|
5 | 5 | use App\Models\Device; |
| 6 | +use App\Models\Location; |
6 | 7 | use Illuminate\Database\Eloquent\Builder; |
7 | 8 | use Illuminate\Http\Request; |
8 | 9 | use Illuminate\Pagination\LengthAwarePaginator; |
@@ -41,18 +42,7 @@ public function index(Request $request, ?string $view = null, ?string $graph = n |
41 | 42 | $hideFilter = $request->input('searchbar') === 'hide'; |
42 | 43 | $perPage = $request->integer('per_page', 50); |
43 | 44 |
|
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); |
56 | 46 | $view = in_array($view, ['basic', 'detail', 'graph']) ? $view : 'detail'; |
57 | 47 |
|
58 | 48 | $graphTemplate = [ |
@@ -231,4 +221,65 @@ private function filterFields(): array |
231 | 221 | ], |
232 | 222 | ]; |
233 | 223 | } |
| 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 | + } |
234 | 285 | } |
0 commit comments