diff --git a/app/Helpers/PositionHelper.php b/app/Helpers/PositionHelper.php index 0f7f360..448875e 100644 --- a/app/Helpers/PositionHelper.php +++ b/app/Helpers/PositionHelper.php @@ -7,6 +7,7 @@ use App\Models\DirectionProfile; use App\Models\Faculty; use App\Models\EducationalInstitution; use App\Models\SubjectType; +use App\Models\Subject; class PositionHelper { @@ -39,4 +40,10 @@ class PositionHelper $maxPosition = SubjectType::max('position'); return $maxPosition >= 254 ? 255 : $maxPosition + 1; } + + public static function subject() + { + $maxPosition = Subject::max('position'); + return $maxPosition >= 254 ? 255 : $maxPosition + 1; + } } diff --git a/app/Http/Controllers/admin/Catalog/Direction/SubjectController.php b/app/Http/Controllers/admin/Catalog/Direction/SubjectController.php index ba7d26a..10c493b 100644 --- a/app/Http/Controllers/admin/Catalog/Direction/SubjectController.php +++ b/app/Http/Controllers/admin/Catalog/Direction/SubjectController.php @@ -2,12 +2,15 @@ namespace App\Http\Controllers\admin\Catalog\Direction; +use App\Helpers\SlugHelper; use App\Http\Controllers\Controller; use App\Http\Requests\admin\Catalog\Direction\StoreSubjectRequest; use App\Http\Requests\admin\Catalog\Direction\UpdateSubjectRequest; use App\Models\Subject; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; class SubjectController extends Controller { @@ -26,13 +29,21 @@ class SubjectController extends Controller { $validated = $request->validated(); + $slug = SlugHelper::get($validated); + $subject = new Subject(); $subject->name = $validated['name']; $subject->description = $validated['description']; - $subject->slug = $validated['slug']; + $subject->slug = $slug; $subject->position = $validated['position']; $subject->save(); + Log::channel('app') + ->info( + 'CREATE предмет {subject} - user {user}', + ['user' => Auth::user()->name, 'faculty' => $subject->name, 'data' => $validated] + ); + return redirect()->route('subjects.index'); } @@ -49,21 +60,51 @@ class SubjectController extends Controller public function update(UpdateSubjectRequest $request, Subject $subject): RedirectResponse { $validated = $request->validated(); + $oldData = $subject->toArray(); $subject->name = $validated['name']; $subject->description = $validated['description']; $subject->slug = $validated['slug']; $subject->position = $validated['position']; - $subject->save(); + Log::channel('app') + ->warning( + 'UPDATE предмет {subject} - user {user}', + [ + 'user' => Auth::user()->name, + 'subject' => $subject->name, + 'oldData' => $oldData, + 'newData' => $validated + ] + ); + + $subject->save(); return redirect()->route('subjects.index'); } public function destroy(Subject $subject): RedirectResponse { if ($subject->entranceExaminations()->exists()) { + Log::channel('app') + ->error( + 'NOT DELETE предмет {subject} - user {user}', + [ + 'user' => Auth::user()->name, + 'subject' => $subject->name, + 'data' => $subject->toArray(), + ] + ); return back(); } + Log::channel('app') + ->critical( + 'DELETE предмет {subject} - user {user}', + [ + 'user' => Auth::user()->name, + 'subject' => $subject->name, + 'data' => $subject->toArray(), + ] + ); $subject->delete(); return redirect()->route('subjects.index'); } diff --git a/app/Http/Requests/admin/Catalog/Direction/StoreSubjectRequest.php b/app/Http/Requests/admin/Catalog/Direction/StoreSubjectRequest.php index 966f2a4..01eeba5 100644 --- a/app/Http/Requests/admin/Catalog/Direction/StoreSubjectRequest.php +++ b/app/Http/Requests/admin/Catalog/Direction/StoreSubjectRequest.php @@ -16,8 +16,25 @@ class StoreSubjectRequest extends FormRequest return [ 'position' => 'required|int|numeric|max:255', 'name' => 'required|string|max:255|unique:subjects,name', - 'description' => 'string', - 'slug' => 'required|string|max:255|unique:subjects,slug', + 'description' => 'nullable|string', + 'slug' => 'nullable|string|max:255|unique:subjects,slug', + ]; + } + + 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 символов.', ]; } } diff --git a/app/Http/Requests/admin/Catalog/Direction/UpdateSubjectRequest.php b/app/Http/Requests/admin/Catalog/Direction/UpdateSubjectRequest.php index e47f23b..32760ec 100644 --- a/app/Http/Requests/admin/Catalog/Direction/UpdateSubjectRequest.php +++ b/app/Http/Requests/admin/Catalog/Direction/UpdateSubjectRequest.php @@ -15,7 +15,7 @@ class UpdateSubjectRequest extends FormRequest { return [ 'position' => 'required|int|numeric|max:255', - 'description' => 'string', + 'description' => 'nullable|string', 'slug' => [ 'string', 'required', diff --git a/lang/ru/tooltips.php b/lang/ru/tooltips.php index 194d8cc..7250373 100644 --- a/lang/ru/tooltips.php +++ b/lang/ru/tooltips.php @@ -51,5 +51,11 @@ return [ 'name' => 'Поле "Название" должно быть уникальным для отображения на сайте', 'description' => 'Поле "Описание" может быть пустым для отображения на сайте', 'slug' => 'Поле "URL" нужно для отображения в браузере' - ] + ], + 'subjects' => [ + 'position' => 'Поле "Позиция" нужно для упорядочивания отображения на сайте', + 'name' => 'Поле "Название" должно быть уникальным для отображения на сайте', + 'description' => 'Поле "Описание" может быть пустым для отображения на сайте', + 'slug' => 'Поле "URL" нужно для отображения в браузере' + ], ]; diff --git a/resources/views/admin/catalog/direction/subject/create.blade.php b/resources/views/admin/catalog/direction/subject/create.blade.php index 5ae0bf2..88ebb7f 100644 --- a/resources/views/admin/catalog/direction/subject/create.blade.php +++ b/resources/views/admin/catalog/direction/subject/create.blade.php @@ -1,59 +1,70 @@ +@php use App\Helpers\PositionHelper; @endphp @extends('layouts.admin_layout') @section('content') @auth()

Создать Предмет

- {{ Form::open(['url' => route('subjects.store'), 'method' => 'POST', 'class' => '']) }} + {{ Form::open(['url' => route('subjects.store'), 'method' => 'POST', 'class' => 'needs-validation', 'novalidate']) }}
+
- {{ Form::label('name', 'Название') }} + {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.position')]) }} + *
- {{ Form::text('name', '', ['class' => 'form-control']) }} + {{ Form::number('position', PositionHelper::subject(), ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.position'), 'required']) }} +
+ Поле "Позиция" обязательно! +
-
+
+ @if ($errors->any()) + {{ $errors->first('position') }} + @endif +
+ + +
+ {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.name')]) }} + * +
+
+ {{ Form::text('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.name'), 'required']) }} +
+ Поле "Название" обязательно! +
+
+
@if ($errors->any()) {{ $errors->first('name') }} @endif
- {{ Form::label('description', 'Описание') }} + {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.description')]) }}
- {{ Form::text('description', '', ['class' => 'form-control']) }} + {{ Form::text('description', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.description')]) }}
-
+
@if ($errors->any()) {{ $errors->first('description') }} @endif
- {{ Form::label('slug', 'URL') }} + {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.slug')]) }}
- {{ Form::text('slug', '', ['class' => 'form-control']) }} + {{ Form::text('slug', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.slug')]) }}
-
+
@if ($errors->any()) {{ $errors->first('slug') }} @endif
-
- {{ Form::label('position', 'Позиция') }} -
-
- {{ Form::text('position', '', ['class' => 'form-control']) }} -
-
- @if ($errors->any()) - {{ $errors->first('position') }} - @endif -
-
{{ Form::submit('Создать', ['class' => 'btn btn-primary']) }}
diff --git a/resources/views/admin/catalog/direction/subject/edit.blade.php b/resources/views/admin/catalog/direction/subject/edit.blade.php index e25fea9..097c79b 100644 --- a/resources/views/admin/catalog/direction/subject/edit.blade.php +++ b/resources/views/admin/catalog/direction/subject/edit.blade.php @@ -5,56 +5,65 @@

Изменить предмет

- {{ Form::open(['url' => route('subjects.update', $subject), 'method' => 'PATCH', 'class' => '']) }} + {{ Form::open(['url' => route('subjects.update', $subject), 'method' => 'PATCH', 'class' => 'needs-validation', 'novalidate']) }}
+
- {{ Form::label('name', 'Название') }} + {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.position')]) }} + *
- {{ Form::text('name', $subject->name, ['class' => 'form-control']) }} + {{ Form::number('position', $subject->position, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.subjects.position'), 'required']) }} +
+ Поле "Позиция" обязательно! +
-
+
+ @if ($errors->any()) + {{ $errors->first('position') }} + @endif +
+ +
+ {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.name')]) }} + * +
+
+ {{ Form::text('name', $subject->name, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.name'), 'required']) }} +
+ Поле "Название" обязательно! +
+
+
@if ($errors->any()) {{ $errors->first('name') }} @endif
- {{ Form::label('description', 'Описание') }} + {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.description')]) }}
- {{ Form::text('description', $subject->description, ['class' => 'form-control']) }} + {{ Form::text('description', $subject->description, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.description')]) }}
-
+
@if ($errors->any()) {{ $errors->first('description') }} @endif
- {{ Form::label('slug', 'URL') }} + {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.slug')]) }}
- {{ Form::text('slug', $subject->slug, ['class' => 'form-control']) }} + {{ Form::text('slug', $subject->slug, ['class' => 'form-control','data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.slug')]) }}
-
+
@if ($errors->any()) {{ $errors->first('slug') }} @endif
-
- {{ Form::label('position', 'Позиция') }} -
-
- {{ Form::text('position', $subject->position, ['class' => 'form-control']) }} -
-
- @if ($errors->any()) - {{ $errors->first('position') }} - @endif -
-
{{ Form::submit('Изменить', ['class' => 'btn btn-primary']) }}