225 lines
6.9 KiB
PHP
225 lines
6.9 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Http\Request;
|
||
use App\Models\Device;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Validator;
|
||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||
use App\Models\Park;
|
||
|
||
class DeviceController extends Controller
|
||
{
|
||
/**
|
||
* 一覧: /device
|
||
*/
|
||
|
||
public function list(Request $request)
|
||
{
|
||
$perPage = \App\Utils::item_per_page ?? 20;
|
||
|
||
// リクエストからソート対象と方向を取得(デフォルト: device_id asc)
|
||
$sort = $request->input('sort', 'device_id');
|
||
$sort_type = $request->input('sort_type', 'asc');
|
||
|
||
// 許可カラム(SQLインジェクション対策)
|
||
$sortable = [
|
||
'device_id',
|
||
'park_id',
|
||
'device_type',
|
||
'device_subject',
|
||
'device_identifier',
|
||
'device_work',
|
||
'device_workstart',
|
||
'device_replace',
|
||
'device_remarks',
|
||
'operator_id',
|
||
'ope_auth1',
|
||
];
|
||
|
||
if (!in_array($sort, $sortable)) {
|
||
$sort = 'device_id';
|
||
}
|
||
if (!in_array(strtolower($sort_type), ['asc','desc'])) {
|
||
$sort_type = 'desc';
|
||
}
|
||
|
||
$list = Device::with('park')
|
||
->orderBy($sort, $sort_type)
|
||
->paginate($perPage)
|
||
->appends([
|
||
'sort' => $sort,
|
||
'sort_type' => $sort_type,
|
||
]);
|
||
|
||
return view('admin.devices.list', [
|
||
'list' => $list,
|
||
'sort' => $sort,
|
||
'sort_type' => $sort_type,
|
||
]);
|
||
}
|
||
|
||
|
||
/**
|
||
* 新規登録(GET 画面 / POST 保存)
|
||
*/
|
||
public function add(Request $request)
|
||
{
|
||
if ($request->isMethod('get')) {
|
||
return view('admin.devices.add', [
|
||
'isEdit' => false,
|
||
'device' => new Device(),
|
||
'parks' => Park::all(),
|
||
|
||
// 初期値(Bladeで old() 使うなら省略可)
|
||
'device_id' => null,
|
||
'park_id' => '',
|
||
'device_type' => '',
|
||
'device_subject' => '',
|
||
'device_identifier'=> '',
|
||
'device_work' => '',
|
||
'device_workstart' => '',
|
||
'device_replace' => '',
|
||
'device_remarks' => '',
|
||
'operator_id' => '',
|
||
]);
|
||
}
|
||
|
||
// 入力値を一旦取得
|
||
$data = $request->all();
|
||
|
||
// --- バリデーション ---
|
||
$rules = [
|
||
'park_id' => ['required','integer'],
|
||
'device_type' => ['required','in:1,2,3'], // 1=サーバー, 2=プリンタ, 3=その他
|
||
'device_subject' => ['required','string','max:255'],
|
||
'device_identifier' => ['required','string','max:255'],
|
||
'device_work' => ['required','in:0,1'], // 1=稼働, 0=停止
|
||
'device_workstart' => ['required','date'],
|
||
'device_replace' => ['nullable','date'],
|
||
'device_remarks' => ['nullable','string','max:255'],
|
||
'operator_id' => ['nullable','integer'],
|
||
];
|
||
|
||
|
||
$request->validate($rules);
|
||
|
||
// 保存処理
|
||
$device = new Device();
|
||
$device->fill($data);
|
||
$device->save();
|
||
|
||
return redirect()->route('devices')->with('success', '登録しました。');
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 編集(GET 画面 / POST 更新)
|
||
*/
|
||
public function edit($id, Request $request)
|
||
{
|
||
$device = Device::find($id);
|
||
if (!$device) abort(404);
|
||
|
||
if ($request->isMethod('get')) {
|
||
return view('admin.devices.edit', [
|
||
'isEdit' => true,
|
||
'device' => $device,
|
||
'parks' => Park::all(),
|
||
]);
|
||
}
|
||
|
||
// 入力値を一旦取得
|
||
$data = $request->all();
|
||
|
||
// --- バリデーション ---
|
||
$rules = [
|
||
'park_id' => ['required','integer'],
|
||
'device_type' => ['required','in:1,2,3'], // 1=サーバー, 2=プリンタ, 3=その他
|
||
'device_subject' => ['required','string','max:255'],
|
||
'device_identifier' => ['required','string','max:255'],
|
||
'device_work' => ['required','in:0,1'], // 1=稼働, 0=停止
|
||
'device_workstart' => ['required','date'],
|
||
'device_replace' => ['nullable','date'],
|
||
'device_remarks' => ['nullable','string','max:255'],
|
||
'operator_id' => ['nullable','integer'],
|
||
];
|
||
|
||
|
||
$request->validate($rules);
|
||
|
||
// 保存処理
|
||
$device->fill($data);
|
||
$device->save();
|
||
|
||
return redirect()->route('devices')->with('success', '更新しました。');
|
||
}
|
||
|
||
/**
|
||
* 詳細: /device/info/{id}
|
||
*/
|
||
// public function info(int $id)
|
||
// {
|
||
// $device = Device::with('park')->findOrFail($id);
|
||
|
||
// return view('admin.devices.info', [
|
||
// 'device' => $device,
|
||
// 'isInfo' => true,
|
||
// 'isEdit' => false,
|
||
// ]);
|
||
// }
|
||
|
||
/**
|
||
* 削除(単体 or 複数)
|
||
*/
|
||
public function delete(Request $request)
|
||
{
|
||
$ids = [];
|
||
|
||
// 単体削除
|
||
if ($request->filled('id')) {
|
||
$ids[] = (int) $request->input('id');
|
||
}
|
||
|
||
// 複数削除
|
||
if ($request->filled('ids')) {
|
||
$ids = array_merge($ids, array_map('intval', (array)$request->input('ids')));
|
||
}
|
||
|
||
$ids = array_unique($ids);
|
||
|
||
if (!$ids) {
|
||
return back()->with('error', '削除対象が選択されていません。');
|
||
}
|
||
|
||
Device::deleteByPk($ids);
|
||
|
||
return redirect()->route('devices')->with('success', '削除しました。');
|
||
}
|
||
|
||
|
||
|
||
|
||
/** バリデーションルール */
|
||
private function rules(?int $id = null): array
|
||
{
|
||
return [
|
||
'park_id' => ['required','integer'], // 駐輪場ID 必須
|
||
'device_type' => ['required','in:1,2,3'], // 1=サーバー, 2=プリンタ, 3=その他
|
||
'device_subject' => ['required','string','max:255'], // デバイス名 必須
|
||
'device_identifier' => ['required','string','max:255'], // 識別子 必須
|
||
'device_work' => ['required','in:0,1'], // 1=稼働, 0=停止
|
||
'device_workstart' => ['required','date'], // 稼働開始日 必須
|
||
'device_replace' => ['nullable','date'], // リプレース予約日 任意
|
||
'device_remarks' => ['nullable','string','max:255'], // 備考 任意
|
||
'operator_id' => ['nullable','integer'], // 任意
|
||
];
|
||
}
|
||
|
||
|
||
|
||
}
|