krgm.so-manager-dev.com/app/Http/Controllers/Admin/ZoneController.php
kin.rinzen 0498760d46
All checks were successful
Deploy main / deploy (push) Successful in 22s
画面修正
2025-10-28 13:58:45 +09:00

251 lines
8.1 KiB
PHP
Raw 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\Zone;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Models\Park;
use App\Models\Ptype;
use App\Models\Psection;
class ZoneController extends Controller
{
/**
* 一覧表示(絞り込み対応)
*/
public function list(Request $request)
{
if ($request->input('action') === 'reset') {
return redirect()->route('zones');
}
// ソート設定
$sort = $request->input('sort', 'zone_id');
$sort_type = $request->input('sort_type', 'asc');
// ベースクエリ
$query = Zone::query();
// === 絞り込み条件 ===
if ($request->filled('zone_id')) {
$query->where('zone_id', $request->zone_id);
}
if ($request->filled('zone_name')) {
$query->where('zone_name', 'LIKE', "%{$request->zone_name}%");
}
if ($request->filled('park_id')) {
$query->where('park_id', $request->park_id);
}
if ($request->filled('ptype_id')) {
$query->where('ptype_id', $request->ptype_id);
}
if ($request->filled('psection_id')) {
$query->where('psection_id', $request->psection_id);
}
if ($request->has('use_flag') && $request->use_flag !== '') {
$query->where('use_flag', $request->use_flag);
}
// ページネーション
$zones = $query->orderBy($sort, $sort_type)->paginate(20);
// === 下拉选单用の一覧データ ===
$parkList = DB::table('park')->pluck('park_name', 'park_id');
$ptypeList = DB::table('ptype')->pluck('ptype_subject', 'ptype_id');
$psectionList = DB::table('psection')->pluck('psection_subject', 'psection_id');
return view('admin.zones.list', compact(
'zones', 'sort', 'sort_type',
'parkList', 'ptypeList', 'psectionList'
));
}
/**
* 新規登録(画面/処理)
*/
public function add(Request $request)
{
if ($request->isMethod('get')) {
$parkList = DB::table('park')->pluck('park_name', 'park_id');
$ptypeList = DB::table('ptype')->pluck('ptype_subject', 'ptype_id');
$psectionList = DB::table('psection')->pluck('psection_subject', 'psection_id');
return view('admin.zones.add', [
'isEdit' => false,
'record' => new Zone(),
'parkList' => $parkList,
'ptypeList' => $ptypeList,
'psectionList' => $psectionList,
]);
}
// ▼ POST時バリデーション
$rules = [
'park_id' => 'required|integer',
'ptype_id' => 'required|integer',
'psection_id' => 'required|integer',
'zone_name' => 'required|string|max:255',
'zone_number' => 'nullable|integer|min:0',
'zone_standard' => 'nullable|integer|min:0',
'zone_tolerance' => 'nullable|integer|min:0',
'zone_sort' => 'nullable|integer|min:0',
];
$messages = [
'park_id.required' => '駐輪場は必須です。',
'ptype_id.required' => '駐輪分類は必須です。',
'psection_id.required' => '車種区分は必須です。',
'zone_name.required' => 'ゾーン名は必須です。',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
// ▼ 登録処理
DB::transaction(function () use ($request) {
$new = new Zone();
$new->fill($request->only([
'park_id',
'ptype_id',
'psection_id',
'zone_name',
'zone_number',
'zone_standard',
'zone_tolerance',
'zone_sort',
]));
$new->save();
});
return redirect()->route('zones')->with('success', '登録しました。');
}
/**
* 編集(画面/処理)
*/
public function edit(Request $request, $id)
{
// 該当データ取得
$record = Zone::find($id);
if (!$record) {
abort(404);
}
$parkList = DB::table('park')->pluck('park_name', 'park_id');
$ptypeList = DB::table('ptype')->pluck('ptype_subject', 'ptype_id');
$psectionList = DB::table('psection')->pluck('psection_subject', 'psection_id');
if ($request->isMethod('get')) {
// 編集画面表示
return view('admin.zones.edit', [
'isEdit' => true,
'record' => $record,
'parkList' => $parkList,
'ptypeList' => $ptypeList,
'psectionList' => $psectionList,
]);
}
// ▼ POST時バリデーション
$rules = [
'park_id' => 'required|integer',
'ptype_id' => 'required|integer',
'psection_id' => 'required|integer',
'zone_name' => 'required|string|max:255',
'zone_number' => 'nullable|integer|min:0',
'zone_standard' => 'nullable|integer|min:0',
'zone_tolerance' => 'nullable|integer|min:0',
'zone_sort' => 'nullable|integer|min:0',
];
$messages = [
'park_id.required' => '駐輪場は必須です。',
'ptype_id.required' => '駐輪分類は必須です。',
'psection_id.required' => '車種区分は必須です。',
'zone_name.required' => 'ゾーン名は必須です。',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
// ▼ 更新処理
DB::transaction(function () use ($request, $record) {
$record->fill($request->only([
'park_id',
'ptype_id',
'psection_id',
'zone_name',
'zone_number',
'zone_standard',
'zone_tolerance',
'zone_sort',
]));
$record->save();
});
return redirect()->route('zones')->with('success', '更新しました。');
}
/**
* 削除(単一/複数対応)
*/
public function delete(Request $request)
{
$ids = [];
// 単一削除id 指定)
if ($request->filled('id')) {
$ids[] = (int) $request->input('id');
}
// 複数削除(チェックボックス pk[]
if (is_array($request->input('pk'))) {
$ids = array_merge($ids, $request->input('pk'));
}
// 重複除去 & 数値変換
$ids = array_values(array_unique(array_map('intval', $ids)));
// 削除対象がない場合
if (empty($ids)) {
return back()->with('error', '削除対象が選択されていません。');
}
// 削除実行
Zone::whereIn('zone_id', $ids)->delete();
return redirect()->route('zones')->with('success', '削除しました。');
}
/**
* バリデーション共通化
*/
private function validateZone(Request $request)
{
return $request->validate([
'zone_name' => 'required|string|max:50',
'park_id' => 'required|integer',
'ptype_id' => 'nullable|integer',
'psection_id' => 'nullable|integer',
'zone_number' => 'nullable|integer|min:0',
'zone_standard' => 'nullable|integer|min:0',
'zone_tolerance' => 'nullable|integer|min:0',
'use_flag' => 'nullable|boolean',
'memo' => 'nullable|string|max:255',
]);
}
}