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

102 lines
3.7 KiB
PHP
Raw Normal View History

2024-02-07 16:30:49 +03:00
<?php
2024-02-12 12:15:46 +03:00
namespace App\Http\Controllers\admin\Catalog;
2024-02-07 16:30:49 +03:00
use App\Http\Controllers\Controller;
2024-02-12 12:15:46 +03:00
use App\Http\Requests\admin\Catalog\StoreFacultyRequest;
use App\Http\Requests\admin\Catalog\UpdateFacultyRequest;
2024-02-07 16:30:49 +03:00
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;
2024-03-11 14:34:23 +03:00
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
2024-02-07 16:30:49 +03:00
class FacultyController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::all();
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.index', compact('faculties'));
2024-02-07 16:30:49 +03:00
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.create', compact('educationalInstitutions'));
2024-02-07 16:30:49 +03:00
}
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'];
}
2024-02-07 16:30:49 +03:00
$faculty = new Faculty();
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
$faculty->slug = $slug;
2024-02-07 16:30:49 +03:00
$faculty->educational_institution_id = $validated['educational_institution_id'];
$faculty->save();
2024-03-11 14:34:23 +03:00
Log::channel('app')
->info('CREATE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'data' => $validated]);
2024-02-07 16:30:49 +03:00
return redirect()->route('faculties.index');
}
public function show(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.show', compact('faculty'));
}
2024-02-07 19:27:25 +03:00
public function edit(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
2024-02-07 16:30:49 +03:00
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
2024-02-12 12:15:46 +03:00
return view('admin.catalog.faculty.edit', compact('faculty', 'educationalInstitutions'));
2024-02-07 16:30:49 +03:00
}
2024-02-07 19:27:25 +03:00
public function update(UpdateFacultyRequest $request, Faculty $faculty): RedirectResponse
2024-02-07 16:30:49 +03:00
{
$validated = $request->validated();
2024-03-11 14:34:23 +03:00
$oldData = $faculty->toArray();
2024-02-07 16:30:49 +03:00
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
2024-02-12 12:15:46 +03:00
$faculty->slug = $validated['slug'];
2024-02-07 16:30:49 +03:00
$faculty->educational_institution_id = $validated['educational_institution_id'];
2024-03-11 14:34:23 +03:00
Log::channel('app')
->info('UPDATE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'oldData' => $oldData, 'newData' => $validated]);
$faculty->save();
2024-02-07 16:30:49 +03:00
return redirect()->route('faculties.index');
}
2024-02-07 19:27:25 +03:00
public function destroy(Faculty $faculty): RedirectResponse
2024-02-07 16:30:49 +03:00
{
2024-03-11 14:34:23 +03:00
Log::channel('app')
->info('DELETE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'data' => $faculty->toArray()]);
if ($faculty->departments()->exists()) {
return back();
}
2024-02-07 16:30:49 +03:00
$faculty->delete();
return redirect()->route('faculties.index');
}
}