diff --git a/src/app/Exports/StationExport.php b/src/app/Exports/StationExport.php new file mode 100644 index 00000000..caaccb60 --- /dev/null +++ b/src/app/Exports/StationExport.php @@ -0,0 +1,35 @@ +select('stationid', 'name', 'district', 'lng', 'lat', 'mainriverbasin', 'subriverbasin', 'rainfall', 'waterlevel', 'siren', 'cctv_link') + ->orderBy('stationid') + ->get(); + } + + public function headings(): array + { + return [ + 'Station ID', + 'Name', + 'District', + 'Longitude', + 'Latitude', + 'Main River Basin', + 'Sub River Basin', + 'Rainfall', + 'Water Level', + 'Siren', + 'CCTV Link', + ]; + } +} \ No newline at end of file diff --git a/src/app/Http/Controllers/AdminController.php b/src/app/Http/Controllers/AdminController.php index 7d4273f0..e24d5c8c 100644 --- a/src/app/Http/Controllers/AdminController.php +++ b/src/app/Http/Controllers/AdminController.php @@ -6,6 +6,9 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Validation\Rules\Password; +use Maatwebsite\Excel\Facades\Excel; +use App\Exports\StationExport; +use App\Imports\StationImport; class AdminController extends Controller { @@ -271,4 +274,33 @@ class AdminController extends Controller return redirect()->back()->with('success',__('toast.userdeleted')); } + // Function Export Stations to CSV + public function exportStations() + { + return Excel::download(new StationExport, 'stations.csv', \Maatwebsite\Excel\Excel::CSV); + } + + // Function Import Stations from CSV + public function importStations(Request $request) + { + $request->validate([ + 'csv_file' => 'required|file|mimes:csv,txt|max:10240', + ]); + + try { + Excel::import(new StationImport, $request->file('csv_file')); + return redirect()->route('stationmanagement')->with('success', __('toast.stationsimported')); + } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { + $failures = $e->failures(); + $firstError = collect($failures)->first(); + $msg = $firstError + ? "Row {$firstError->row()}: {$firstError->errors()[0]}" + : __('toast.error'); + return redirect()->route('stationmanagement')->with('error', $msg); + } catch (\Exception $e) { + Log::error('Station CSV import failed', ['error' => $e->getMessage()]); + return redirect()->route('stationmanagement')->with('error', __('toast.error')); + } + } + } diff --git a/src/app/Imports/StationImport.php b/src/app/Imports/StationImport.php new file mode 100644 index 00000000..3ac81426 --- /dev/null +++ b/src/app/Imports/StationImport.php @@ -0,0 +1,37 @@ +updateOrInsert( + ['stationid' => $stationid], + [ + 'name' => $row['name'] ?? '', + 'district' => $row['district'] ?? '', + 'lng' => is_numeric($row['longitude'] ?? null) ? $row['longitude'] : 0, + 'lat' => is_numeric($row['latitude'] ?? null) ? $row['latitude'] : 0, + 'mainriverbasin' => $row['main_river_basin'] ?? $row['mainriverbasin'] ?? '', + 'subriverbasin' => $row['sub_river_basin'] ?? $row['subriverbasin'] ?? '', + 'rainfall' => intval($row['rainfall'] ?? 0), + 'waterlevel' => intval($row['water_level'] ?? $row['waterlevel'] ?? 0), + 'siren' => intval($row['siren'] ?? 0), + 'cctv_link' => $row['cctv_link'] ?? null, + ] + ); + } + } +} \ No newline at end of file diff --git a/src/lang/bm/messages.php b/src/lang/bm/messages.php index 1bfbf61f..25356ba3 100644 --- a/src/lang/bm/messages.php +++ b/src/lang/bm/messages.php @@ -94,6 +94,9 @@ 'nohistorysiren' => 'Tiada Data Sejarah Siren', 'nocurrentsiren' => 'Tiada siren aktif sejak 7 hari lalu', 'nodataavailable' => 'Tiada Data Tersedia', + 'dailyrainfall' => 'Hujan Harian', + 'importcsv' => 'Import CSV', + 'exportcsv' => 'Eksport CSV', //Form 'selectstation' => 'Pilih Stesen', diff --git a/src/lang/bm/toast.php b/src/lang/bm/toast.php index 2dbe9ee9..683a8a30 100644 --- a/src/lang/bm/toast.php +++ b/src/lang/bm/toast.php @@ -8,6 +8,8 @@ 'passwordupdated' => 'Kata laluan berjaya dikemaskini', 'stationdeleted' => 'Stesen berjaya dipadam', 'userdeleted' => 'Pengguna berjaya dipadam', + 'linksupdated' => 'Pautan CCTV berjaya dikemaskini', + 'stationsimported' => 'Stesen berjaya diimport', ]; diff --git a/src/lang/en/messages.php b/src/lang/en/messages.php index 74af8d7b..024206a8 100644 --- a/src/lang/en/messages.php +++ b/src/lang/en/messages.php @@ -90,7 +90,10 @@ 'nohistorywl' => 'No Water Level Notification History Data', 'nohistorysiren' => 'No Siren History Data', 'nocurrentsiren' => 'No siren triggered for the past 7 days', - 'nodataavailable' => 'No Data Avaiable', + 'nodataavailable' => 'No Data Avaiable', + 'dailyrainfall' => 'Daily Rainfall', + 'importcsv' => 'Import CSV', + 'exportcsv' => 'Export CSV', //Form 'selectstation' => 'Select Station', diff --git a/src/lang/en/toast.php b/src/lang/en/toast.php index d0476cbb..6baaaf29 100644 --- a/src/lang/en/toast.php +++ b/src/lang/en/toast.php @@ -8,6 +8,8 @@ 'passwordupdated' => 'Password updated succesfully', 'stationdeleted' => 'Station deleted succesfully', 'userdeleted' => 'User deleted successfully', + 'linksupdated' => 'CCTV link updated successfully', + 'stationsimported' => 'Stations imported successfully', ]; diff --git a/src/resources/views/layout/admin/stationmgmt.blade.php b/src/resources/views/layout/admin/stationmgmt.blade.php index 67628173..d702dfc6 100644 --- a/src/resources/views/layout/admin/stationmgmt.blade.php +++ b/src/resources/views/layout/admin/stationmgmt.blade.php @@ -60,8 +60,11 @@