Primo commit
This commit is contained in:
45
app/Livewire/Territori/TerritorioCestino.php
Normal file
45
app/Livewire/Territori/TerritorioCestino.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Territori;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use App\Models\Territorio;
|
||||
|
||||
class TerritorioCestino extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public function restore(int $id)
|
||||
{
|
||||
$territorio = Territorio::onlyTrashed()->findOrFail($id);
|
||||
$territorio->restore();
|
||||
activity()->causedBy(auth()->user())
|
||||
->performedOn($territorio)
|
||||
->log('restored');
|
||||
session()->flash('success', "Territorio {$territorio->numero} ripristinato.");
|
||||
}
|
||||
|
||||
public function forceDelete(int $id)
|
||||
{
|
||||
$territorio = Territorio::onlyTrashed()->findOrFail($id);
|
||||
|
||||
if ($territorio->assegnazioni()->exists()) {
|
||||
session()->flash('error', 'Impossibile eliminare definitivamente: il territorio ha assegnazioni nello storico.');
|
||||
return;
|
||||
}
|
||||
|
||||
activity()->causedBy(auth()->user())
|
||||
->withProperties(['numero' => $territorio->numero])
|
||||
->log('force_deleted_territorio');
|
||||
$territorio->forceDelete();
|
||||
session()->flash('success', 'Territorio eliminato definitivamente.');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.territori.territorio-cestino', [
|
||||
'territori' => Territorio::onlyTrashed()->orderByDesc('deleted_at')->paginate(20),
|
||||
]);
|
||||
}
|
||||
}
|
||||
74
app/Livewire/Territori/TerritorioCreate.php
Normal file
74
app/Livewire/Territori/TerritorioCreate.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Territori;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
use App\Models\Territorio;
|
||||
use App\Models\Zona;
|
||||
use App\Models\Tipologia;
|
||||
|
||||
class TerritorioCreate extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public string $numero = '';
|
||||
public ?int $zona_id = null;
|
||||
public ?int $tipologia_id = null;
|
||||
public string $note = '';
|
||||
public string $confini = '';
|
||||
public $pdf;
|
||||
public bool $prioritario = false;
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'numero' => 'required|string|max:20|unique:territori,numero',
|
||||
'zona_id' => 'nullable|exists:zone,id',
|
||||
'tipologia_id' => 'nullable|exists:tipologie,id',
|
||||
'note' => 'nullable|string|max:5000',
|
||||
'confini' => 'nullable|string|max:5000',
|
||||
'pdf' => 'nullable|file|mimes:pdf|max:10240',
|
||||
'prioritario' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$data = [
|
||||
'numero' => $this->numero,
|
||||
'zona_id' => $this->zona_id,
|
||||
'tipologia_id' => $this->tipologia_id,
|
||||
'note' => $this->note ?: null,
|
||||
'confini' => $this->confini ?: null,
|
||||
'prioritario' => $this->prioritario,
|
||||
'attivo' => true,
|
||||
];
|
||||
|
||||
if ($this->pdf) {
|
||||
$data['pdf_path'] = $this->pdf->store('territori-pdf', 'public');
|
||||
}
|
||||
|
||||
$territorio = Territorio::create($data);
|
||||
|
||||
activity()->causedBy(auth()->user())
|
||||
->performedOn($territorio)
|
||||
->withProperties(['numero' => $territorio->numero])
|
||||
->log('created');
|
||||
|
||||
session()->flash('success', "Territorio {$territorio->numero} creato.");
|
||||
return redirect()->route('territori.index');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.territori.territorio-form', [
|
||||
'zone' => Zona::attive()->get(),
|
||||
'tipologie' => Tipologia::attive()->get(),
|
||||
'isEdit' => false,
|
||||
'title' => 'Nuovo Territorio',
|
||||
]);
|
||||
}
|
||||
}
|
||||
96
app/Livewire/Territori/TerritorioEdit.php
Normal file
96
app/Livewire/Territori/TerritorioEdit.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Territori;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
use App\Models\Territorio;
|
||||
use App\Models\Zona;
|
||||
use App\Models\Tipologia;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class TerritorioEdit extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public Territorio $territorio;
|
||||
public string $numero = '';
|
||||
public ?int $zona_id = null;
|
||||
public ?int $tipologia_id = null;
|
||||
public string $note = '';
|
||||
public string $confini = '';
|
||||
public $pdf;
|
||||
public bool $prioritario = false;
|
||||
|
||||
public function mount(Territorio $territorio)
|
||||
{
|
||||
$this->territorio = $territorio;
|
||||
$this->numero = $territorio->numero;
|
||||
$this->zona_id = $territorio->zona_id;
|
||||
$this->tipologia_id = $territorio->tipologia_id;
|
||||
$this->note = $territorio->note ?? '';
|
||||
$this->confini = $territorio->confini ?? '';
|
||||
$this->prioritario = $territorio->prioritario;
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'numero' => 'required|string|max:20|unique:territori,numero,' . $this->territorio->id,
|
||||
'zona_id' => 'nullable|exists:zone,id',
|
||||
'tipologia_id' => 'nullable|exists:tipologie,id',
|
||||
'note' => 'nullable|string|max:5000',
|
||||
'confini' => 'nullable|string|max:5000',
|
||||
'pdf' => 'nullable|file|mimes:pdf|max:10240',
|
||||
'prioritario' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$data = [
|
||||
'numero' => $this->numero,
|
||||
'zona_id' => $this->zona_id,
|
||||
'tipologia_id' => $this->tipologia_id,
|
||||
'note' => $this->note ?: null,
|
||||
'confini' => $this->confini ?: null,
|
||||
'prioritario' => $this->prioritario,
|
||||
];
|
||||
|
||||
if ($this->pdf) {
|
||||
// Remove old PDF
|
||||
if ($this->territorio->pdf_path) {
|
||||
Storage::disk('public')->delete($this->territorio->pdf_path);
|
||||
}
|
||||
$data['pdf_path'] = $this->pdf->store('territori-pdf', 'public');
|
||||
}
|
||||
|
||||
$this->territorio->update($data);
|
||||
|
||||
session()->flash('success', "Territorio {$this->territorio->numero} aggiornato.");
|
||||
return redirect()->route('territori.show', $this->territorio);
|
||||
}
|
||||
|
||||
public function removePdf()
|
||||
{
|
||||
if ($this->territorio->pdf_path) {
|
||||
Storage::disk('public')->delete($this->territorio->pdf_path);
|
||||
$this->territorio->update(['pdf_path' => null]);
|
||||
activity()->causedBy(auth()->user())
|
||||
->performedOn($this->territorio)
|
||||
->log('removed_pdf');
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.territori.territorio-form', [
|
||||
'zone' => Zona::attive()->get(),
|
||||
'tipologie' => Tipologia::attive()->get(),
|
||||
'isEdit' => true,
|
||||
'title' => "Modifica Territorio {$this->territorio->numero}",
|
||||
]);
|
||||
}
|
||||
}
|
||||
105
app/Livewire/Territori/TerritorioIndex.php
Normal file
105
app/Livewire/Territori/TerritorioIndex.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Territori;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use App\Models\Territorio;
|
||||
use App\Models\Zona;
|
||||
use App\Models\Tipologia;
|
||||
|
||||
class TerritorioIndex extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public string $search = '';
|
||||
public string $filterZona = '';
|
||||
public string $filterTipologia = '';
|
||||
public string $filterStato = '';
|
||||
public string $sortField = 'numero';
|
||||
public string $sortDirection = 'asc';
|
||||
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'filterZona' => ['except' => ''],
|
||||
'filterTipologia' => ['except' => ''],
|
||||
'filterStato' => ['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 = 'asc';
|
||||
}
|
||||
}
|
||||
|
||||
public function toggleActive(Territorio $territorio)
|
||||
{
|
||||
$territorio->update(['attivo' => !$territorio->attivo]);
|
||||
activity()->causedBy(auth()->user())
|
||||
->performedOn($territorio)
|
||||
->withProperties(['attivo' => $territorio->attivo])
|
||||
->log($territorio->attivo ? 'activated' : 'deactivated');
|
||||
}
|
||||
|
||||
public function togglePriority(Territorio $territorio)
|
||||
{
|
||||
$territorio->update(['prioritario' => !$territorio->prioritario]);
|
||||
activity()->causedBy(auth()->user())
|
||||
->performedOn($territorio)
|
||||
->withProperties(['prioritario' => $territorio->prioritario])
|
||||
->log($territorio->prioritario ? 'set_priority' : 'unset_priority');
|
||||
}
|
||||
|
||||
public function deleteTerritorio(Territorio $territorio)
|
||||
{
|
||||
activity()->causedBy(auth()->user())
|
||||
->performedOn($territorio)
|
||||
->log('soft_deleted');
|
||||
$territorio->delete();
|
||||
session()->flash('success', "Territorio {$territorio->numero} spostato nel cestino.");
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$query = Territorio::with(['zona', 'tipologia', 'assegnazioneCorrente.proclamatore']);
|
||||
|
||||
if ($this->search) {
|
||||
$query->where('numero', 'like', "%{$this->search}%");
|
||||
}
|
||||
|
||||
if ($this->filterZona) {
|
||||
$query->where('zona_id', $this->filterZona);
|
||||
}
|
||||
|
||||
if ($this->filterTipologia) {
|
||||
$query->where('tipologia_id', $this->filterTipologia);
|
||||
}
|
||||
|
||||
if ($this->filterStato) {
|
||||
match ($this->filterStato) {
|
||||
'in_reparto' => $query->inReparto(),
|
||||
'assegnato' => $query->assegnato(),
|
||||
'da_rientrare' => $query->daRientrare(),
|
||||
'inattivo' => $query->where('attivo', false),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
$query->orderBy($this->sortField, $this->sortDirection);
|
||||
|
||||
return view('livewire.territori.territorio-index', [
|
||||
'territori' => $query->paginate(20),
|
||||
'zone' => Zona::attive()->get(),
|
||||
'tipologie' => Tipologia::attive()->get(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
31
app/Livewire/Territori/TerritorioShow.php
Normal file
31
app/Livewire/Territori/TerritorioShow.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Territori;
|
||||
|
||||
use Livewire\Component;
|
||||
use App\Models\Territorio;
|
||||
use App\Models\Assegnazione;
|
||||
use App\Models\AnnoTeocratico;
|
||||
|
||||
class TerritorioShow extends Component
|
||||
{
|
||||
public Territorio $territorio;
|
||||
|
||||
public function mount(Territorio $territorio)
|
||||
{
|
||||
$this->territorio = $territorio->load(['zona', 'tipologia']);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$assegnazioni = Assegnazione::where('territorio_id', $this->territorio->id)
|
||||
->with(['proclamatore', 'annoTeocratico', 'campagna', 'creatoDa', 'rientratoDa'])
|
||||
->orderByDesc('assigned_at')
|
||||
->get()
|
||||
->groupBy(fn($a) => $a->annoTeocratico->label);
|
||||
|
||||
return view('livewire.territori.territorio-show', [
|
||||
'assegnazioniPerAnno' => $assegnazioni,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user