fixed multi school account
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
<?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;
|
||||
@@ -9,6 +12,87 @@ if (!isset($iduserlogin)) {
|
||||
$dbHandler = DBHandlerSelect::getInstance();
|
||||
$pdo = $dbHandler->getConnection();
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 1) Carico le scuole attive dell'utente
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT s.id, s.name, s.logo
|
||||
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]);
|
||||
@@ -16,7 +100,14 @@ $user = $stmt->fetch();
|
||||
$avatar = $user['avatar'] ? '../upload/users/' . $user['avatar'] : '../assets/images/default-avatar.png';
|
||||
|
||||
// === SCUOLA CORRENTE ===
|
||||
$school_id = session('school_id');
|
||||
$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
|
||||
|
||||
@@ -286,14 +377,55 @@ $active_orders = count(array_filter($orders, fn($o) => $o['status'] === 'complet
|
||||
<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 text-center py-5">
|
||||
<i class="bx bx-building-house bx-lg text-muted"></i>
|
||||
<h4 class="mt-3 text-muted">Le tue scuole</h4>
|
||||
<p class="text-muted">Qui compariranno tutte le scuole in cui sei iscritto</p>
|
||||
<div class="spinner-border text-primary mt-4" role="status">
|
||||
<span class="visually-hidden">Caricamento...</span>
|
||||
</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 = (!empty($s['logo']) && file_exists("photoschool/" . $s['logo']))
|
||||
? "photoschool/" . $s['logo']
|
||||
: null;
|
||||
$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">
|
||||
<?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 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>
|
||||
|
||||
Reference in New Issue
Block a user