gruppo = $gruppo; } private function servizio(): GruppiServizio { $this->authorize('settings.manage'); return app(GruppiServizio::class); } private function esegui(callable $azione, string $ok): void { try { $azione(); } catch (InvalidArgumentException $e) { $this->avvisaErrore($e->getMessage()); return; } $this->gruppo->refresh(); $this->avvisa($ok); } public function aggiungiMembri(): void { $ids = array_map('intval', $this->nuoviMembri); $this->esegui(fn () => $this->servizio()->aggiungiMembri($this->gruppo, $ids), count($ids) . ' membri aggiunti.'); $this->nuoviMembri = []; } public function togliMembro(int $proclamatoreId): void { $membro = Proclamatore::findOrFail($proclamatoreId); $this->esegui(fn () => $this->servizio()->togliMembro($this->gruppo, $membro), "{$membro->nome_completo} tolto dal gruppo."); } public function aggiungiTerritori(): void { $ids = array_map('intval', $this->nuoviTerritori); $this->esegui(fn () => $this->servizio()->aggiungiAlPacchetto($this->gruppo, $ids), count($ids) . ' territori aggiunti al pacchetto.'); $this->nuoviTerritori = []; } public function rimanda(int $territorioId): void { $territorio = Territorio::findOrFail($territorioId); $this->esegui(fn () => $this->servizio()->rimandaInReparto($this->gruppo, $territorio), "Territorio {$territorio->numero} rimandato in reparto."); } /** Il sorvegliante ha perso la password: una nuova temporanea, da cambiare al primo accesso. */ public function nuovaPassword(): void { $this->servizio(); $utente = User::where('proclamatore_id', $this->gruppo->sorvegliante_id)->first(); if (! $utente || $this->gruppo->sciolto_at) { $this->avvisaErrore('Il gruppo non ha un sorvegliante con utenza attiva.'); return; } $password = Str::password(12, symbols: false); $utente->forceFill(['password' => $password, 'password_change_required' => true])->save(); activity()->causedBy(auth()->user())->performedOn($utente)->log('password_reimpostata'); $this->passwordTemporanea = $password; } /** Toglie il ruolo di sorvegliante: il gruppo si scioglie. */ public function sciogli(): void { $this->esegui(fn () => $this->servizio()->sciogli($this->gruppo), "Gruppo {$this->gruppo->nome} sciolto."); $this->passwordTemporanea = null; } public function render() { $this->gruppo->load(['sorvegliante.utente']); $membri = $this->gruppo->membri()->get()->sortBy(fn ($p) => mb_strtolower($p->cognome . ' ' . $p->nome))->values(); $territori = $this->gruppo->territori()->with(['zona', 'assegnazioneCorrente.proclamatore'])->get()->sortBy('numero', SORT_NATURAL)->values(); $attivo = ! $this->gruppo->sciolto_at; return view('livewire.gruppi.gruppo-show', [ 'attivo' => $attivo, 'membri' => $membri, 'territori' => $territori, // Si possono aggiungere i proclamatori attivi senza gruppo... 'candidatiMembri' => $attivo ? Proclamatore::attivi()->whereNull('gruppo_servizio_id')->get()->sortBy(fn ($p) => mb_strtolower($p->cognome . ' ' . $p->nome))->values() : collect(), // ...e i territori in reparto (attivi, non assegnati, fuori da ogni pacchetto). 'candidatiTerritori' => $attivo ? Territorio::inReparto()->with('zona')->get()->sortBy('numero', SORT_NATURAL)->values() : collect(), 'assegnazioniDelGruppo' => $this->gruppo->assegnazioni()->count(), ]); } }