- Gli elenchi tengono ricerca, filtri e ordinamento nell'indirizzo (#[Url]); la pagina c'era già. - I link verso schede e operazioni (data-ritorno) portano ?da= con la pagina di partenza, o il da già ricevuto: si risale all'origine (elenco -> scheda -> Rientra -> elenco filtrato). - Ritorno::valida accetta solo percorsi interni: niente rimandi esterni. - Trait RicordaRitorno: redirect dopo il salvataggio, Annulla e "Torna indietro" usano la pagina di partenza. Senza da, tutto come prima. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
5.3 KiB
PHP
159 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Proclamatori;
|
|
|
|
use App\Livewire\Concerns\Avvisa;
|
|
use Livewire\Attributes\Url;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
use App\Models\Proclamatore;
|
|
|
|
class ProclamatoreIndex extends Component
|
|
{
|
|
use Avvisa;
|
|
use WithPagination;
|
|
|
|
// Ricerca, filtro e ordinamento nell'indirizzo: si ritrovano tornando.
|
|
#[Url(except: '')]
|
|
public string $search = '';
|
|
#[Url(except: '')]
|
|
public string $filtroStato = '';
|
|
#[Url(except: 'cognome')]
|
|
public string $sortField = 'cognome';
|
|
#[Url(except: 'asc')]
|
|
public string $sortDirection = 'asc';
|
|
|
|
protected $queryString = [
|
|
'search' => ['except' => ''],
|
|
'filtroStato' => ['except' => ''],
|
|
];
|
|
|
|
public function updatingSearch()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function updatingFiltroStato()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function sortBy(string $field)
|
|
{
|
|
if ($this->sortField === $field) {
|
|
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
|
} else {
|
|
$this->sortField = $field;
|
|
$this->sortDirection = 'asc';
|
|
}
|
|
}
|
|
|
|
public function toggleActive(int $id)
|
|
{
|
|
// La lista è in sola lettura per i non-admin: creare/modificare/
|
|
// eliminare proclamatori è riservato all'amministratore. Il check sta
|
|
// nel metodo perché la rotta index resta accessibile con
|
|
// proclamatori.manage.
|
|
$this->authorize('settings.manage');
|
|
|
|
$proclamatore = Proclamatore::findOrFail($id);
|
|
$proclamatore->update(['attivo' => !$proclamatore->attivo]);
|
|
}
|
|
|
|
public function deleteProclamatore(int $id)
|
|
{
|
|
$this->authorize('settings.manage');
|
|
|
|
$proclamatore = Proclamatore::findOrFail($id);
|
|
|
|
if ($proclamatore->assegnazioni()->aperte()->exists()) {
|
|
$this->avvisaErrore("Il proclamatore {$proclamatore->nome_completo} ha assegnazioni aperte. Rientra prima i territori.");
|
|
return;
|
|
}
|
|
|
|
$proclamatore->delete();
|
|
$this->avvisa("Proclamatore {$proclamatore->nome_completo} spostato nel cestino.");
|
|
}
|
|
|
|
public function anonimizza(int $id)
|
|
{
|
|
$this->authorize('settings.manage');
|
|
|
|
$proclamatore = Proclamatore::findOrFail($id);
|
|
$proclamatore->anonimizza();
|
|
$this->avvisa("Dati personali del proclamatore anonimizzati (GDPR).");
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
// withCount rather than letting the view call
|
|
// ->assegnazioni()->aperte()->count() per row, which issued one
|
|
// COUNT(*) per displayed proclamatore on every render.
|
|
$query = Proclamatore::query()->withCount([
|
|
'assegnazioni as assegnazioni_aperte_count' => fn ($q) => $q->whereNull('returned_at'),
|
|
]);
|
|
|
|
if ($this->search !== '') {
|
|
// In-memory filter because nome/cognome are encrypted
|
|
$all = $query->get();
|
|
$filtered = $all->filter(function ($p) {
|
|
return str_contains(
|
|
mb_strtolower($p->nome . ' ' . $p->cognome),
|
|
mb_strtolower($this->search)
|
|
);
|
|
});
|
|
|
|
if ($this->filtroStato === 'attivo') {
|
|
$filtered = $filtered->where('attivo', true);
|
|
} elseif ($this->filtroStato === 'inattivo') {
|
|
$filtered = $filtered->where('attivo', false);
|
|
}
|
|
|
|
// Sort in-memory
|
|
$filtered = $filtered->sortBy(function ($p) {
|
|
return match ($this->sortField) {
|
|
'nome' => mb_strtolower($p->nome),
|
|
'cognome' => mb_strtolower($p->cognome),
|
|
default => mb_strtolower($p->cognome),
|
|
};
|
|
}, SORT_REGULAR, $this->sortDirection === 'desc');
|
|
|
|
// Manual pagination
|
|
$page = $this->getPage();
|
|
$perPage = 20;
|
|
$items = $filtered->slice(($page - 1) * $perPage, $perPage)->values();
|
|
$proclamatori = new \Illuminate\Pagination\LengthAwarePaginator(
|
|
$items, $filtered->count(), $perPage, $page,
|
|
['path' => request()->url()]
|
|
);
|
|
} else {
|
|
if ($this->filtroStato === 'attivo') {
|
|
$query->where('attivo', true);
|
|
} elseif ($this->filtroStato === 'inattivo') {
|
|
$query->where('attivo', false);
|
|
}
|
|
|
|
// cognome/nome encrypted, so sort in-memory
|
|
$all = $query->get()->sortBy(function ($p) {
|
|
return match ($this->sortField) {
|
|
'nome' => mb_strtolower($p->nome),
|
|
'cognome' => mb_strtolower($p->cognome),
|
|
default => mb_strtolower($p->cognome),
|
|
};
|
|
}, SORT_REGULAR, $this->sortDirection === 'desc');
|
|
|
|
$page = $this->getPage();
|
|
$perPage = 20;
|
|
$items = $all->slice(($page - 1) * $perPage, $perPage)->values();
|
|
$proclamatori = new \Illuminate\Pagination\LengthAwarePaginator(
|
|
$items, $all->count(), $perPage, $page,
|
|
['path' => request()->url()]
|
|
);
|
|
}
|
|
|
|
return view('livewire.proclamatori.proclamatore-index', [
|
|
'proclamatori' => $proclamatori,
|
|
]);
|
|
}
|
|
}
|