From 64b16cd93e6e3e6ae48d12d712fdb299e8649c57 Mon Sep 17 00:00:00 2001 From: Kevin Adametz Date: Fri, 19 Jun 2026 08:59:38 +0000 Subject: [PATCH] Optimierung 2.5: Kontakt-Helfer + tagSuggestionsFor zentralisiert Erkenntnis: Die Admin-Inline-Query (Contact::withoutGlobalScopes()->where ->orderBy) ist exakt der Contact::forCompany()-Scope -> alle vier Kontakt-Helfer waren funktional identisch. companyContacts/companyContact/ defaultContactIdFor in neuen Concern InteractsWithPressReleaseContacts (ueber forCompany) ausgelagert. tagSuggestionsFor() (4x identisch) in InteractsWithPressReleaseTags verschoben. Ungenutzte Collection-Importe in den Customer-Editoren entfernt; selectedCompany() bleibt je Kontext (Admin: withoutGlobalScopes find; Customer: CustomerCompanyContext). Tests: volle Suite gruen (794 passed, 1 skipped). Co-Authored-By: Claude Opus 4.8 (1M context) --- .../InteractsWithPressReleaseContacts.php | 47 +++++++++++++++++++ .../InteractsWithPressReleaseTags.php | 29 ++++++++++++ .../admin/press-releases/create.blade.php | 45 +----------------- .../admin/press-releases/edit.blade.php | 45 +----------------- .../customer/press-releases/create.blade.php | 46 +----------------- .../customer/press-releases/edit.blade.php | 46 +----------------- 6 files changed, 80 insertions(+), 178 deletions(-) create mode 100644 app/Livewire/Concerns/InteractsWithPressReleaseContacts.php diff --git a/app/Livewire/Concerns/InteractsWithPressReleaseContacts.php b/app/Livewire/Concerns/InteractsWithPressReleaseContacts.php new file mode 100644 index 00000000..7e495bf0 --- /dev/null +++ b/app/Livewire/Concerns/InteractsWithPressReleaseContacts.php @@ -0,0 +1,47 @@ + + */ + private function companyContacts(int $companyId): Collection + { + if ($companyId <= 0) { + return new Collection; + } + + return Contact::forCompany($companyId) + ->get(['id', 'company_id', 'first_name', 'last_name', 'responsibility', 'phone', 'email']); + } + + private function companyContact(int $contactId, int $companyId): ?Contact + { + if ($contactId <= 0 || $companyId <= 0) { + return null; + } + + return Contact::forCompany($companyId)->whereKey($contactId)->first(); + } + + private function defaultContactIdFor(int $companyId): ?int + { + if ($companyId <= 0) { + return null; + } + + return $this->companyContacts($companyId)->first()?->id; + } +} diff --git a/app/Livewire/Concerns/InteractsWithPressReleaseTags.php b/app/Livewire/Concerns/InteractsWithPressReleaseTags.php index f7262221..4f3de16c 100644 --- a/app/Livewire/Concerns/InteractsWithPressReleaseTags.php +++ b/app/Livewire/Concerns/InteractsWithPressReleaseTags.php @@ -2,6 +2,8 @@ namespace App\Livewire\Concerns; +use App\Models\Company; + /** * Themen-Tag-Verwaltung für die PM-Editoren (Erstellen/Bearbeiten). * @@ -65,4 +67,31 @@ trait InteractsWithPressReleaseTags ->values() ->all(); } + + /** + * Tag-Vorschläge: Portal-Label und Länderbezug der Firma plus generische + * Defaults. Identisch in allen vier PM-Editoren. + * + * @return list + */ + private function tagSuggestionsFor(?Company $company): array + { + $defaults = [ + __('Mittelstand'), + __('Unternehmen'), + __('Eröffnung'), + __('Innovation'), + __('Nachhaltigkeit'), + ]; + + if (! $company) { + return $defaults; + } + + return array_values(array_unique(array_filter([ + $company->portal?->label(), + $company->country_code === 'DE' ? __('Deutschland') : null, + ...$defaults, + ]))); + } } diff --git a/resources/views/livewire/admin/press-releases/create.blade.php b/resources/views/livewire/admin/press-releases/create.blade.php index e40da60b..307edec6 100644 --- a/resources/views/livewire/admin/press-releases/create.blade.php +++ b/resources/views/livewire/admin/press-releases/create.blade.php @@ -19,6 +19,7 @@ use Livewire\Attributes\Title; use Livewire\Volt\Component; new #[Layout('components.layouts.app'), Title('Neue Pressemitteilung')] class extends Component { + use \App\Livewire\Concerns\InteractsWithPressReleaseContacts; use \App\Livewire\Concerns\InteractsWithPressReleaseScheduling; use \App\Livewire\Concerns\InteractsWithPressReleaseTags; use \App\Livewire\Concerns\ProvidesPressReleaseFormRules; @@ -209,50 +210,6 @@ new #[Layout('components.layouts.app'), Title('Neue Pressemitteilung')] class ex : null; } - private function defaultContactIdFor(int $companyId): ?int - { - if ($companyId <= 0) { - return null; - } - - return $this->companyContacts($companyId)->first()?->id; - } - - /** - * @return Collection - */ - private function companyContacts(int $companyId): Collection - { - return Contact::withoutGlobalScopes() - ->where('company_id', $companyId) - ->orderBy('last_name') - ->orderBy('first_name') - ->get(['id', 'company_id', 'first_name', 'last_name', 'responsibility', 'phone', 'email']); - } - - private function companyContact(int $contactId, int $companyId): ?Contact - { - if ($contactId <= 0 || $companyId <= 0) { - return null; - } - - return Contact::withoutGlobalScopes()->where('company_id', $companyId)->whereKey($contactId)->first(); - } - - /** - * @return list - */ - private function tagSuggestionsFor(?Company $company): array - { - $defaults = [__('Mittelstand'), __('Unternehmen'), __('Eröffnung'), __('Innovation'), __('Nachhaltigkeit')]; - - if (!$company) { - return $defaults; - } - - return array_values(array_unique(array_filter([$company->portal?->label(), $company->country_code === 'DE' ? __('Deutschland') : null, ...$defaults]))); - } - private function categoryOptions(): Collection { return app(AdminPerformanceCache::class)->remember(AdminPerformanceCache::ActiveCategoryOptions, AdminPerformanceCache::OptionsTtl, fn() => Category::query()->with('translations')->where('is_active', true)->orderBy('id')->get()); diff --git a/resources/views/livewire/admin/press-releases/edit.blade.php b/resources/views/livewire/admin/press-releases/edit.blade.php index e6864b01..4215f199 100644 --- a/resources/views/livewire/admin/press-releases/edit.blade.php +++ b/resources/views/livewire/admin/press-releases/edit.blade.php @@ -24,6 +24,7 @@ use Livewire\Attributes\Title; use Livewire\Volt\Component; new #[Layout('components.layouts.app'), Title('Pressemitteilung bearbeiten')] class extends Component { + use \App\Livewire\Concerns\InteractsWithPressReleaseContacts; use \App\Livewire\Concerns\InteractsWithPressReleaseScheduling; use \App\Livewire\Concerns\InteractsWithPressReleaseTags; use \App\Livewire\Concerns\ProvidesPressReleaseFormRules; @@ -398,50 +399,6 @@ new #[Layout('components.layouts.app'), Title('Pressemitteilung bearbeiten')] cl : null; } - private function defaultContactIdFor(int $companyId): ?int - { - if ($companyId <= 0) { - return null; - } - - return $this->companyContacts($companyId)->first()?->id; - } - - /** - * @return Collection - */ - private function companyContacts(int $companyId): Collection - { - return Contact::withoutGlobalScopes() - ->where('company_id', $companyId) - ->orderBy('last_name') - ->orderBy('first_name') - ->get(['id', 'company_id', 'first_name', 'last_name', 'responsibility', 'phone', 'email']); - } - - private function companyContact(int $contactId, int $companyId): ?Contact - { - if ($contactId <= 0 || $companyId <= 0) { - return null; - } - - return Contact::withoutGlobalScopes()->where('company_id', $companyId)->whereKey($contactId)->first(); - } - - /** - * @return list - */ - private function tagSuggestionsFor(?Company $company): array - { - $defaults = [__('Mittelstand'), __('Unternehmen'), __('Eröffnung'), __('Innovation'), __('Nachhaltigkeit')]; - - if (!$company) { - return $defaults; - } - - return array_values(array_unique(array_filter([$company->portal?->label(), $company->country_code === 'DE' ? __('Deutschland') : null, ...$defaults]))); - } - private function categoryOptions(): Collection { return app(AdminPerformanceCache::class)->remember(AdminPerformanceCache::ActiveCategoryOptions, AdminPerformanceCache::OptionsTtl, fn() => Category::query()->with('translations')->where('is_active', true)->orderBy('id')->get()); diff --git a/resources/views/livewire/customer/press-releases/create.blade.php b/resources/views/livewire/customer/press-releases/create.blade.php index 44ba12ef..dd2bde14 100644 --- a/resources/views/livewire/customer/press-releases/create.blade.php +++ b/resources/views/livewire/customer/press-releases/create.blade.php @@ -14,7 +14,6 @@ use App\Services\PressRelease\PressReleaseHtmlSanitizer; use App\Services\PressRelease\PressReleaseService; use App\Services\PressRelease\QuotaExceededException; use Flux\Flux; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Str; use Illuminate\Validation\Rule; use Livewire\Attributes\Computed; @@ -25,6 +24,7 @@ use Livewire\Volt\Component; new #[Layout('components.layouts.app'), Title('Neue Pressemitteilung')] class extends Component { + use \App\Livewire\Concerns\InteractsWithPressReleaseContacts; use \App\Livewire\Concerns\InteractsWithPressReleaseScheduling; use \App\Livewire\Concerns\InteractsWithPressReleaseTags; use \App\Livewire\Concerns\ProvidesPressReleaseFormRules; @@ -415,25 +415,6 @@ new #[Layout('components.layouts.app'), Title('Neue Pressemitteilung')] class ex return $this->tagsArray(); } - private function defaultContactIdFor(int $companyId): ?int - { - return $this->companyContacts($companyId)->first()?->id; - } - - /** - * @return Collection - */ - private function companyContacts(int $companyId): Collection - { - return Contact::forCompany($companyId) - ->get(['id', 'company_id', 'first_name', 'last_name', 'responsibility', 'phone', 'email']); - } - - private function companyContact(int $contactId, int $companyId): ?Contact - { - return Contact::forCompany($companyId)->whereKey($contactId)->first(); - } - private function selectedCompany(): ?Company { if (! $this->companyId) { @@ -444,31 +425,6 @@ new #[Layout('components.layouts.app'), Title('Neue Pressemitteilung')] class ex ->findFor(auth()->user(), (int) $this->companyId); } - /** - * @return list - */ - private function tagSuggestionsFor(?Company $company): array - { - $defaults = [ - __('Mittelstand'), - __('Unternehmen'), - __('Eröffnung'), - __('Innovation'), - __('Nachhaltigkeit'), - ]; - - if (! $company) { - return $defaults; - } - - $portalLabel = $company->portal?->label(); - - return array_values(array_unique(array_filter([ - $portalLabel, - $company->country_code === 'DE' ? __('Deutschland') : null, - ...$defaults, - ]))); - } }; ?>
diff --git a/resources/views/livewire/customer/press-releases/edit.blade.php b/resources/views/livewire/customer/press-releases/edit.blade.php index f7d85b59..def56397 100644 --- a/resources/views/livewire/customer/press-releases/edit.blade.php +++ b/resources/views/livewire/customer/press-releases/edit.blade.php @@ -15,7 +15,6 @@ use App\Services\PressRelease\PressReleaseHtmlSanitizer; use App\Services\PressRelease\PressReleaseService; use App\Services\PressRelease\QuotaExceededException; use Flux\Flux; -use Illuminate\Database\Eloquent\Collection; use Livewire\Attributes\Computed; use Livewire\Attributes\Layout; use Livewire\Attributes\Locked; @@ -25,6 +24,7 @@ use Livewire\Volt\Component; new #[Layout('components.layouts.app'), Title('Pressemitteilung bearbeiten')] class extends Component { + use \App\Livewire\Concerns\InteractsWithPressReleaseContacts; use \App\Livewire\Concerns\InteractsWithPressReleaseScheduling; use \App\Livewire\Concerns\InteractsWithPressReleaseTags; use \App\Livewire\Concerns\ProvidesPressReleaseFormRules; @@ -398,25 +398,6 @@ new #[Layout('components.layouts.app'), Title('Pressemitteilung bearbeiten')] cl return $this->tagsArray(); } - private function defaultContactIdFor(int $companyId): ?int - { - return $this->companyContacts($companyId)->first()?->id; - } - - /** - * @return Collection - */ - private function companyContacts(int $companyId): Collection - { - return Contact::forCompany($companyId) - ->get(['id', 'company_id', 'first_name', 'last_name', 'responsibility', 'phone', 'email']); - } - - private function companyContact(int $contactId, int $companyId): ?Contact - { - return Contact::forCompany($companyId)->whereKey($contactId)->first(); - } - private function getMyPR(): PressRelease { // Pro Livewire-Request memoisiert: mount(), with() und save() greifen @@ -435,31 +416,6 @@ new #[Layout('components.layouts.app'), Title('Pressemitteilung bearbeiten')] cl ->findFor(auth()->user(), (int) $this->companyId); } - /** - * @return list - */ - private function tagSuggestionsFor(?Company $company): array - { - $defaults = [ - __('Mittelstand'), - __('Unternehmen'), - __('Eröffnung'), - __('Innovation'), - __('Nachhaltigkeit'), - ]; - - if (! $company) { - return $defaults; - } - - $portalLabel = $company->portal?->label(); - - return array_values(array_unique(array_filter([ - $portalLabel, - $company->country_code === 'DE' ? __('Deutschland') : null, - ...$defaults, - ]))); - } }; ?>