From 5fa4b20ec741c7d0bbcb5d2b9e67e1380dca2bce Mon Sep 17 00:00:00 2001 From: aslan Date: Wed, 13 Mar 2024 10:56:14 +0300 Subject: [PATCH] add bootstrap validation and Logging for Department --- app/Helpers/PositionHelper.php | 6 +++ .../admin/Catalog/DepartmentController.php | 42 +++++++++++++++++++ .../admin/Catalog/StoreDepartmentRequest.php | 25 ++++++++++- .../admin/Catalog/UpdateDepartmentRequest.php | 23 +++++++++- lang/ru/tooltips.php | 7 ++++ .../admin/catalog/department/create.blade.php | 42 ++++++++++++------- .../admin/catalog/department/edit.blade.php | 41 +++++++++++------- .../admin/catalog/faculty/create.blade.php | 1 + 8 files changed, 153 insertions(+), 34 deletions(-) diff --git a/app/Helpers/PositionHelper.php b/app/Helpers/PositionHelper.php index a0654c9..94e9953 100644 --- a/app/Helpers/PositionHelper.php +++ b/app/Helpers/PositionHelper.php @@ -60,4 +60,10 @@ class PositionHelper $maxPosition = EntranceExamination::max('position'); return $maxPosition >= 254 ? 255 : $maxPosition + 1; } + + public static function department() + { + $maxPosition = EntranceExamination::max('position'); + return $maxPosition >= 254 ? 255 : $maxPosition + 1; + } } diff --git a/app/Http/Controllers/admin/Catalog/DepartmentController.php b/app/Http/Controllers/admin/Catalog/DepartmentController.php index a4085d9..cfd7276 100644 --- a/app/Http/Controllers/admin/Catalog/DepartmentController.php +++ b/app/Http/Controllers/admin/Catalog/DepartmentController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\admin\Catalog; +use App\Helpers\SlugHelper; use App\Http\Controllers\Controller; use App\Http\Requests\admin\Catalog\StoreDepartmentRequest; use App\Http\Requests\admin\Catalog\UpdateDepartmentRequest; @@ -11,6 +12,8 @@ use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Foundation\Application; use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; class DepartmentController extends Controller { @@ -30,6 +33,8 @@ class DepartmentController extends Controller { $validated = $request->validated(); + $slug = SlugHelper::get($validated); + $department = new Department(); $department->name = $validated['name']; $department->description = $validated['description']; @@ -38,6 +43,12 @@ class DepartmentController extends Controller $department->slug = $validated['slug']; $department->save(); + Log::channel('app') + ->info( + 'CREATE кафедра {department} - user {user}', + ['user' => Auth::user()->name, 'department' => $department->name, 'data' => $validated] + ); + return redirect()->route('departments.index'); } @@ -57,12 +68,25 @@ class DepartmentController extends Controller public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse { $validated = $request->validated(); + $oldData = $department->toArray(); $department->name = $validated['name']; $department->description = $validated['description']; $department->position = $validated['position']; $department->faculty_id = $validated['faculty_id']; $department->slug = $validated['slug']; + + Log::channel('app') + ->warning( + 'UPDATE кафедра {department} - user {user}', + [ + 'user' => Auth::user()->name, + 'department' => $department->name, + 'oldData' => $oldData, + 'newData' => $validated + ] + ); + $department->save(); return redirect()->route('departments.index'); @@ -71,8 +95,26 @@ class DepartmentController extends Controller public function destroy(Department $department): RedirectResponse { if ($department->directions()->exists()) { + Log::channel('app') + ->error( + 'NOT DELETE кафедра {department} - user {user}', + [ + 'user' => Auth::user()->name, + 'department' => $department->name, + 'data' => $department->toArray(), + ] + ); return back(); } + Log::channel('app') + ->critical( + 'DELETE кафедра {department} - user {user}', + [ + 'user' => Auth::user()->name, + 'department' => $department->name, + 'data' => $department->toArray(), + ] + ); $department->delete(); return redirect()->route('departments.index'); } diff --git a/app/Http/Requests/admin/Catalog/StoreDepartmentRequest.php b/app/Http/Requests/admin/Catalog/StoreDepartmentRequest.php index 7d0d7f7..84967e6 100644 --- a/app/Http/Requests/admin/Catalog/StoreDepartmentRequest.php +++ b/app/Http/Requests/admin/Catalog/StoreDepartmentRequest.php @@ -17,9 +17,30 @@ class StoreDepartmentRequest extends FormRequest return [ 'position' => 'required|int|numeric|max:255', 'name' => 'required|string|max:255|unique:departments,name', - 'description' => 'string', - 'slug' => 'required|string|max:255|unique:departments,slug', + 'description' => 'nullable|string', + 'slug' => 'nullable|string|max:255|unique:departments,slug', 'faculty_id' => 'required|numeric|int|max:1000', ]; } + + public function messages(): array + { + return [ + 'position.required' => 'Поле позиция обязательно.', + 'position.int' => 'Позиция должно быть целым числом.', + 'position.numeric' => 'Позиция должно быть числом.', + 'position.max' => 'Позиция не должен быть больше :max', + 'name.required' => 'Поле название обязательно.', + 'name.string' => 'Поле название должен быть строкой.', + 'name.max' => 'Поле название не должен превышать :max символов.', + 'name.unique' => 'Название уже занят.', + 'description.string' => 'Поле описание должен быть строкой.', + 'slug.string' => 'Поле URL должен быть строкой.', + 'slug.max' => 'Поле URL не должен превышать :max символов.', + 'faculty_id.required' => 'Поле учебное заведение обязательно.', + 'faculty_id.int' => 'Учебное заведение должно быть целым числом.', + 'faculty_id.numeric' => 'Учебное заведение должно быть числом.', + 'faculty_id.max' => 'Поле учебное заведение не должен быть больше :max.', + ]; + } } diff --git a/app/Http/Requests/admin/Catalog/UpdateDepartmentRequest.php b/app/Http/Requests/admin/Catalog/UpdateDepartmentRequest.php index f230924..13e9b6f 100644 --- a/app/Http/Requests/admin/Catalog/UpdateDepartmentRequest.php +++ b/app/Http/Requests/admin/Catalog/UpdateDepartmentRequest.php @@ -14,7 +14,7 @@ class UpdateDepartmentRequest extends FormRequest { return [ 'position' => 'required|int|numeric|max:255', - 'description' => 'string', + 'description' => 'nullable|string', 'slug' => [ 'string', 'required', @@ -30,4 +30,25 @@ class UpdateDepartmentRequest extends FormRequest ], ]; } + + public function messages(): array + { + return [ + 'position.required' => 'Поле позиция обязательно.', + 'position.int' => 'Позиция должно быть целым числом.', + 'position.numeric' => 'Позиция должно быть числом.', + 'position.max' => 'Позиция не должен быть больше :max', + 'name.required' => 'Поле название обязательно.', + 'name.string' => 'Поле название должен быть строкой.', + 'name.max' => 'Поле название не должен превышать :max символов.', + 'name.unique' => 'Название уже занят.', + 'description.string' => 'Поле описание должен быть строкой.', + 'slug.string' => 'Поле URL должен быть строкой.', + 'slug.max' => 'Поле URL не должен превышать :max символов.', + 'faculty_id.required' => 'Поле учебное заведение обязательно.', + 'faculty_id.int' => 'Учебное заведение должно быть целым числом.', + 'faculty_id.numeric' => 'Учебное заведение должно быть числом.', + 'faculty_id.max' => 'Поле учебное заведение не должен быть больше :max.', + ]; + } } diff --git a/lang/ru/tooltips.php b/lang/ru/tooltips.php index a0ebaaf..fd689ed 100644 --- a/lang/ru/tooltips.php +++ b/lang/ru/tooltips.php @@ -75,4 +75,11 @@ return [ 'description' => 'Поле "Описание" может быть пустым для отображения на сайте', 'slug' => 'Поле "URL" нужно для отображения в браузере' ], + 'departments' => [ + 'position' => 'Поле "Позиция" нужно для упорядочивания отображения на сайте', + 'name' => 'Поле "Название" должно быть уникальным для отображения на сайте', + 'description' => 'Поле "Описание" может быть пустым для отображения на сайте', + 'faculty_id' => 'Поле "Факультет" указывает на привязку к факультету', + 'slug' => 'Поле "URL" нужно для отображения в браузере' + ], ]; diff --git a/resources/views/admin/catalog/department/create.blade.php b/resources/views/admin/catalog/department/create.blade.php index b522a18..157d1f0 100644 --- a/resources/views/admin/catalog/department/create.blade.php +++ b/resources/views/admin/catalog/department/create.blade.php @@ -1,3 +1,4 @@ +@php use App\Helpers\PositionHelper; @endphp @extends('layouts.admin_layout') @section('content') @auth() @@ -6,60 +7,71 @@

Создать кафедру

{{ Form::open(['url' => route('departments.store'), 'method' => 'POST', 'class' => '']) }}
+
- {{ Form::label('position', 'Позиция') }} + {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position')]) }} + *
- {{ Form::text('position', '', ['class' => 'form-control']) }} + {{ Form::number('position', PositionHelper::department(), ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position'), 'required']) }} +
+ Поле "Позиция" обязательно! +
-
+
@if ($errors->any()) {{ $errors->first('position') }} @endif
- {{ Form::label('name', 'Название') }} + {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name')]) }} + *
- {{ Form::text('name', '', ['class' => 'form-control']) }} + {{ Form::text('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name'), 'required']) }} +
+ Поле "Название" обязательно! +
-
+
@if ($errors->any()) {{ $errors->first('name') }} @endif
- {{ Form::label('description', 'Описание') }} + {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
- {{ Form::text('description', '', ['class' => 'form-control']) }} + {{ Form::text('description', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
-
+
@if ($errors->any()) {{ $errors->first('description') }} @endif
+
- {{ Form::label('faculty_id', 'Факультет') }} + {{ Form::label('faculty_id', 'Факультет', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }} + *
- {{ Form::select('faculty_id', $faculties, null, ['class' => 'form-select']) }} + {{ Form::select('faculty_id', $faculties, null, ['class' => 'form-select', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }}
-
+
@if ($errors->any()) {{ $errors->first('faculty_id') }} @endif
- {{ Form::label('slug', 'URL') }} + {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
- {{ Form::text('slug', '', ['class' => 'form-control']) }} + {{ Form::text('slug', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
-
+
@if ($errors->any()) {{ $errors->first('slug') }} @endif diff --git a/resources/views/admin/catalog/department/edit.blade.php b/resources/views/admin/catalog/department/edit.blade.php index 8908b68..1fe2f78 100644 --- a/resources/views/admin/catalog/department/edit.blade.php +++ b/resources/views/admin/catalog/department/edit.blade.php @@ -7,59 +7,68 @@ {{ Form::open(['url' => route('departments.update', $department), 'method' => 'PATCH', 'class' => '']) }}
- {{ Form::label('position', 'Позиция') }} + {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position')]) }} + *
- {{ Form::text('position', $department->position, ['class' => 'form-control']) }} + {{ Form::number('position', $department->position, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position'), 'required']) }} +
+ Поле "Позиция" обязательно! +
-
+
@if ($errors->any()) {{ $errors->first('position') }} @endif
- {{ Form::label('name', 'Название') }} + {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name')]) }} + *
- {{ Form::text('name', $department->name, ['class' => 'form-control']) }} + {{ Form::text('name', $department->name, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name'), 'required']) }} +
+ Поле "Название" обязательно! +
-
+
@if ($errors->any()) {{ $errors->first('name') }} @endif
- {{ Form::label('description', 'Описание') }} + {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
- {{ Form::text('description', $department->description, ['class' => 'form-control']) }} + {{ Form::text('description', $department->description, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
-
+
@if ($errors->any()) {{ $errors->first('description') }} @endif
- {{ Form::label('educational_institution_id', 'Факультет') }} + {{ Form::label('faculty_id', 'Факультет', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }} + *
- {{ Form::select('faculty_id', $faculties, $department->faculty->id, ['class' => 'form-select']) }} + {{ Form::select('faculty_id', $faculties, $department->faculty->id, ['class' => 'form-select', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }}
-
+
@if ($errors->any()) - {{ $errors->first('educational_institution_id') }} + {{ $errors->first('faculty_id') }} @endif
- {{ Form::label('slug', 'URL') }} + {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
- {{ Form::text('slug', $department->slug, ['class' => 'form-control']) }} + {{ Form::text('slug', $department->slug, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
-
+
@if ($errors->any()) {{ $errors->first('slug') }} @endif diff --git a/resources/views/admin/catalog/faculty/create.blade.php b/resources/views/admin/catalog/faculty/create.blade.php index 4d9dd5a..1e65e53 100644 --- a/resources/views/admin/catalog/faculty/create.blade.php +++ b/resources/views/admin/catalog/faculty/create.blade.php @@ -7,6 +7,7 @@

Создать факультет

{{ Form::open(['url' => route('faculties.store'), 'method' => 'POST', 'class' => 'needs-validation', 'novalidate']) }}
+
{{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.position')]) }} *