applicant-site/app/Http/Controllers/admin/Catalog/Direction/DirectionProfileController.php

124 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers\admin\Catalog\Direction;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\Direction\StoreDirectionProfileRequest;
use App\Http\Requests\admin\Catalog\Direction\UpdateDirectionProfileRequest;
use App\Models\Direction;
use App\Models\DirectionProfile;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class DirectionProfileController extends Controller
{
public function index(): View
{
$directionProfiles = DirectionProfile::all();
return view('admin.catalog.direction.direction_profile.index', compact('directionProfiles'));
}
public function create(): View
{
$directions = Direction::pluck('name', 'id');
return view('admin.catalog.direction.direction_profile.create', compact('directions'));
}
public function store(StoreDirectionProfileRequest $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'];
}
$directionProfile = new DirectionProfile();
$directionProfile->name = $validated['name'];
$directionProfile->description = $validated['description'];
$directionProfile->slug = $slug;
$directionProfile->position = $validated['position'];
$directionProfile->save();
Log::channel('app')
->info(
'CREATE профиль подготовки {directionProfile} - user {user}',
['user' => Auth::user()->name, 'directionProfile' => $directionProfile->name, 'data' => $validated]
);
return redirect()->route('direction_profiles.index');
}
public function show(DirectionProfile $directionProfile): View
{
return view('admin.catalog.direction.direction_profile.show', compact('directionProfile'));
}
public function edit(DirectionProfile $directionProfile): View
{
$directions = Direction::pluck('name', 'id');
return view(
'admin.catalog.direction.direction_profile.edit',
compact('directionProfile', 'directions')
);
}
public function update(UpdateDirectionProfileRequest $request, DirectionProfile $directionProfile): RedirectResponse
{
$validated = $request->validated();
$oldData = $directionProfile->toArray();
$directionProfile->name = $validated['name'];
$directionProfile->description = $validated['description'];
$directionProfile->slug = $validated['slug'];
$directionProfile->position = $validated['position'];
$directionProfile->save();
Log::channel('app')
->warning(
'UPDATE профиль подготовки {directionProfile} - user {user}',
[
'user' => Auth::user()->name,
'directionProfile' => $directionProfile->name,
'oldData' => $oldData,
'newData' => $validated
]
);
return redirect()->route('direction_profiles.index');
}
public function destroy(DirectionProfile $directionProfile): RedirectResponse
{
if ($directionProfile->direction()->exists()) {
Log::channel('app')
->error(
'NOT DELETE профиль подготовки {directionProfile} - user {user}',
[
'user' => Auth::user()->name,
'directionProfile' => $directionProfile->name,
'data' => $directionProfile->toArray(),
]
);
return back();
}
Log::channel('app')
->critical(
'DELETE профиль подготовки {directionProfile} - user {user}',
[
'user' => Auth::user()->name,
'directionProfile' => $directionProfile->name,
'data' => $directionProfile->toArray(),
]
);
$directionProfile->delete();
return redirect()->route('direction_profiles.index');
}
}