228 lines
6.6 KiB
PHP
228 lines
6.6 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Http\Request;
|
||
use App\Models\Pplace;
|
||
use App\Models\Ope;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Validator;
|
||
use Response;
|
||
|
||
class PplaceController extends Controller
|
||
{
|
||
public function list(Request $request)
|
||
{
|
||
$inputs = [
|
||
'isExport' => 0,
|
||
'sort' => $request->input('sort', ''),
|
||
'sort_type' => $request->input('sort_type', ''),
|
||
'page' => $request->get('page', 1),
|
||
];
|
||
|
||
$inputs['list'] = Pplace::search($inputs);
|
||
|
||
if ($inputs['list']->total() > 0 && $inputs['page'] > $inputs['list']->lastPage()) {
|
||
return redirect()->route('pplaces');
|
||
}
|
||
|
||
return view('admin.pplace.list', $inputs);
|
||
}
|
||
|
||
/**
|
||
* 新規登録(画面/処理)
|
||
*/
|
||
public function add(Request $request)
|
||
{
|
||
if ($request->isMethod('get')) {
|
||
// 新規時:空のレコードとオペレーターリストを渡す
|
||
return view('admin.pplace.add', [
|
||
'isEdit' => false,
|
||
'record' => new Pplace(),
|
||
'operators' => Ope::getList(),
|
||
]);
|
||
}
|
||
|
||
// POST時:バリデーション
|
||
$rules = [
|
||
'pplace_number' => 'required|string|max:255',
|
||
'pplace_remarks' => 'nullable|string|max:255',
|
||
'operator_id' => 'nullable|integer',
|
||
];
|
||
$messages = [
|
||
'pplace_number.required' => '駐輪場所番号は必須です。',
|
||
];
|
||
|
||
$validator = Validator::make($request->all(), $rules, $messages);
|
||
|
||
if ($validator->fails()) {
|
||
return redirect()->back()
|
||
->withErrors($validator)
|
||
->withInput()
|
||
->with(['operators' => Ope::getList()]);
|
||
}
|
||
|
||
// トランザクションで登録処理
|
||
DB::transaction(function () use ($request) {
|
||
$new = new Pplace();
|
||
$new->fill($request->only(['pplace_number', 'pplace_remarks', 'operator_id']));
|
||
$new->save();
|
||
});
|
||
|
||
return redirect()->route('pplaces')->with('success', '登録しました。');
|
||
}
|
||
|
||
|
||
/**
|
||
* 編集(画面/処理)
|
||
*/
|
||
public function edit(Request $request, $id)
|
||
{
|
||
// 該当データ取得
|
||
$record = Pplace::find($id);
|
||
if (!$record) {
|
||
abort(404);
|
||
}
|
||
|
||
// オペレーターリスト取得(常に渡す)
|
||
$operators = Ope::getList();
|
||
|
||
if ($request->isMethod('get')) {
|
||
// 編集画面表示
|
||
return view('admin.pplace.edit', [
|
||
'isEdit' => true,
|
||
'record' => $record,
|
||
'operators' => $operators,
|
||
]);
|
||
}
|
||
|
||
// POST時:バリデーション
|
||
$rules = [
|
||
'pplace_number' => 'required|string|max:255',
|
||
'pplace_remarks' => 'nullable|string|max:255',
|
||
'operator_id' => 'nullable|integer',
|
||
];
|
||
$messages = [
|
||
'pplace_number.required' => '駐輪場所番号は必須です。',
|
||
];
|
||
|
||
$validator = Validator::make($request->all(), $rules, $messages);
|
||
|
||
if ($validator->fails()) {
|
||
return redirect()->back()
|
||
->withErrors($validator)
|
||
->withInput()
|
||
->with(['operators' => $operators]);
|
||
}
|
||
|
||
// 更新処理
|
||
DB::transaction(function () use ($request, $record) {
|
||
$record->fill($request->only(['pplace_number', 'pplace_remarks', 'operator_id']));
|
||
$record->save();
|
||
});
|
||
|
||
return redirect()->route('pplaces')->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', '削除対象が選択されていません。');
|
||
}
|
||
|
||
// 削除実行
|
||
Pplace::whereIn('pplace_id', $ids)->delete();
|
||
|
||
return redirect()->route('pplaces')->with('success', '削除しました。');
|
||
}
|
||
|
||
|
||
|
||
public function export()
|
||
{
|
||
$filename = '駐輪車室マスタ' . now()->format('YmdHis') . '.csv';
|
||
|
||
$file = fopen($filename, 'w+');
|
||
fwrite($file, "\xEF\xBB\xBF"); // BOM追加(UTF-8)
|
||
|
||
$columns = ['駐輪車室ID', '番号', '備考', 'オペレータID'];
|
||
fputcsv($file, $columns);
|
||
|
||
$data = Pplace::all();
|
||
foreach ($data as $item) {
|
||
fputcsv($file, [
|
||
$item->pplace_id,
|
||
$item->pplace_number,
|
||
$item->pplace_remarks,
|
||
$item->operator_id,
|
||
]);
|
||
}
|
||
|
||
fclose($file);
|
||
|
||
$headers = [
|
||
"Content-Type" => "text/csv; charset=UTF-8",
|
||
"Content-Disposition" => "attachment; filename={$filename}",
|
||
];
|
||
|
||
return response()->download($filename, $filename, $headers)->deleteFileAfterSend(true);
|
||
}
|
||
|
||
|
||
public function import(Request $request)
|
||
{
|
||
$file = $request->file('file');
|
||
if (!$file) {
|
||
return redirect()->route('pplaces')->with('error', 'CSVファイルを選択してください');
|
||
}
|
||
|
||
$data = \App\Utils::csvToArray($file);
|
||
$record = 0;
|
||
|
||
DB::beginTransaction();
|
||
try {
|
||
foreach ($data as $key => $row) {
|
||
$record = $key + 2;
|
||
if (count($row) < 3) throw new \Exception('列数が不正です');
|
||
|
||
Pplace::create([
|
||
'pplace_number' => $row[0],
|
||
'pplace_remarks' => $row[1],
|
||
'operator_id' => $row[2],
|
||
]);
|
||
}
|
||
DB::commit();
|
||
return redirect()->route('pplaces')->with('success', 'インポート成功');
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
return redirect()->route('pplaces')->with('error', "行 {$record} : " . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
private function __buildErrorMessasges($validator)
|
||
{
|
||
return implode("\n", $validator->errors()->all());
|
||
}
|
||
}
|