Roman_applicant-site/app/Http/Controllers/admin/Catalog/DirectionController.php

311 lines
12 KiB
PHP

<?php
namespace App\Http\Controllers\admin\Catalog;
use App\Enums\ExaminationTypeEnum;
use App\Helpers\SlugHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\StoreDirectionRequest;
use App\Http\Requests\admin\Catalog\UpdateDirectionRequest;
use App\Models\Department;
use App\Models\Direction;
use App\Models\DirectionProfile;
use App\Models\EducationForm;
use App\Models\EducationLevel;
use App\Models\EntranceExamination;
use App\Models\ExaminationType;
use App\Models\Faculty;
use App\Models\Subject;
use App\Models\SubjectType;
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 DirectionController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$directions = Direction::all()->sortBy('created_at', SORT_REGULAR, true);
return view('admin.catalog.direction.index', compact('directions'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$levels = EducationLevel::pluck('name', 'id');
$forms = EducationForm::pluck('name', 'id');
$departments = Department::pluck('name', 'id');
$examination_types = ExaminationType::pluck('name', 'id');
$subjects = Subject::pluck('name', 'id');
$subjectTypes = SubjectType::pluck('name', 'id');
$directionProfiles = DirectionProfile::pluck('name', 'id');
return view(
'admin.catalog.direction.create',
compact(
'departments',
'levels',
'forms',
'examination_types',
'subjectTypes',
'subjects',
'directionProfiles',
)
);
}
public function store(StoreDirectionRequest $request): RedirectResponse
{
$validated = $request->validated();
$slug = SlugHelper::get($validated);
$direction = new Direction();
$direction->name = $validated['name'];
$direction->full_name = "{$validated['code']} {$validated['name']}";
$direction->description = $validated['description'];
$direction->position = $validated['position'];
$direction->slug = $slug;
$direction->code = $validated['code'];
$direction->education_level_id = $validated['education_level_id'];
$direction->education_form_id = $validated['education_form_id'];
$direction->department_id = $validated['department_id'];
$direction->budget_places = $validated['budget_places'];
$direction->quota = $validated['quota'];
$direction->paid_places = $validated['paid_places'];
$direction->cost_paid_place = $validated['cost_paid_place'];
$direction->period = $validated['period'];
$direction->save();
if (array_key_exists('entrance-examination', $validated)) {
foreach ($validated['entrance-examination'] as $data) {
$entranceExamination = new EntranceExamination();
$entranceExamination->examination_type_id = $data['examination_type_id'];
$entranceExamination->direction_id = $direction->id;
$entranceExamination->subject_id = $data['subject_id'];
$entranceExamination->scores = $data['scores'];
$entranceExamination->position = $data['position'];
$entranceExamination->subject_type_id = $data['subject_type_id'];
$entranceExamination->save();
}
}
if (array_key_exists('direction_profiles', $validated)) {
$direction->directionProfiles()->attach($validated['direction_profiles']);
}
Log::channel('app')
->info(
'CREATE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $validated
]
);
return redirect()->route('directions.index');
}
public function show(Direction $direction): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$department = $direction->department;
$faculty = Faculty::find($department->faculty->id);
$educationalInstitution = $faculty->educationalInstitution;
$ege = $direction
->entranceExaminations
->where('examination_type_id', '=', ExaminationTypeEnum::Ege->value)
->sortBy('position')
->pluck('scores', 'subject_id');
$spo = $direction
->entranceExaminations->where('examination_type_id', '=', ExaminationTypeEnum::Spo->value)
->sortBy('position')
->pluck('scores', 'subject_id');
$magistracy = $direction
->entranceExaminations
->where('examination_type_id', '=', ExaminationTypeEnum::Magistracy->value)
->sortBy('position')
->pluck('scores', 'subject_id');
return view(
'admin.catalog.direction.show',
compact(
'direction',
'educationalInstitution',
'faculty',
'department',
'ege',
'spo',
'magistracy',
)
);
}
public function edit(Direction $direction): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$levels = EducationLevel::pluck('name', 'id');
$forms = EducationForm::pluck('name', 'id');
$departments = Department::pluck('name', 'id');
$examination_types = ExaminationType::pluck('name', 'id');
$subjects = Subject::pluck('name', 'id');
$subjectTypes = SubjectType::pluck('name', 'id');
$directionProfiles = DirectionProfile::pluck('name', 'id');
return view(
'admin.catalog.direction.edit',
compact(
'direction',
'departments',
'levels',
'forms',
'examination_types',
'subjectTypes',
'subjects',
'directionProfiles',
)
);
}
public function update(UpdateDirectionRequest $request, Direction $direction): RedirectResponse
{
$oldData = $direction->toArray();
$validated = $request->validated();
$direction->name = $validated['name'];
$direction->full_name = "{$validated['code']} {$validated['name']}";
$direction->description = $validated['description'];
$direction->position = $validated['position'];
$direction->slug = $validated['slug'];
$direction->code = $validated['code'];
$direction->education_level_id = $validated['education_level_id'];
$direction->education_form_id = $validated['education_form_id'];
$direction->department_id = $validated['department_id'];
$direction->budget_places = $validated['budget_places'];
$direction->quota = $validated['quota'];
$direction->paid_places = $validated['paid_places'];
$direction->cost_paid_place = $validated['cost_paid_place'];
$direction->period = $validated['period'];
Log::channel('app')
->warning(
'UPDATE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'oldData' => $oldData,
'newData' => $validated
]
);
$direction->save();
if (array_key_exists('entrance-examination', $validated)) {
foreach ($validated['entrance-examination'] as $id => $data) {
$entranceExamination = EntranceExamination::firstOrNew(['id' => $id]);
$entranceExamination->direction_id = $direction->id;
$entranceExamination->subject_id = $data['subject_id'];
$entranceExamination->scores = $data['scores'];
$entranceExamination->position = $data['position'];
$entranceExamination->subject_type_id = $data['subject_type_id'];
$entranceExamination->examination_type_id = $data['examination_type_id'];
$entranceExamination->save();
}
}
if (array_key_exists('delete', $validated)) {
foreach ($validated['delete'] as $id => $value) {
$entranceExamination = EntranceExamination::firstOrNew(['id' => $id]);
$entranceExamination->delete();
}
}
if (array_key_exists('direction_profiles', $validated)) {
$direction->directionProfiles()->sync($validated['direction_profiles']);
}
return redirect()->route('directions.index');
}
public function destroy(Direction $direction): RedirectResponse
{
if ($direction->entranceExaminations()->exists()) {
Log::channel('app')
->error(
'NOT DELETE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $direction->toArray(),
]
);
return back();
}
Log::channel('app')
->critical(
'DELETE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $direction->toArray(),
]
);
$direction->directionProfiles()->detach();
$direction->delete();
return redirect()->route('directions.index');
}
public function duplication(Direction $direction): RedirectResponse
{
$now = now()->toAtomString();
$newDirection = new Direction();
$newDirection->name = $direction->name;
$newDirection->full_name = "{$direction->code} {$direction->name}";
$newDirection->description = $direction->description;
$newDirection->position = $direction->position;
$newDirection->slug = "{$direction->slug} {$now}";
$newDirection->code = $direction->code;
$newDirection->education_level_id = $direction->education_level_id;
$newDirection->education_form_id = $direction->education_form_id;
$newDirection->department_id = $direction->department_id;
$newDirection->budget_places = $direction->budget_places;
$newDirection->quota = $direction->quota;
$newDirection->paid_places = $direction->paid_places;
$newDirection->cost_paid_place = $direction->cost_paid_place;
$newDirection->period = $direction->period;
$newDirection->save();
foreach ($direction->entranceExaminations as $entranceExamination) {
$newEntranceExamination = new EntranceExamination();
$newEntranceExamination->examination_type_id = $entranceExamination->examination_type_id;
$newEntranceExamination->direction_id = $newDirection->id;
$newEntranceExamination->subject_id = $entranceExamination->subject_id;
$newEntranceExamination->scores = $entranceExamination->scores;
$newEntranceExamination->position = $entranceExamination->position;
$newEntranceExamination->subject_type_id = $entranceExamination->subject_type_id;
$newEntranceExamination->save();
}
$newDirection->directionProfiles()->attach($direction->directionProfiles);
Log::channel('app')
->info(
'DUPLICATE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $direction->toArray(),
]
);
return redirect()->route('directions.index');
}
}