diff --git a/app/Http/Controllers/Admin/DeviceController.php b/app/Http/Controllers/Admin/DeviceController.php index e6b9ed1..8e2b98a 100644 --- a/app/Http/Controllers/Admin/DeviceController.php +++ b/app/Http/Controllers/Admin/DeviceController.php @@ -8,6 +8,7 @@ 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 { @@ -17,126 +18,207 @@ class DeviceController extends Controller public function list(Request $request) { - $perPage = \App\Utils::item_per_page ?? 20; + $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('device_id', 'desc') - ->paginate($perPage); + ->orderBy($sort, $sort_type) + ->paginate($perPage) + ->appends([ + 'sort' => $sort, + 'sort_type' => $sort_type, + ]); return view('admin.devices.list', [ 'list' => $list, - 'sort' => 'device_id', - 'sort_type' => 'desc', + 'sort' => $sort, + 'sort_type' => $sort_type, ]); } /** - * 新規追加: /device/add + * 新規登録(GET 画面 / POST 保存) */ public function add(Request $request) { - if ($request->isMethod('post')) { - $v = Validator::make($request->all(), $this->rules()); - if ($v->fails()) return back()->withErrors($v)->withInput(); + if ($request->isMethod('get')) { + return view('admin.devices.add', [ + 'isEdit' => false, + 'device' => new Device(), + 'parks' => Park::all(), - DB::transaction(function () use ($request) { - Device::create($request->only([ - 'park_id','device_type','device_subject','device_identifier', - 'device_work','device_workstart','device_replace','device_remarks','operator_id', - ])); - }); - - return redirect()->route('devices')->with('success', 'デバイスを登録しました。'); + // 初期値(Bladeで old() 使うなら省略可) + 'device_id' => null, + 'park_id' => '', + 'device_type' => '', + 'device_subject' => '', + 'device_identifier'=> '', + 'device_work' => '', + 'device_workstart' => '', + 'device_replace' => '', + 'device_remarks' => '', + 'operator_id' => '', + ]); } - return view('admin.devices.add', [ - 'device' => new Device(), - 'isInfo' => false, - 'isEdit' => false, - ]); + // 入力値を一旦取得 + $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', '登録しました。'); } + /** - * 編集: /device/edit/{id} + * 編集(GET 画面 / POST 更新) */ - public function edit(Request $request, int $id) + public function edit($id, Request $request) { - $device = Device::findOrFail($id); + $device = Device::find($id); + if (!$device) abort(404); - if ($request->isMethod('post')) { - $v = Validator::make($request->all(), $this->rules($id)); - if ($v->fails()) return back()->withErrors($v)->withInput(); - - DB::transaction(function () use ($request, $device) { - $device->update($request->only([ - 'park_id','device_type','device_subject','device_identifier', - 'device_work','device_workstart','device_replace','device_remarks','operator_id', - ])); - }); - - return redirect()->route('devices')->with('success', 'デバイスを更新しました。'); + if ($request->isMethod('get')) { + return view('admin.devices.edit', [ + 'isEdit' => true, + 'device' => $device, + 'parks' => Park::all(), + ]); } - return view('admin.devices.edit', [ - 'device' => $device, - 'isInfo' => false, - 'isEdit' => true, - ]); + // 入力値を一旦取得 + $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); + // public function info(int $id) + // { + // $device = Device::with('park')->findOrFail($id); - return view('admin.devices.info', [ - 'device' => $device, - 'isInfo' => true, - 'isEdit' => false, - ]); - } + // return view('admin.devices.info', [ + // 'device' => $device, + // 'isInfo' => true, + // 'isEdit' => false, + // ]); + // } /** - * 削除: /device/delete + * 削除(単体 or 複数) */ public function delete(Request $request) { - $ids = $request->input('ids'); - $id = $request->input('id'); + $ids = []; - if ($id) $ids = [$id]; - if (!is_array($ids) || empty($ids)) { - return back()->with('error', '削除対象が指定されていません。'); + // 単体削除 + if ($request->filled('id')) { + $ids[] = (int) $request->input('id'); } - DB::transaction(function () use ($ids) { - Device::whereIn('device_id', $ids)->delete(); - }); + // 複数削除 + if ($request->filled('ids')) { + $ids = array_merge($ids, array_map('intval', (array)$request->input('ids'))); + } - return redirect()->route('devices')->with('success', 'デバイスを削除しました。'); + $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' => ['nullable','integer'], - 'device_type' => ['required','string','max:255'], - 'device_subject' => ['required','string','max:255'], - 'device_identifier' => ['nullable','string','max:255'], - 'device_work' => ['nullable','string','max:255'], - 'device_workstart' => ['nullable','date'], - 'device_replace' => ['nullable','date'], - 'device_remarks' => ['nullable','string','max:255'], - 'operator_id' => ['nullable','integer'], + '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'], // 任意 ]; } + } diff --git a/app/Models/Device.php b/app/Models/Device.php index 0bc9983..b1ba896 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -41,4 +41,9 @@ class Device extends Model { return static::orderBy('device_subject')->pluck('device_subject', 'device_id')->toArray(); } + + public static function deleteByPk(array $ids): int + { + return static::whereIn('device_id', $ids)->delete(); + } } diff --git a/resources/lang/ja/validation.php b/resources/lang/ja/validation.php index 35db3b8..22f0aaa 100644 --- a/resources/lang/ja/validation.php +++ b/resources/lang/ja/validation.php @@ -401,6 +401,15 @@ return [ 'tax_day' => '適用日', //SWA-80 'payment_companyname' => '事業者名', +//SWA-78 + 'device_id' => 'デバイスID', + 'device_type' => 'デバイス種別', + 'device_subject' => 'デバイス名', + 'device_identifier' => '識別子', + 'device_work' => '稼働状況', + 'device_workstart' => '稼働開始日', + 'device_replace' => '交換日', + 'device_remarks' => '備考', diff --git a/resources/views/admin/devices/_form.blade.php b/resources/views/admin/devices/_form.blade.php index b5d29fe..00b0637 100644 --- a/resources/views/admin/devices/_form.blade.php +++ b/resources/views/admin/devices/_form.blade.php @@ -1,4 +1,4 @@ -{{-- アラート --}} + + +{{-- アラート --}} +@if(Session::has('success')) + @endif +@if($errors->any()) +
+ +

{{ __('入力内容に不備があります') }}

+ +
+@endif + + {{-- ===== フォーム ===== --}}
- @if(!empty($isInfo) || !empty($isEdit)) + @if(!empty($isEdit)) {{-- デバイスID --}}
@@ -37,34 +58,56 @@
@endif - {{-- 駐輪場ID --}} + {{-- 駐輪場 --}}
- +
- +
+ + {{-- デバイス種別 --}}
-
- +
+ @php $deviceType = old('device_type', $device->device_type ?? '1'); @endphp + +
+ + +
+ +
+ + +
+ +
+ + +
+ + {{-- デバイス名 --}}
@@ -75,13 +118,13 @@ name="device_subject" value="{{ old('device_subject', $device->device_subject ?? '') }}" class="form-control form-control-lg" - @if(!empty($isInfo)) readonly @else placeholder="{{ __('validation.attributes.device_subject') }}" @endif> + placeholder="{{ __('validation.attributes.device_subject') }}">
{{-- 識別子 --}}
- +
@@ -89,43 +132,43 @@ name="device_identifier" value="{{ old('device_identifier', $device->device_identifier ?? '') }}" class="form-control form-control-lg" - @if(!empty($isInfo)) readonly @else placeholder="{{ __('validation.attributes.device_identifier') }}" @endif> + placeholder="{{ __('validation.attributes.device_identifier') }}">
{{-- 稼働/停止 --}}
- +
- @php $work = old('device_work', $device->device_work ?? ''); @endphp + @php $work = old('device_work', $device->device_work ?? '1'); @endphp +
- - + +
+
- - + +
+ {{-- 稼働開始日 --}}
- +
+ class="form-control form-control-lg">
@@ -138,8 +181,7 @@ + class="form-control form-control-lg">
@@ -154,18 +196,33 @@ value="{{ old('device_remarks', $device->device_remarks ?? '') }}" class="form-control form-control-lg" maxlength="255" - @if(!empty($isInfo)) readonly @else placeholder="{{ __('validation.attributes.device_remarks') }}" @endif> + placeholder="{{ __('validation.attributes.device_remarks') }}"> - + {{-- ▼ 下部ボタン --}} +
+
-{{-- 下部ボタン --}} - @if($isEdit) - - -@else - - - @endif + {{-- 登録ボタン --}} + @if($isEdit) + + @else + + @endif + + {{-- 削除ボタン(編集時のみ表示) --}} + @if($isEdit) + + @endif + +
+
+ \ No newline at end of file diff --git a/resources/views/admin/devices/add.blade.php b/resources/views/admin/devices/add.blade.php index 7f516d6..83f88e0 100644 --- a/resources/views/admin/devices/add.blade.php +++ b/resources/views/admin/devices/add.blade.php @@ -1,37 +1,45 @@ @extends('layouts.app') -@section('title', '[東京都|〇〇駐輪場] デバイス管理マスタ') +@section('title', '新規') @section('content') -
-
-
-
-

新規登録

-
-
- -
+ +
+
+
+
+

新規

+
+ +
+
+
-
-
-
-
-
-
- @csrf - @include('admin.devices._form', ['isEdit' => 0, 'isInfo' => 0, 'device' => $device]) + +
+
+
+
+
+
+ + @csrf + @include('admin.devices._form', ['isEdit' => false])
-
+
+
@endsection diff --git a/resources/views/admin/devices/edit.blade.php b/resources/views/admin/devices/edit.blade.php index ea8a155..a32dc93 100644 --- a/resources/views/admin/devices/edit.blade.php +++ b/resources/views/admin/devices/edit.blade.php @@ -1,17 +1,23 @@ @extends('layouts.app') -@section('title', '[東京都|〇〇駐輪場] デバイス管理マスタ') +@section('title', '編集') @section('content') + {{-- Content Header --}}

編集

+ @@ -19,15 +25,27 @@
+ {{-- Main Content --}}
-
+ {{-- Edit Form --}} + @csrf - {{-- 編集モード --}} - @include('admin.devices._form', ['isEdit' => 1, 'isInfo' => 0, 'device' => $device]) + @include('admin.devices._form', ['isEdit' => true]) +
+ + {{-- Delete Form --}} +
diff --git a/resources/views/admin/devices/info.blade.php b/resources/views/admin/devices/info.blade.php deleted file mode 100644 index fa22a9e..0000000 --- a/resources/views/admin/devices/info.blade.php +++ /dev/null @@ -1,35 +0,0 @@ -@extends('layouts.app') -@section('title', '[東京都|〇〇駐輪場] デバイス管理マスタ') - -@section('content') -
-
-
-
-

詳細

-
-
- -
-
-
-
- -
-
-
-
-
- {{-- 詳細モード --}} - @include('admin.devices._form', ['isEdit' => 0, 'isInfo' => 1, 'device' => $device]) -
-
-
-
-
-@endsection diff --git a/resources/views/admin/devices/list.blade.php b/resources/views/admin/devices/list.blade.php index bbaa4da..456a877 100644 --- a/resources/views/admin/devices/list.blade.php +++ b/resources/views/admin/devices/list.blade.php @@ -1,5 +1,5 @@ @extends('layouts.app') -@section('title', '[東京都|〇〇駐輪場] デバイス管理マスタ') +@section('title', 'デバイス管理マスタ') @section('content')
@@ -9,7 +9,6 @@
@@ -27,141 +26,110 @@ - -
-
- - + +
+
+ + +
+
+ {{ $list->appends(['sort'=>$sort,'sort_type'=>$sort_type])->links('pagination') }} +
-
- {{ $list->appends(['sort'=>$sort,'sort_type'=>$sort_type])->links('pagination') }} + + {{-- フラッシュ --}} +
+ @if(Session::has('success')) + + @elseif(Session::has('error')) +
+ +

{{ __('入力内容に不備があります') }}:

+ {!! Session::get('error') !!} +
+ @elseif(isset($errorMsg)) +
+ +

{{ __('入力内容に不備があります') }}:

+ {!! $errorMsg !!} +
+ @endif
-
- {{-- フラッシュ --}} -
- @if(Session::has('success')) - - @elseif(Session::has('error')) -
- -

{{ __('誤差') }}:

- {!! Session::get('error') !!} -
- @elseif(isset($errorMsg)) -
- -

{{ __('誤差') }}:

- {!! $errorMsg !!} -
- @endif -
+ {{-- 単一テーブル --}} +
+
+
+ @csrf + @php + $TYPE = [1=>'サーバー', 2=>'プリンタ', 3=>'その他']; + $WORK = ['1'=>'稼働', '0'=>'停止', 1=>'稼働', 0=>'停止']; + @endphp - {{-- 単一テーブル --}} -
-
- - @csrf - @php - $TYPE = [1=>'サーバー', 2=>'プリンタ', 3=>'その他']; - $WORK = ['1'=>'稼働', '0'=>'停止', 1=>'稼働', 0=>'停止']; - @endphp - - - - - - - - - - - - - - - - - - @foreach($list as $item) +
- - {{ __('デバイスID') }}{{ __('駐輪場ID') }}{{ __('デバイス種別') }}{{ __('デバイス名') }}{{ __('識別子') }}{{ __('稼働/停止') }}{{ __('稼働開始日') }}{{ __('リプレース予約日') }}{{ __('備考') }}
+ - + + - - - - - - - - - + + + + + + + + + + + + + - @endforeach - -
- - + + {{ __('デバイスID') }}{{ $item->device_id }} - {{ $item->park_id }} - @if($item->relationLoaded('park') && $item->park) - : {{ $item->park->park_name ?? '' }} - @endif - {{ $TYPE[$item->device_type] ?? $item->device_type }}{{ $item->device_subject }}{{ $item->device_identifier }}{{ $WORK[$item->device_work] ?? $item->device_work }}{{ optional($item->device_workstart)->format('Y/m/d') }}{{ optional($item->device_replace)->format('Y/m/d') }}{{ $item->device_remarks }}{{ __('駐輪場ID') }}{{ __('デバイス種別') }}{{ __('デバイス名') }}{{ __('識別子') }}{{ __('稼働停止') }}{{ __('稼働開始日') }}{{ __('リプレース予約日') }}{{ __('備考') }}
- -
-
+ + + @foreach($list as $item) + + + + + + {{ $item->device_id }} + + @if($item->relationLoaded('park') && $item->park) + {{ $item->park->park_name }} + @endif + + {{ $TYPE[$item->device_type] ?? $item->device_type }} + {{ $item->device_subject }} + {{ $item->device_identifier }} + {{ $WORK[$item->device_work] ?? $item->device_work }} + {{ optional($item->device_workstart)->format('Y/m/d') }} + {{ optional($item->device_replace)->format('Y/m/d') }} + {{ $item->device_remarks }} + + @endforeach + + +
+
+
-@push('scripts') - -@endpush @endsection