211 lines
6.8 KiB
PHP
211 lines
6.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Http\Request;
|
||
use App\Models\ContractAllowableCity;
|
||
use App\Models\City;
|
||
use App\Models\Park;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||
|
||
class ContractAllowableCityController extends Controller
|
||
{
|
||
/**
|
||
* 一覧表示
|
||
*/
|
||
public function list(Request $request)
|
||
{
|
||
$inputs = $request->all();
|
||
$inputs['isMethodPost'] = $request->isMethod('post');
|
||
|
||
// 解除処理
|
||
if ($request->isMethod('post') && $request->input('action') === 'unlink') {
|
||
// バリデーション:解除条件が1つも入力されていない場合はエラー
|
||
if (
|
||
!$request->filled('contract_allowable_city_id')
|
||
&& !$request->filled('city_id')
|
||
&& !$request->filled('contract_allowable_city_name')
|
||
&& !$request->filled('park_id')
|
||
) {
|
||
return back()->withErrors(['解除条件を1つ以上入力してください。']);
|
||
}
|
||
|
||
$query = ContractAllowableCity::query();
|
||
|
||
if ($request->filled('contract_allowable_city_id')) {
|
||
$query->where('contract_allowable_city_id', $request->contract_allowable_city_id);
|
||
}
|
||
if ($request->filled('city_id')) {
|
||
$query->where('city_id', $request->city_id);
|
||
}
|
||
if ($request->filled('contract_allowable_city_name')) {
|
||
$query->where('contract_allowable_city_name', 'like', '%' . $request->contract_allowable_city_name . '%');
|
||
}
|
||
if ($request->filled('park_id')) {
|
||
$query->where('park_id', $request->park_id);
|
||
}
|
||
|
||
// 推荐:直接批量删除
|
||
$count = $query->delete();
|
||
return redirect()->route('contract_allowable_cities')->with('success', '解除しました');
|
||
}
|
||
|
||
// 通常の絞り込み処理
|
||
$list = ContractAllowableCity::search($inputs);
|
||
|
||
return view('admin.contract_allowable_cities.list', [
|
||
'list' => $list,
|
||
'inputs' => $inputs,
|
||
'sort' => $inputs['sort'] ?? '',
|
||
'sort_type' => $inputs['sort_type'] ?? '',
|
||
'cityList' => City::getList(),
|
||
'parkList' => Park::getList(),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 新規登録
|
||
*/
|
||
public function add(Request $request)
|
||
{
|
||
if ($request->isMethod('post')) {
|
||
$validated = $request->validate([
|
||
'city_id' => 'required|integer',
|
||
'contract_allowable_city_name' => 'required|string|max:20',
|
||
'park_id' => 'required|integer',
|
||
'same_district_flag' => 'required|integer',
|
||
]);
|
||
|
||
$validated['operator_id'] = Auth::user()->ope_id;
|
||
|
||
ContractAllowableCity::create($validated);
|
||
|
||
return redirect()->route('contract_allowable_cities')
|
||
->with('success', '登録しました。');
|
||
}
|
||
|
||
return view('admin.contract_allowable_cities.add', [
|
||
'record' => null,
|
||
'cityList' => City::getList(),
|
||
'parkList' => Park::getList(),
|
||
]);
|
||
}
|
||
|
||
|
||
/**
|
||
* 編集
|
||
*/
|
||
public function edit(Request $request, $id)
|
||
{
|
||
$record = ContractAllowableCity::getByPk($id);
|
||
if (!$record) {
|
||
return redirect()->route('contract_allowable_cities')
|
||
->with('error', 'データが存在しません');
|
||
}
|
||
|
||
if ($request->isMethod('post')) {
|
||
$validated = $request->validate([
|
||
'city_id' => 'required|integer',
|
||
'contract_allowable_city_name' => 'required|string|max:20',
|
||
'park_id' => 'required|integer',
|
||
'same_district_flag' => 'required|integer',
|
||
]);
|
||
|
||
$record->fill($validated);
|
||
$record->operator_id = Auth::user()->ope_id;
|
||
$record->save();
|
||
|
||
return redirect()->route('contract_allowable_cities')
|
||
->with('success', '更新しました。');
|
||
}
|
||
|
||
return view('admin.contract_allowable_cities.edit', [
|
||
'record' => $record,
|
||
'cityList' => City::getList(),
|
||
'parkList' => Park::getList(),
|
||
]);
|
||
}
|
||
|
||
|
||
/**
|
||
* 詳細参照(表示のみ)
|
||
*/
|
||
public function info($id)
|
||
{
|
||
$record = ContractAllowableCity::getByPk($id);
|
||
if (!$record) {
|
||
return redirect()->route('contract_allowable_cities')->with('error', 'データが存在しません');
|
||
}
|
||
|
||
return view('admin.contract_allowable_cities.edit', [
|
||
'record' => $record,
|
||
'cityList' => City::getList(),
|
||
'parkList' => Park::getList(),
|
||
'mode' => 'info'
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 一括削除(単一・複数対応)
|
||
*/
|
||
public function delete(Request $request)
|
||
{
|
||
// バリデーション:'id'は必須、配列の場合は各要素が整数
|
||
$request->validate([
|
||
'id' => 'required',
|
||
'id.*' => 'integer',
|
||
]);
|
||
|
||
// idを配列化(単一でも複数でも対応)
|
||
$ids = (array)$request->input('id');
|
||
|
||
// 削除処理
|
||
// ContractAllowableCity::destroy($ids) が使える場合
|
||
$deleted = ContractAllowableCity::destroy($ids);
|
||
|
||
// 削除件数でメッセージ分岐
|
||
if ($deleted > 0) {
|
||
return redirect()->route('contract_allowable_cities')->with('success', '削除しました。');
|
||
} else {
|
||
return redirect()->route('contract_allowable_cities')->with('error', '削除に失敗しました。');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* CSVエクスポート
|
||
*/
|
||
public function export(Request $request)
|
||
{
|
||
$filename = 'contract_allowable_cities_' . now()->format('Ymd_His') . '.csv';
|
||
|
||
$list = ContractAllowableCity::search($request->all());
|
||
|
||
$headers = [
|
||
'Content-Type' => 'text/csv',
|
||
'Content-Disposition' => "attachment; filename=\"$filename\"",
|
||
];
|
||
|
||
return new StreamedResponse(function () use ($list) {
|
||
$handle = fopen('php://output', 'w');
|
||
|
||
// ヘッダー
|
||
fputcsv($handle, ['契約許容市区ID', '市区ID', '許容市区名', '駐輪場ID', '隣接区フラグ']);
|
||
|
||
foreach ($list as $item) {
|
||
fputcsv($handle, [
|
||
$item->contract_allowable_city_id,
|
||
$item->city_id,
|
||
$item->contract_allowable_city_name,
|
||
$item->park_id,
|
||
$item->same_district_flag == 0 ? '隣接市' : 'その他'
|
||
]);
|
||
}
|
||
|
||
fclose($handle);
|
||
}, 200, $headers);
|
||
}
|
||
|
||
}
|