yogiboook_new/public/userarea/user_dashboard.php
2025-12-23 09:39:59 +01:00

490 lines
23 KiB
PHP

<?php
session_start();
include('include/headscript.php');
error_log("DASHBOARD HIT - user=" . ($_SESSION['iduserlogin'] ?? 'NOUSER') . " school=" . var_export($_SESSION['school_id'] ?? null, true));
if (!isset($iduserlogin)) {
header('Location: login.php');
exit;
}
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
/*
|--------------------------------------------------------------------------
| 1) Carico le scuole attive dell'utente
|--------------------------------------------------------------------------
*/
$stmt = $pdo->prepare("
SELECT
s.id,
s.name,
s.logo,
s.address_street,
s.address_city,
s.address_postal_code,
s.address_province,
s.address_country
FROM user_schools us
JOIN schools s ON us.school_id = s.id
WHERE us.user_id = ?
AND us.status = 'active'
AND s.status = 'active'
ORDER BY s.name
");
$stmt->execute([(int)$iduserlogin]);
$userSchools = $stmt->fetchAll(PDO::FETCH_ASSOC);
/*
|--------------------------------------------------------------------------
| 2) Cambio scuola da modale (POST)
|--------------------------------------------------------------------------
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'change_school') {
$newSchoolId = (int)($_POST['school_id'] ?? 0);
$allowedIds = array_map(fn($r) => (int)$r['id'], $userSchools);
if ($newSchoolId > 0 && in_array($newSchoolId, $allowedIds, true)) {
// imposto sessione
$_SESSION['school_id'] = $newSchoolId;
// imposto anche il nome (comodo per UI)
foreach ($userSchools as $r) {
if ((int)$r['id'] === $newSchoolId) {
$_SESSION['school_name'] = $r['name'];
break;
}
}
// flag: selezione esplicita
$_SESSION['school_selected'] = 1;
header('Location: user_dashboard.php');
exit;
}
}
/*
|--------------------------------------------------------------------------
| 3) Validazione school_id in sessione
|--------------------------------------------------------------------------
*/
$allowedIds = array_map(fn($r) => (int)$r['id'], $userSchools);
// Se l'utente non ha scuole -> vai alla select (dove vedrà le pubbliche)
if (count($userSchools) === 0) {
header('Location: select_school.php');
exit;
}
// Se school_id esiste ma NON appartiene all'utente -> reset
if (!empty($_SESSION['school_id']) && !in_array((int)$_SESSION['school_id'], $allowedIds, true)) {
unset($_SESSION['school_id'], $_SESSION['school_name'], $_SESSION['school_selected']);
}
// Caso: una sola scuola -> auto-select (OK)
if (count($userSchools) === 1 && empty($_SESSION['school_id'])) {
$_SESSION['school_id'] = (int)$userSchools[0]['id'];
$_SESSION['school_name'] = $userSchools[0]['name'];
$_SESSION['school_selected'] = 1;
}
// Caso: più scuole -> OBBLIGO selezione esplicita
if (count($userSchools) > 1) {
if (empty($_SESSION['school_id']) || empty($_SESSION['school_selected'])) {
header('Location: select_school.php');
exit;
}
}
// === DATI UTENTE ===
$stmt = $pdo->prepare("SELECT first_name, last_name, email, avatar FROM auth_users WHERE id = ?");
$stmt->execute([$iduserlogin]);
$user = $stmt->fetch();
$avatar = $user['avatar'] ? '../upload/users/' . $user['avatar'] : '../assets/images/default-avatar.png';
// === SCUOLA CORRENTE ===
$school_id = (int)($_SESSION['school_id'] ?? 0);
if ($school_id <= 0) {
header('Location: select_school.php');
exit;
}
$school_name = 'Nessuna scuola selezionata';
$school_logo_path = null; // niente default
if ($school_id) {
$stmt = $pdo->prepare("SELECT name, logo FROM schools WHERE id = ?");
$stmt->execute([$school_id]);
$school = $stmt->fetch();
if ($school) {
$school_name = $school['name'];
if (!empty($school['logo']) && file_exists("photoschool/" . $school['logo'])) {
$school_logo_path = "photoschool/" . $school['logo'];
}
}
}
// === ORDINI UTENTE ===
$stmt = $pdo->prepare("
SELECT o.*, p.name AS product_name, pv.name AS variation_name,
c.name AS class_name, ct.level, ct.day_of_week
FROM orders o
JOIN products p ON o.product_id = p.id
LEFT JOIN product_variations pv ON o.variation_id = pv.id
LEFT JOIN classes c ON o.class_id = c.id
LEFT JOIN class_types ct ON o.class_type_id = ct.id
WHERE o.user_id = ? AND o.school_id = ?
ORDER BY o.created_at DESC
");
$stmt->execute([$iduserlogin, $school_id]);
$orders = $stmt->fetchAll();
// === STATISTICHE RAPIDE ===
$total_spent = array_sum(array_column($orders, 'price'));
$total_entries = array_sum(array_column($orders, 'total_entries'));
$available_entries = array_sum(array_column($orders, 'available_entries'));
$active_orders = count(array_filter($orders, fn($o) => $o['status'] === 'completed' && (!$o['expiration_date'] || strtotime($o['expiration_date']) >= time())));
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>La mia Dashboard - Yogiboook</title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
:root {
--pastel-blue: #94bacc;
--pastel-green: #a3d9b1;
--pastel-pink: #f8bbd0;
--pastel-yellow: #fff8c4;
}
.card-pastel {
background: linear-gradient(135deg, #94bacc, #a3d9b1);
color: white;
}
.card-soft {
background: #ffffff;
border: 1px solid #e0e0e0;
}
.stat-card {
background: white;
border-radius: 15px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
}
.stat-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}
.avatar-circle {
width: 100px;
height: 100px;
object-fit: cover;
border: 4px solid white;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
.table-pastel thead {
background-color: var(--pastel-blue);
color: white;
}
.badge-soft {
padding: 0.4em 0.8em;
border-radius: 50px;
font-size: 0.85rem;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content" style="background: linear-gradient(to bottom, #f0f8ff, #f8f9fa); min-height: 100vh;">
<!-- SCUOLA + BENVENUTO -->
<div class="container-fluid px-4 pt-4">
<div class="card card-pastel radius-20 shadow-lg mb-4 position-relative">
<div class="card-body text-center py-5">
<!-- Logo solo se esiste -->
<?php if ($school_logo_path): ?>
<img src="<?php echo htmlspecialchars($school_logo_path); ?>"
alt="Logo <?php echo htmlspecialchars($school_name); ?>"
class="mb-4 rounded-3 shadow"
style="height: 90px; object-fit: contain;">
<?php endif; ?>
<!-- Saluto -->
<h2 class="mb-2 text-white">
Ciao<?php echo $user['first_name'] ? ', ' . htmlspecialchars($user['first_name']) : ''; ?>!
</h2>
<p class="mb-0 fs-4 text-white opacity-90">
Sei nella scuola: <strong><?php echo htmlspecialchars($school_name); ?></strong>
</p>
<!-- PULSANTI IN RIGA (uno accanto all'altro) -->
<div class="position-absolute top-0 end-0 mt-3 me-3 d-flex gap-2">
<a href="my_lessons.php" class="btn btn-light btn-lg shadow-sm px-4">
<i class="bx bx-calendar-heart me-2"></i>Le mie lezioni
</a>
<button type="button" class="btn btn-outline-light btn-lg shadow-sm px-4"
data-bs-toggle="modal" data-bs-target="#changeSchoolModal">
<i class="bx bx-transfer me-2"></i>Cambia scuola
</button>
</div>
</div>
</div>
<!-- PROFILO + STATISTICHE -->
<div class="row g-4 mb-5">
<div class="col-lg-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<img src="<?php echo $avatar; ?>" alt="Avatar" class="rounded-circle avatar-circle mb-3">
<h5><?php echo htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?></h5>
<p class="text-muted"><?php echo htmlspecialchars($user['email']); ?></p>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="row g-4">
<div class="col-md-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<h3 class="text-primary fw-bold"><?php echo count($orders); ?></h3>
<p class="mb-0">Ordini totali</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<h3 class="text-success fw-bold">€<?php echo number_format($total_spent, 2); ?></h3>
<p class="mb-0">Speso in totale</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<h3 class="text-info fw-bold"><?php echo $available_entries; ?></h3>
<p class="mb-0">Ingressi disponibili</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- I TUOI ORDINI -->
<div class="card radius-20 shadow-lg">
<div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
<h4 class="mb-0">I tuoi ordini</h4>
<span class="badge bg-light text-dark"><?php echo count($orders); ?> totali</span>
</div>
<div class="card-body p-0">
<?php if (empty($orders)): ?>
<div class="text-center py-5">
<i class="bx bx-package bx-lg text-muted"></i>
<h5 class="text-muted mt-3">Non hai ancora effettuato ordini</h5>
<a href="products.php" class="btn btn-primary mt-3">Vai ai corsi</a>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-pastel">
<tr>
<th>Data</th>
<th>Ordine</th>
<th>Prodotto</th>
<th>Ingressi</th>
<th>Scadenza</th>
<th>Prezzo</th>
<th>Stato</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $o): ?>
<tr class="table-light">
<td><?php echo date('d/m/Y', strtotime($o['created_at'])); ?></td>
<td><strong>#<?php echo $o['order_number']; ?></strong></td>
<td>
<strong><?php echo htmlspecialchars($o['product_name']); ?></strong>
<?php if ($o['variation_name']): ?>
<br><small class="text-success"><?php echo htmlspecialchars($o['variation_name']); ?></small>
<?php endif; ?>
<?php if ($o['class_name']): ?>
<br><small class="text-info"><?php echo htmlspecialchars($o['class_name']); ?></small>
<?php endif; ?>
</td>
<td>
<?php if ($o['available_entries'] != $o['total_entries']): ?>
<span class="text-success"><?php echo $o['available_entries']; ?></span>/<?php echo $o['total_entries']; ?>
<?php else: ?>
<?php echo $o['total_entries']; ?>
<?php endif; ?>
</td>
<td>
<?php if ($o['expiration_date']): ?>
<span class="<?php echo strtotime($o['expiration_date']) < time() ? 'text-danger' : 'text-warning'; ?>">
<?php echo date('d/m/Y', strtotime($o['expiration_date'])); ?>
</span>
<?php else: ?>
<em>Nessuna</em>
<?php endif; ?>
</td>
<td class="text-success fw-bold">€<?php echo number_format($o['price'], 2); ?></td>
<td>
<span class="badge-soft <?php echo $o['status'] == 'completed' ? 'bg-success' : ($o['status'] == 'pending' ? 'bg-warning' : 'bg-secondary'); ?>">
<?php echo ucfirst($o['status']); ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<!-- Modal Cambia Scuola -->
<div class="modal fade" id="changeSchoolModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title">Scegli la scuola</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<?php if (empty($userSchools)): ?>
<div class="text-center text-muted py-4">
Nessuna scuola associata.
</div>
<?php else: ?>
<div class="row g-3">
<?php foreach ($userSchools as $s): ?>
<?php
$sid = (int)$s['id'];
$sname = $s['name'];
$logoPath = null;
$logoRaw = trim((string)($s['logo'] ?? ''));
if ($logoRaw !== '') {
// se in DB è già "photoschool/xxx.jpg" lo uso così com'è
$logoRel = ltrim($logoRaw, '/');
// check file su disco (stessa cartella di questo file)
$disk1 = __DIR__ . '/' . $logoRel;
$disk2 = __DIR__ . '/../' . $logoRel;
if (is_file($disk1)) {
$logoPath = $logoRel;
} elseif (is_file($disk2)) {
$logoPath = '../' . $logoRel;
} else {
// fallback web
$logoPath = $logoRel;
}
}
$isCurrent = ($sid === (int)$school_id);
?>
<div class="col-md-6">
<div class="card shadow-sm h-100">
<div class="card-body d-flex align-items-center gap-3">
<?php if ($logoPath): ?>
<img src="<?= htmlspecialchars($logoPath) ?>" style="height:50px;width:auto;" class="rounded" onerror="this.style.display='none';">
<?php else: ?>
<i class="bx bx-building-house bx-md text-muted"></i>
<?php endif; ?>
<div class="flex-grow-1">
<div class="fw-bold"><?= htmlspecialchars($sname) ?></div>
<?php
$addrParts = [];
if (!empty($s['address_street'])) {
$addrParts[] = $s['address_street'];
}
$cityLine = trim(
($s['address_postal_code'] ?? '') . ' ' .
($s['address_city'] ?? '') .
(!empty($s['address_province']) ? ' (' . $s['address_province'] . ')' : '')
);
if ($cityLine !== '') {
$addrParts[] = $cityLine;
}
if (!empty($s['address_country'])) {
$addrParts[] = $s['address_country'];
}
$fullAddress = implode(', ', array_map('trim', $addrParts));
?>
<?php if ($fullAddress !== ''): ?>
<div class="text-muted small">
<i class="bx bx-map me-1"></i><?= htmlspecialchars($fullAddress) ?>
</div>
<?php endif; ?>
<?php if ($isCurrent): ?>
<div class="text-success small">Selezionata</div>
<?php endif; ?>
</div>
<?php if (!$isCurrent): ?>
<form method="POST" class="m-0">
<input type="hidden" name="action" value="change_school">
<input type="hidden" name="school_id" value="<?= $sid ?>">
<button type="submit" class="btn btn-primary">
Seleziona
</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</body>
</html>