313 lines
9.8 KiB
PHP
313 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\AppSetting;
|
|
use App\Models\EmailSetting;
|
|
use App\Models\Ruolo;
|
|
use App\Models\TipologiaDocumento;
|
|
use App\Models\TipologiaEvento;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ImpostazioniController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$tipologie = TipologiaDocumento::orderBy('ordine')->get();
|
|
$tipologieEventi = TipologiaEvento::orderBy('ordine')->get();
|
|
$ruoli = Ruolo::orderBy('ordine')->get();
|
|
$appSettings = AppSetting::first();
|
|
$emailSettings = EmailSetting::first() ?? new EmailSetting();
|
|
|
|
return view('impostazioni.index', compact('tipologie', 'tipologieEventi', 'ruoli', 'appSettings', 'emailSettings'));
|
|
}
|
|
|
|
public function saveAppSettings(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$validated = $request->validate([
|
|
'nome_applicazione' => 'nullable|string|max:100',
|
|
'nome_organizzazione' => 'nullable|string|max:255',
|
|
'footer_text' => 'nullable|string|max:255',
|
|
'app_version' => 'nullable|string|max:50',
|
|
'dashboard_welcome' => 'nullable|string|max:1000',
|
|
'show_version' => 'boolean',
|
|
]);
|
|
|
|
$settings = AppSetting::first() ?? new AppSetting();
|
|
|
|
$settings->nome_applicazione = $validated['nome_applicazione'] ?? null;
|
|
$settings->nome_organizzazione = $validated['nome_organizzazione'] ?? null;
|
|
$settings->footer_text = $validated['footer_text'] ?? null;
|
|
$settings->app_version = $validated['app_version'] ?? null;
|
|
$settings->dashboard_welcome = $validated['dashboard_welcome'] ?? null;
|
|
$settings->show_version = $request->boolean('show_version');
|
|
|
|
$settings->save();
|
|
|
|
return redirect('/impostazioni#applicazione')->with('success', 'Impostazioni applicazione salvate.');
|
|
}
|
|
|
|
public function tipologieStore(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$validated = $request->validate([
|
|
'nome' => 'required|string|max:50|unique:tipologie_documenti,nome',
|
|
'descrizione' => 'nullable|string|max:255',
|
|
]);
|
|
|
|
$maxOrdine = TipologiaDocumento::max('ordine') ?? 0;
|
|
|
|
TipologiaDocumento::create([
|
|
'nome' => $validated['nome'],
|
|
'descrizione' => $validated['descrizione'] ?? null,
|
|
'ordine' => $maxOrdine + 1,
|
|
'attiva' => true,
|
|
]);
|
|
|
|
return redirect('/impostazioni#tipologie')->with('success', 'Tipologia aggiunta.');
|
|
}
|
|
|
|
public function tipologieUpdate(Request $request, $id)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$tipologia = TipologiaDocumento::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'nome' => 'required|string|max:50|unique:tipologie_documenti,nome,' . $id,
|
|
'descrizione' => 'nullable|string|max:255',
|
|
'attiva' => 'boolean',
|
|
]);
|
|
|
|
$tipologia->update($validated);
|
|
|
|
return redirect('/impostazioni#tipologie')->with('success', 'Tipologia aggiornata.');
|
|
}
|
|
|
|
public function tipologieDestroy($id)
|
|
{
|
|
$this->authorizeDelete('settings');
|
|
$tipologia = TipologiaDocumento::findOrFail($id);
|
|
|
|
$count = $tipologia->documenti()->count();
|
|
if ($count > 0) {
|
|
return redirect('/impostazioni#tipologie')->with('error', 'Impossibile eliminare: ' . $count . ' documenti associati.');
|
|
}
|
|
|
|
$tipologia->delete();
|
|
|
|
return redirect('/impostazioni#tipologie')->with('success', 'Tipologia eliminata.');
|
|
}
|
|
|
|
public function tipologieReorder(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$order = $request->input('order', []);
|
|
|
|
foreach ($order as $index => $id) {
|
|
TipologiaDocumento::where('id', $id)->update(['ordine' => $index]);
|
|
}
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
public function ruoliStore(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$validated = $request->validate([
|
|
'nome' => 'required|string|max:50|unique:ruoli,nome',
|
|
'descrizione' => 'nullable|string|max:255',
|
|
]);
|
|
|
|
$maxOrdine = Ruolo::max('ordine') ?? 0;
|
|
|
|
Ruolo::create([
|
|
'nome' => $validated['nome'],
|
|
'descrizione' => $validated['descrizione'] ?? null,
|
|
'ordine' => $maxOrdine + 1,
|
|
'attiva' => true,
|
|
]);
|
|
|
|
return redirect('/impostazioni#ruoli')->with('success', 'Ruolo aggiunto.');
|
|
}
|
|
|
|
public function ruoliUpdate(Request $request, $id)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$ruolo = Ruolo::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'nome' => 'required|string|max:50|unique:ruoli,nome,' . $id,
|
|
'descrizione' => 'nullable|string|max:255',
|
|
'attiva' => 'boolean',
|
|
]);
|
|
|
|
$ruolo->update($validated);
|
|
|
|
return redirect('/impostazioni#ruoli')->with('success', 'Ruolo aggiornato.');
|
|
}
|
|
|
|
public function ruoliDestroy($id)
|
|
{
|
|
$this->authorizeDelete('settings');
|
|
|
|
$ruolo = Ruolo::findOrFail($id);
|
|
|
|
$count = \DB::table('gruppo_individuo')
|
|
->whereNotNull('ruolo_ids')
|
|
->whereRaw("JSON_CONTAINS(ruolo_ids, ?)", [json_encode($id)])
|
|
->count();
|
|
|
|
if ($count > 0) {
|
|
return redirect('/impostazioni#ruoli')->with('error', 'Impossibile eliminare: ' . $count . ' membri associati a questo ruolo.');
|
|
}
|
|
|
|
$ruolo->delete();
|
|
|
|
return redirect('/impostazioni#ruoli')->with('success', 'Ruolo eliminato.');
|
|
}
|
|
|
|
public function ruoliReorder(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$order = $request->input('order', []);
|
|
|
|
foreach ($order as $index => $id) {
|
|
Ruolo::where('id', $id)->update(['ordine' => $index]);
|
|
}
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
public function tipologieEventiStore(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$validated = $request->validate([
|
|
'nome' => 'required|string|max:50|unique:tipologie_eventi,nome',
|
|
'descrizione' => 'nullable|string|max:255',
|
|
]);
|
|
|
|
$maxOrdine = TipologiaEvento::max('ordine') ?? 0;
|
|
|
|
TipologiaEvento::create([
|
|
'nome' => $validated['nome'],
|
|
'descrizione' => $validated['descrizione'] ?? null,
|
|
'ordine' => $maxOrdine + 1,
|
|
'attiva' => true,
|
|
]);
|
|
|
|
return redirect('/impostazioni#tipologie-eventi')->with('success', 'Tipologia evento aggiunta.');
|
|
}
|
|
|
|
public function tipologieEventiUpdate(Request $request, $id)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$tipologia = TipologiaEvento::findOrFail($id);
|
|
|
|
$validated = $request->validate([
|
|
'nome' => 'required|string|max:50|unique:tipologie_eventi,nome,' . $id,
|
|
'descrizione' => 'nullable|string|max:255',
|
|
'attiva' => 'boolean',
|
|
]);
|
|
|
|
$tipologia->update($validated);
|
|
|
|
return redirect('/impostazioni#tipologie-eventi')->with('success', 'Tipologia evento aggiornata.');
|
|
}
|
|
|
|
public function tipologieEventiDestroy($id)
|
|
{
|
|
$this->authorizeDelete('settings');
|
|
$tipologia = TipologiaEvento::findOrFail($id);
|
|
|
|
$count = $tipologia->eventi()->count();
|
|
if ($count > 0) {
|
|
return redirect('/impostazioni#tipologie-eventi')->with('error', 'Impossibile eliminare: ' . $count . ' eventi associati.');
|
|
}
|
|
|
|
$tipologia->delete();
|
|
|
|
return redirect('/impostazioni#tipologie-eventi')->with('success', 'Tipologia evento eliminata.');
|
|
}
|
|
|
|
public function tipologieEventiReorder(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$order = $request->input('order', []);
|
|
|
|
foreach ($order as $index => $id) {
|
|
TipologiaEvento::where('id', $id)->update(['ordine' => $index]);
|
|
}
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
public function uploadLogo(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$request->validate([
|
|
'logo' => 'nullable|image|mimes:png,jpg,jpeg,gif,svg|max:2048',
|
|
'logo_small' => 'nullable|image|mimes:png,jpg,jpeg,gif,svg|max:512',
|
|
]);
|
|
|
|
$settings = AppSetting::first() ?? new AppSetting();
|
|
|
|
if ($request->hasFile('logo')) {
|
|
$path = $request->file('logo')->store('logos', 'public');
|
|
$settings->logo_path = $path;
|
|
}
|
|
|
|
if ($request->hasFile('logo_small')) {
|
|
$path = $request->file('logo_small')->store('logos', 'public');
|
|
$settings->logo_small_path = $path;
|
|
}
|
|
|
|
if ($settings->id) {
|
|
$settings->save();
|
|
} else {
|
|
$settings->save();
|
|
}
|
|
|
|
return redirect('/impostazioni#logo')->with('success', 'Logo caricato con successo.');
|
|
}
|
|
|
|
public function removeLogo(Request $request)
|
|
{
|
|
$this->authorizeWrite('settings');
|
|
|
|
$type = $request->input('type');
|
|
|
|
$settings = AppSetting::first();
|
|
|
|
if ($settings) {
|
|
if ($type === 'logo') {
|
|
if ($settings->logo_path) {
|
|
Storage::disk('public')->delete($settings->logo_path);
|
|
$settings->logo_path = null;
|
|
}
|
|
} elseif ($type === 'logo_small') {
|
|
if ($settings->logo_small_path) {
|
|
Storage::disk('public')->delete($settings->logo_small_path);
|
|
$settings->logo_small_path = null;
|
|
}
|
|
}
|
|
$settings->save();
|
|
}
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
} |