belongsTo(GruppoServizio::class, 'gruppo_servizio_id'); } /** L'utenza, se è sorvegliante di un gruppo. */ public function utente() { return $this->hasOne(User::class, 'proclamatore_id'); } protected function casts(): array { return [ 'nome' => 'encrypted', 'cognome' => 'encrypted', 'attivo' => 'boolean', 'gruppo_servizio_id' => 'integer', ]; } public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->logOnly(['attivo']) // Do NOT log nome/cognome in audit (encrypted, GDPR) ->logOnlyDirty() ->dontSubmitEmptyLogs(); } /** * Full name (decrypted, only visible in PHP/UI). */ public function getNomeCompletoAttribute(): string { return trim($this->cognome . ' ' . $this->nome); } /** * L'opzione per il campo di scelta con ricerca (): si cerca per * cognome e nome. Nome e cognome sono cifrati nel database: la ricerca * avviene sui valori già decifrati, nella pagina, mai in SQL. */ public function opzioneScelta(?string $motivo = null): array { return array_filter([ 'valore' => (string) $this->id, 'etichetta' => $this->nome_completo, 'chiavi' => [(string) $this->cognome, (string) $this->nome], 'motivo' => $motivo, ], fn ($v) => $v !== null); } /** * Le opzioni di un elenco di proclamatori, in ordine di cognome e nome. * L'ordine si fa qui: in SQL ordinerebbe i testi cifrati. */ public static function opzioniScelta(iterable $proclamatori): array { return collect($proclamatori) ->sortBy(fn (self $p) => mb_strtolower($p->cognome . ' ' . $p->nome)) ->map(fn (self $p) => $p->opzioneScelta($p->attivo ? null : 'non attivo')) ->values() ->all(); } public function assegnazioni() { return $this->hasMany(Assegnazione::class, 'proclamatore_id'); } public function assegnazioniAperte() { return $this->hasMany(Assegnazione::class, 'proclamatore_id') ->whereNull('returned_at'); } public function scopeAttivi($query) { return $query->where('attivo', true); } /** * Anonymize this proclamatore (GDPR right to be forgotten). */ public function anonimizza(): void { $this->nome = 'Anonimo'; $this->cognome = 'Proclamatore #' . $this->id; $this->attivo = false; $this->save(); $this->delete(); // soft delete } }