netgescon-day0/app/Http/Controllers/SuperAdmin/ImpostazioniController.php

165 lines
7.5 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\SuperAdmin;
use App\Http\Controllers\Controller;
use App\Models\Amministratore;
use App\Models\SystemSetting;
use App\Models\UserSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
class ImpostazioniController extends Controller
{
public function index()
{
// Carica le impostazioni attuali dell'utente
$settings = [
'dark_mode' => UserSetting::get('dark_mode', 'false'),
'bg_color' => UserSetting::get('bg_color', '#ffffff'),
'text_color' => UserSetting::get('text_color', '#1e293b'),
'accent_color' => UserSetting::get('accent_color', '#6366f1'),
'sidebar_bg_color' => UserSetting::get('sidebar_bg_color', '#fde047'),
'sidebar_text_color' => UserSetting::get('sidebar_text_color', '#1e293b'),
'sidebar_accent_color' => UserSetting::get('sidebar_accent_color', '#6366f1'),
'registration_enabled' => (string) (SystemSetting::bool('registration_enabled', true) ? 'true' : 'false'),
'require_email_verification' => (string) (SystemSetting::bool('require_email_verification', true) ? 'true' : 'false'),
'recaptcha_version' => (string) SystemSetting::getValue('recaptcha_version', 'none'),
'recaptcha_site_key' => (string) SystemSetting::getValue('recaptcha_site_key', ''),
'recaptcha_secret_key' => (string) SystemSetting::getValue('recaptcha_secret_key', ''),
'telegram_bot_token' => (string) SystemSetting::getValue('telegram_bot_token', ''),
'telegram_webhook_token' => (string) SystemSetting::getValue('telegram_webhook_token', ''),
'whatsapp_phone_number_id' => (string) SystemSetting::getValue('whatsapp_phone_number_id', ''),
'whatsapp_business_account_id' => (string) SystemSetting::getValue('whatsapp_business_account_id', ''),
'whatsapp_access_token' => (string) SystemSetting::getValue('whatsapp_access_token', ''),
'whatsapp_webhook_token' => (string) SystemSetting::getValue('whatsapp_webhook_token', ''),
];
$googleStatus = $this->googleStatus();
return view('superadmin.impostazioni.index', compact('settings', 'googleStatus'));
}
public function store(Request $request)
{
$validated = $request->validate([
'dark_mode' => 'string|in:true,false',
'bg_color' => 'string|max:7',
'text_color' => 'string|max:7',
'accent_color' => 'string|max:7',
'sidebar_bg_color' => 'string|max:7',
'sidebar_text_color' => 'string|max:7',
'sidebar_accent_color' => 'string|max:7',
'registration_enabled' => 'nullable|string|in:true,false',
'require_email_verification' => 'nullable|string|in:true,false',
'recaptcha_version' => 'nullable|string|in:none,v2,v3',
'recaptcha_site_key' => 'nullable|string|max:255',
'recaptcha_secret_key' => 'nullable|string|max:255',
'telegram_bot_token' => 'nullable|string|max:255',
'telegram_webhook_token' => 'nullable|string|max:255',
'whatsapp_phone_number_id' => 'nullable|string|max:255',
'whatsapp_business_account_id' => 'nullable|string|max:255',
'whatsapp_access_token' => 'nullable|string|max:500',
'whatsapp_webhook_token' => 'nullable|string|max:255',
]);
$globalKeys = [
'registration_enabled',
'require_email_verification',
'recaptcha_version',
'recaptcha_site_key',
'recaptcha_secret_key',
'telegram_bot_token',
'telegram_webhook_token',
'whatsapp_phone_number_id',
'whatsapp_business_account_id',
'whatsapp_access_token',
'whatsapp_webhook_token',
];
foreach ($validated as $key => $value) {
if (in_array($key, $globalKeys, true)) {
SystemSetting::setValue($key, $value);
} else {
UserSetting::set($key, $value);
}
}
if ($request->expectsJson()) {
return response()->json(['success' => true, 'message' => 'Impostazioni salvate con successo!']);
}
return back()->with('success', 'Impostazioni salvate con successo.');
}
public function theme(Request $request)
{
$theme = $request->input('theme', 'default');
$themes = [
'default' => [
'bg_color' => '#ffffff',
'text_color' => '#1e293b',
'accent_color' => '#6366f1',
'sidebar_bg_color' => '#fde047',
'sidebar_text_color' => '#1e293b',
'sidebar_accent_color' => '#6366f1',
],
'dark' => [
'bg_color' => '#1e293b',
'text_color' => '#f1f5f9',
'accent_color' => '#fbbf24',
'sidebar_bg_color' => '#374151',
'sidebar_text_color' => '#f1f5f9',
'sidebar_accent_color' => '#fbbf24',
],
'ocean' => [
'bg_color' => '#f0f9ff',
'text_color' => '#0c4a6e',
'accent_color' => '#0ea5e9',
'sidebar_bg_color' => '#0ea5e9',
'sidebar_text_color' => '#ffffff',
'sidebar_accent_color' => '#f0f9ff',
],
];
if (isset($themes[$theme])) {
foreach ($themes[$theme] as $key => $value) {
UserSetting::set($key, $value);
}
}
return response()->json(['success' => true, 'settings' => $themes[$theme] ?? []]);
}
private function googleStatus(): array
{
$clientId = (string) config('services.google.client_id', '');
$clientSecret = (string) config('services.google.client_secret', '');
$redirect = (string) config('services.google.redirect', '');
$admin = null;
$user = Auth::user();
if ($user && method_exists($user, 'amministratore') && $user->amministratore) {
$admin = $user->amministratore;
}
if (! $admin instanceof Amministratore) {
$admin = Amministratore::query()->latest('id')->first();
}
$google = Arr::get($admin?->impostazioni ?? [], 'google', []);
$oauth = Arr::get($google, 'oauth', []);
return [
'client_id_configured' => $clientId !== '',
'client_secret_configured' => $clientSecret !== '',
'redirect_configured' => $redirect !== '',
'oauth_connected' => (bool) Arr::get($oauth, 'connected', false),
'oauth_email' => (string) Arr::get($oauth, 'email', ''),
'calendar_enabled' => (bool) Arr::get($oauth, 'connected', false),
'contacts_enabled' => (bool) Arr::get($oauth, 'connected', false),
'drive_enabled' => (bool) Arr::get($oauth, 'connected', false),
];
}
}