12-05-2026 Frontend dev
This commit is contained in:
parent
405df0a122
commit
5b8bdf4182
779 changed files with 480564 additions and 6241 deletions
421
resources/views/livewire/admin/users/create.blade.php
Normal file
421
resources/views/livewire/admin/users/create.blade.php
Normal file
|
|
@ -0,0 +1,421 @@
|
|||
<?php
|
||||
|
||||
use App\Enums\Portal;
|
||||
use App\Enums\RegistrationType;
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use App\Services\Admin\AdminPerformanceCache;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Volt\Component;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
new #[Layout('components.layouts.app'), Title('Benutzer anlegen')] class extends Component
|
||||
{
|
||||
public string $name = '';
|
||||
|
||||
public string $email = '';
|
||||
|
||||
public string $portal = '';
|
||||
|
||||
public string $registrationType = '';
|
||||
|
||||
public string $language = 'de';
|
||||
|
||||
public bool $isActive = true;
|
||||
|
||||
public bool $isSuperAdmin = false;
|
||||
|
||||
/** @var array<int, string> */
|
||||
public array $selectedRoles = [];
|
||||
|
||||
/** @var array<int, int> */
|
||||
public array $linkedCompanyIds = [];
|
||||
|
||||
/** @var array<int, string> */
|
||||
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']);
|
||||
}
|
||||
}; ?>
|
||||
|
||||
<form wire:submit="save" class="space-y-6">
|
||||
<flux:card>
|
||||
<flux:heading size="lg">{{ __('Benutzer anlegen') }}</flux:heading>
|
||||
<flux:subheading>{{ __('Rollen, Firmen und optional Rechnungsadresse direkt mitsetzen.') }}</flux:subheading>
|
||||
</flux:card>
|
||||
|
||||
<flux:card>
|
||||
<flux:heading size="lg" class="mb-4">{{ __('Basisdaten') }}</flux:heading>
|
||||
|
||||
<div class="grid gap-4 sm:grid-cols-2">
|
||||
<flux:field>
|
||||
<flux:label>{{ __('Name') }}</flux:label>
|
||||
<flux:input wire:model="name" />
|
||||
<flux:error name="name" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<flux:label>{{ __('E-Mail') }}</flux:label>
|
||||
<flux:input wire:model="email" type="email" />
|
||||
<flux:error name="email" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<flux:label>{{ __('Portal') }}</flux:label>
|
||||
<flux:select wire:model="portal">
|
||||
@foreach($portalOptions as $portalOption)
|
||||
<option value="{{ $portalOption->value }}">{{ $portalOption->label() }}</option>
|
||||
@endforeach
|
||||
</flux:select>
|
||||
<flux:error name="portal" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<flux:label>{{ __('Registrierungstyp') }}</flux:label>
|
||||
<flux:select wire:model="registrationType">
|
||||
@foreach($registrationTypeOptions as $registrationTypeOption)
|
||||
<option value="{{ $registrationTypeOption->value }}">{{ $registrationTypeOption->label() }}</option>
|
||||
@endforeach
|
||||
</flux:select>
|
||||
<flux:error name="registrationType" />
|
||||
</flux:field>
|
||||
</div>
|
||||
|
||||
<div class="mt-4 flex flex-wrap gap-6">
|
||||
<flux:checkbox wire:model="isActive" label="{{ __('Aktiv') }}" />
|
||||
<flux:checkbox wire:model="isSuperAdmin" label="{{ __('Super Admin') }}" />
|
||||
</div>
|
||||
</flux:card>
|
||||
|
||||
<flux:card>
|
||||
<flux:heading size="lg" class="mb-4">{{ __('Rollenzuweisung') }}</flux:heading>
|
||||
<div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
||||
@foreach($availableRoles as $role)
|
||||
<flux:checkbox wire:model="selectedRoles" value="{{ $role->name }}" label="{{ $role->name }}" />
|
||||
@endforeach
|
||||
</div>
|
||||
<flux:error name="selectedRoles" class="mt-4" />
|
||||
</flux:card>
|
||||
|
||||
<flux:card>
|
||||
<flux:heading size="lg" class="mb-4">{{ __('Firmenverknüpfung') }}</flux:heading>
|
||||
|
||||
<div class="mb-4">
|
||||
<flux:select
|
||||
wire:model.live="selectedLookupCompanyId"
|
||||
variant="combobox"
|
||||
:filter="false"
|
||||
placeholder="{{ __('Firma suchen und hinzufügen…') }}"
|
||||
>
|
||||
<x-slot name="input">
|
||||
<flux:select.input
|
||||
wire:model.live.debounce.300ms="companyLookup"
|
||||
placeholder="{{ __('Name, Slug oder E-Mail…') }}"
|
||||
/>
|
||||
</x-slot>
|
||||
|
||||
@foreach($companyLookupResults as $company)
|
||||
<flux:select.option :value="$company->id" wire:key="create-company-{{ $company->id }}">
|
||||
{{ $company->name }}@if($company->email)<span class="ml-1 text-zinc-400">· {{ $company->email }}</span>@endif
|
||||
</flux:select.option>
|
||||
@endforeach
|
||||
|
||||
<x-slot name="empty">
|
||||
<flux:select.option.empty>
|
||||
@if(blank(trim($companyLookup)))
|
||||
{{ __('Mindestens 1 Zeichen eingeben…') }}
|
||||
@else
|
||||
{{ __('Keine Firma gefunden.') }}
|
||||
@endif
|
||||
</flux:select.option.empty>
|
||||
</x-slot>
|
||||
</flux:select>
|
||||
</div>
|
||||
|
||||
<div class="space-y-3">
|
||||
@forelse($linkedCompanies as $company)
|
||||
<div class="grid gap-3 rounded-lg border border-zinc-200 p-3 dark:border-zinc-700 sm:grid-cols-[1fr,160px,auto] sm:items-center">
|
||||
<div>
|
||||
<flux:text weight="semibold">{{ $company->name }}</flux:text>
|
||||
<flux:text class="text-sm text-zinc-500">{{ $company->slug }}</flux:text>
|
||||
</div>
|
||||
|
||||
<flux:select wire:model="companyRoles.{{ $company->id }}">
|
||||
<option value="member">{{ __('Member') }}</option>
|
||||
<option value="responsible">{{ __('Responsible') }}</option>
|
||||
<option value="owner">{{ __('Owner') }}</option>
|
||||
</flux:select>
|
||||
|
||||
<flux:button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
icon="x-mark"
|
||||
wire:click="removeLinkedCompany({{ $company->id }})"
|
||||
>
|
||||
{{ __('Entfernen') }}
|
||||
</flux:button>
|
||||
</div>
|
||||
@empty
|
||||
<flux:text class="text-sm text-zinc-500">{{ __('Noch keine Firma verknüpft.') }}</flux:text>
|
||||
@endforelse
|
||||
</div>
|
||||
</flux:card>
|
||||
|
||||
<flux:card>
|
||||
<flux:heading size="lg" class="mb-4">{{ __('Rechnungsadresse (optional)') }}</flux:heading>
|
||||
|
||||
<div class="grid gap-3 sm:grid-cols-2">
|
||||
<flux:field>
|
||||
<flux:label>{{ __('Anrede') }}</flux:label>
|
||||
<flux:select wire:model="billing.salutation_key">
|
||||
<option value="">{{ __('Bitte wählen') }}</option>
|
||||
@foreach($salutations as $key => $labels)
|
||||
<option value="{{ $key }}">{{ $labels['de'] ?? $key }}</option>
|
||||
@endforeach
|
||||
</flux:select>
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<flux:label>{{ __('Titel') }}</flux:label>
|
||||
<flux:input wire:model="billing.title" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field class="sm:col-span-2">
|
||||
<flux:label>{{ __('Rechnungsname') }}</flux:label>
|
||||
<flux:input wire:model="billing.name" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field class="sm:col-span-2">
|
||||
<flux:label>{{ __('Adresse Zeile 1') }}</flux:label>
|
||||
<flux:input wire:model="billing.address1" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field class="sm:col-span-2">
|
||||
<flux:label>{{ __('Adresse Zeile 2') }}</flux:label>
|
||||
<flux:input wire:model="billing.address2" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<flux:label>{{ __('PLZ') }}</flux:label>
|
||||
<flux:input wire:model="billing.postal_code" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<flux:label>{{ __('Stadt') }}</flux:label>
|
||||
<flux:input wire:model="billing.city" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<flux:label>{{ __('Land') }}</flux:label>
|
||||
<flux:select wire:model="billing.country_code">
|
||||
<option value="">{{ __('Bitte wählen') }}</option>
|
||||
@foreach($countries as $countryCode => $countryName)
|
||||
<option value="{{ $countryCode }}">{{ $countryName }}</option>
|
||||
@endforeach
|
||||
</flux:select>
|
||||
</flux:field>
|
||||
</div>
|
||||
</flux:card>
|
||||
|
||||
<flux:card>
|
||||
<div class="flex justify-end gap-3">
|
||||
<flux:button variant="ghost" href="{{ route('admin.users.index') }}" wire:navigate>
|
||||
{{ __('Abbrechen') }}
|
||||
</flux:button>
|
||||
<flux:button type="submit" variant="primary">
|
||||
{{ __('Benutzer anlegen') }}
|
||||
</flux:button>
|
||||
</div>
|
||||
</flux:card>
|
||||
</form>
|
||||
Loading…
Add table
Add a link
Reference in a new issue