yogiboook_new/public/userarea/school_settings.php
2026-01-15 17:35:16 +01:00

360 lines
19 KiB
PHP

<?php
include('include/headscript.php');
if (!isset($iduserlogin)) die("Errore: utente non loggato.");
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// Recupera la scuola
$stmt = $pdo->prepare("SELECT id, name FROM schools WHERE owner_id = ?");
$stmt->execute([$iduserlogin]);
$school = $stmt->fetch();
if (!$school) die("Scuola non trovata.");
$school_id = $school['id'];
// Recupera i settings (anche se non esistono ancora)
$stmt = $pdo->prepare("SELECT * FROM school_settings WHERE school_id = ?");
$stmt->execute([$school_id]);
$settings = $stmt->fetch();
// Ricarica con default se manca la colonna (per scuole vecchie)
if ($settings && !array_key_exists('portal_purchases_enabled', $settings)) {
$settings['portal_purchases_enabled'] = 1;
}
$is_new = !$settings;
$success_message = $error = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// === Aspetto ===
$header_color = $_POST['header_color'] ?? '#ffffff';
$sidebar_color = $_POST['sidebar_color'] ?? '#ffffff';
$currency_code = strtoupper(trim($_POST['currency_code'] ?? 'EUR'));
$enable_notifications = !empty($_POST['enable_notifications']) ? 1 : 0;
// === Metodi di pagamento (multipli) ===
$payment_methods_array = $_POST['payment_methods'] ?? ['manual'];
$payment_methods = implode(',', array_unique(array_filter($payment_methods_array)));
if (empty($payment_methods)) $payment_methods = 'manual'; // sicurezza
// === Impostazioni prodotti ===
$allow_freeze_global = !empty($_POST['allow_freeze_global']) ? 1 : 0;
$freeze_max_days_global = $allow_freeze_global ? max(0, (int)($_POST['freeze_max_days_global'] ?? 30)) : 0;
$auto_propagate_on_purchase = !empty($_POST['auto_propagate_on_purchase']) ? 1 : 0;
$allow_full_access_rebooking = !empty($_POST['allow_full_access_rebooking']) ? 1 : 0;
// Tipi di prodotto consentiti
$product_types = [];
if (!empty($_POST['allow_subscription'])) $product_types[] = 'subscription';
if (!empty($_POST['allow_carnet'])) $product_types[] = 'carnet';
if (!empty($_POST['allow_drop_in'])) $product_types[] = 'drop_in';
$allowed_product_types = !empty($product_types) ? implode(',', $product_types) : 'none';
$portal_purchases_enabled = !empty($_POST['portal_purchases_enabled']) ? 1 : 0;
// Se acquisti portale disabilitati → forza anche propagate a 0
$auto_propagate_on_purchase = $portal_purchases_enabled
? (!empty($_POST['auto_propagate_on_purchase']) ? 1 : 0)
: 0;
// === SALVATAGGIO ===
try {
if ($is_new) {
$stmt = $pdo->prepare("
INSERT INTO school_settings (
school_id, header_color, sidebar_color, payment_methods, currency_code, enable_notifications,
allow_freeze_global, freeze_max_days_global, auto_propagate_on_purchase,
allow_full_access_rebooking, allowed_product_types,
portal_purchases_enabled
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$school_id,
$header_color,
$sidebar_color,
$payment_methods,
$currency_code,
$enable_notifications,
$allow_freeze_global,
$freeze_max_days_global,
$auto_propagate_on_purchase,
$allow_full_access_rebooking,
$allowed_product_types,
$portal_purchases_enabled
]);
$success_message = "Impostazioni create con successo!";
} else {
$stmt = $pdo->prepare("
UPDATE school_settings SET
header_color = ?, sidebar_color = ?, payment_methods = ?, currency_code = ?, enable_notifications = ?,
allow_freeze_global = ?, freeze_max_days_global = ?, auto_propagate_on_purchase = ?,
allow_full_access_rebooking = ?, allowed_product_types = ?,
portal_purchases_enabled = ?
WHERE school_id = ?
");
$stmt->execute([
$header_color,
$sidebar_color,
$payment_methods,
$currency_code,
$enable_notifications,
$allow_freeze_global,
$freeze_max_days_global,
$auto_propagate_on_purchase,
$allow_full_access_rebooking,
$allowed_product_types,
$portal_purchases_enabled,
$school_id
]);
$success_message = "Impostazioni aggiornate con successo!";
}
// Ricarica i settings aggiornati
$stmt = $pdo->prepare("SELECT * FROM school_settings WHERE school_id = ?");
$stmt->execute([$school_id]);
$settings = $stmt->fetch();
} catch (Exception $e) {
$error = "Errore database: " . $e->getMessage();
}
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Impostazioni Scuola - <?php echo htmlspecialchars($school['name']); ?></title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
.card {
border-radius: 15px;
}
.form-check-input:checked {
background-color: #0d6efd;
border-color: #0d6efd;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="container-xl">
<div class="row">
<div class="col-12">
<div class="card shadow">
<div class="card-header bg-primary text-white">
<h4 class="mb-0">Impostazioni Scuola</h4>
</div>
<div class="card-body">
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show">
<?php echo $success_message; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST">
<!-- Aspetto e Pagamenti -->
<h5 class="text-primary mb-4">Aspetto e Pagamenti</h5>
<div class="row g-3 mb-4">
<div class="col-md-3">
<label>Colore Header</label>
<input type="color" name="header_color" class="form-control form-control-color" value="<?php echo $settings['header_color'] ?? '#ffffff'; ?>">
</div>
<div class="col-md-3">
<label>Colore Sidebar</label>
<input type="color" name="sidebar_color" class="form-control form-control-color" value="<?php echo $settings['sidebar_color'] ?? '#ffffff'; ?>">
</div>
<div class="col-md-3">
<label>Valuta</label>
<input type="text" name="currency_code" class="form-control" value="<?php echo $settings['currency_code'] ?? 'EUR'; ?>" maxlength="3" style="text-transform:uppercase">
</div>
<div class="col-md-3">
<label>Notifiche</label><br>
<div class="form-check form-switch d-inline-block">
<input class="form-check-input" type="checkbox" name="enable_notifications" id="notif" <?php echo ($settings['enable_notifications'] ?? 1) ? 'checked' : ''; ?>>
<label class="form-check-label" for="notif">Attive</label>
</div>
</div>
</div>
<div class="mt-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="portal_purchases_enabled" id="portal_enabled"
<?php echo ($settings['portal_purchases_enabled'] ?? 1) ? 'checked' : ''; ?>>
<label class="form-check-label" for="portal_enabled">
Acquisti pacchetti attivi nel portale YoGiBook
</label>
</div>
<small class="form-text text-muted">Se disattivato, anche la propagazione automatica e i pagamenti vengono forzati a NO.</small>
</div>
<br>
<div class="col-12 mb-4">
<label class="form-label">Metodi di pagamento accettati</label>
<div class="row g-3">
<div class="col-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="payment_methods[]" value="manual" id="pay_manual"
<?php echo strpos($settings['payment_methods'] ?? 'manual', 'manual') !== false ? 'checked' : ''; ?>>
<label class="form-check-label fw-bold" for="pay_manual">Pagamento Manuale</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="payment_methods[]" value="stripe" id="pay_stripe"
<?php echo strpos($settings['payment_methods'] ?? '', 'stripe') !== false ? 'checked' : ''; ?>>
<label class="form-check-label fw-bold" for="pay_stripe">Stripe</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="payment_methods[]" value="paypal" id="pay_paypal"
<?php echo strpos($settings['payment_methods'] ?? '', 'paypal') !== false ? 'checked' : ''; ?>>
<label class="form-check-label fw-bold" for="pay_paypal">PayPal</label>
</div>
</div>
</div>
</div>
<hr class="my-5">
<!-- Impostazioni Prodotti -->
<h5 class="text-primary mb-4">Impostazioni Prodotti (valori predefiniti)</h5>
<div class="row g-4 align-items-center">
<div class="col-md-6">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="allow_freeze_global" id="allow_freeze" <?php echo ($settings['allow_freeze_global'] ?? 1) ? 'checked' : ''; ?>>
<label class="form-check-label" for="allow_freeze">Permetti congelamento abbonamenti</label>
</div>
</div>
<div class="col-md-6">
<label>Giorni massimi di congelamento</label>
<input type="number" name="freeze_max_days_global" class="form-control" value="<?php echo $settings['freeze_max_days_global'] ?? 30; ?>" min="0"
<?php echo ($settings['allow_freeze_global'] ?? 1) ? '' : 'disabled'; ?>>
</div>
</div>
<div class="mt-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="auto_propagate_on_purchase" id="auto_propagate" <?php echo ($settings['auto_propagate_on_purchase'] ?? 1) ? 'checked' : ''; ?>>
<label class="form-check-label" for="auto_propagate">Propaga automaticamente le sessioni dopo l'acquisto</label>
</div>
</div>
<div class="mt-4">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="allow_full_access_rebooking" id="full_access" <?php echo ($settings['allow_full_access_rebooking'] ?? 1) ? 'checked' : ''; ?>>
<label class="form-check-label" for="full_access">Permetti riprenotazione su qualsiasi lezione</label>
</div>
</div>
<div class="mt-4">
<label class="form-label">Tipi di prodotto consentiti</label>
<div class="row">
<div class="col-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="allow_subscription" id="sub" <?php echo strpos($settings['allowed_product_types'] ?? 'subscription,carnet,drop_in', 'subscription') !== false ? 'checked' : ''; ?>>
<label class="form-check-label" for="sub">Abbonamenti</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="allow_carnet" id="car" <?php echo strpos($settings['allowed_product_types'] ?? '', 'carnet') !== false ? 'checked' : ''; ?>>
<label class="form-check-label" for="car">Carnet</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="allow_drop_in" id="drop" <?php echo strpos($settings['allowed_product_types'] ?? '', 'drop_in') !== false ? 'checked' : ''; ?>>
<label class="form-check-label" for="drop">Lezioni singole</label>
</div>
</div>
</div>
</div>
<div class="text-center mt-5">
<button type="submit" class="btn btn-primary btn-lg px-5">
Salva Impostazioni
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
document.getElementById('allow_freeze')?.addEventListener('change', function() {
document.querySelector('input[name="freeze_max_days_global"]').disabled = !this.checked;
});
</script>
<script>
const portal = document.getElementById('portal_enabled');
const propagate = document.getElementById('auto_propagate');
const stripe = document.getElementById('pay_stripe');
const paypal = document.getElementById('pay_paypal');
const manual = document.getElementById('pay_manual');
function syncPortalState() {
if (!portal) return;
const isEnabled = portal.checked;
// Propaga
if (propagate) {
propagate.disabled = !isEnabled;
if (!isEnabled) propagate.checked = false;
}
// Stripe + PayPal
if (stripe) {
stripe.disabled = !isEnabled;
if (!isEnabled) stripe.checked = false;
}
if (paypal) {
paypal.disabled = !isEnabled;
if (!isEnabled) paypal.checked = false;
}
if (manual) {
manual.disabled = !isEnabled;
if (!isEnabled) manual.checked = false;
}
}
if (portal) {
portal.addEventListener('change', syncPortalState);
// Esegui subito (importante per il caricamento iniziale)
syncPortalState();
}
</script>
</body>
</html>