dashboard

This commit is contained in:
2026-01-25 21:22:35 +01:00
parent 8d8e213f1c
commit bd9d3811f6
6 changed files with 2814 additions and 348 deletions
+186 -199
View File
@@ -1,258 +1,247 @@
<?php
// Forza la visualizzazione degli errori
// Forza la visualizzazione degli errori (solo dev)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('include/headscript.php');
// Connessione al database
// Connessione DB
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// Verifica che iduserlogin sia definito
// Verifica utente loggato
if (!isset($iduserlogin)) {
die("Errore: ID utente non definito.");
header("Location: login.php");
exit;
}
// Recupera i dati della scuola in base all'utente loggato
$stmt = $pdo->prepare("SELECT id, name FROM schools WHERE owner_id = ?");
// Controlla se esiste almeno un salone
$stmt = $pdo->prepare("SELECT COUNT(*) FROM shops WHERE owner_id = ?");
$stmt->execute([$iduserlogin]);
$school = $stmt->fetch();
if (!$school) {
die("Errore: Nessuna scuola trovata per l'utente loggato.");
if ($stmt->fetchColumn() === 0) {
header("Location: onboarding_salon.php");
exit;
}
$school_id = $school['id'];
$school_name = $school['name'];
// Gestione delle azioni (aggiunta, modifica, cancellazione)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['action'])) {
$action = $_POST['action'];
// 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);
// Aggiunta di un giorno di chiusura
if ($action === 'add') {
$start_date = $_POST['start_date'] ?? '';
$end_date = $_POST['end_date'] ?? '';
$description = $_POST['description'] ?? null;
if (!$shop) {
die("Errore: salone non trovato.");
}
// Validazione: assicurarsi che end_date >= start_date
if (empty($start_date) || empty($end_date)) {
$error = "Le date di inizio e fine sono obbligatorie.";
} elseif (strtotime($end_date) < strtotime($start_date)) {
$error = "La data di fine non può essere precedente alla data di inizio.";
} else {
$shop_id = $shop['id'];
$shop_name = $shop['name'];
// =========================================================================
// 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;
// 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') {
$stmt = $pdo->prepare("
INSERT INTO day_off (school_id, start_date, end_date, description)
VALUES (?, ?, ?, ?)
INSERT INTO shop_day_off (shop_id, date, title, description, is_recurring, created_at)
VALUES (?, ?, ?, ?, 0, NOW())
");
$success = $stmt->execute([
$school_id,
$start_date,
$end_date,
$description
]);
if ($success) {
$success_message = "Giorno di chiusura aggiunto con successo!";
} else {
$error = "Errore durante l'aggiunta del giorno di chiusura.";
}
}
}
// Modifica di un giorno di chiusura
if ($action === 'edit') {
$id = $_POST['id'] ?? 0;
$start_date = $_POST['start_date'] ?? '';
$end_date = $_POST['end_date'] ?? '';
$description = $_POST['description'] ?? null;
// Validazione: assicurarsi che end_date >= start_date
if (empty($start_date) || empty($end_date)) {
$error = "Le date di inizio e fine sono obbligatorie.";
} elseif (strtotime($end_date) < strtotime($start_date)) {
$error = "La data di fine non può essere precedente alla data di inizio.";
} else {
$ok = $stmt->execute([$shop_id, $start_date, $description ?: 'Chiusura', $description]);
$success_message = $ok ? "Giorno di chiusura aggiunto!" : "Errore durante l'aggiunta.";
} else { // edit
$stmt = $pdo->prepare("
UPDATE day_off
SET start_date = ?, end_date = ?, description = ?
WHERE id = ? AND school_id = ?
UPDATE shop_day_off
SET date = ?, title = ?, description = ?, updated_at = NOW()
WHERE id = ? AND shop_id = ?
");
$success = $stmt->execute([
$start_date,
$end_date,
$description,
$id,
$school_id
]);
if ($success) {
$success_message = "Giorno di chiusura aggiornato con successo!";
} else {
$error = "Errore durante l'aggiornamento del giorno di chiusura.";
}
$ok = $stmt->execute([$start_date, $description ?: 'Chiusura', $description, $id, $shop_id]);
$success_message = $ok ? "Giorno di chiusura aggiornato!" : "Errore durante l'aggiornamento.";
}
}
}
// Cancellazione di un giorno di chiusura
if ($action === 'delete') {
$id = $_POST['id'] ?? 0;
$stmt = $pdo->prepare("DELETE FROM day_off WHERE id = ? AND school_id = ?");
$success = $stmt->execute([$id, $school_id]);
if ($success) {
$success_message = "Giorno di chiusura eliminato con successo!";
} else {
$error = "Errore durante l'eliminazione del giorno di chiusura.";
}
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.";
}
}
// Reindirizza per evitare il doppio invio del form
header("Location: day_off.php");
// Evita doppio submit
if ($success_message || $error_message) {
header("Location: day_off.php" . ($success_message ? "?msg=success" : "?msg=error"));
exit;
}
}
// Recupera tutti i giorni di chiusura della scuola
// Recupera tutti i giorni di chiusura
$stmt = $pdo->prepare("
SELECT *
FROM day_off
WHERE school_id = ?
ORDER BY start_date
SELECT id, date, title, description, is_recurring
FROM shop_day_off
WHERE shop_id = ?
ORDER BY date ASC
");
$stmt->execute([$school_id]);
$stmt->execute([$shop_id]);
$days_off = $stmt->fetchAll();
?>
<!doctype html>
<html lang="en">
<html lang="it">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--favicon-->
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<title>Giorni di Chiusura - <?= htmlspecialchars($shop_name) ?></title>
</head>
<body>
<!--wrapper-->
<div class="wrapper">
<!--sidebar wrapper -->
<?php include('include/navbar.php'); ?>
<!--end sidebar wrapper -->
<!--start header -->
<?php include('include/topbar.php'); ?>
<!--end header -->
<!--start page wrapper -->
<div class="page-wrapper">
<div class="page-content">
<div class="card radius-10">
<div class="card-header">
<div class="d-flex align-items-center">
<div>
<h6 class="mb-0">Giorni di Chiusura - <?php echo htmlspecialchars($school_name); ?></h6>
</div>
<div class="ms-auto">
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addDayOffModal">
Aggiungi Giorno di Chiusura
</button>
<a href="school_dashboard.php" class="btn btn-secondary ms-2">Torna alla Dashboard</a>
</div>
<div class="card-header bg-light d-flex align-items-center justify-content-between">
<h6 class="mb-0">Giorni di Chiusura - <?= htmlspecialchars($shop_name) ?></h6>
<div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addDayOffModal">
<i class="bx bx-plus me-1"></i> Aggiungi Chiusura
</button>
<a href="salon_dashboard.php" class="btn btn-outline-secondary ms-2">
<i class="bx bx-arrow-back me-1"></i> Dashboard
</a>
</div>
</div>
<div class="card-body">
<?php if (isset($success_message)): ?>
<div class="alert alert-success" role="alert">
<?php echo $success_message; ?>
</div>
<?php if (isset($_GET['msg'])): ?>
<?php if ($_GET['msg'] === 'success'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?= htmlspecialchars($success_message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php elseif ($_GET['msg'] === 'error'): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?= htmlspecialchars($error_message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger" role="alert">
<?php echo $error; ?>
<?php if (empty($days_off)): ?>
<div class="alert alert-info text-center py-4">
Non hai ancora impostato giorni di chiusura.<br>
Aggiungine uno per bloccare le prenotazioni in quei giorni.
</div>
<?php endif; ?>
<div class="table-responsive">
<table id="daysOffTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Data Inizio</th>
<th>Data Fine</th>
<th>Descrizione</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($days_off as $day_off): ?>
<?php else: ?>
<div class="table-responsive">
<table id="daysOffTable" class="table table-striped table-hover table-bordered">
<thead>
<tr>
<td><?php echo htmlspecialchars($day_off['start_date']); ?></td>
<td><?php echo htmlspecialchars($day_off['end_date']); ?></td>
<td><?php echo htmlspecialchars($day_off['description'] ?? ''); ?></td>
<td>
<button type="button" class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editDayOffModal"
onclick='fillEditDayOffModal(<?php echo json_encode([
"id" => $day_off['id'],
"start_date" => $day_off['start_date'],
"end_date" => $day_off['end_date'],
"description" => htmlspecialchars($day_off['description'] ?? '', ENT_QUOTES)
]); ?>)'>
Modifica
</button>
<form action="" method="POST" style="display:inline;" onsubmit="return confirm('Sei sicuro di voler eliminare questo giorno di chiusura?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?php echo $day_off['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger">Elimina</button>
</form>
</td>
<th>Data</th>
<th>Titolo / Descrizione</th>
<th>Ricorrente</th>
<th>Azioni</th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</thead>
<tbody>
<?php foreach ($days_off as $day): ?>
<tr>
<td><?= htmlspecialchars($day['date']) ?></td>
<td><?= htmlspecialchars($day['title'] ?: $day['description'] ?: 'Chiusura') ?></td>
<td>
<?php if ($day['is_recurring']): ?>
<span class="badge bg-success"> (ogni anno)</span>
<?php else: ?>
<span class="badge bg-secondary">No</span>
<?php endif; ?>
</td>
<td>
<button type="button" class="btn btn-sm btn-warning me-1"
data-bs-toggle="modal" data-bs-target="#editDayOffModal"
onclick='fillEditModal(<?= json_encode([
"id" => $day['id'],
"date" => $day['date'],
"title" => htmlspecialchars($day['title'] ?? '', ENT_QUOTES),
"description" => htmlspecialchars($day['description'] ?? '', ENT_QUOTES)
]) ?>)'>
<i class="bx bx-edit"></i> Modifica
</button>
<form action="" method="POST" style="display:inline;"
onsubmit="return confirm('Confermi l\'eliminazione?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= $day['id'] ?>">
<button type="submit" class="btn btn-sm btn-danger">
<i class="bx bx-trash"></i> Elimina
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!--end page wrapper -->
<!--start overlay-->
<div class="overlay toggle-icon"></div>
<!--end overlay-->
<!--Start Back To Top Button-->
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
<!--End Back To Top Button-->
<?php include('include/footer.php'); ?>
</div>
<!--end wrapper-->
<!-- Modale per aggiungere un giorno di chiusura -->
<div class="modal fade" id="addDayOffModal" tabindex="-1" aria-labelledby="addDayOffModalLabel" aria-hidden="true">
<!-- Modal Aggiungi -->
<div class="modal fade" id="addDayOffModal" tabindex="-1" aria-labelledby="addDayOffModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="addDayOffModalLabel">Aggiungi Giorno di Chiusura</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="add">
<div class="mb-3">
<label for="add_day_off_start_date" class="form-label">Data Inizio</label>
<input type="date" class="form-control" id="add_day_off_start_date" name="start_date" required>
<label class="form-label fw-bold">Data <span class="text-danger">*</span></label>
<input type="date" class="form-control" name="start_date" required>
</div>
<div class="mb-3">
<label for="add_day_off_end_date" class="form-label">Data Fine</label>
<input type="date" class="form-control" id="add_day_off_end_date" name="end_date" required>
</div>
<div class="mb-3">
<label for="add_day_off_description" class="form-label">Descrizione</label>
<input type="text" class="form-control" id="add_day_off_description" name="description" placeholder="Es. Natale">
<label class="form-label fw-bold">Descrizione / Motivo</label>
<input type="text" class="form-control" name="description"
placeholder="Es: Ferragosto, Ferie estive, Corso formazione">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Chiudi</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button type="submit" class="btn btn-primary">Aggiungi</button>
</div>
</form>
@@ -260,34 +249,31 @@ $days_off = $stmt->fetchAll();
</div>
</div>
<!-- Modale per modificare un giorno di chiusura -->
<div class="modal fade" id="editDayOffModal" tabindex="-1" aria-labelledby="editDayOffModalLabel" aria-hidden="true">
<!-- Modal Modifica -->
<div class="modal fade" id="editDayOffModal" tabindex="-1" aria-labelledby="editDayOffModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="modal-header bg-warning text-dark">
<h5 class="modal-title" id="editDayOffModalLabel">Modifica Giorno di Chiusura</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" id="edit_day_off_id">
<input type="hidden" name="id" id="edit_id">
<div class="mb-3">
<label for="edit_day_off_start_date" class="form-label">Data Inizio</label>
<input type="date" class="form-control" id="edit_day_off_start_date" name="start_date" required>
<label class="form-label fw-bold">Data <span class="text-danger">*</span></label>
<input type="date" class="form-control" name="start_date" id="edit_date" required>
</div>
<div class="mb-3">
<label for="edit_day_off_end_date" class="form-label">Data Fine</label>
<input type="date" class="form-control" id="edit_day_off_end_date" name="end_date" required>
</div>
<div class="mb-3">
<label for="edit_day_off_description" class="form-label">Descrizione</label>
<input type="text" class="form-control" id="edit_day_off_description" name="description" placeholder="Es. Natale">
<label class="form-label fw-bold">Descrizione / Motivo</label>
<input type="text" class="form-control" name="description" id="edit_description"
placeholder="Es: Ferie natalizie">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Chiudi</button>
<button type="submit" class="btn btn-primary">Salva Modifiche</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button type="submit" class="btn btn-warning">Salva Modifiche</button>
</div>
</form>
</div>
@@ -296,21 +282,22 @@ $days_off = $stmt->fetchAll();
<?php include('jsinclude.php'); ?>
<!-- Script per inizializzare DataTables e gestire i modali -->
<script>
$(document).ready(function() {
$('#daysOffTable').DataTable({
"language": {
"url": "//cdn.datatables.net/plug-ins/1.10.25/i18n/Italian.json"
}
language: {
url: '//cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
},
order: [
[0, 'asc']
]
});
});
function fillEditDayOffModal(data) {
document.getElementById('edit_day_off_id').value = data.id;
document.getElementById('edit_day_off_start_date').value = data.start_date;
document.getElementById('edit_day_off_end_date').value = data.end_date;
document.getElementById('edit_day_off_description').value = data.description;
function fillEditModal(data) {
document.getElementById('edit_id').value = data.id;
document.getElementById('edit_date').value = data.date;
document.getElementById('edit_description').value = data.description || '';
}
</script>
</body>