Primo commit
This commit is contained in:
101
app/Livewire/Registro.php
Normal file
101
app/Livewire/Registro.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use App\Models\Assegnazione;
|
||||
use App\Models\AnnoTeocratico;
|
||||
use App\Models\Zona;
|
||||
use App\Models\Tipologia;
|
||||
|
||||
class Registro extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public string $search = '';
|
||||
public string $filtroAnno = '';
|
||||
public string $filtroZona = '';
|
||||
public string $filtroTipologia = '';
|
||||
public string $filtroStato = ''; // aperte, chiuse
|
||||
public string $sortField = 'assigned_at';
|
||||
public string $sortDirection = 'desc';
|
||||
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'filtroAnno' => ['except' => ''],
|
||||
'filtroZona' => ['except' => ''],
|
||||
'filtroTipologia' => ['except' => ''],
|
||||
'filtroStato' => ['except' => ''],
|
||||
];
|
||||
|
||||
public function updatingSearch()
|
||||
{
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function sortBy(string $field)
|
||||
{
|
||||
if ($this->sortField === $field) {
|
||||
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
||||
} else {
|
||||
$this->sortField = $field;
|
||||
$this->sortDirection = 'desc';
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$query = Assegnazione::with(['territorio.zona', 'proclamatore', 'annoTeocratico', 'campagna']);
|
||||
|
||||
if ($this->filtroAnno) {
|
||||
$query->where('anno_teocratico_id', $this->filtroAnno);
|
||||
}
|
||||
|
||||
if ($this->filtroStato === 'aperte') {
|
||||
$query->aperte();
|
||||
} elseif ($this->filtroStato === 'chiuse') {
|
||||
$query->chiuse();
|
||||
}
|
||||
|
||||
if ($this->filtroZona) {
|
||||
$query->whereHas('territorio', fn($q) => $q->where('zona_id', $this->filtroZona));
|
||||
}
|
||||
|
||||
if ($this->filtroTipologia) {
|
||||
$query->whereHas('territorio', fn($q) => $q->where('tipologia_id', $this->filtroTipologia));
|
||||
}
|
||||
|
||||
$query->orderBy($this->sortField, $this->sortDirection);
|
||||
|
||||
// In-memory search for encrypted proclamatore fields / territorio numero
|
||||
if ($this->search !== '') {
|
||||
$all = $query->get();
|
||||
$filtered = $all->filter(function ($a) {
|
||||
$haystack = mb_strtolower(
|
||||
($a->territorio?->numero ?? '') . ' ' .
|
||||
($a->proclamatore?->nome ?? '') . ' ' .
|
||||
($a->proclamatore?->cognome ?? '')
|
||||
);
|
||||
return str_contains($haystack, mb_strtolower($this->search));
|
||||
});
|
||||
|
||||
$page = $this->getPage();
|
||||
$perPage = 25;
|
||||
$items = $filtered->slice(($page - 1) * $perPage, $perPage)->values();
|
||||
$assegnazioni = new \Illuminate\Pagination\LengthAwarePaginator(
|
||||
$items, $filtered->count(), $perPage, $page,
|
||||
['path' => request()->url()]
|
||||
);
|
||||
} else {
|
||||
$assegnazioni = $query->paginate(25);
|
||||
}
|
||||
|
||||
return view('livewire.registro', [
|
||||
'assegnazioni' => $assegnazioni,
|
||||
'anni' => AnnoTeocratico::orderByDesc('start_date')->get(),
|
||||
'zone' => Zona::attive()->orderBy('nome')->get(),
|
||||
'tipologie' => Tipologia::orderBy('nome')->get(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user