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

122 lines
4.3 KiB
PHP
Raw Normal View History

2024-02-08 15:59:44 +03:00
<?php
2024-02-12 09:19:08 +03:00
namespace App\Http\Controllers\admin\Catalog;
2024-02-08 15:59:44 +03:00
use App\Helpers\SlugHelper;
2024-02-08 15:59:44 +03:00
use App\Http\Controllers\Controller;
2024-02-12 09:19:08 +03:00
use App\Http\Requests\admin\Catalog\StoreDepartmentRequest;
use App\Http\Requests\admin\Catalog\UpdateDepartmentRequest;
2024-02-08 15:59:44 +03:00
use App\Models\Department;
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;
2024-02-08 15:59:44 +03:00
class DepartmentController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$departments = Department::all();
2024-02-12 09:19:08 +03:00
return view('admin.catalog.department.index', compact('departments'));
2024-02-08 15:59:44 +03:00
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
2024-02-12 09:19:08 +03:00
return view('admin.catalog.department.create', compact('faculties'));
2024-02-08 15:59:44 +03:00
}
public function store(StoreDepartmentRequest $request): RedirectResponse
{
$validated = $request->validated();
$slug = SlugHelper::get($validated);
2024-02-08 15:59:44 +03:00
$department = new Department();
$department->name = $validated['name'];
$department->description = $validated['description'];
$department->position = $validated['position'];
$department->faculty_id = $validated['faculty_id'];
2024-03-13 12:33:48 +03:00
$department->slug = $slug;
2024-02-08 15:59:44 +03:00
$department->save();
Log::channel('app')
->info(
'CREATE кафедра {department} - user {user}',
['user' => Auth::user()->name, 'department' => $department->name, 'data' => $validated]
);
2024-02-08 15:59:44 +03:00
return redirect()->route('departments.index');
}
public function show(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
2024-02-12 09:19:08 +03:00
$faculty = Faculty::find($department->faculty->id);
$educationalInstitution = $faculty->educationalInstitution;
return view('admin.catalog.department.show', compact('department', 'faculty', 'educationalInstitution'));
2024-02-08 15:59:44 +03:00
}
public function edit(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
2024-02-12 09:19:08 +03:00
return view('admin.catalog.department.edit', compact('department', 'faculties'));
2024-02-08 15:59:44 +03:00
}
2024-02-10 15:14:02 +03:00
public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse
2024-02-08 15:59:44 +03:00
{
$validated = $request->validated();
$oldData = $department->toArray();
2024-02-08 15:59:44 +03:00
$department->name = $validated['name'];
$department->description = $validated['description'];
$department->position = $validated['position'];
$department->faculty_id = $validated['faculty_id'];
2024-02-12 10:27:08 +03:00
$department->slug = $validated['slug'];
Log::channel('app')
->warning(
'UPDATE кафедра {department} - user {user}',
[
'user' => Auth::user()->name,
'department' => $department->name,
'oldData' => $oldData,
'newData' => $validated
]
);
2024-02-08 15:59:44 +03:00
$department->save();
return redirect()->route('departments.index');
}
public function destroy(Department $department): RedirectResponse
{
2024-02-10 15:14:02 +03:00
if ($department->directions()->exists()) {
Log::channel('app')
->error(
'NOT DELETE кафедра {department} - user {user}',
[
'user' => Auth::user()->name,
'department' => $department->name,
'data' => $department->toArray(),
]
);
2024-02-10 15:14:02 +03:00
return back();
}
Log::channel('app')
->critical(
'DELETE кафедра {department} - user {user}',
[
'user' => Auth::user()->name,
'department' => $department->name,
'data' => $department->toArray(),
]
);
2024-02-08 15:59:44 +03:00
$department->delete();
return redirect()->route('departments.index');
}
}