id = $id; $contact = Contact::query()->findOrFail($id); $this->companyId = $contact->company_id; $this->portal = $contact->portal?->value ?? Portal::Both->value; $this->salutationKey = $contact->salutation_key; $this->title = $contact->title; $this->firstName = $contact->first_name; $this->lastName = $contact->last_name; $this->responsibility = $contact->responsibility; $this->phone = $contact->phone; $this->fax = $contact->fax; $this->email = $contact->email; } public function save(): void { $validated = $this->validate([ 'companyId' => ['required', 'integer', Rule::exists('companies', 'id')], 'portal' => ['required', Rule::in(array_map(static fn (Portal $portal): string => $portal->value, Portal::cases()))], 'salutationKey' => ['nullable', 'string', 'max:20'], 'title' => ['nullable', 'string', 'max:80'], 'firstName' => ['nullable', 'string', 'max:80'], 'lastName' => ['nullable', 'string', 'max:80'], 'responsibility' => ['nullable', 'string', 'max:255'], 'phone' => ['nullable', 'string', 'max:40'], 'fax' => ['nullable', 'string', 'max:40'], 'email' => ['nullable', 'email', 'max:255'], ]); $contact = Contact::query()->findOrFail($this->id); $contact->update([ 'company_id' => (int) $validated['companyId'], 'portal' => $validated['portal'], 'salutation_key' => $validated['salutationKey'] ?: null, 'title' => $validated['title'] ?: null, 'first_name' => $validated['firstName'] ?: null, 'last_name' => $validated['lastName'] ?: null, 'responsibility' => $validated['responsibility'] ?: null, 'phone' => $validated['phone'] ?: null, 'fax' => $validated['fax'] ?: null, 'email' => $validated['email'] ?: null, ]); session()->flash('success', __('Kontakt wurde aktualisiert.')); $this->redirect(route('admin.contacts.index'), navigate: true); } public function updatedCompanySearch(): void { $this->resetErrorBag('companyId'); } public function updatedCompanyId(): void { if (! $this->companyId) { return; } $company = Company::withoutGlobalScopes()->find((int) $this->companyId); if ($company) { $this->portal = $company->portal?->value ?? Portal::Both->value; } } public function with(): array { $term = trim($this->companySearch); $companies = Company::withoutGlobalScopes() ->when(filled($term), function ($q) use ($term): void { $q->where(function ($q) use ($term): void { if ($this->supportsFullTextSearch($term)) { $q->whereFullText(['name', 'email', 'slug'], $term); return; } $q->where('name', 'like', '%'.$term.'%') ->orWhere('slug', 'like', '%'.$term.'%'); }); }) ->when(blank($term) && $this->companyId, function ($q): void { // Aktuell gewählte Firma immer einschließen, damit Label + Modal-Text korrekt sind $q->whereIn('id', [(int) $this->companyId]); }) ->when(blank($term) && ! $this->companyId, function ($q): void { $q->whereRaw('0 = 1'); }) ->orderBy('name') ->limit(50) ->get(['id', 'name']); return [ 'companies' => $companies, 'salutations' => config('salutations.items', []), 'portalOptions' => Portal::cases(), ]; } private function supportsFullTextSearch(string $term): bool { return mb_strlen($term) >= 3 && in_array(DB::connection()->getDriverName(), ['mysql', 'pgsql'], true); } public function deleteContact(): void { $contact = Contact::query()->find($this->id); if (! $contact) { session()->flash('error', __('Der angeforderte Kontakt wurde nicht gefunden.')); $this->redirect(route('admin.contacts.index'), navigate: true); return; } $contact->delete(); session()->flash('success', __('Kontakt wurde gelöscht.')); $this->redirect(route('admin.contacts.index'), navigate: true); } private function currentPortalLabel(): string { return Portal::tryFrom($this->portal)?->label() ?? __('Unbekannt'); } private function currentPortalBadgeColor(): string { return match (Portal::tryFrom($this->portal)) { Portal::Presseecho => 'blue', Portal::Businessportal24 => 'purple', Portal::Both => 'zinc', default => 'zinc', }; } }; ?>
{{-- ============== PAGE HEADER ============== --}}
{{ __('Zuordnung') }}
{{ __('Firma') }} @foreach ($companies as $company) {{ $company->name }} @endforeach @if (blank(trim($companySearch))) {{ __('Mindestens 1 Zeichen eingeben…') }} @else {{ __('Keine Firma gefunden.') }} @endif {{ __('Portal') }} @foreach ($portalOptions as $portalOption) @endforeach
{{ __('Kontaktdaten') }}
{{ __('Anrede') }} @foreach ($salutations as $key => $labels) @endforeach {{ __('Titel') }} {{ __('Vorname') }} {{ __('Nachname') }} {{ __('Verantwortlichkeit') }} {{ __('E-Mail') }} {{ __('Telefon') }} {{ __('Fax') }}
{{ __('Danger Zone & Aktionen') }}
{{ __('Löschen') }}
{{ __('Abbrechen') }} {{ __('Speichern') }}
@php $contactDisplayName = trim(($firstName ?? '').' '.($lastName ?? '')) ?: __('Kontakt ohne Name'); $selectedCompanyName = $companies->firstWhere('id', (int) $companyId)?->name ?? __('Unbekannte Firma'); @endphp
{{ __('Kontakt wirklich löschen?') }} {{ __('Du löschst: :contact (Firma: :company). Dieser Kontakt wird archiviert (Soft Delete) und aus den Standardlisten entfernt.', ['contact' => $contactDisplayName, 'company' => $selectedCompanyName]) }}
{{ __('Abbrechen') }} {{ __('Löschung bestätigen') }}