companyId = $id; $company = Company::query()->find($id); if (! $company) { session()->flash('error', __('Die angeforderte Firma wurde nicht gefunden.')); $this->redirect(route('admin.companies.index'), navigate: true); return; } $this->portal = $company->portal?->value ?? Portal::Both->value; $this->allowBothPortal = $company->portal === Portal::Both || $company->portal === null; $this->company_name = $company->name; $this->description = ''; $this->email = $company->email ?? ''; $this->phone = $company->phone ?? ''; $this->website = $company->website ?? ''; $this->street = $company->address ?? ''; $this->zip = ''; $this->city = ''; $this->state = ''; $this->country = $company->country_code ?? 'DE'; $this->is_verified = false; $this->is_active = (bool) $company->is_active; $this->goneAt = $company->gone_at?->format('d.m.Y H:i'); $this->current_logo_url = $company->logoUrl(); } public function update(ImageService $imageService): void { $this->authorize('companies:write'); $this->validate(); $company = Company::query()->find($this->companyId); if (! $company) { session()->flash('error', __('Die angeforderte Firma wurde nicht gefunden.')); $this->redirect(route('admin.companies.index'), navigate: true); return; } $allowedPortals = [Portal::Presseecho->value, Portal::Businessportal24->value]; if ($company->portal === Portal::Both || $company->portal === null) { $allowedPortals[] = Portal::Both->value; } if (! in_array($this->portal, $allowedPortals, true)) { throw ValidationException::withMessages([ 'portal' => __('„Beide Portale" kann nicht mehr neu zugewiesen werden.'), ]); } $slug = $company->generateUniqueSlug($this->company_name, ['portal' => $this->portal]); $previousLogoPath = $company->logo_path; $previousLogoVariants = $company->logo_variants; $logoPath = $previousLogoPath; $logoVariants = $previousLogoVariants; $discardPreviousLogo = false; $stored = null; if ($this->remove_logo) { $logoPath = null; $logoVariants = null; $discardPreviousLogo = true; } if ($this->logo) { // Erst die neue Datei speichern, dann die DB-Referenz wechseln, das // alte Logo erst danach löschen: Scheitert der Storage, bleiben // bisheriges Logo und Datensatz unangetastet (F23). try { $stored = $imageService->storeCompanyLogo( $this->logo, $this->portal === Portal::Both->value ? Portal::Presseecho->value : $this->portal, $company->id, ); } catch (\RuntimeException $exception) { report($exception); throw ValidationException::withMessages([ 'logo' => __('Das Logo konnte nicht gespeichert werden. Bitte versuchen Sie es erneut.'), ]); } $logoPath = $stored['path']; $logoVariants = $stored['variants']; $discardPreviousLogo = true; } try { $company->update([ 'portal' => $this->portal, 'name' => $this->company_name, 'slug' => $slug, 'address' => $this->composeAddress(), 'country_code' => strtoupper($this->country), 'phone' => $this->phone ?: null, 'email' => $this->email ?: null, 'website' => $this->website ?: null, 'logo_path' => $logoPath, 'logo_variants' => $logoVariants, 'is_active' => $this->is_active, ]); } catch (\Throwable $exception) { // Ohne DB-Referenz wäre die neue Datei verwaist. if ($stored !== null) { $imageService->deleteCompanyLogo($stored['path'], $stored['variants']); } throw $exception; } if ($discardPreviousLogo && $previousLogoPath !== null && $previousLogoPath !== $logoPath) { $imageService->deleteCompanyLogo($previousLogoPath, $previousLogoVariants); } session()->flash('success', 'Firma erfolgreich aktualisiert.'); $this->redirect(route('admin.companies.index'), navigate: true); } public function with(): array { return [ 'countries' => collect([ ['code' => 'DE', 'name' => 'Deutschland'], ['code' => 'AT', 'name' => 'Österreich'], ['code' => 'CH', 'name' => 'Schweiz'], ['code' => 'FR', 'name' => 'Frankreich'], ['code' => 'GB', 'name' => 'Großbritannien'], ['code' => 'US', 'name' => 'USA'], ]), 'portalOptions' => array_filter( Portal::cases(), fn (Portal $portalCase): bool => $portalCase !== Portal::Both || $this->allowBothPortal, ), ]; } /** * Entfernung mit 410 statt Löschung: die Newsroom-URL liefert danach einen * Tombstone (410 Gone) statt eines 404. Umkehrbar über restoreGone(). */ public function markGone(CompanyRemovalService $removal): void { $this->authorize('companies:write'); $company = Company::query()->find($this->companyId); if (! $company) { session()->flash('error', __('Die angeforderte Firma wurde nicht gefunden.')); $this->redirect(route('admin.companies.index'), navigate: true); return; } $affected = $removal->markGone($company, $this->goneIncludesPressReleases); session()->flash('success', $affected > 0 ? __('Newsroom mit 410 entfernt — inklusive :count Pressemitteilung(en).', ['count' => $affected]) : __('Newsroom mit 410 entfernt. Die URL liefert jetzt 410 Gone.')); $this->redirect(route('admin.companies.index'), navigate: true); } public function restoreGone(CompanyRemovalService $removal): void { $this->authorize('companies:write'); $company = Company::query()->find($this->companyId); if (! $company) { session()->flash('error', __('Die angeforderte Firma wurde nicht gefunden.')); $this->redirect(route('admin.companies.index'), navigate: true); return; } $removal->restoreFromGone($company); session()->flash('success', __('410-Entfernung aufgehoben. Bereits entfernte Pressemitteilungen bleiben entfernt.')); $this->redirect(route('admin.companies.edit', $this->companyId), navigate: true); } public function deleteCompany(): void { $this->authorize('companies:write'); $company = Company::query()->find($this->companyId); if (! $company) { session()->flash('error', __('Die angeforderte Firma wurde nicht gefunden.')); $this->redirect(route('admin.companies.index'), navigate: true); return; } $company->delete(); session()->flash('success', __('Firma wurde erfolgreich gelöscht.')); $this->redirect(route('admin.companies.index'), navigate: true); } protected function composeAddress(): ?string { $lineOne = trim($this->street); $lineTwo = trim(trim($this->zip).' '.trim($this->city)); $lineThree = trim($this->state); $parts = array_filter([$lineOne, $lineTwo, $lineThree], fn ($value) => $value !== ''); return $parts !== [] ? implode(', ', $parts) : null; } }; ?>