krgm.so-manager-dev.com/app/Http/Controllers/Admin/NeighborStationController.php

120 lines
3.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\NeighborStation;
class NeighborStationController extends Controller
{
// 一覧表示
public function list(Request $request)
{
$sort = $request->input('sort', 'station_id');
$sort_type = $request->input('sort_type', 'asc');
$allowedSorts = ['station_id', 'park_id', 'station_neighbor_station', 'station_name_ruby', 'station_route_name'];
if (!in_array($sort, $allowedSorts)) {
$sort = 'station_id';
}
if (!in_array($sort_type, ['asc', 'desc'])) {
$sort_type = 'asc';
}
$stations = NeighborStation::select([
'station_id',
'station_neighbor_station',
'station_name_ruby',
'station_route_name',
// 'station_latitude',
// 'station_longitude',
'park_id'
])->orderBy($sort, $sort_type)->paginate(20);
return view('admin.neighbor_stations.list', compact('stations', 'sort', 'sort_type'));
}
// 新規登録画面と登録処理
public function add(Request $request)
{
if ($request->isMethod('post')) {
$validated = $request->validate([
'station_neighbor_station' => 'required|string|max:255',
'station_name_ruby' => 'nullable|string|max:255',
'station_route_name' => 'nullable|string|max:255',
'park_id' => 'nullable|integer',
'operator_id' => 'nullable|integer',
]);
NeighborStation::create($validated);
return redirect()->route('neighbor_stations')->with('success', '近傍駅が登録されました');
}
return view('admin.neighbor_stations.add');
}
// 編集画面・更新処理
public function edit(Request $request, $id)
{
$station = NeighborStation::findOrFail($id);
if ($request->isMethod('post')) {
$validated = $request->validate([
'station_neighbor_station' => 'required|string|max:255',
'station_name_ruby' => 'nullable|string|max:255',
'station_route_name' => 'nullable|string|max:255',
'park_id' => 'nullable|integer',
'operator_id' => 'nullable|integer',
]);
$station->update($validated);
return redirect()->route('neighbor_stations')->with('success', '更新しました');
}
return view('admin.neighbor_stations.edit', compact('station'));
}
// 詳細表示
public function info($id)
{
$station = NeighborStation::findOrFail($id);
return view('admin.neighbor_stations.info', compact('station'));
}
// 削除処理
public function delete(Request $request)
{
$ids = $request->input('pk'); // ← 接收复数 checkbox 名称 pk[]
if (!empty($ids)) {
NeighborStation::destroy($ids); // 一次性删除多个
return redirect()->route('neighbor_stations')->with('success', '削除しました');
}
return redirect()->route('neighbor_stations')->with('error', '削除対象が見つかりません');
}
// CSVインポート
public function import(Request $request)
{
// TODO: 実装
return redirect()->route('neighbor_stations')->with('info', 'CSVインポートは未実装です');
}
// CSVエクスポート
public function export()
{
// TODO: 実装
return response()->streamDownload(function () {
echo "id,station_neighbor_station,station_name_ruby,station_route_name,park_id,operator_id\n";
foreach (NeighborStation::all() as $station) {
echo "{$station->id},{$station->station_neighbor_station},{$station->station_name_ruby},{$station->station_route_name},{$station->park_id},{$station->operator_id}\n";
}
}, 'neighbor_stations.csv');
}
}