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

102 lines
3.7 KiB
PHP

<?php
namespace App\Http\Controllers\admin\Catalog;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\StoreFacultyRequest;
use App\Http\Requests\admin\Catalog\UpdateFacultyRequest;
use App\Models\EducationalInstitution;
use App\Models\Faculty;
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;
use Illuminate\Support\Str;
class FacultyController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::all();
return view('admin.catalog.faculty.index', compact('faculties'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
return view('admin.catalog.faculty.create', compact('educationalInstitutions'));
}
public function store(StoreFacultyRequest $request): RedirectResponse
{
$validated = $request->validated();
if ($validated['slug'] === null) {
$transliterationSlug = Str::slug($validated['name']);
$randomNumber = random_int(100, 999);
$slug = "{$transliterationSlug}-{$randomNumber}";
} else {
$slug = $validated['slug'];
}
$faculty = new Faculty();
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
$faculty->slug = $slug;
$faculty->educational_institution_id = $validated['educational_institution_id'];
$faculty->save();
Log::channel('app')
->info('CREATE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'data' => $validated]);
return redirect()->route('faculties.index');
}
public function show(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
return view('admin.catalog.faculty.show', compact('faculty'));
}
public function edit(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
return view('admin.catalog.faculty.edit', compact('faculty', 'educationalInstitutions'));
}
public function update(UpdateFacultyRequest $request, Faculty $faculty): RedirectResponse
{
$validated = $request->validated();
$oldData = $faculty->toArray();
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
$faculty->slug = $validated['slug'];
$faculty->educational_institution_id = $validated['educational_institution_id'];
Log::channel('app')
->info('UPDATE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'oldData' => $oldData, 'newData' => $validated]);
$faculty->save();
return redirect()->route('faculties.index');
}
public function destroy(Faculty $faculty): RedirectResponse
{
Log::channel('app')
->info('DELETE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'data' => $faculty->toArray()]);
if ($faculty->departments()->exists()) {
return back();
}
$faculty->delete();
return redirect()->route('faculties.index');
}
}