163 lines
4.6 KiB
PHP
163 lines
4.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\RedirectResponse;
|
|
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.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
/** @var Builder $userModels */
|
|
$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);
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.users.index';
|
|
|
|
return view($view, ['userModels' => $userModels, 'search' => $search, 'searchFields' => $searchFields]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(User $user): View
|
|
{
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.users.create';
|
|
|
|
return view($view, ['userModel' => $user]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreUserRequest $request): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$user = new User();
|
|
$user->fill($request->validated());
|
|
|
|
if ($user->saveOrFail()) {
|
|
return to_route('bo.users.edit', $user)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(__('models.classes.user')),
|
|
]));
|
|
}
|
|
|
|
return back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(__('models.classes.user')),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(User $user): View
|
|
{
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.users.edit';
|
|
|
|
return view($view, ['userModel' => $user]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $user) {
|
|
$user->fill($request->validated());
|
|
|
|
if ($user->saveOrFail()) {
|
|
return to_route('bo.users.edit', $user)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(__('models.classes.user')),
|
|
]));
|
|
}
|
|
|
|
return to_route('bo.users.edit', $user)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(__('models.classes.user')),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(User $user): RedirectResponse
|
|
{
|
|
if ($user->deleteOrFail()) {
|
|
return to_route('bo.users.index')
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(__('models.classes.user')),
|
|
]));
|
|
}
|
|
|
|
return back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(__('models.classes.user')),
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*/
|
|
public function duplicate(User $user): View
|
|
{
|
|
return $this->create($user->replicate());
|
|
}
|
|
}
|