Files
yogibook_aury_new/public/dayoff.php
T
2026-08-09 07:01:23 +02:00

785 lines
30 KiB
PHP

<?php
require_once('include/headscript.php');
/**
* Gestione giorni di pausa (dayoff): inserimento singolo o per intervallo,
* elenco in lista e vista calendario mensile con i giorni evidenziati.
*/
$pdo = DBHandlerSelect::getInstance()->getConnection();
function e($value): string
{
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}
$idTeacher = 1; // TODO: legare all'insegnante loggato quando ce ne sarà più d'uno
$message = $_GET['message'] ?? 'n';
$insertFeedback = null;
/* -------------------------------------------------------------------------
* Inserimento dayoff (POST): singolo giorno o intervallo, con PRG
* ---------------------------------------------------------------------- */
if (isset($_POST['submit']) || isset($_POST['submit_range'])) {
$startRaw = trim($_POST['dayoff_start'] ?? '');
$endRaw = trim($_POST['dayoff_end'] ?? '');
$startTs = $startRaw !== '' ? strtotime($startRaw) : false;
$endTs = $endRaw !== '' ? strtotime($endRaw) : $startTs;
if ($startTs === false) {
$insertFeedback = ['type' => 'error', 'text' => 'Inserisci almeno la data di inizio valida.'];
} elseif ($endTs < $startTs) {
$insertFeedback = ['type' => 'error', 'text' => 'La data di fine non può essere precedente a quella di inizio.'];
} else {
$daysSpan = (int) floor(($endTs - $startTs) / 86400) + 1;
if ($daysSpan > 366) {
$insertFeedback = ['type' => 'error', 'text' => 'L\'intervallo è troppo ampio (massimo un anno).'];
} else {
try {
$pdo->beginTransaction();
$stmt = $pdo->prepare(
"INSERT IGNORE INTO dayoff (idteacher, dayoffdate) VALUES (:idteacher, :dayoffdate)"
);
$inserted = 0;
$cursor = $startTs;
while ($cursor <= $endTs) {
$stmt->execute([':idteacher' => $idTeacher, ':dayoffdate' => date('Y-m-d', $cursor)]);
$inserted += $stmt->rowCount();
$cursor = strtotime('+1 day', $cursor);
}
$pdo->commit();
header('Location: ' . $_SERVER['PHP_SELF'] . '?message=inserted&n=' . $inserted);
exit;
} catch (Throwable $ex) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
error_log('dayoff insert error: ' . $ex->getMessage());
$insertFeedback = ['type' => 'error', 'text' => 'Errore durante l\'inserimento. Riprova.'];
}
}
}
}
/* -------------------------------------------------------------------------
* Elenco dayoff
* ---------------------------------------------------------------------- */
$stmt = $pdo->prepare("SELECT iddayoff, dayoffdate FROM dayoff WHERE idteacher = :idteacher ORDER BY dayoffdate DESC");
$stmt->execute([':idteacher' => $idTeacher]);
$documents = $stmt->fetchAll();
// Mappa data(Y-m-d) => iddayoff, passata al JS per la vista calendario
$dayoffMap = [];
foreach ($documents as $d) {
$ts = strtotime($d['dayoffdate']);
if ($ts) {
$dayoffMap[date('Y-m-d', $ts)] = (int) $d['iddayoff'];
}
}
$italianMonths = [
"January" => "Gennaio",
"February" => "Febbraio",
"March" => "Marzo",
"April" => "Aprile",
"May" => "Maggio",
"June" => "Giugno",
"July" => "Luglio",
"August" => "Agosto",
"September" => "Settembre",
"October" => "Ottobre",
"November" => "Novembre",
"December" => "Dicembre"
];
$fmtDate = function ($d) use ($italianMonths) {
$ts = strtotime($d);
if (!$ts) return e($d);
return date('d', $ts) . ' ' . $italianMonths[date('F', $ts)] . ' ' . date('Y', $ts);
};
$insertedCount = isset($_GET['n']) ? (int) $_GET['n'] : null;
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8" />
<title>YogiBook - Giorni di pausa</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="YogiBook - Prenotazione facile YogaSoul" name="description" />
<meta content="Advanced Creative Solutions" name="author" />
<link rel="shortcut icon" href="assets/images/favicon.ico">
<link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
<link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
<link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
$("#dayoff_start").datepicker({
dateFormat: "yy-mm-dd",
changeYear: true,
changeMonth: true,
onSelect: function(d) {
$("#dayoff_end").datepicker("option", "minDate", d);
}
});
$("#dayoff_end").datepicker({
dateFormat: "yy-mm-dd",
changeYear: true,
changeMonth: true
});
});
function confirmRemoveDayoff(iddayoff, dateLabel) {
Swal.fire({
title: 'Rimuovere questo giorno?',
html: 'Stai per rimuovere il giorno di pausa <strong>' + dateLabel + '</strong>.',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#e74c3c',
cancelButtonColor: '#6b7280',
confirmButtonText: 'Sì, rimuovi',
cancelButtonText: 'Annulla'
}).then((result) => {
if (result.isConfirmed) {
window.location.href = 'removedayoff.php?iddayoff=' + iddayoff;
}
});
}
</script>
<style>
.admin-card {
border: none;
border-radius: 14px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
}
.admin-card .card-body {
padding: 26px 30px;
}
.section-title {
font-size: 15px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
color: #6b7280;
margin-bottom: 4px;
}
.section-lead {
color: #9ca3af;
font-size: 14px;
margin-bottom: 22px;
}
.form-label {
font-weight: 600;
font-size: 14px;
color: #374151;
}
.form-control {
border-radius: 8px;
border: 1px solid #e2e4e9;
padding: 10px 12px;
}
.form-control:focus {
border-color: #1ebf73;
box-shadow: 0 0 0 3px rgba(30, 191, 115, 0.12);
}
.btn-save {
border-radius: 8px;
padding: 10px 26px;
font-weight: 600;
}
.hint {
font-size: 12px;
color: #9ca3af;
margin-top: 6px;
}
.dayoff-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 16px;
border-radius: 10px;
background: #f8fafc;
border: 1px solid #eef0f3;
margin-bottom: 8px;
}
.dayoff-item:hover {
background: #f1f6fb;
}
.dayoff-date {
display: flex;
align-items: center;
gap: 12px;
}
.dayoff-icon {
width: 40px;
height: 40px;
border-radius: 10px;
background: #fdecea;
color: #c0392b;
display: flex;
align-items: center;
justify-content: center;
font-size: 17px;
}
.dayoff-label {
font-weight: 600;
color: #1f2937;
}
.btn-remove {
border: none;
border-radius: 8px;
padding: 7px 14px;
font-size: 13px;
font-weight: 600;
color: #fff;
background: #e74c3c;
cursor: pointer;
transition: filter 0.15s;
}
.btn-remove:hover {
filter: brightness(0.93);
}
.empty-state {
text-align: center;
padding: 30px 10px;
color: #9ca3af;
}
.empty-state i {
font-size: 32px;
margin-bottom: 8px;
display: block;
color: #d5d9e0;
}
/* ---- Toggle vista ---- */
.view-toggle {
display: inline-flex;
background: #eef2f7;
border-radius: 10px;
padding: 4px;
gap: 4px;
}
.view-toggle button {
border: none;
background: transparent;
padding: 7px 16px;
border-radius: 8px;
font-size: 13px;
font-weight: 600;
color: #6b7280;
cursor: pointer;
transition: all 0.15s;
}
.view-toggle button.active {
background: #fff;
color: #1ebf73;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}
/* ---- Calendario ---- */
.cal-nav {
display: flex;
align-items: center;
justify-content: center;
gap: 20px;
margin-bottom: 18px;
}
.cal-nav h4 {
margin: 0;
font-size: 18px;
font-weight: 700;
color: #374151;
min-width: 200px;
text-align: center;
}
.cal-nav button {
border: none;
background: #eef6f1;
color: #1ebf73;
width: 36px;
height: 36px;
border-radius: 10px;
cursor: pointer;
transition: filter 0.15s;
}
.cal-nav button:hover {
filter: brightness(0.94);
}
.calendar-grid {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 6px;
}
.cal-dow {
text-align: center;
font-size: 12px;
font-weight: 700;
text-transform: uppercase;
color: #9ca3af;
padding: 6px 0;
}
.cal-cell {
aspect-ratio: 1 / 1;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 15px;
font-weight: 600;
background: #f8fafc;
border: 1px solid #eef0f3;
color: #374151;
position: relative;
}
.cal-cell.empty {
background: transparent;
border: none;
}
.cal-cell.today {
border-color: #1ebf73;
border-width: 2px;
}
.cal-cell.dayoff {
background: #e74c3c;
color: #fff;
border-color: #e74c3c;
cursor: pointer;
transition: filter 0.15s;
}
.cal-cell.dayoff:hover {
filter: brightness(1.08);
}
.cal-cell.dayoff::after {
content: '\f0f4';
font-family: 'Font Awesome 6 Free';
font-weight: 900;
position: absolute;
bottom: 3px;
right: 5px;
font-size: 9px;
opacity: 0.8;
}
.cal-cell.free {
cursor: pointer;
}
.cal-cell.free:hover {
background: #eef6f1;
border-color: #1ebf73;
}
.cal-cell.range-start {
background: #1ebf73;
color: #fff;
border-color: #1ebf73;
}
.cal-selection-hint {
text-align: center;
margin-top: 14px;
padding: 10px 14px;
background: #eef6f1;
border-radius: 10px;
font-size: 13px;
color: #1a7f52;
}
.cal-legend {
display: flex;
gap: 18px;
justify-content: center;
margin-top: 16px;
font-size: 13px;
color: #6b7280;
}
.cal-legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend-dot {
width: 14px;
height: 14px;
border-radius: 4px;
display: inline-block;
}
</style>
</head>
<body>
<div id="layout-wrapper">
<header id="page-topbar" class="isvertical-topbar">
<div class="navbar-header">
<div class="d-flex">
<?php include('include/logoarea.php'); ?>
<button type="button" class="btn btn-sm px-3 font-size-24 header-item waves-effect vertical-menu-btn">
<i class="bx bx-menu align-middle"></i>
</button>
<div class="page-title-box align-self-center d-none d-md-block">
<h4 class="page-title mb-0">Giorni di pausa</h4>
</div>
</div>
<div class="d-flex">
<?php include('include/languageselection.php'); ?>
<?php include('include/profiletopbar.php'); ?>
</div>
</div>
</header>
<?php include('include/sidebar.php'); ?>
<header class="ishorizontal-topbar">
<div class="navbar-header">
<div class="d-flex"></div>
</div>
<div class="topnav">
<div class="container-fluid">
<nav class="navbar navbar-light navbar-expand-lg topnav-menu"></nav>
</div>
</div>
</header>
<div class="main-content">
<div class="page-content">
<div class="container-fluid">
<?php if ($message === 'success') : ?>
<div class="alert alert-success" role="alert">Giorno di pausa rimosso con successo.</div>
<?php elseif ($message === 'inserted') : ?>
<div class="alert alert-success" role="alert">
<?php if ($insertedCount !== null && $insertedCount > 0) : ?>
<?php echo $insertedCount === 1 ? 'Giorno di pausa inserito' : $insertedCount . ' giorni di pausa inseriti'; ?> con successo.
<?php else : ?>
Nessun nuovo giorno inserito (erano già tutti presenti).
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ($insertFeedback) : ?>
<div class="alert alert-<?php echo $insertFeedback['type'] === 'success' ? 'success' : 'danger'; ?>" role="alert">
<?php echo e($insertFeedback['text']); ?>
</div>
<?php endif; ?>
<!-- Inserimento -->
<div class="row">
<div class="col-12">
<div class="card admin-card mb-4">
<div class="card-body">
<div class="section-title">Nuovo giorno o periodo di pausa</div>
<p class="section-lead">Aggiungi un singolo giorno o un intervallo: tutte le date nel periodo verranno escluse dalle lezioni.</p>
<form method="post">
<div class="row">
<div class="col-md-5">
<div class="mb-3">
<label for="dayoff_start" class="form-label">Data inizio</label>
<input type="text" id="dayoff_start" class="form-control" name="dayoff_start" placeholder="AAAA-MM-GG" required>
</div>
</div>
<div class="col-md-5">
<div class="mb-3">
<label for="dayoff_end" class="form-label">Data fine <span style="font-weight:400;color:#9ca3af;">(opzionale)</span></label>
<input type="text" id="dayoff_end" class="form-control" name="dayoff_end" placeholder="AAAA-MM-GG">
<div class="hint">Lascia vuoto per inserire un solo giorno.</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary btn-save" name="submit">Inserisci</button>
</form>
</div>
</div>
</div>
</div>
<!-- Elenco con toggle Lista/Calendario -->
<div class="row">
<div class="col-12">
<div class="card admin-card">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center flex-wrap mb-3" style="gap:12px;">
<div>
<div class="section-title">Giorni di pausa programmati</div>
<p class="section-lead mb-0">Ciao <?php echo e($firstname); ?>, questi sono i giorni esclusi dalle lezioni.</p>
</div>
<div class="view-toggle">
<button type="button" id="btn-list" class="active" onclick="switchView('list')"><i class="fas fa-list me-1"></i> Lista</button>
<button type="button" id="btn-cal" onclick="switchView('cal')"><i class="fas fa-calendar-alt me-1"></i> Calendario</button>
</div>
</div>
<!-- Vista LISTA -->
<div id="view-list">
<?php if (empty($documents)) : ?>
<div class="empty-state">
<i class="fas fa-calendar-day"></i>
Nessun giorno di pausa impostato.
</div>
<?php else : ?>
<?php foreach ($documents as $d) :
$idoff = (int) $d['iddayoff'];
$label = $fmtDate($d['dayoffdate']);
?>
<div class="dayoff-item">
<div class="dayoff-date">
<div class="dayoff-icon"><i class="fas fa-mug-hot"></i></div>
<span class="dayoff-label"><?php echo e($label); ?></span>
</div>
<button type="button" class="btn-remove" onclick="confirmRemoveDayoff(<?php echo $idoff; ?>, '<?php echo e($label); ?>')">
<i class="fas fa-trash"></i> Rimuovi
</button>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Vista CALENDARIO -->
<div id="view-cal" style="display:none;">
<div class="cal-nav">
<button type="button" onclick="calPrev()"><i class="fas fa-chevron-left"></i></button>
<h4 id="cal-title"></h4>
<button type="button" onclick="calNext()"><i class="fas fa-chevron-right"></i></button>
</div>
<div class="calendar-grid" id="cal-grid"></div>
<div class="cal-legend">
<span><span class="legend-dot" style="background:#e74c3c;"></span> Giorno di pausa</span>
<span><span class="legend-dot" style="background:#fff;border:2px solid #1ebf73;"></span> Oggi</span>
</div>
<div class="cal-selection-hint" id="cal-selection-hint" style="display:none;"></div>
<p class="text-center mt-3 mb-0" style="font-size:13px;color:#9ca3af;">
<i class="fas fa-hand-pointer me-1"></i>
Clicca un giorno libero per aggiungere una pausa (anche un intervallo), o un giorno rosso per rimuoverlo.
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
</div>
</div>
<script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/libs/metismenujs/metismenujs.min.js"></script>
<script src="assets/libs/simplebar/simplebar.min.js"></script>
<script src="assets/libs/eva-icons/eva.min.js"></script>
<script src="assets/js/app.js"></script>
<script>
// Dati dayoff dal PHP: { "YYYY-MM-DD": iddayoff, ... }
const dayoffMap = <?php echo json_encode($dayoffMap, JSON_UNESCAPED_UNICODE); ?>;
const monthNames = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
const dowNames = ['Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab', 'Dom'];
let calYear, calMonth;
let rangeStart = null; // prima data selezionata per un nuovo intervallo (stringa YYYY-MM-DD)
function switchView(which) {
const isList = which === 'list';
document.getElementById('view-list').style.display = isList ? 'block' : 'none';
document.getElementById('view-cal').style.display = isList ? 'none' : 'block';
document.getElementById('btn-list').classList.toggle('active', isList);
document.getElementById('btn-cal').classList.toggle('active', !isList);
if (!isList) {
rangeStart = null;
renderCalendar();
}
}
function calPrev() {
calMonth--;
if (calMonth < 0) {
calMonth = 11;
calYear--;
}
renderCalendar();
}
function calNext() {
calMonth++;
if (calMonth > 11) {
calMonth = 0;
calYear++;
}
renderCalendar();
}
function pad(n) {
return String(n).padStart(2, '0');
}
function labelFor(dateStr) {
const p = dateStr.split('-');
return parseInt(p[2], 10) + ' ' + monthNames[parseInt(p[1], 10) - 1] + ' ' + p[0];
}
// Submit programmatico del form di inserimento riusando il POST sicuro.
// NB: evitiamo name="submit" perché sovrascriverebbe f.submit() rendendolo
// non richiamabile. Usiamo un campo dedicato e submit() dal prototype.
function submitRange(startStr, endStr) {
const f = document.createElement('form');
f.method = 'post';
f.action = window.location.pathname;
f.innerHTML =
'<input type="hidden" name="dayoff_start" value="' + startStr + '">' +
'<input type="hidden" name="dayoff_end" value="' + endStr + '">' +
'<input type="hidden" name="submit_range" value="1">';
document.body.appendChild(f);
HTMLFormElement.prototype.submit.call(f);
}
// Click su un giorno LIBERO: gestisce la selezione dell'intervallo
function handleFreeDayClick(dateStr) {
if (rangeStart === null) {
// Primo click: fissa l'inizio
rangeStart = dateStr;
renderCalendar();
return;
}
// Secondo click: ordina le due date e conferma
let a = rangeStart,
b = dateStr;
if (b < a) {
const t = a;
a = b;
b = t;
}
rangeStart = null;
const sameDay = (a === b);
Swal.fire({
title: sameDay ? 'Aggiungere questo giorno?' : 'Aggiungere questo periodo?',
html: sameDay ?
'Vuoi impostare come pausa il giorno <strong>' + labelFor(a) + '</strong>?' :
'Vuoi impostare come pausa tutti i giorni da <strong>' + labelFor(a) + '</strong> a <strong>' + labelFor(b) + '</strong>?',
icon: 'question',
showCancelButton: true,
confirmButtonColor: '#1ebf73',
cancelButtonColor: '#6b7280',
confirmButtonText: 'Sì, aggiungi',
cancelButtonText: 'Annulla'
}).then((result) => {
if (result.isConfirmed) {
submitRange(a, b);
} else {
renderCalendar();
}
});
}
function renderCalendar() {
const grid = document.getElementById('cal-grid');
document.getElementById('cal-title').textContent = monthNames[calMonth] + ' ' + calYear;
grid.innerHTML = '';
dowNames.forEach(function(d) {
const h = document.createElement('div');
h.className = 'cal-dow';
h.textContent = d;
grid.appendChild(h);
});
const firstDow = (new Date(calYear, calMonth, 1).getDay() + 6) % 7;
const daysInMonth = new Date(calYear, calMonth + 1, 0).getDate();
for (let i = 0; i < firstDow; i++) {
const empty = document.createElement('div');
empty.className = 'cal-cell empty';
grid.appendChild(empty);
}
const todayStr = (function() {
const t = new Date();
return t.getFullYear() + '-' + pad(t.getMonth() + 1) + '-' + pad(t.getDate());
})();
for (let day = 1; day <= daysInMonth; day++) {
const dateStr = calYear + '-' + pad(calMonth + 1) + '-' + pad(day);
const cell = document.createElement('div');
cell.className = 'cal-cell';
cell.textContent = day;
if (dateStr === todayStr) cell.classList.add('today');
if (rangeStart === dateStr) cell.classList.add('range-start');
if (dayoffMap.hasOwnProperty(dateStr)) {
// Giorno di pausa: click per rimuovere
cell.classList.add('dayoff');
cell.title = 'Giorno di pausa — clicca per rimuovere';
const iddayoff = dayoffMap[dateStr];
cell.addEventListener('click', function() {
confirmRemoveDayoff(iddayoff, labelFor(dateStr));
});
} else {
// Giorno libero: click per aggiungere (singolo o range)
cell.classList.add('free');
cell.title = rangeStart === null ?
'Clicca per aggiungere una pausa' :
'Clicca per chiudere l\'intervallo';
cell.addEventListener('click', function() {
handleFreeDayClick(dateStr);
});
}
grid.appendChild(cell);
}
// Suggerimento selezione in corso
const hint = document.getElementById('cal-selection-hint');
if (hint) {
hint.style.display = rangeStart ? 'block' : 'none';
if (rangeStart) hint.innerHTML = 'Inizio selezionato: <strong>' + labelFor(rangeStart) + '</strong> — clicca un altro giorno per chiudere l\'intervallo, oppure lo stesso per un giorno singolo.';
}
}
(function() {
const now = new Date();
calYear = now.getFullYear();
calMonth = now.getMonth();
})();
</script>
</body>
</html>