*/ public array $selectedRoles = []; /** @var array */ public array $linkedCompanyIds = []; /** @var array */ public array $companyRoles = []; public string $companyLookup = ''; public ?int $selectedLookupCompanyId = null; /** * @var array{ * salutation_key:?string, * title:?string, * name:?string, * address1:?string, * address2:?string, * postal_code:?string, * city:?string, * country_code:?string * } */ public array $billing = [ 'salutation_key' => null, 'title' => null, 'name' => null, 'address1' => null, 'address2' => null, 'postal_code' => null, 'city' => null, 'country_code' => null, ]; public function mount(): void { $this->portal = Portal::Both->value; $this->registrationType = RegistrationType::ExistingLegacy->value; } public function updatedSelectedLookupCompanyId(): void { if ($this->selectedLookupCompanyId) { $this->addLinkedCompany(); } } public function addLinkedCompany(): void { if (! $this->selectedLookupCompanyId) { return; } $this->linkedCompanyIds = array_map('intval', $this->linkedCompanyIds); if (! in_array($this->selectedLookupCompanyId, $this->linkedCompanyIds, true)) { $this->linkedCompanyIds[] = $this->selectedLookupCompanyId; } $this->companyRoles[$this->selectedLookupCompanyId] ??= 'member'; $this->companyLookup = ''; $this->selectedLookupCompanyId = null; } public function removeLinkedCompany(int $companyId): void { $this->linkedCompanyIds = array_values(array_filter( $this->linkedCompanyIds, fn (int|string $id): bool => (int) $id !== $companyId )); unset($this->companyRoles[$companyId]); } public function save(): void { $validated = $this->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'email', Rule::unique('users', 'email')], 'portal' => ['required', Rule::in(array_map(static fn (Portal $portal): string => $portal->value, Portal::cases()))], 'registrationType' => ['required', Rule::in(array_map(static fn (RegistrationType $type): string => $type->value, RegistrationType::cases()))], 'language' => ['required', 'string', Rule::in(['de', 'en'])], 'selectedRoles' => ['required', 'array', 'min:1'], 'selectedRoles.*' => ['string', Rule::exists('roles', 'name')], 'linkedCompanyIds' => ['array'], 'linkedCompanyIds.*' => ['integer', Rule::exists('companies', 'id')], 'companyRoles' => ['array'], 'companyRoles.*' => ['string', Rule::in(['member', 'responsible', 'owner'])], 'billing.salutation_key' => ['nullable', 'string', 'max:20'], 'billing.title' => ['nullable', 'string', 'max:80'], 'billing.name' => ['nullable', 'string', 'max:255'], 'billing.address1' => ['nullable', 'string', 'max:255'], 'billing.address2' => ['nullable', 'string', 'max:255'], 'billing.postal_code' => ['nullable', 'string', 'max:20'], 'billing.city' => ['nullable', 'string', 'max:120'], 'billing.country_code' => ['nullable', 'string', 'size:2'], ]); $user = User::query()->create([ 'name' => $validated['name'], 'email' => $validated['email'], 'portal' => $validated['portal'], 'registration_type' => $validated['registrationType'], 'language' => $validated['language'], 'is_active' => $this->isActive, 'is_super_admin' => $this->isSuperAdmin, 'password' => null, ]); $user->syncRoles($validated['selectedRoles']); $companySyncPayload = []; foreach ($validated['linkedCompanyIds'] as $companyId) { $companySyncPayload[$companyId] = [ 'role' => $validated['companyRoles'][$companyId] ?? 'member', ]; } $user->companies()->sync($companySyncPayload); if ($this->billingIsComplete()) { $user->billingAddress()->create([ 'salutation_key' => $validated['billing']['salutation_key'] ?: null, 'title' => $validated['billing']['title'] ?: null, 'name' => (string) $validated['billing']['name'], 'address1' => (string) $validated['billing']['address1'], 'address2' => $validated['billing']['address2'] ?: null, 'postal_code' => (string) $validated['billing']['postal_code'], 'city' => (string) $validated['billing']['city'], 'country_code' => strtoupper((string) $validated['billing']['country_code']), ]); } session()->flash('success', __('Benutzer wurde angelegt und verknuepft.')); $this->redirect(route('admin.users.edit', $user->id), navigate: true); } public function with(): array { $companyLookupResults = collect(); $companyLookupTerm = trim($this->companyLookup); if (mb_strlen($companyLookupTerm) >= 1) { $companyLookupResults = Company::withoutGlobalScopes() ->whereNotIn('id', $this->linkedCompanyIds ?: [-1]) ->where(function ($query) use ($companyLookupTerm): void { if ($this->supportsFullTextSearch($companyLookupTerm)) { $query->whereFullText(['name', 'email', 'slug'], $companyLookupTerm); return; } $query ->where('name', 'like', '%'.$companyLookupTerm.'%') ->orWhere('slug', 'like', '%'.$companyLookupTerm.'%') ->orWhere('email', 'like', '%'.$companyLookupTerm.'%'); }) ->orderBy('name') ->limit(50) ->get(['id', 'name', 'slug', 'email']); } return [ 'availableRoles' => $this->availableRoles(), 'linkedCompanies' => Company::withoutGlobalScopes() ->whereIn('id', $this->linkedCompanyIds ?: [-1]) ->orderBy('name') ->get(['id', 'name', 'slug', 'email']), 'companyLookupResults' => $companyLookupResults, 'salutations' => config('salutations.items', []), 'countries' => config('countries.items', []), 'portalOptions' => Portal::cases(), 'registrationTypeOptions' => RegistrationType::cases(), ]; } private function availableRoles(): Collection { return app(AdminPerformanceCache::class)->remember(AdminPerformanceCache::RoleOptions, AdminPerformanceCache::OptionsTtl, fn () => Role::query() ->orderBy('name') ->get(['id', 'name'])); } private function supportsFullTextSearch(string $term): bool { return mb_strlen($term) >= 3 && in_array(DB::connection()->getDriverName(), ['mysql', 'pgsql'], true); } protected function billingIsComplete(): bool { return filled($this->billing['name']) && filled($this->billing['address1']) && filled($this->billing['postal_code']) && filled($this->billing['city']) && filled($this->billing['country_code']); } }; ?>
{{-- ============== PAGE HEADER ============== --}}
{{ __('Admin Backend') }} {{ __('Administration · Benutzer · Neu') }}

{{ __('Benutzer anlegen') }}

{{ __('Rollen, Firmen und optional Rechnungsadresse direkt mitsetzen.') }}

{{ __('Zurück') }}
{{ __('Basisdaten') }}
{{ __('Name') }} {{ __('E-Mail') }} {{ __('Portal') }} @foreach($portalOptions as $portalOption) @endforeach {{ __('Registrierungstyp') }} @foreach($registrationTypeOptions as $registrationTypeOption) @endforeach
{{ __('Rollenzuweisung') }}
@foreach ($availableRoles as $role) @endforeach
{{ __('Firmenverknüpfung') }}
@foreach($companyLookupResults as $company) {{ $company->name }}@if($company->email)· {{ $company->email }}@endif @endforeach @if(blank(trim($companyLookup))) {{ __('Mindestens 1 Zeichen eingeben…') }} @else {{ __('Keine Firma gefunden.') }} @endif
@forelse ($linkedCompanies as $company)
{{ $company->name }}
{{ $company->slug }}
{{ __('Entfernen') }}
@empty

{{ __('Noch keine Firma verknüpft.') }}

@endforelse
{{ __('Rechnungsadresse (optional)') }}
{{ __('Anrede') }} @foreach($salutations as $key => $labels) @endforeach {{ __('Titel') }} {{ __('Rechnungsname') }} {{ __('Adresse Zeile 1') }} {{ __('Adresse Zeile 2') }} {{ __('PLZ') }} {{ __('Stadt') }} {{ __('Land') }} @foreach ($countries as $countryCode => $countryName) @endforeach
{{ __('Abbrechen') }} {{ __('Benutzer anlegen') }}