2024-12-13 09:21:37 +01:00
|
|
|
<?php
|
|
|
|
|
|
2025-09-08 13:12:33 -04:00
|
|
|
namespace App\Filament\Admin\Resources\Users\Pages;
|
2024-12-13 09:21:37 +01:00
|
|
|
|
2026-01-27 11:36:07 +01:00
|
|
|
use App\Enums\TablerIcon;
|
2025-09-08 13:12:33 -04:00
|
|
|
use App\Filament\Admin\Resources\Users\UserResource;
|
2024-12-13 09:21:37 +01:00
|
|
|
use App\Models\Role;
|
|
|
|
|
use App\Services\Users\UserCreationService;
|
2025-06-19 18:24:25 +02:00
|
|
|
use App\Traits\Filament\CanCustomizeHeaderActions;
|
|
|
|
|
use App\Traits\Filament\CanCustomizeHeaderWidgets;
|
|
|
|
|
use Filament\Actions\Action;
|
|
|
|
|
use Filament\Actions\ActionGroup;
|
2024-12-13 09:21:37 +01:00
|
|
|
use Filament\Resources\Pages\CreateRecord;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
|
|
class CreateUser extends CreateRecord
|
|
|
|
|
{
|
2025-06-19 18:24:25 +02:00
|
|
|
use CanCustomizeHeaderActions;
|
|
|
|
|
use CanCustomizeHeaderWidgets;
|
|
|
|
|
|
2024-12-13 09:21:37 +01:00
|
|
|
protected static string $resource = UserResource::class;
|
|
|
|
|
|
|
|
|
|
protected static bool $canCreateAnother = false;
|
|
|
|
|
|
|
|
|
|
private UserCreationService $service;
|
|
|
|
|
|
|
|
|
|
public function boot(UserCreationService $service): void
|
|
|
|
|
{
|
|
|
|
|
$this->service = $service;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 18:24:25 +02:00
|
|
|
/** @return array<Action|ActionGroup> */
|
|
|
|
|
protected function getDefaultHeaderActions(): array
|
2024-12-13 09:21:37 +01:00
|
|
|
{
|
|
|
|
|
return [
|
2026-01-27 20:07:18 -05:00
|
|
|
Action::make('create')
|
|
|
|
|
->hiddenLabel()
|
|
|
|
|
->action('create')
|
|
|
|
|
->keyBindings(['mod+s'])
|
|
|
|
|
->tooltip(trans('filament-panels::resources/pages/create-record.form.actions.create.label'))
|
2026-01-27 11:36:07 +01:00
|
|
|
->icon(TablerIcon::UserPlus),
|
2024-12-13 09:21:37 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getFormActions(): array
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 14:44:03 -04:00
|
|
|
protected function prepareForValidation($attributes): array
|
|
|
|
|
{
|
|
|
|
|
$attributes['data']['email'] = mb_strtolower($attributes['data']['email']);
|
|
|
|
|
|
|
|
|
|
return $attributes;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 09:21:37 +01:00
|
|
|
protected function handleRecordCreation(array $data): Model
|
|
|
|
|
{
|
|
|
|
|
$data['root_admin'] = false;
|
|
|
|
|
|
|
|
|
|
$roles = $data['roles'];
|
|
|
|
|
$roles = collect($roles)->map(fn ($role) => Role::findById($role));
|
|
|
|
|
unset($data['roles']);
|
|
|
|
|
|
|
|
|
|
$user = $this->service->handle($data);
|
|
|
|
|
|
|
|
|
|
$user->syncRoles($roles);
|
|
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
}
|