fixed multi school account

This commit is contained in:
2025-12-20 18:18:15 +01:00
parent 70d01f160e
commit 67cc0742ff
8 changed files with 1176 additions and 201 deletions
+421
View File
@@ -0,0 +1,421 @@
<?php
session_start();
include('include/headscript.php');
error_log("SELECT_SCHOOL HIT - user=" . ($_SESSION['iduserlogin'] ?? 'NOUSER') . " school=" . var_export($_SESSION['school_id'] ?? null, true));
// ----------------------------------------------------
// SYNC school_id between Laravel session and PHP $_SESSION
// ----------------------------------------------------
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// se Laravel ha school_id e PHP no → copio in PHP
// if (!isset($_SESSION['school_id']) && function_exists('session') && session()->has('school_id')) {
// $_SESSION['school_id'] = session('school_id');
// if (session()->has('school_name')) {
// $_SESSION['school_name'] = session('school_name');
// }
// }
// se PHP ha school_id e Laravel no → copio in Laravel
if (isset($_SESSION['school_id']) && function_exists('session') && !session()->has('school_id')) {
session([
'school_id' => $_SESSION['school_id'],
'school_name' => $_SESSION['school_name'] ?? null,
]);
}
/*
|--------------------------------------------------------------------------
| AUTH CHECK
|--------------------------------------------------------------------------
*/
if (!isset($_SESSION['iduserlogin'])) {
header('Location: login.php');
exit;
}
/*
|--------------------------------------------------------------------------
| SE SCHOOL GIÀ IN SESSIONE VAI AVANTI
|--------------------------------------------------------------------------
*/
// Se ho già una school_id in sessione, vado avanti SOLO se l'utente ha 1 scuola sola.
// Se ne ha >1, devo sempre mostrare la selezione.
if (!empty($_SESSION['school_id'])) {
// Non faccio redirect qui: lo decido DOPO aver caricato $userSchools
// (vedi sotto, dopo la query delle scuole utente)
}
$iduserlogin = $_SESSION['iduserlogin'];
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
/*
|--------------------------------------------------------------------------
| DATI UTENTE
|--------------------------------------------------------------------------
*/
$stmt = $pdo->prepare("SELECT first_name, avatar FROM auth_users WHERE id = ?");
$stmt->execute([$iduserlogin]);
$user = $stmt->fetch();
$first_name = htmlspecialchars($user['first_name'] ?? '');
/*
|--------------------------------------------------------------------------
| POST SELEZIONE SCUOLA
|--------------------------------------------------------------------------
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['school_id'])) {
$school_id = (int) $_POST['school_id'];
$stmt = $pdo->prepare("
SELECT id, name
FROM schools
WHERE id = ? AND status = 'active'
");
$stmt->execute([$school_id]);
$school = $stmt->fetch();
if ($school) {
// Iscrizione automatica se non esiste
$check = $pdo->prepare("
SELECT 1 FROM user_schools
WHERE user_id = ? AND school_id = ?
");
$check->execute([$iduserlogin, $school_id]);
if (!$check->fetch()) {
$pdo->prepare("
INSERT INTO user_schools (user_id, school_id, status)
VALUES (?, ?, 'active')
")->execute([$iduserlogin, $school_id]);
}
// Sessione
$_SESSION['school_id'] = $school['id'];
$_SESSION['school_name'] = $school['name'];
$_SESSION['school_selected'] = 1;
if (function_exists('session')) {
session([
'school_id' => $school['id'],
'school_name' => $school['name'],
]);
}
header('Location: user_dashboard.php');
exit;
}
}
/*
|--------------------------------------------------------------------------
| RECUPERO SCUOLE UTENTE
|--------------------------------------------------------------------------
*/
$stmt = $pdo->prepare("
SELECT s.id, s.name, s.logo, s.address_city
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([$iduserlogin]);
$userSchools = $stmt->fetchAll();
// --- VALIDAZIONE school_id: se non appartiene all'utente, la resetto ---
$allowedIds = array_map(fn($r) => (int)$r['id'], $userSchools);
if (!empty($_SESSION['school_id']) && !in_array((int)$_SESSION['school_id'], $allowedIds, true)) {
unset($_SESSION['school_id'], $_SESSION['school_name'], $_SESSION['school_selected']);
if (function_exists('session')) {
session()->forget('school_id');
session()->forget('school_name');
}
}
// Caso 1: una sola scuola -> auto-select e vado in dashboard
if (count($userSchools) === 1) {
$_SESSION['school_id'] = (int)$userSchools[0]['id'];
$_SESSION['school_name'] = $userSchools[0]['name'];
$_SESSION['school_selected'] = 1;
if (function_exists('session')) {
session([
'school_id' => (int)$userSchools[0]['id'],
'school_name' => $userSchools[0]['name'],
]);
}
header('Location: user_dashboard.php');
exit;
}
// Caso 2: più scuole -> se school_id è presente MA non c'è selezione esplicita, mostro comunque la select
// Se invece school_id + school_selected sono presenti, posso andare in dashboard
if (count($userSchools) > 1 && !empty($_SESSION['school_id']) && !empty($_SESSION['school_selected'])) {
header('Location: user_dashboard.php');
exit;
}
/*
/*
|--------------------------------------------------------------------------
| CASO: NESSUNA SCUOLA MOSTRA PUBBLICHE
|--------------------------------------------------------------------------
*/
if (empty($userSchools)) {
$stmt = $pdo->query("
SELECT id, name, logo, address_city
FROM schools
WHERE status = 'active'
ORDER BY name
");
$schools = $stmt->fetchAll();
$title = "Benvenuto!";
$subtitle = "Scegli la scuola dove vuoi prenotare le lezioni";
} else {
$schools = $userSchools;
$title = "Ciao $first_name!";
$subtitle = "Seleziona la scuola in cui entrare oggi";
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scegli la scuola - Yogiboook</title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
:root {
--pastel-blue: #94bacc;
--pastel-green: #a3d9b1;
--pastel-pink: #f8bbd0;
--pastel-yellow: #fff8c4;
}
body {
background: linear-gradient(to bottom, #f0f8ff, #f8f9fa);
min-height: 100vh;
}
.card-pastel {
background: linear-gradient(135deg, #94bacc, #a3d9b1);
color: white;
}
.radius-20 {
border-radius: 20px;
}
.school-card {
border-radius: 20px;
overflow: hidden;
cursor: pointer;
transition: all 0.25s ease;
border: 2px solid transparent;
}
.school-card:hover {
transform: translateY(-8px);
box-shadow: 0 18px 45px rgba(0, 0, 0, 0.12);
border-color: rgba(148, 186, 204, 0.9);
}
.school-logo {
height: 120px;
object-fit: contain;
background: #f8f9fa;
padding: 16px;
}
.btn-select {
background: linear-gradient(135deg, var(--pastel-blue), var(--pastel-green));
border: none;
color: white;
font-weight: 700;
border-radius: 14px;
padding: 12px 14px;
transition: transform 0.15s ease;
}
.btn-select:hover {
transform: scale(1.03);
color: white;
}
.badge-soft {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 0.35rem 0.75rem;
border-radius: 999px;
background: rgba(255, 255, 255, 0.25);
color: #fff;
font-weight: 600;
font-size: 0.9rem;
}
/* =========================================================
FORCE FULL WIDTH WHEN SIDEBAR IS NOT INCLUDED
========================================================= */
.no-sidebar .page-wrapper,
.no-sidebar .page-content {
margin-left: 0 !important;
}
.no-sidebar .topbar,
.no-sidebar .header,
.no-sidebar .navbar,
.no-sidebar .topbar-nav {
left: 0 !important;
width: 100% !important;
}
.no-sidebar .wrapper {
width: 100% !important;
}
.no-sidebar .sidebar-wrapper {
display: none !important;
}
</style>
</head>
<body class="no-sidebar">
<div class="wrapper">
<!-- niente navbar -->
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content" style="min-height: 100vh;">
<div class="container-fluid px-4 pt-5 pb-5">
<!-- HERO -->
<div class="card card-pastel radius-20 shadow-lg mb-5">
<div class="card-body text-center py-5 position-relative">
<div class="badge-soft mb-3">
<i class="bx bx-buildings"></i>
Selezione scuola
</div>
<h1 class="display-6 fw-bold mb-2"><?= htmlspecialchars($title) ?></h1>
<p class="fs-5 opacity-90 mb-0"><?= htmlspecialchars($subtitle) ?></p>
<div class="mt-4 text-white-50">
<small>Utente: <strong class="text-white"><?= htmlspecialchars($first_name) ?></strong></small>
</div>
</div>
</div>
<!-- CONTENUTO -->
<?php if (empty($schools)): ?>
<div class="text-center py-5">
<i class="bx bx-building-house bx-lg text-muted"></i>
<h4 class="mt-3 text-muted">Nessuna scuola disponibile al momento</h4>
<p class="text-muted mb-0">Riprova più tardi oppure contatta lassistenza.</p>
</div>
<?php else: ?>
<form method="POST" id="schoolForm">
<div class="row g-4 justify-content-center">
<?php foreach ($schools as $school): ?>
<?php
$sid = (int)$school['id'];
$sname = $school['name'] ?? '';
$scity = $school['address_city'] ?? '';
$logoPath = null;
if (!empty($school['logo']) && file_exists("photoschool/" . $school['logo'])) {
$logoPath = "photoschool/" . $school['logo'];
}
?>
<div class="col-md-6 col-lg-4">
<div class="card school-card h-100 shadow-sm" onclick="selectSchool(<?= $sid ?>)">
<div class="text-center">
<?php if ($logoPath): ?>
<img src="<?= htmlspecialchars($logoPath) ?>"
class="school-logo w-100"
alt="<?= htmlspecialchars($sname) ?>">
<?php else: ?>
<div class="school-logo d-flex align-items-center justify-content-center">
<i class="bx bx-building-house display-4 text-muted"></i>
</div>
<?php endif; ?>
</div>
<div class="card-body text-center pb-4">
<h5 class="card-title mb-2"><?= htmlspecialchars($sname) ?></h5>
<?php if (!empty($scity)): ?>
<p class="text-muted small mb-0">
<i class="bx bx-map me-1"></i><?= htmlspecialchars($scity) ?>
</p>
<?php endif; ?>
</div>
<div class="card-footer bg-transparent border-0 pt-0 pb-4 px-4">
<button type="submit" class="btn btn-select w-100 shadow-sm">
<i class="bx bx-check me-2"></i>Seleziona questa scuola
</button>
</div>
</div>
<input type="radio"
name="school_id"
value="<?= $sid ?>"
id="school_<?= $sid ?>"
class="d-none"
required>
</div>
<?php endforeach; ?>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
function selectSchool(id) {
const r = document.getElementById('school_' + id);
if (r) r.checked = true;
document.getElementById('schoolForm').submit();
}
</script>
</body>
</html>