yogiboook_new/public/userarea/student_profile.php

328 lines
19 KiB
PHP

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
if (!isset($iduserlogin)) {
die("Errore: Utente non loggato.");
}
// Recupera dati da auth_users
$stmt = $pdo->prepare("SELECT first_name, last_name, email FROM auth_users WHERE id = ?");
$stmt->execute([$iduserlogin]);
$user = $stmt->fetch();
if (!$user) {
die("Utente non trovato.");
}
// Recupera (o crea) record studente
$stmt = $pdo->prepare("SELECT * FROM students WHERE user_id = ?");
$stmt->execute([$iduserlogin]);
$student = $stmt->fetch();
$is_new = !$student;
$success_message = $error = '';
// === GESTIONE SALVATAGGIO ===
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$codice_fiscale = trim($_POST['codice_fiscale'] ?? '');
$partita_iva = trim($_POST['partita_iva'] ?? '');
$company_name = trim($_POST['company_name'] ?? '');
$billing_address = trim($_POST['billing_address']);
$billing_postal_code = trim($_POST['billing_postal_code']);
$billing_city = trim($_POST['billing_city']);
$billing_province = strtoupper(trim($_POST['billing_province']));
$billing_country = trim($_POST['billing_country'] ?: 'Italia');
$same_shipping = !empty($_POST['shipping_same_as_billing']);
$shipping_address = $same_shipping ? $billing_address : trim($_POST['shipping_address'] ?? '');
$shipping_postal_code = $same_shipping ? $billing_postal_code : trim($_POST['shipping_postal_code'] ?? '');
$shipping_city = $same_shipping ? $billing_city : trim($_POST['shipping_city'] ?? '');
$shipping_province = $same_shipping ? $billing_province : strtoupper(trim($_POST['shipping_province'] ?? ''));
$shipping_country = $same_shipping ? $billing_country : trim($_POST['shipping_country'] ?? 'Italia');
$emergency_name = trim($_POST['emergency_contact_name'] ?? '');
$emergency_phone = trim($_POST['emergency_contact_phone'] ?? '');
$medical_notes = trim($_POST['medical_notes'] ?? '');
$privacy_consent = !empty($_POST['privacy_consent']);
$marketing_consent = !empty($_POST['marketing_consent']);
// Validazione minima
if (empty($billing_address) || empty($billing_city) || empty($billing_postal_code) || empty($billing_province)) {
$error = "Compila tutti i campi obbligatori dell'indirizzo di fatturazione.";
} elseif (!$privacy_consent) {
$error = "Devi accettare l'informativa privacy per continuare.";
} else {
try {
if ($is_new) {
$stmt = $pdo->prepare("
INSERT INTO students (
user_id, codice_fiscale, partita_iva, company_name,
billing_address, billing_postal_code, billing_city, billing_province, billing_country,
shipping_same_as_billing, shipping_address, shipping_postal_code, shipping_city, shipping_province, shipping_country,
emergency_contact_name, emergency_contact_phone, medical_notes,
privacy_consent, marketing_consent
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
");
$stmt->execute([
$iduserlogin,
$codice_fiscale,
$partita_iva,
$company_name,
$billing_address,
$billing_postal_code,
$billing_city,
$billing_province,
$billing_country,
$same_shipping ? 1 : 0,
$shipping_address,
$shipping_postal_code,
$shipping_city,
$shipping_province,
$shipping_country,
$emergency_name,
$emergency_phone,
$medical_notes,
$privacy_consent ? 1 : 0,
$marketing_consent ? 1 : 0
]);
$success_message = "Profilo completato con successo! Benvenuto";
} else {
$stmt = $pdo->prepare("
UPDATE students SET
codice_fiscale = ?, partita_iva = ?, company_name = ?,
billing_address = ?, billing_postal_code = ?, billing_city = ?, billing_province = ?, billing_country = ?,
shipping_same_as_billing = ?, shipping_address = ?, shipping_postal_code = ?, shipping_city = ?, shipping_province = ?, shipping_country = ?,
emergency_contact_name = ?, emergency_contact_phone = ?, medical_notes = ?,
privacy_consent = ?, marketing_consent = ?
WHERE user_id = ?
");
$stmt->execute([
$codice_fiscale,
$partita_iva,
$company_name,
$billing_address,
$billing_postal_code,
$billing_city,
$billing_province,
$billing_country,
$same_shipping ? 1 : 0,
$shipping_address,
$shipping_postal_code,
$shipping_city,
$shipping_province,
$shipping_country,
$emergency_name,
$emergency_phone,
$medical_notes,
$privacy_consent ? 1 : 0,
$marketing_consent ? 1 : 0,
$iduserlogin
]);
$success_message = "Dati aggiornati con successo!";
}
// Ricarica i dati aggiornati
$stmt = $pdo->prepare("SELECT * FROM students WHERE user_id = ?");
$stmt->execute([$iduserlogin]);
$student = $stmt->fetch();
$is_new = false;
} catch (Exception $e) {
$error = "Errore del database: " . $e->getMessage();
}
}
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo $is_new ? 'Completa il tuo profilo' : 'Il mio profilo'; ?> - Yogiboook</title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
.form-check-input:checked {
background-color: #0d6efd;
border-color: #0d6efd;
}
.required-label::after {
content: " *";
color: red;
}
</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-x1">
<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card radius-15 shadow-lg">
<div class="card-header bg-primary text-white text-center">
<h4 class="mb-0">
<?php echo $is_new ? 'Completa il tuo profilo studente' : 'I miei dati'; ?>
</h4>
</div>
<div class="card-body p-4">
<?php if ($success_message): ?>
<div class="alert alert-success text-center"><?php echo $success_message; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST" class="needs-validation" novalidate>
<h5 class="mb-4 text-primary">Dati personali</h5>
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Nome</label>
<input type="text" class="form-control" value="<?php echo htmlspecialchars($user['first_name'] ?? ''); ?>" disabled>
</div>
<div class="col-md-6">
<label class="form-label">Cognome</label>
<input type="text" class="form-control" value="<?php echo htmlspecialchars($user['last_name'] ?? ''); ?>" disabled>
</div>
<div class="col-md-6">
<label class="form-label">Email</label>
<input type="email" class="form-control" value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>" disabled>
</div>
<div class="col-md-6">
<label class="form-label">Codice Fiscale</label>
<input type="text" class="form-control" name="codice_fiscale" value="<?php echo htmlspecialchars($student['codice_fiscale'] ?? ''); ?>" maxlength="16" style="text-transform:uppercase">
</div>
<div class="col-md-6">
<label class="form-label">Partita IVA <small class="text-muted">(opzionale, per aziende)</small></label>
<input type="text" class="form-control" name="partita_iva" value="<?php echo htmlspecialchars($student['partita_iva'] ?? ''); ?>" maxlength="11">
</div>
<div class="col-md-6">
<label class="form-label">Ragione sociale <small class="text-muted">(se P.IVA)</small></label>
<input type="text" class="form-control" name="company_name" value="<?php echo htmlspecialchars($student['company_name'] ?? ''); ?>">
</div>
</div>
<hr class="my-5">
<h5 class="mb-4 text-primary">Indirizzo di fatturazione <span class="required-label"></span></h5>
<div class="row g-3">
<div class="col-12">
<label class="form-label required-label">Indirizzo</label>
<input type="text" class="form-control" name="billing_address" value="<?php echo htmlspecialchars($student['billing_address'] ?? ''); ?>" required>
</div>
<div class="col-md-3">
<label class="form-label required-label">CAP</label>
<input type="text" class="form-control" name="billing_postal_code" value="<?php echo htmlspecialchars($student['billing_postal_code'] ?? ''); ?>" required>
</div>
<div class="col-md-5">
<label class="form-label required-label">Città</label>
<input type="text" class="form-control" name="billing_city" value="<?php echo htmlspecialchars($student['billing_city'] ?? ''); ?>" required>
</div>
<div class="col-md-2">
<label class="form-label required-label">Provincia</label>
<input type="text" class="form-control" name="billing_province" value="<?php echo htmlspecialchars($student['billing_province'] ?? ''); ?>" maxlength="2" style="text-transform:uppercase" required>
</div>
<div class="col-md-2">
<label class="form-label">Nazione</label>
<input type="text" class="form-control" name="billing_country" value="<?php echo htmlspecialchars($student['billing_country'] ?? 'Italia'); ?>">
</div>
</div>
<div class="form-check mt-4">
<input class="form-check-input" type="checkbox" id="same_shipping" name="shipping_same_as_billing" <?php echo ($student['shipping_same_as_billing'] ?? 1) ? 'checked' : ''; ?>>
<label class="form-check-label fw-bold text-primary" for="same_shipping">
L'indirizzo di spedizione è uguale a quello di fatturazione
</label>
</div>
<div id="shipping_fields" style="display: <?php echo ($student['shipping_same_as_billing'] ?? 1) ? 'none' : 'block'; ?>;">
<hr class="my-4">
<h5 class="mb-3 text-primary">Indirizzo di spedizione</h5>
<div class="row g-3">
<div class="col-12"><input type="text" class="form-control" name="shipping_address" placeholder="Indirizzo" value="<?php echo htmlspecialchars($student['shipping_address'] ?? ''); ?>"></div>
<div class="col-md-3"><input type="text" class="form-control" name="shipping_postal_code" placeholder="CAP" value="<?php echo htmlspecialchars($student['shipping_postal_code'] ?? ''); ?>"></div>
<div class="col-md-5"><input type="text" class="form-control" name="shipping_city" placeholder="Città" value="<?php echo htmlspecialchars($student['shipping_city'] ?? ''); ?>"></div>
<div class="col-md-2"><input type="text" class="form-control" name="shipping_province" placeholder="Prov" maxlength="2" style="text-transform:uppercase" value="<?php echo htmlspecialchars($student['shipping_province'] ?? ''); ?>"></div>
<div class="col-md-2"><input type="text" class="form-control" name="shipping_country" placeholder="Nazione" value="<?php echo htmlspecialchars($student['shipping_country'] ?? 'Italia'); ?>"></div>
</div>
</div>
<hr class="my-5">
<h5 class="mb-4 text-primary">Contatto di emergenza e note mediche</h5>
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Nome contatto emergenza</label>
<input type="text" class="form-control" name="emergency_contact_name" value="<?php echo htmlspecialchars($student['emergency_contact_name'] ?? ''); ?>">
</div>
<div class="col-md-6">
<label class="form-label">Telefono emergenza</label>
<input type="text" class="form-control" name="emergency_contact_phone" value="<?php echo htmlspecialchars($student['emergency_contact_phone'] ?? ''); ?>">
</div>
<div class="col-12">
<label class="form-label">Note mediche / allergie / infortuni</label>
<textarea class="form-control" name="medical_notes" rows="3"><?php echo htmlspecialchars($student['medical_notes'] ?? ''); ?></textarea>
</div>
</div>
<hr class="my-5">
<h5 class="mb-4 text-primary">Consensi</h5>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="privacy_consent" name="privacy_consent" required <?php echo ($student['privacy_consent'] ?? false) ? 'checked' : ''; ?>>
<label class="form-check-label" for="privacy_consent">
Accetto l'<a href="privacy.php" target="_blank">informativa privacy</a> (obbligatorio)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="marketing_consent" name="marketing_consent" <?php echo ($student['marketing_consent'] ?? false) ? 'checked' : ''; ?>>
<label class="form-check-label" for="marketing_consent">
Voglio ricevere newsletter e offerte
</label>
</div>
<div class="text-center mt-5">
<button type="submit" class="btn btn-primary btn-lg px-5">
<?php echo $is_new ? 'Completa registrazione' : 'Salva modifiche'; ?>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
// Mostra/nascondi indirizzo spedizione
document.getElementById('same_shipping').addEventListener('change', function() {
document.getElementById('shipping_fields').style.display = this.checked ? 'none' : 'block';
});
</script>
</body>
</html>