ldc-gen-game-web/app/Http/Controllers/Bo/UserController.php

164 lines
4.6 KiB
PHP
Raw Normal View History

2023-11-23 17:31:28 +01:00
<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
2023-11-23 17:31:28 +01:00
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Users\StoreUserRequest;
use App\Http\Requests\Bo\Users\UpdateUserRequest;
use App\Models\User;
use App\Traits\Controllers\UpdateModelPublished;
2026-10-02 12:09:46 +02:00
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
2023-11-23 17:31:28 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class UserController extends Controller
{
use UpdateModelPublished;
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(User::class);
}
/**
* Display a listing of the resource.
*/
2026-10-02 12:09:46 +02:00
public function index(Request $request): View
2023-11-23 17:31:28 +01:00
{
2026-10-02 12:09:46 +02:00
/** @var Builder $userModels */
2023-11-23 17:31:28 +01:00
$userModels = User::query();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$userModels,
$search,
null,
'first_name',
'last_name',
'email',
'role',
);
}
$searchFields = \implode(', ', [
trans('validation.attributes.first_name'),
trans('validation.attributes.last_name'),
trans('validation.attributes.email'),
trans('validation.attributes.role'),
]);
/** Sort columns with a query */
$this->sortQuery($userModels);
/** Custom pagination */
$userModels = $this->paginate($userModels);
2026-10-02 12:09:46 +02:00
/** @var view-string $view */
$view = 'back::pages.users.index';
2023-11-23 17:31:28 +01:00
2026-10-02 12:09:46 +02:00
return view($view, ['userModels' => $userModels, 'search' => $search, 'searchFields' => $searchFields]);
}
2023-11-23 17:31:28 +01:00
/**
* Show the form for creating a new resource.
*/
2026-10-02 12:09:46 +02:00
public function create(User $user): View
2023-11-23 17:31:28 +01:00
{
2026-10-02 12:09:46 +02:00
/** @var view-string $view */
$view = 'back::pages.users.create';
return view($view, ['userModel' => $user]);
2023-11-23 17:31:28 +01:00
}
/**
* Store a newly created resource in storage.
*/
2026-10-02 12:09:46 +02:00
public function store(StoreUserRequest $request): RedirectResponse
2023-11-23 17:31:28 +01:00
{
return DB::transaction(function () use ($request) {
$user = new User();
$user->fill($request->validated());
if ($user->saveOrFail()) {
2026-10-02 12:09:46 +02:00
return to_route('bo.users.edit', $user)
2023-11-23 17:31:28 +01:00
->with('success', __('crud.messages.has_been_created', [
2026-10-02 12:09:46 +02:00
'model' => Str::ucfirst(__('models.classes.user')),
2023-11-23 17:31:28 +01:00
]));
}
2026-10-02 12:09:46 +02:00
return back()
2023-11-23 17:31:28 +01:00
->with('error', __('crud.messages.cannot_be_created', [
2026-10-02 12:09:46 +02:00
'model' => Str::ucfirst(__('models.classes.user')),
2023-11-23 17:31:28 +01:00
]));
});
}
/**
* Show the form for editing the specified resource.
*/
2026-10-02 12:09:46 +02:00
public function edit(User $user): View
2023-11-23 17:31:28 +01:00
{
2026-10-02 12:09:46 +02:00
/** @var view-string $view */
$view = 'back::pages.users.edit';
return view($view, ['userModel' => $user]);
2023-11-23 17:31:28 +01:00
}
/**
* Update the specified resource in storage.
*/
2026-10-02 12:09:46 +02:00
public function update(UpdateUserRequest $request, User $user): RedirectResponse
2023-11-23 17:31:28 +01:00
{
return DB::transaction(function () use ($request, $user) {
$user->fill($request->validated());
if ($user->saveOrFail()) {
2026-10-02 12:09:46 +02:00
return to_route('bo.users.edit', $user)
2023-11-23 17:31:28 +01:00
->with('success', __('crud.messages.has_been_updated', [
2026-10-02 12:09:46 +02:00
'model' => Str::ucfirst(__('models.classes.user')),
2023-11-23 17:31:28 +01:00
]));
}
2026-10-02 12:09:46 +02:00
return to_route('bo.users.edit', $user)
2023-11-23 17:31:28 +01:00
->with('error', __('crud.messages.cannot_be_updated', [
2026-10-02 12:09:46 +02:00
'model' => Str::ucfirst(__('models.classes.user')),
2023-11-23 17:31:28 +01:00
]));
});
}
/**
* Remove the specified resource from storage.
*/
2026-10-02 12:09:46 +02:00
public function destroy(User $user): RedirectResponse
2023-11-23 17:31:28 +01:00
{
if ($user->deleteOrFail()) {
2026-10-02 12:09:46 +02:00
return to_route('bo.users.index')
2023-11-23 17:31:28 +01:00
->with('success', __('crud.messages.has_been_deleted', [
2026-10-02 12:09:46 +02:00
'model' => Str::ucfirst(__('models.classes.user')),
2023-11-23 17:31:28 +01:00
]));
}
2026-10-02 12:09:46 +02:00
return back()
2023-11-23 17:31:28 +01:00
->with('error', __('crud.messages.cannot_be_deleted', [
2026-10-02 12:09:46 +02:00
'model' => Str::ucfirst(__('models.classes.user')),
2023-11-23 17:31:28 +01:00
]));
}
/**
* Duplicate the specified resource.
*/
2026-10-02 12:09:46 +02:00
public function duplicate(User $user): View
2023-11-23 17:31:28 +01:00
{
return $this->create($user->replicate());
}
}