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

120 lines
4.2 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
2024-03-12 12:40:40 +03:00
use App\Helpers\SlugHelper;
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();
2024-03-12 12:40:40 +03:00
$slug = SlugHelper::get($validated);
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')
2024-03-12 09:32:57 +03:00
->info(
'CREATE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'data' => $validated]
);
2024-03-11 14:34:23 +03:00
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')
->warning(
2024-03-12 09:32:57 +03:00
'UPDATE факультет {faculty} - user {user}',
[
'user' => Auth::user()->name,
'faculty' => $faculty->name,
'oldData' => $oldData,
'newData' => $validated
]
);
2024-03-11 14:34:23 +03:00
$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
{
if ($faculty->departments()->exists()) {
Log::channel('app')
->critical(
'NOT DELETE факультет {faculty} - user {user}',
[
'user' => Auth::user()->name,
'faculty' => $faculty->name,
'data' => $faculty->toArray(),
]
);
return back();
}
Log::channel('app')
->critical(
'DELETE факультет {faculty} - user {user}',
[
'user' => Auth::user()->name,
'faculty' => $faculty->name,
'data' => $faculty->toArray(),
]
);
2024-02-07 16:30:49 +03:00
$faculty->delete();
return redirect()->route('faculties.index');
}
}