getConnection();
// Verifica utente loggato
if (!isset($iduserlogin)) {
header("Location: login.php");
exit;
}
// Controlla se esiste almeno un salone
$stmt = $pdo->prepare("SELECT COUNT(*) FROM shops WHERE owner_id = ?");
$stmt->execute([$iduserlogin]);
if ((int)$stmt->fetchColumn() === 0) {
header("Location: onboarding_salon.php");
exit;
}
// Prendi il primo salone (o quello attivo – puoi aggiungere switcher dopo)
$stmt = $pdo->prepare("
SELECT id, name
FROM shops
WHERE owner_id = ?
ORDER BY created_at ASC
LIMIT 1
");
$stmt->execute([$iduserlogin]);
$shop = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$shop) {
die("Errore: salone non trovato.");
}
$shop_id = (int)$shop['id'];
$shop_name = $shop['name'];
// =========================================================================
// Helpers
// =========================================================================
function isValidDateYmd(string $date): bool {
$d = DateTime::createFromFormat('Y-m-d', $date);
return $d && $d->format('Y-m-d') === $date;
}
function setFlash(string $type, string $text): void {
$_SESSION['flash'] = ['type' => $type, 'text' => $text];
}
function getFlash(): ?array {
if (!isset($_SESSION['flash'])) return null;
$f = $_SESSION['flash'];
unset($_SESSION['flash']);
return $f;
}
// =========================================================================
// Gestione POST (add / edit / delete)
// =========================================================================
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
try {
if ($action === 'add') {
$start_date = trim($_POST['start_date'] ?? '');
$end_date = trim($_POST['end_date'] ?? '');
$title = trim($_POST['title'] ?? '');
$description = trim($_POST['description'] ?? '');
if ($start_date === '' || $end_date === '') {
setFlash('danger', "Le date di inizio e fine sono obbligatorie.");
} elseif (!isValidDateYmd($start_date) || !isValidDateYmd($end_date)) {
setFlash('danger', "Formato data non valido.");
} elseif (strtotime($end_date) < strtotime($start_date)) {
setFlash('danger', "La data di fine non può essere precedente alla data di inizio.");
} else {
$pdo->beginTransaction();
// Inserisco una riga per ogni giorno del range (B2)
$stmt = $pdo->prepare("
INSERT INTO shop_day_off (shop_id, date, title, description, is_recurring)
VALUES (?, ?, ?, ?, 0)
ON DUPLICATE KEY UPDATE
title = VALUES(title),
description = VALUES(description),
updated_at = CURRENT_TIMESTAMP
");
$start = new DateTime($start_date);
$end = new DateTime($end_date);
$end->setTime(0,0,0);
// +1 giorno per includere la fine
$period = new DatePeriod($start, new DateInterval('P1D'), (clone $end)->modify('+1 day'));
$inserted = 0;
foreach ($period as $dt) {
$day = $dt->format('Y-m-d');
$ok = $stmt->execute([
$shop_id,
$day,
$title !== '' ? $title : 'Chiusura',
$description
]);
if ($ok) $inserted++;
}
$pdo->commit();
setFlash('success', "Chiusura aggiunta: salvati/aggiornati {$inserted} giorni.");
}
header("Location: day_off.php");
exit;
}
if ($action === 'edit') {
$id = (int)($_POST['id'] ?? 0);
$date = trim($_POST['start_date'] ?? ''); // nel form edit usiamo start_date come "data singola"
$title = trim($_POST['title'] ?? '');
$description = trim($_POST['description'] ?? '');
if ($id <= 0) {
setFlash('danger', "ID non valido.");
} elseif ($date === '' || !isValidDateYmd($date)) {
setFlash('danger', "La data è obbligatoria e deve essere valida.");
} else {
$stmt = $pdo->prepare("
UPDATE shop_day_off
SET date = ?, title = ?, description = ?, updated_at = NOW()
WHERE id = ? AND shop_id = ?
");
$ok = $stmt->execute([
$date,
$title !== '' ? $title : 'Chiusura',
$description,
$id,
$shop_id
]);
setFlash($ok ? 'success' : 'danger', $ok ? "Giorno di chiusura aggiornato!" : "Errore durante l'aggiornamento.");
}
header("Location: day_off.php");
exit;
}
if ($action === 'delete') {
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
setFlash('danger', "ID non valido.");
} else {
$stmt = $pdo->prepare("DELETE FROM shop_day_off WHERE id = ? AND shop_id = ?");
$ok = $stmt->execute([$id, $shop_id]);
setFlash($ok ? 'success' : 'danger', $ok ? "Giorno di chiusura eliminato!" : "Errore durante l'eliminazione.");
}
header("Location: day_off.php");
exit;
}
// action sconosciuta
setFlash('danger', "Azione non valida.");
header("Location: day_off.php");
exit;
} catch (Throwable $e) {
if ($pdo->inTransaction()) $pdo->rollBack();
setFlash('danger', "Errore: " . $e->getMessage());
header("Location: day_off.php");
exit;
}
}
// =========================================================================
// Recupera tutti i giorni di chiusura
// =========================================================================
$stmt = $pdo->prepare("
SELECT id, date, title, description, is_recurring
FROM shop_day_off
WHERE shop_id = ?
ORDER BY date ASC
");
$stmt->execute([$shop_id]);
$days_off = $stmt->fetchAll(PDO::FETCH_ASSOC);
$flash = getFlash();
?>
Giorni di Chiusura - = htmlspecialchars($shop_name) ?>
= htmlspecialchars($flash['text']) ?>
Non hai ancora impostato giorni di chiusura.
Aggiungine uno per bloccare le prenotazioni in quei giorni.
| Data |
Titolo / Descrizione |
Ricorrente |
Azioni |
| = htmlspecialchars($day['date']) ?> |
= htmlspecialchars(($day['title'] ?: '') . (($day['description'] ?? '') ? ' — ' . $day['description'] : '')) ?: 'Chiusura' ?> |
Sì (ogni anno)
No
|
|