'ID', 'shortname' => 'Сокращение', 'name' => 'Название', 'areas_count' => 'Районов', 'actions' => '', ]; protected array $searchFields = ['shortname', 'name']; public function index(Request $request): View { $allowedSortFields = ['id', 'shortname', 'name', 'areas_count']; $sortBy = in_array($request->get('sortBy'), $allowedSortFields, true) ? $request->get('sortBy') : 'id'; $orderBy = $request->get('order') === 'desc' ? 'desc' : 'asc'; $districts = District::query()->withCount('areas'); if ($request->filled('s')) { $search = $request->get('s'); $districts->where(function ($query) use ($search) { $query->where('shortname', 'like', '%' . $search . '%') ->orWhere('name', 'like', '%' . $search . '%'); }); } $districts = $districts ->orderBy($sortBy, $orderBy) ->get(); return view('admin.districts.index', [ 'active' => 'admin_districts', 'id' => $this->id, 'header' => $this->header, 'sortBy' => $sortBy, 'orderBy' => $orderBy, 'searchFields' => $this->searchFields, 'districts' => $districts, ]); } public function show(int $districtId): View { $district = District::withTrashed()->findOrFail($districtId); return view('admin.districts.edit', [ 'active' => 'admin_districts', 'district' => $district, ]); } public function store(Request $request): RedirectResponse { $validated = $request->validate([ 'id' => ['nullable', 'integer'], 'shortname' => ['required', 'string', 'max:50'], 'name' => ['required', 'string', 'max:255'], ]); if (!empty($validated['id'])) { $district = District::withTrashed()->findOrFail($validated['id']); $district->update([ 'shortname' => $validated['shortname'], 'name' => $validated['name'], ]); } else { District::create([ 'shortname' => $validated['shortname'], 'name' => $validated['name'], ]); } return redirect()->route('admin.district.index')->with('success', 'Округ сохранён'); } public function destroy(District $district): RedirectResponse { if ($district->areas()->count() > 0) { return redirect()->route('admin.district.index') ->with('error', 'Невозможно удалить округ с привязанными районами'); } $district->delete(); return redirect()->route('admin.district.index')->with('success', 'Округ удалён'); } public function undelete(int $districtId): RedirectResponse { $district = District::withTrashed()->findOrFail($districtId); $district->restore(); return redirect()->route('admin.district.index')->with('success', 'Округ восстановлен'); } public function export(Request $request): RedirectResponse { $service = new ExportDistrictsService(); $link = $service->handle($request->user()->id); return redirect()->away($link); } public function import(Request $request): RedirectResponse { $request->validate([ 'import_file' => ['required', 'file', 'mimes:xlsx,xls'], ]); $file = $request->file('import_file'); $path = $file->store('import/districts', 'upload'); $fullPath = Storage::disk('upload')->path($path); $service = new ImportDistrictsService($fullPath, $request->user()->id); $result = $service->handle(); if ($result['success']) { return redirect()->route('admin.district.index') ->with('success', 'Импорт завершён успешно'); } return redirect()->route('admin.district.index') ->with('error', 'Ошибка импорта: ' . ($result['error'] ?? 'неизвестная ошибка')); } }