2026-05-26 08:14:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-05-26 11:47:36 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-05-26 08:14:29 +02:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Gruppo;
|
|
|
|
|
use App\Models\Individuo;
|
|
|
|
|
use App\Models\Diocesi;
|
|
|
|
|
use Illuminate\Http\Request;
|
2026-06-07 17:27:00 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2026-05-26 08:14:29 +02:00
|
|
|
|
|
|
|
|
class GruppoController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeRead('gruppi');
|
|
|
|
|
|
|
|
|
|
$viewMode = request('view', 'table');
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
|
|
$vista = \App\Models\VistaReport::where('user_id', $user->id)
|
|
|
|
|
->where('tipo', 'gruppi')
|
|
|
|
|
->where('is_default', true)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
$allColumns = ['nome', 'descrizione', 'diocesi', 'livello', 'padre', 'membri', 'responsabili', 'indirizzo', 'citta', 'telefono', 'email'];
|
|
|
|
|
$defaultVisible = ['nome', 'livello', 'padre', 'membri', 'responsabili'];
|
|
|
|
|
|
|
|
|
|
if ($vista && !empty($vista->colonne_visibili)) {
|
|
|
|
|
$visibleColumns = $vista->colonne_visibili;
|
|
|
|
|
} else {
|
|
|
|
|
$visibleColumns = $defaultVisible;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 11:47:36 +02:00
|
|
|
$allGruppi = Gruppo::with(['diocesi', 'parent', 'children', 'individui', 'avatar'])
|
2026-05-26 08:14:29 +02:00
|
|
|
->orderBy('nome')
|
|
|
|
|
->get()
|
|
|
|
|
->map(function ($gruppo) {
|
|
|
|
|
$depth = 0;
|
|
|
|
|
$parent = $gruppo->parent;
|
|
|
|
|
while ($parent) {
|
|
|
|
|
$depth++;
|
|
|
|
|
$parent = $parent->parent;
|
|
|
|
|
}
|
|
|
|
|
$gruppo->depth = $depth;
|
|
|
|
|
return $gruppo;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if ($viewMode === 'table') {
|
|
|
|
|
$gruppi = $this->sortGruppiHierarchically($allGruppi);
|
|
|
|
|
} else {
|
|
|
|
|
$gruppi = $allGruppi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('gruppi.index', compact(
|
|
|
|
|
'gruppi',
|
|
|
|
|
'allColumns',
|
|
|
|
|
'visibleColumns',
|
|
|
|
|
'viewMode',
|
|
|
|
|
'vista'
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function sortGruppiHierarchically($gruppi)
|
|
|
|
|
{
|
|
|
|
|
$rootGroups = $gruppi->filter(fn($g) => $g->parent_id === null)->sortBy('nome');
|
|
|
|
|
$sorted = collect();
|
|
|
|
|
|
|
|
|
|
foreach ($rootGroups as $root) {
|
|
|
|
|
$sorted->push($root);
|
|
|
|
|
$this->addChildrenRecursive($root, $gruppi, $sorted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $sorted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function addChildrenRecursive($parent, $allGruppi, &$sorted)
|
|
|
|
|
{
|
|
|
|
|
$children = $allGruppi->filter(fn($g) => $g->parent_id === $parent->id)->sortBy('nome');
|
|
|
|
|
|
|
|
|
|
foreach ($children as $child) {
|
|
|
|
|
$sorted->push($child);
|
|
|
|
|
$this->addChildrenRecursive($child, $allGruppi, $sorted);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeWrite('gruppi');
|
|
|
|
|
$diocesi = Diocesi::orderBy('nome')->get();
|
|
|
|
|
$allGruppi = Gruppo::orderBy('nome')->get();
|
|
|
|
|
$selectedParent = $request->query('parent_id') ? Gruppo::find($request->query('parent_id')) : null;
|
|
|
|
|
$individui = Individuo::orderBy('cognome')->orderBy('nome')->get();
|
|
|
|
|
return view('gruppi.create', compact('diocesi', 'allGruppi', 'selectedParent', 'individui'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeWrite('gruppi');
|
|
|
|
|
$data = $request->validate([
|
|
|
|
|
'nome' => 'required|string|max:255',
|
|
|
|
|
'descrizione' => 'nullable|string',
|
|
|
|
|
'parent_id' => 'nullable|exists:gruppi,id',
|
|
|
|
|
'diocesi_id' => 'nullable|exists:diocesi,id',
|
|
|
|
|
'responsabile_ids' => 'nullable|array',
|
|
|
|
|
'responsabile_ids.*' => 'exists:individui,id',
|
|
|
|
|
'indirizzo_incontro' => 'nullable|string|max:500',
|
|
|
|
|
'cap_incontro' => 'nullable|string|max:10',
|
|
|
|
|
'città_incontro' => 'nullable|string|max:255',
|
|
|
|
|
'sigla_provincia_incontro' => 'nullable|string|max:2',
|
|
|
|
|
'mappa_posizione' => 'nullable|string',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!empty($data['responsabile_ids'])) {
|
|
|
|
|
$data['responsabile_ids'] = json_encode(array_values($data['responsabile_ids']));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$gruppo = Gruppo::create($data);
|
|
|
|
|
|
|
|
|
|
if ($request->has('individui')) {
|
|
|
|
|
foreach ($request->individui as $individuoData) {
|
|
|
|
|
if (!empty($individuoData['individuo_id'])) {
|
|
|
|
|
$gruppo->individui()->attach($individuoData['individuo_id'], [
|
|
|
|
|
'ruolo_nel_gruppo' => $individuoData['ruolo_nel_gruppo'] ?? null,
|
|
|
|
|
'data_adesione' => $individuoData['data_adesione'] ?? null,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect('/gruppi/' . $gruppo->id)->with('success', 'Gruppo creato con successo.');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 11:47:36 +02:00
|
|
|
public function show($id)
|
2026-05-26 08:14:29 +02:00
|
|
|
{
|
|
|
|
|
$this->authorizeRead('gruppi');
|
|
|
|
|
$gruppo = Gruppo::with(['parent', 'children', 'diocesi', 'individui.contatti', 'individui.documenti', 'individui' => function ($q) {
|
|
|
|
|
$q->withPivot('ruolo_ids', 'ruolo_nel_gruppo', 'data_adesione');
|
2026-05-26 11:47:36 +02:00
|
|
|
}, 'avatar', 'eventi' => function ($q) {
|
2026-05-26 08:14:29 +02:00
|
|
|
$q->where('is_incontro_gruppo', true);
|
2026-05-26 11:47:36 +02:00
|
|
|
}, 'eventi.responsabili.contatti'])->findOrFail($id);
|
2026-05-26 08:14:29 +02:00
|
|
|
return view('gruppi.show', compact('gruppo'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 11:47:36 +02:00
|
|
|
public function edit($id)
|
2026-05-26 08:14:29 +02:00
|
|
|
{
|
|
|
|
|
$this->authorizeWrite('gruppi');
|
2026-05-26 11:47:36 +02:00
|
|
|
$gruppo = Gruppo::with(['individui.contatti', 'individui.documenti', 'avatar'])->findOrFail($id);
|
2026-05-26 08:14:29 +02:00
|
|
|
$diocesi = Diocesi::orderBy('nome')->get();
|
|
|
|
|
$gruppi = Gruppo::where('id', '!=', $gruppo->id)->orderBy('nome')->get();
|
|
|
|
|
$membri = $gruppo->individui()->withPivot('ruolo_ids', 'ruolo_nel_gruppo', 'data_adesione')->orderBy('cognome')->orderBy('nome')->get();
|
|
|
|
|
return view('gruppi.edit', compact('gruppo', 'diocesi', 'gruppi', 'membri'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 11:47:36 +02:00
|
|
|
public function update(Request $request, $id)
|
2026-05-26 08:14:29 +02:00
|
|
|
{
|
|
|
|
|
$this->authorizeWrite('gruppi');
|
2026-05-26 11:47:36 +02:00
|
|
|
$gruppo = Gruppo::findOrFail($id);
|
2026-05-26 08:14:29 +02:00
|
|
|
$data = $request->validate([
|
|
|
|
|
'nome' => 'required|string|max:255',
|
|
|
|
|
'descrizione' => 'nullable|string',
|
|
|
|
|
'parent_id' => 'nullable|exists:gruppi,id',
|
|
|
|
|
'diocesi_id' => 'nullable|exists:diocesi,id',
|
|
|
|
|
'responsabile_ids' => 'nullable|array',
|
|
|
|
|
'responsabile_ids.*' => 'exists:individui,id',
|
|
|
|
|
'indirizzo_incontro' => 'nullable|string|max:500',
|
|
|
|
|
'cap_incontro' => 'nullable|string|max:10',
|
|
|
|
|
'città_incontro' => 'nullable|string|max:255',
|
|
|
|
|
'sigla_provincia_incontro' => 'nullable|string|max:2',
|
|
|
|
|
'mappa_posizione' => 'nullable|string',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!empty($data['responsabile_ids'])) {
|
|
|
|
|
$data['responsabile_ids'] = json_encode(array_values($data['responsabile_ids']));
|
|
|
|
|
} else {
|
|
|
|
|
$data['responsabile_ids'] = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$gruppo->update($data);
|
|
|
|
|
|
|
|
|
|
if ($request->has('individui')) {
|
|
|
|
|
$gruppo->individui()->detach();
|
|
|
|
|
foreach ($request->individui as $individuoData) {
|
|
|
|
|
if (!empty($individuoData['individuo_id'])) {
|
|
|
|
|
$gruppo->individui()->attach($individuoData['individuo_id'], [
|
|
|
|
|
'ruolo_ids' => !empty($individuoData['ruolo_ids']) ? json_encode(array_values($individuoData['ruolo_ids'])) : null,
|
|
|
|
|
'data_adesione' => $individuoData['data_adesione'] ?? null,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect('/gruppi/' . $gruppo->id)->with('success', 'Gruppo aggiornato.');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 11:47:36 +02:00
|
|
|
public function destroy($id)
|
2026-05-26 08:14:29 +02:00
|
|
|
{
|
|
|
|
|
$this->authorizeDelete('gruppi');
|
2026-05-26 11:47:36 +02:00
|
|
|
$gruppo = Gruppo::with('children', 'individui')->findOrFail($id);
|
2026-05-26 08:14:29 +02:00
|
|
|
|
|
|
|
|
$hasChildren = $gruppo->children()->count() > 0;
|
|
|
|
|
$isSuperAdmin = auth()->user()->isSuperAdmin();
|
|
|
|
|
|
|
|
|
|
if ($hasChildren && !$isSuperAdmin) {
|
|
|
|
|
return redirect('/gruppi')->with('error', 'Solo il superamministratore può eliminare un gruppo con sottogruppi.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($hasChildren && $isSuperAdmin) {
|
|
|
|
|
foreach ($gruppo->children as $child) {
|
|
|
|
|
$this->destroyGruppo($child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->destroyGruppo($gruppo);
|
|
|
|
|
|
|
|
|
|
return redirect('/gruppi')->with('success', 'Gruppo eliminato.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function destroyGruppo(Gruppo $gruppo): void
|
|
|
|
|
{
|
|
|
|
|
$gruppo->individui()->detach();
|
|
|
|
|
$gruppo->eventi()->detach();
|
|
|
|
|
foreach ($gruppo->documenti as $documento) {
|
|
|
|
|
if ($documento->file_path && file_exists(storage_path('app/public/' . $documento->file_path))) {
|
|
|
|
|
unlink(storage_path('app/public/' . $documento->file_path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$gruppo->documenti()->delete();
|
|
|
|
|
$gruppo->delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function allGruppi()
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeRead('gruppi');
|
|
|
|
|
$gruppi = Gruppo::select('id', 'nome')
|
|
|
|
|
->orderBy('nome')
|
|
|
|
|
->get()
|
|
|
|
|
->map(function($g) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $g->id,
|
|
|
|
|
'nome' => $g->nome,
|
|
|
|
|
'full_path' => $g->getFullPathAttribute(),
|
|
|
|
|
];
|
|
|
|
|
});
|
|
|
|
|
return response()->json($gruppi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function individuiEmail(Gruppo $gruppo)
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeRead('gruppi');
|
|
|
|
|
$individui = $gruppo->individui()->with('contatti')->get();
|
|
|
|
|
|
|
|
|
|
$result = $individui->map(function($ind) {
|
|
|
|
|
$emails = $ind->contatti->where('tipo', 'email')->pluck('valore')->toArray();
|
|
|
|
|
return [
|
|
|
|
|
'id' => $ind->id,
|
|
|
|
|
'codice_id' => $ind->codice_id,
|
|
|
|
|
'cognome' => $ind->cognome,
|
|
|
|
|
'nome' => $ind->nome,
|
|
|
|
|
'emails' => $emails,
|
|
|
|
|
];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return response()->json($result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function saveVista(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$data = $request->validate([
|
|
|
|
|
'nome' => 'required|string|max:255',
|
|
|
|
|
'colonne_visibili' => 'nullable|array',
|
|
|
|
|
'colonne_visibili.*' => 'string',
|
|
|
|
|
'is_default' => 'nullable|boolean',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!empty($data['is_default'])) {
|
|
|
|
|
\App\Models\VistaReport::where('user_id', auth()->id())
|
|
|
|
|
->where('tipo', 'gruppi')
|
|
|
|
|
->where('is_default', true)
|
|
|
|
|
->update(['is_default' => false]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$vista = \App\Models\VistaReport::create([
|
|
|
|
|
'user_id' => auth()->id(),
|
|
|
|
|
'nome' => $data['nome'],
|
|
|
|
|
'tipo' => 'gruppi',
|
|
|
|
|
'colonne_visibili' => $data['colonne_visibili'] ?? [],
|
|
|
|
|
'is_default' => !empty($data['is_default']),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return response()->json(['success' => true, 'vista_id' => $vista->id]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 11:47:36 +02:00
|
|
|
public function deleteVista($id)
|
2026-05-26 08:14:29 +02:00
|
|
|
{
|
|
|
|
|
$vista = \App\Models\VistaReport::where('user_id', auth()->id())
|
2026-05-26 11:47:36 +02:00
|
|
|
->where('id', $id)
|
2026-05-26 08:14:29 +02:00
|
|
|
->firstOrFail();
|
|
|
|
|
$vista->delete();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('gruppi.index')->with('success', 'Vista eliminata.');
|
|
|
|
|
}
|
2026-06-07 17:27:00 +02:00
|
|
|
|
|
|
|
|
public function import()
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeWrite('gruppi');
|
|
|
|
|
return view('gruppi.import');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function importStore(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeWrite('gruppi');
|
|
|
|
|
$request->validate([
|
|
|
|
|
'file' => 'required|file|mimes:csv,txt',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$file = $request->file('file');
|
|
|
|
|
$handle = fopen($file->path(), 'r');
|
|
|
|
|
$header = fgetcsv($handle, 1000, ',');
|
|
|
|
|
|
|
|
|
|
if ($header === false) {
|
|
|
|
|
fclose($handle);
|
|
|
|
|
return back()->with('error', 'Il file CSV non è valido.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$header = array_map('trim', $header);
|
|
|
|
|
$imported = 0;
|
|
|
|
|
$skipped = 0;
|
|
|
|
|
$errors = [];
|
|
|
|
|
$rowNumber = 1;
|
|
|
|
|
|
|
|
|
|
while (($row = fgetcsv($handle, 1000, ',')) !== false) {
|
|
|
|
|
$rowNumber++;
|
|
|
|
|
|
|
|
|
|
if (count($row) < count($header)) {
|
|
|
|
|
$skipped++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = array_combine($header, $row);
|
|
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
|
$skipped++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = array_map('trim', $data);
|
|
|
|
|
$nome = $data['nome'] ?? '';
|
|
|
|
|
|
|
|
|
|
if (empty($nome)) {
|
|
|
|
|
$skipped++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Gruppo::create([
|
|
|
|
|
'nome' => $nome,
|
|
|
|
|
'descrizione' => $data['descrizione'] ?? null,
|
|
|
|
|
'parent_id' => !empty($data['parent_id']) ? (int) $data['parent_id'] : null,
|
|
|
|
|
'diocesi_id' => !empty($data['diocesi_id']) ? (int) $data['diocesi_id'] : null,
|
|
|
|
|
'indirizzo_incontro' => $data['indirizzo_incontro'] ?? null,
|
|
|
|
|
'cap_incontro' => $data['cap_incontro'] ?? null,
|
|
|
|
|
'città_incontro' => $data['città_incontro'] ?? null,
|
|
|
|
|
'sigla_provincia_incontro' => $data['sigla_provincia_incontro'] ?? null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$imported++;
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::warning('Errore import gruppo riga ' . $rowNumber . ': ' . $e->getMessage());
|
|
|
|
|
$errors[] = 'Errore riga ' . $rowNumber . ': ' . $e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
|
|
$message = 'Importati ' . $imported . ' gruppi.';
|
|
|
|
|
if ($skipped > 0) {
|
|
|
|
|
$message .= ' Saltate ' . $skipped . ' righe (vuote o incomplete).';
|
|
|
|
|
}
|
|
|
|
|
if (count($errors) > 0) {
|
|
|
|
|
return back()->with('error', $message . ' Errori: ' . implode('; ', $errors));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->route('gruppi.index')->with('success', $message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function downloadTemplate()
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeRead('gruppi');
|
|
|
|
|
$headers = ['nome', 'descrizione', 'parent_id', 'diocesi_id', 'indirizzo_incontro', 'cap_incontro', 'città_incontro', 'sigla_provincia_incontro'];
|
|
|
|
|
$example = ['Gruppo Giovani', 'Giovani dai 18 ai 30 anni', '', '', 'Via Roma 10', '00100', 'Roma', 'RM'];
|
|
|
|
|
|
|
|
|
|
$csv = implode(',', $headers) . "\n" . implode(',', $example);
|
|
|
|
|
|
|
|
|
|
return response($csv, 200, [
|
|
|
|
|
'Content-Type' => 'text/csv',
|
|
|
|
|
'Content-Disposition' => 'attachment; filename="template_gruppi.csv"',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-05-26 11:47:36 +02:00
|
|
|
}
|