diff --git a/public/userarea/day_off.php b/public/userarea/day_off.php index 8f05b9d..a8b9f52 100644 --- a/public/userarea/day_off.php +++ b/public/userarea/day_off.php @@ -4,6 +4,10 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); +if (session_status() === PHP_SESSION_NONE) { + session_start(); +} + include('include/headscript.php'); // Connessione DB @@ -19,17 +23,17 @@ if (!isset($iduserlogin)) { // Controlla se esiste almeno un salone $stmt = $pdo->prepare("SELECT COUNT(*) FROM shops WHERE owner_id = ?"); $stmt->execute([$iduserlogin]); -if ($stmt->fetchColumn() === 0) { +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 + SELECT id, name + FROM shops + WHERE owner_id = ? + ORDER BY created_at ASC LIMIT 1 "); $stmt->execute([$iduserlogin]); @@ -39,66 +43,150 @@ if (!$shop) { die("Errore: salone non trovato."); } -$shop_id = $shop['id']; +$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) // ========================================================================= -$success_message = ''; -$error_message = ''; - if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { $action = $_POST['action']; - if ($action === 'add' || $action === 'edit') { - $start_date = trim($_POST['start_date'] ?? ''); - $end_date = trim($_POST['end_date'] ?? ''); - $description = trim($_POST['description'] ?? ''); - $id = ($action === 'edit') ? (int)($_POST['id'] ?? 0) : 0; + try { + if ($action === 'add') { + $start_date = trim($_POST['start_date'] ?? ''); + $end_date = trim($_POST['end_date'] ?? ''); + $title = trim($_POST['title'] ?? ''); + $description = trim($_POST['description'] ?? ''); - // Validazioni - if (empty($start_date) || empty($end_date)) { - $error_message = "Le date di inizio e fine sono obbligatorie."; - } elseif (strtotime($end_date) < strtotime($start_date)) { - $error_message = "La data di fine non può essere precedente alla data di inizio."; - } else { - if ($action === 'add') { + 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, created_at) - VALUES (?, ?, ?, ?, 0, NOW()) + 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 "); - $ok = $stmt->execute([$shop_id, $start_date, $description ?: 'Chiusura', $description]); - $success_message = $ok ? "Giorno di chiusura aggiunto!" : "Errore durante l'aggiunta."; - } else { // edit + + $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 + UPDATE shop_day_off SET date = ?, title = ?, description = ?, updated_at = NOW() WHERE id = ? AND shop_id = ? "); - $ok = $stmt->execute([$start_date, $description ?: 'Chiusura', $description, $id, $shop_id]); - $success_message = $ok ? "Giorno di chiusura aggiornato!" : "Errore durante l'aggiornamento."; + $ok = $stmt->execute([ + $date, + $title !== '' ? $title : 'Chiusura', + $description, + $id, + $shop_id + ]); + + setFlash($ok ? 'success' : 'danger', $ok ? "Giorno di chiusura aggiornato!" : "Errore durante l'aggiornamento."); } - } - } - if ($action === 'delete') { - $id = (int)($_POST['id'] ?? 0); - if ($id > 0) { - $stmt = $pdo->prepare("DELETE FROM shop_day_off WHERE id = ? AND shop_id = ?"); - $ok = $stmt->execute([$id, $shop_id]); - $success_message = $ok ? "Giorno di chiusura eliminato!" : "Errore durante l'eliminazione."; + header("Location: day_off.php"); + exit; } - } - // Evita doppio submit - if ($success_message || $error_message) { - header("Location: day_off.php" . ($success_message ? "?msg=success" : "?msg=error")); + 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 @@ -106,12 +194,13 @@ $stmt = $pdo->prepare(" ORDER BY date ASC "); $stmt->execute([$shop_id]); -$days_off = $stmt->fetchAll(); +$days_off = $stmt->fetchAll(PDO::FETCH_ASSOC); + +$flash = getFlash(); ?> - @@ -122,184 +211,197 @@ $days_off = $stmt->fetchAll(); -
- - +
+ + -
-
-
-
-
Giorni di Chiusura -
-
- - - Dashboard - +
+
+
+
+
Giorni di Chiusura -
+
+ + + Dashboard + +
+
+ +
+ + -
+ -
- - - - - - - + +
+ Non hai ancora impostato giorni di chiusura.
+ Aggiungine uno per bloccare le prenotazioni in quei giorni. +
+ +
+ + + + + + + + + + + + + + + + + + + +
DataTitolo / DescrizioneRicorrenteAzioni
+ + Sì (ogni anno) + + No + + + - -
- Non hai ancora impostato giorni di chiusura.
- Aggiungine uno per bloccare le prenotazioni in quei giorni. -
- -
- - - - - - - - - - - - - - - - - - - -
DataTitolo / DescrizioneRicorrenteAzioni
- - Sì (ogni anno) - - No - - - - -
- - - -
-
-
- - +
+ + + +
+
+
+
- -
- -