Anzeigename eines Kontakts (Vor-/Nachname mit Fallback) als Model-Methode. Ersetzt die identische Inline-Logik an 6 eindeutigen Relation-$contact-Stellen (Admin: Benutzer-/PM-/Firmen-Detail; Kunde: Press-Kit-/PM-Detail). Bewusst noch offen: Select-/toBase-Varianten ($contactOption/$contactResult/ $lookupContact, stdClass-Risiko) sowie web/release-detail (User-Frontend). Test: ContactDisplayNameTest. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
87 lines
2.3 KiB
PHP
87 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Portal;
|
|
use App\Scopes\PortalScope;
|
|
use Database\Factories\ContactFactory;
|
|
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\SoftDeletes;
|
|
|
|
class Contact extends Model
|
|
{
|
|
/** @use HasFactory<ContactFactory> */
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope(new PortalScope);
|
|
}
|
|
|
|
/**
|
|
* Kontakte einer Firma, portal-unabhängig (ohne GlobalScope) und in der
|
|
* fürs Kunden-Panel üblichen Sortierung. Single Source of Truth für die
|
|
* Kontaktauswahl in Erstellen/Bearbeiten und für Kontakt-Zählungen.
|
|
*/
|
|
public function scopeForCompany(Builder $query, int $companyId): Builder
|
|
{
|
|
return $query->withoutGlobalScopes()
|
|
->where('company_id', $companyId)
|
|
->orderBy('last_name')
|
|
->orderBy('first_name');
|
|
}
|
|
|
|
protected $fillable = [
|
|
'company_id',
|
|
'portal',
|
|
'salutation_key',
|
|
'title',
|
|
'first_name',
|
|
'last_name',
|
|
'responsibility',
|
|
'phone',
|
|
'fax',
|
|
'email',
|
|
'legacy_portal',
|
|
'legacy_id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'portal' => Portal::class,
|
|
'deleted_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
/**
|
|
* Anzeigename eines Kontakts (Vor- + Nachname), mit Fallback, wenn beides
|
|
* leer ist. Single Source of Truth für die Listen-/Detail-Ansichten.
|
|
*/
|
|
public function displayName(): string
|
|
{
|
|
$name = trim(($this->first_name ?? '').' '.($this->last_name ?? ''));
|
|
|
|
return $name !== '' ? $name : __('Kontakt ohne Name');
|
|
}
|
|
|
|
public function pressReleases(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(PressRelease::class, 'press_release_contact');
|
|
}
|
|
|
|
public function users(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class)
|
|
->withTimestamps();
|
|
}
|
|
}
|