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()