Rechtlich oder redaktionell veranlasste Entfernungen brauchten bisher die Löschung — danach antwortet die URL mit 404, als hätte es die Meldung nie gegeben. Newsroom und Pressemitteilung lassen sich jetzt stattdessen mit gone_at markieren: Der Inhalt verschwindet aus allen öffentlichen Listen, die bestehende URL liefert aber einen Tombstone mit Status 410. Die vollständige Löschung bleibt unverändert daneben stehen und erzeugt weiterhin 404. Umkehrbar heißt hier wirklich umkehrbar. markGone() überschreibt zwei Felder — companies.is_active (damit der Newsroom ohne Sonderbehandlung aus Newsroom-Liste und Sitemap fällt, die beide darauf filtern) und press_releases.no_export. Beide Vorzustände werden gesichert und beim Aufheben zurückgestellt, sonst stünde eine schon vorher deaktivierte Firma danach öffentlich in der Übersicht und eine Mitteilung dauerhaft als "kein Export" da, ohne dass das je jemand gewählt hätte. Der Auslieferungs-Cache kennt keine Tags, und die Keys der Listenseiten sind über Portal, Sprache, Seite und Filter verstreut. Eine Entfernung erhöht deshalb eine Generations-Nummer im Cache-Key und verwirft damit alle Seiten-Caches auf einen Schlag — grob, aber Entfernungen sind selten und dürfen nicht bis zu einer Stunde nachhängen. Der Zähler liegt bewusst außerhalb des Namensraums, den er invalidiert, und wird zwischen zwei Queue-Jobs neu gelesen: Ein Worker lebt über beliebig viele Jobs und arbeitete sonst dauerhaft mit dem Stand seines ersten Zugriffs. Beim Newsroom läuft die Tombstone-Prüfung bewusst VOR dem Seiten-Cache — eine Entfernung muss sofort greifen, auch wenn für den Slug noch ein Stale-Eintrag liegt. Kaskadiert man die Entfernung auf die Meldungen einer Firma, wird der Cache einmal am Ende verworfen statt einmal je Meldung; sonst zwänge eine Bestandsfirma mit dreistellig vielen Meldungen die gesamte öffentliche Seite ebenso oft in den Neuaufbau. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
215 lines
6.3 KiB
PHP
215 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\CompanyType;
|
|
use App\Enums\Portal;
|
|
use App\Models\Concerns\HasUniqueSlug;
|
|
use App\Scopes\PortalScope;
|
|
use Database\Factories\CompanyFactory;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Company extends Model
|
|
{
|
|
/** @use HasFactory<CompanyFactory> */
|
|
use HasFactory, HasUniqueSlug, SoftDeletes;
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
protected function slugScopeAttributes(): array
|
|
{
|
|
return ['portal'];
|
|
}
|
|
|
|
protected function slugFallback(): string
|
|
{
|
|
return 'company';
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope(new PortalScope);
|
|
}
|
|
|
|
protected $fillable = [
|
|
'portal',
|
|
'owner_user_id',
|
|
'type',
|
|
'name',
|
|
'slug',
|
|
'address',
|
|
'country_code',
|
|
'phone',
|
|
'fax',
|
|
'email',
|
|
'website',
|
|
'logo_path',
|
|
'logo_variants',
|
|
'boilerplate',
|
|
'is_active',
|
|
'gone_at',
|
|
'is_active_before_gone',
|
|
'disable_footer_code',
|
|
'legacy_portal',
|
|
'legacy_id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'portal' => Portal::class,
|
|
'type' => CompanyType::class,
|
|
'logo_variants' => 'array',
|
|
'is_active' => 'boolean',
|
|
'gone_at' => 'datetime',
|
|
'is_active_before_gone' => 'boolean',
|
|
'disable_footer_code' => 'boolean',
|
|
'deleted_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Firmensuche für die Admin-Combobox: Volltext (name/email/slug) bei
|
|
* unterstützenden Treibern und Suchbegriffen ab 3 Zeichen, sonst LIKE.
|
|
* Bei leerem Begriff bleibt die Query unverändert. Single Source of Truth
|
|
* für die zuvor in mehreren Editoren duplizierte Such- und Treiberlogik.
|
|
*/
|
|
public function scopeSearchByTerm(Builder $query, string $term): Builder
|
|
{
|
|
$term = Portal::stripTrailingAbbreviation($term);
|
|
|
|
if (! filled($term)) {
|
|
return $query;
|
|
}
|
|
|
|
$supportsFullText = mb_strlen($term) >= 3
|
|
&& in_array(DB::connection()->getDriverName(), ['mysql', 'pgsql'], true);
|
|
|
|
return $query->where(function (Builder $query) use ($term, $supportsFullText): void {
|
|
if ($supportsFullText) {
|
|
$query->whereFullText(['name', 'email', 'slug'], $term);
|
|
|
|
return;
|
|
}
|
|
|
|
$query->where('name', 'like', '%'.$term.'%')
|
|
->orWhere('slug', 'like', '%'.$term.'%');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Firmen, auf die ein User zugreifen darf: als Owner (owner_user_id) oder
|
|
* als Mitglied über den company_user-Pivot. Single Source of Truth für die
|
|
* zuvor in mehreren API-Controllern und Services duplizierte Closure. Das
|
|
* Scope-Verhalten (mit/ohne globale Scopes) bestimmt der Aufrufer.
|
|
*/
|
|
public function scopeAccessibleBy(Builder $query, User $user): Builder
|
|
{
|
|
return $query->where(function (Builder $query) use ($user): void {
|
|
$query->where('owner_user_id', $user->id)
|
|
->orWhereHas('users', fn (Builder $users) => $users->whereKey($user->id));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Mit 410 entfernter Newsroom: die Newsroom-URL bleibt bestehen und liefert
|
|
* einen Tombstone statt eines 404. Gegenstück zur vollständigen Löschung
|
|
* (Soft Delete).
|
|
*/
|
|
public function isGone(): bool
|
|
{
|
|
return $this->gone_at !== null;
|
|
}
|
|
|
|
public function owner(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'owner_user_id');
|
|
}
|
|
|
|
public function users(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class)
|
|
->withPivot('role')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function contacts(): HasMany
|
|
{
|
|
return $this->hasMany(Contact::class);
|
|
}
|
|
|
|
public function pressReleases(): HasMany
|
|
{
|
|
return $this->hasMany(PressRelease::class);
|
|
}
|
|
|
|
public function userPaymentOptions(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(UserPaymentOption::class, 'user_payment_option_company')
|
|
->withPivot('is_active')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function logoUrl(): ?string
|
|
{
|
|
if (blank($this->logo_path)) {
|
|
return null;
|
|
}
|
|
|
|
$logoPath = trim((string) $this->logo_path);
|
|
$legacyPortal = $this->legacy_portal ?: $this->portal?->value;
|
|
$logoFilename = basename((string) (parse_url($logoPath, PHP_URL_PATH) ?: $logoPath));
|
|
$candidates = [];
|
|
|
|
if (Str::startsWith($logoPath, '/storage/')) {
|
|
$candidates[] = Str::after($logoPath, '/storage/');
|
|
} elseif (! Str::startsWith($logoPath, ['http://', 'https://'])) {
|
|
$candidates[] = ltrim($logoPath, '/');
|
|
}
|
|
|
|
if ($legacyPortal && filled($logoFilename)) {
|
|
$candidates[] = "company-logos/{$legacyPortal}/{$this->id}/{$logoFilename}";
|
|
$candidates[] = "{$legacyPortal}/company/{$logoFilename}";
|
|
}
|
|
|
|
foreach (array_unique($candidates) as $candidate) {
|
|
if (Storage::disk('public')->exists($candidate)) {
|
|
return asset('storage/'.$candidate);
|
|
}
|
|
}
|
|
|
|
if (Str::startsWith($logoPath, ['http://', 'https://']) && blank($this->legacy_portal)) {
|
|
return $logoPath;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Absolute Website-URL inklusive Schema. Bestandsdaten enthalten oft
|
|
* Adressen wie „www.example.de“ — als roher href würde der Browser
|
|
* daraus einen relativen Link auf der aktuellen Seite machen.
|
|
*/
|
|
public function websiteUrl(): ?string
|
|
{
|
|
if (blank($this->website)) {
|
|
return null;
|
|
}
|
|
|
|
$website = trim((string) $this->website);
|
|
|
|
return Str::startsWith($website, ['http://', 'https://'])
|
|
? $website
|
|
: 'https://'.$website;
|
|
}
|
|
}
|