78 lines
2.4 KiB
PHP
78 lines
2.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* shop_context.php
|
|
* Determina i saloni del titolare loggato e gestisce il "salone attivo" in sessione.
|
|
*
|
|
* Richiede: headscript.php già incluso (fornisce $iduserlogin e la connessione).
|
|
* Espone:
|
|
* $owner_shops -> array di tutti i saloni di cui l'utente è owner
|
|
* $active_shop_id -> id del salone attivo (int) o null se non ne ha
|
|
* $active_shop -> record completo del salone attivo (array) o null
|
|
*/
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// Connessione (riusa l'istanza singleton)
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
$owner_shops = [];
|
|
$active_shop_id = null;
|
|
$active_shop = null;
|
|
|
|
if (isset($iduserlogin)) {
|
|
|
|
// Tutti i saloni di cui l'utente loggato è proprietario
|
|
$stmt = $pdo->prepare("
|
|
SELECT id, name, slug, logo, city, active
|
|
FROM shops
|
|
WHERE owner_id = ?
|
|
ORDER BY name ASC
|
|
");
|
|
$stmt->execute([$iduserlogin]);
|
|
$owner_shops = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (!empty($owner_shops)) {
|
|
|
|
// Cambio salone attivo richiesto via GET (?set_shop=ID)
|
|
if (isset($_GET['set_shop'])) {
|
|
$requested = (int)$_GET['set_shop'];
|
|
// Accetta solo se il salone appartiene davvero a questo owner
|
|
foreach ($owner_shops as $s) {
|
|
if ((int)$s['id'] === $requested) {
|
|
$_SESSION['active_shop_id'] = $requested;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Se in sessione c'è un salone attivo, verifica che sia ancora dell'owner
|
|
if (isset($_SESSION['active_shop_id'])) {
|
|
$valid = false;
|
|
foreach ($owner_shops as $s) {
|
|
if ((int)$s['id'] === (int)$_SESSION['active_shop_id']) {
|
|
$valid = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!$valid) {
|
|
unset($_SESSION['active_shop_id']);
|
|
}
|
|
}
|
|
|
|
// Default: primo salone della lista
|
|
if (!isset($_SESSION['active_shop_id'])) {
|
|
$_SESSION['active_shop_id'] = (int)$owner_shops[0]['id'];
|
|
}
|
|
|
|
$active_shop_id = (int)$_SESSION['active_shop_id'];
|
|
|
|
// Carica i dati completi del salone attivo
|
|
$stmt = $pdo->prepare("SELECT * FROM shops WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$active_shop_id]);
|
|
$active_shop = $stmt->fetch(PDO::FETCH_ASSOC) ?: null;
|
|
}
|
|
}
|