selectedExistingContactId) { $this->attachExistingContact(); } } public function mount(int $id): void { $this->id = $id; $companyExists = Company::query()->whereKey($id)->exists(); if (! $companyExists) { session()->flash('error', __('Die angeforderte Firma wurde nicht gefunden.')); $this->redirect(route('admin.companies.index'), navigate: true); return; } } public function setTab(string $tab): void { if (in_array($tab, ['overview', 'contacts'], true)) { $this->activeTab = $tab; } } public function updatedContactLookup(): void { $this->selectedExistingContactId = null; } public function attachExistingContact(): void { if (! $this->selectedExistingContactId) { return; } $company = Company::query()->find($this->id); if (! $company) { session()->flash('error', __('Die angeforderte Firma wurde nicht gefunden.')); return; } $contact = Contact::query()->find($this->selectedExistingContactId); if (! $contact) { session()->flash('error', __('Der ausgewählte Kontakt wurde nicht gefunden.')); return; } if ($contact->company_id === $company->id) { session()->flash('info', __('Der Kontakt ist bereits dieser Firma zugeordnet.')); return; } $contact->update(['company_id' => $company->id]); $this->contactLookup = ''; $this->selectedExistingContactId = null; session()->flash('success', __('Kontakt wurde der Firma zugeordnet.')); } public function with(): array { $company = Company::query() ->with([ 'pressReleases' => fn ($query) => $query->latest('id')->limit(10), 'users:id,name,email', ]) ->withCount(['pressReleases', 'contacts', 'users']) ->find($this->id); if (! $company) { return [ 'company' => Company::make([ 'id' => $this->id, 'name' => __('Unbekannte Firma'), 'is_active' => false, ]), 'filteredContacts' => collect(), 'filteredContactsTotal' => 0, 'contactLookupResults' => collect(), 'recentPressReleases' => collect(), ]; } $filteredContacts = collect(); $filteredContactsTotal = 0; if ($this->activeTab === 'contacts') { $contactsQuery = Contact::query() ->where('company_id', $company->id) ->when(filled($this->contactSearch), function ($query): void { $search = trim($this->contactSearch); if ($this->supportsFullTextSearch($search)) { $query->whereFullText(['first_name', 'last_name', 'email', 'responsibility'], $search); return; } $query->where(function ($query) use ($search): void { $query->where('first_name', 'like', '%'.$search.'%') ->orWhere('last_name', 'like', '%'.$search.'%') ->orWhere('email', 'like', '%'.$search.'%') ->orWhere('responsibility', 'like', '%'.$search.'%'); }); }); $filteredContactsTotal = (clone $contactsQuery)->count(); $filteredContacts = $contactsQuery ->orderBy('last_name') ->orderBy('first_name') ->limit(100) ->get(['id', 'company_id', 'portal', 'first_name', 'last_name', 'responsibility', 'email']); } $contactLookupResults = collect(); $lookupTerm = trim($this->contactLookup); if ($this->activeTab === 'contacts' && mb_strlen($lookupTerm) >= 1) { $contactLookupResults = Contact::withoutGlobalScopes() ->with('company:id,name') ->where('company_id', '!=', $company->id) ->where(function ($query) use ($lookupTerm): void { if ($this->supportsFullTextSearch($lookupTerm)) { $query->whereFullText(['first_name', 'last_name', 'email', 'responsibility'], $lookupTerm); return; } $query ->where('first_name', 'like', '%'.$lookupTerm.'%') ->orWhere('last_name', 'like', '%'.$lookupTerm.'%') ->orWhere('email', 'like', '%'.$lookupTerm.'%'); }) ->orderBy('last_name') ->orderBy('first_name') ->limit(50) ->get(['id', 'company_id', 'first_name', 'last_name', 'email']); } return [ 'company' => $company, 'filteredContacts' => $filteredContacts, 'filteredContactsTotal' => $filteredContactsTotal, 'contactLookupResults' => $contactLookupResults, 'recentPressReleases' => $company->pressReleases, ]; } private function supportsFullTextSearch(string $term): bool { return mb_strlen($term) >= 3 && in_array(DB::connection()->getDriverName(), ['mysql', 'pgsql'], true); } private function portalBadgeColor(?Portal $portal): string { return match ($portal) { Portal::Presseecho => 'blue', Portal::Businessportal24 => 'purple', Portal::Both => 'zinc', default => 'zinc', }; } }; ?>
@php($logoUrl = $company->logoUrl()) @if($logoUrl) {{ $company->name }} @else
@endif
{{ $company->name }}
@if($company->is_active) {{ __('Aktiv') }} @else {{ __('Inaktiv') }} @endif {{ $company->portal?->label() ?? __('Unbekannt') }} ID: {{ $company->id }}
{{ __('Bearbeiten') }} @if (\Illuminate\Support\Facades\Route::has('admin.companies.contacts.create')) {{ __('Kontakt hinzufügen') }} @endif
{{ __('Pressemitteilungen') }} {{ $company->press_releases_count }} {{ __('Kontakte') }} {{ $company->contacts_count }} {{ __('Verknüpfte Benutzer') }} {{ $company->users_count }}
{{ __('Überblick') }} {{ __('Kontakte') }}
@if($activeTab === 'overview')
{{ __('Kontaktinformationen') }}
{{ $company->email ?: __('Keine E-Mail hinterlegt') }} {{ $company->phone ?: __('Kein Telefon hinterlegt') }} {{ $company->website ?: __('Keine Website hinterlegt') }}
{{ __('Adresse') }}
{{ $company->address ?: __('Keine Adresse hinterlegt') }} {{ $company->country_code ?: __('Kein Land hinterlegt') }}
{{ __('Aktuelle Pressemitteilungen') }} {{ __('Alle anzeigen') }}
@forelse($recentPressReleases as $pressRelease) {{ $pressRelease->title ?? __('Ohne Titel') }} {{ $pressRelease->created_at?->format('d.m.Y') ?? '-' }} @empty {{ __('Keine Pressemitteilungen vorhanden') }} @endforelse
@endif @if($activeTab === 'contacts')
{{ __('Ansprechpartner') }} ({{ $filteredContactsTotal }})
@if (\Illuminate\Support\Facades\Route::has('admin.companies.contacts.create')) {{ __('Neu') }} @endif
{{ __('Bestehenden Kontakt zuordnen') }} @foreach($contactLookupResults as $lookupContact) @php($lookupName = trim(($lookupContact->first_name ?? '').' '.($lookupContact->last_name ?? '')) ?: __('Kontakt ohne Name')) {{ $lookupName }} @if($lookupContact->email) · {{ $lookupContact->email }} @endif · {{ $lookupContact->company?->name ?? __('Unbekannte Firma') }} @endforeach @if(blank(trim($contactLookup))) {{ __('Mindestens 1 Zeichen eingeben…') }} @else {{ __('Kein Kontakt gefunden.') }} @endif
@forelse($filteredContacts as $contact)
{{ trim(($contact->first_name ?? '').' '.($contact->last_name ?? '')) ?: __('Kontakt ohne Name') }} {{ $contact->portal?->label() ?? __('Unbekannt') }}
{{ $contact->responsibility ?: __('Keine Rolle hinterlegt') }} @if($contact->email) {{ $contact->email }} @endif
@if (\Illuminate\Support\Facades\Route::has('admin.contacts.edit')) @endif
@empty {{ __('Keine Kontakte gefunden') }} @endforelse
@if($filteredContactsTotal > $filteredContacts->count()) {{ __('Es werden die ersten :count Kontakte angezeigt. Bitte Suche eingrenzen, um weitere Treffer zu finden.', ['count' => $filteredContacts->count()]) }} @endif
@endif