Files
yogibook_aury_new/public/admin-services.php
T

702 lines
38 KiB
PHP

<?php
require_once('include/headscript.php');
/**
* Connessione unica PDO (singleton).
*/
$pdo = DBHandlerSelect::getInstance()->getConnection();
function e($value): string
{
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}
function teacherInitials(string $first, string $last): string
{
$ini = mb_strtoupper(mb_substr(trim($first), 0, 1) . mb_substr(trim($last), 0, 1));
return $ini !== '' ? $ini : '?';
}
function teacherColor(string $seed): string
{
$palette = ['#1ebf73', '#2980b9', '#8e44ad', '#e67e22', '#16a085', '#c0392b', '#2c3e50', '#b08d57'];
return $palette[abs(crc32($seed)) % count($palette)];
}
$message = $_GET['message'] ?? 'N';
$insertFeedback = null;
/* -------------------------------------------------------------------------
* Insegnanti (titolare per primo, poi attivi, poi alfabetico)
* ---------------------------------------------------------------------- */
$teachers = $pdo->query(
"SELECT idemployee, nameemployee, surnameemployee, photo, is_owner, active
FROM employee
ORDER BY is_owner DESC, active DESC, surnameemployee, nameemployee"
)->fetchAll();
$teachersById = [];
$ownerId = 0;
foreach ($teachers as $t) {
$teachersById[(int) $t['idemployee']] = $t;
if ($t['is_owner'] === 'Y' && !$ownerId) {
$ownerId = (int) $t['idemployee'];
}
}
/* -------------------------------------------------------------------------
* Assegnazione insegnante a una classe esistente (POST)
* ---------------------------------------------------------------------- */
if (($_POST['action'] ?? '') === 'assignteacher') {
$idservice = (int) ($_POST['idservice'] ?? 0);
$idteacher = (int) ($_POST['idteacher'] ?? 0);
if ($idservice > 0 && isset($teachersById[$idteacher])) {
$stmt = $pdo->prepare("UPDATE service SET idteacher = :t WHERE idservice = :s");
$stmt->execute([':t' => $idteacher, ':s' => $idservice]);
header('Location: ' . $_SERVER['PHP_SELF'] . '?message=teacherassigned');
} else {
header('Location: ' . $_SERVER['PHP_SELF'] . '?message=teachererror');
}
exit;
}
/* -------------------------------------------------------------------------
* Inserimento nuova classe (POST) con prepared statement
* ---------------------------------------------------------------------- */
if (isset($_POST['classinsert']) && $_POST['classinsert'] === 'Y') {
$servicename = trim($_POST['servicename'] ?? '');
$wpcatalog = $_POST['wpcatalognumber'] ?? '';
$day = trim($_POST['classdayday'] ?? '');
$time = trim($_POST['classtime'] ?? '');
$category = $_POST['servicecategory'] ?? '';
$maxcapacity = $_POST['maxcapacity'] ?? '';
$colorclass = trim($_POST['colorclass'] ?? '');
$classduration = $_POST['classduration'] ?? '';
$idteacher = (int) ($_POST['idteacher'] ?? 0);
if ($servicename === '') {
$insertFeedback = ['type' => 'error', 'text' => 'La descrizione della classe è obbligatoria.'];
} elseif (!isset($teachersById[$idteacher])) {
$insertFeedback = ['type' => 'error', 'text' => 'Seleziona l\'insegnante della classe.'];
} else {
$sql = "INSERT INTO service
(servicename, wpcatalognumber, day, time, category, maxcapacity, colorclass, classduration, idteacher)
VALUES
(:servicename, :wpcatalog, :day, :time, :category, :maxcapacity, :colorclass, :classduration, :idteacher)";
$stmt = $pdo->prepare($sql);
$ok = $stmt->execute([
':servicename' => $servicename,
':wpcatalog' => $wpcatalog !== '' ? (int) $wpcatalog : null,
':day' => $day,
':time' => $time,
':category' => $category !== '' ? (int) $category : null,
':maxcapacity' => $maxcapacity !== '' ? (int) $maxcapacity : null,
':colorclass' => $colorclass,
':classduration' => $classduration !== '' ? (float) $classduration : null,
':idteacher' => $idteacher,
]);
if ($ok) {
// Redirect per evitare re-invio del form al refresh (pattern PRG)
header('Location: ' . $_SERVER['PHP_SELF'] . '?message=inserted');
exit;
}
$insertFeedback = ['type' => 'error', 'text' => 'Errore durante l\'inserimento della classe. Riprova.'];
}
}
/* -------------------------------------------------------------------------
* Categorie per il dropdown
* ---------------------------------------------------------------------- */
$categories = $pdo->query("SELECT idservicecategory, namecategory FROM servicecategory ORDER BY namecategory")->fetchAll();
/* -------------------------------------------------------------------------
* Elenco classi esistenti
* ---------------------------------------------------------------------- */
$services = $pdo->query(
"SELECT service.*, servicecategory.namecategory
FROM service
LEFT JOIN servicecategory ON service.category = servicecategory.idservicecategory
ORDER BY service.servicename"
)->fetchAll();
// Propagazioni già effettuate, raggruppate per servizio
$propagationsByService = [];
$propRows = $pdo->query(
"SELECT p.idpropagation, p.idservice, p.token, p.startdate, p.enddate, p.created_at,
COUNT(s.idserviceschedule) AS occorrenze
FROM propagation p
LEFT JOIN serviceschedule s ON s.propagation_token = p.token
GROUP BY p.idpropagation
ORDER BY p.created_at DESC"
)->fetchAll();
foreach ($propRows as $pr) {
$propagationsByService[$pr['idservice']][] = $pr;
}
/**
* Opzioni del select insegnanti.
* Gli insegnanti non attivi compaiono solo se già assegnati a quella classe.
*/
function teacherOptions(array $teachers, int $selectedId): string
{
$html = '';
$found = false;
foreach ($teachers as $t) {
$tid = (int) $t['idemployee'];
$isSel = $tid === $selectedId;
if ($t['active'] !== 'Y' && !$isSel) {
continue;
}
if ($isSel) $found = true;
$label = trim($t['nameemployee'] . ' ' . $t['surnameemployee'])
. ($t['is_owner'] === 'Y' ? ' (titolare)' : '')
. ($t['active'] !== 'Y' ? ' (non attivo)' : '');
$html .= '<option value="' . $tid . '"' . ($isSel ? ' selected' : '') . '>' . e($label) . '</option>';
}
if (!$found) {
$html = '<option value="" selected disabled>— Non assegnato —</option>' . $html;
}
return $html;
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8" />
<title>YogiBook - Inserimento e Propagazione Classi</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">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.8.1/spectrum.min.css">
<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 type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.8.1/spectrum.min.js"></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,
.form-select {
border-radius: 8px;
border: 1px solid #e2e4e9;
padding: 10px 12px;
}
.form-control:focus,
.form-select: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;
}
.class-table thead th {
font-size: 12px;
text-transform: uppercase;
letter-spacing: 0.03em;
color: #6b7280;
border-bottom: 2px solid #eef0f3;
}
.class-table td {
vertical-align: middle;
}
.color-dot {
display: inline-block;
width: 14px;
height: 14px;
border-radius: 50%;
border: 1px solid rgba(0, 0, 0, 0.1);
vertical-align: middle;
margin-right: 6px;
}
.expanded-row {
display: none;
}
.expanded-row.expanded {
display: table-row;
}
.expanded-content {
background: #f8fbf9;
border-radius: 10px;
padding: 18px 20px;
margin: 8px 0;
}
.action-btns .btn {
margin: 2px;
}
.teacher-cell {
display: flex;
align-items: center;
gap: 8px;
min-width: 210px;
}
.teacher-mini {
flex: 0 0 30px;
width: 30px;
height: 30px;
border-radius: 50%;
object-fit: cover;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-size: 11px;
font-weight: 800;
}
.teacher-mini.none {
background: #f3f4f6;
color: #c0c4cc;
}
.teacher-select {
padding: 5px 30px 5px 10px !important;
font-size: 13px;
}
.teacher-select.unassigned {
border-color: #f0b27a;
background-color: #fff8f1;
color: #b9770e;
}
</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">Inserimento e Propagazione Classi</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 === 'inserted') : ?>
<div class="alert alert-success" role="alert">Classe inserita con successo.</div>
<?php endif; ?>
<?php if ($message === 'teacherassigned') : ?>
<div class="alert alert-success" role="alert">Insegnante assegnato con successo.</div>
<?php endif; ?>
<?php if ($message === 'teachererror') : ?>
<div class="alert alert-danger" role="alert">Impossibile assegnare l'insegnante. Riprova.</div>
<?php endif; ?>
<?php if ($message === 'success') : ?>
<div class="alert alert-success" role="alert">Propagazione avvenuta con successo.</div>
<?php endif; ?>
<?php if ($message === 'propdeleted') : ?>
<div class="alert alert-success" role="alert">Propagazione eliminata con successo.</div>
<?php endif; ?>
<?php if ($message === 'confirmdelete') : ?>
<div class="alert alert-warning" role="alert">
Attenzione: questa propagazione ha <strong><?php echo (int) ($_GET['count'] ?? 0); ?></strong> prenotazioni utente collegate.
Vuoi eliminarla comunque insieme alle lezioni?
<a href="cancelpropagation.php?token=<?php echo e($_GET['token'] ?? ''); ?>&confirm=Y"
class="btn btn-danger btn-sm ms-2"
onclick="return confirm('Confermi la cancellazione definitiva?');">
Sì, elimina tutto
</a>
<a href="admin-services.php" class="btn btn-secondary btn-sm ms-1">Annulla</a>
</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; ?>
<?php if (empty($teachers)) : ?>
<div class="alert alert-warning" role="alert">
Non ci sono insegnanti: <a href="insegnanti.php">aggiungine almeno uno</a> prima di creare una classe.
</div>
<?php endif; ?>
<!-- Form inserimento classe -->
<div class="row">
<div class="col-12">
<div class="card admin-card mb-4">
<div class="card-body">
<div class="section-title">Nuova classe</div>
<p class="section-lead">Inserisci i dettagli della classe da aggiungere al catalogo.</p>
<form method="post" name="insertclass" id="insertclass">
<input type="hidden" name="classinsert" value="Y">
<div class="mb-3">
<label for="servicename" class="form-label">Descrizione classe</label>
<input type="text" class="form-control" placeholder="Es. Hatha Yoga" id="servicename" name="servicename" required>
</div>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label for="wpcatalognumber" class="form-label">WP catalog number</label>
<input type="text" class="form-control" placeholder="WP Number" id="wpcatalognumber" name="wpcatalognumber">
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<label for="classdayday" class="form-label">Giorno</label>
<select class="form-select" id="classdayday" name="classdayday">
<option value="">Seleziona</option>
<option value="Monday">Lunedì</option>
<option value="Tuesday">Martedì</option>
<option value="Wednesday">Mercoledì</option>
<option value="Thursday">Giovedì</option>
<option value="Friday">Venerdì</option>
<option value="Saturday">Sabato</option>
<option value="Sunday">Domenica</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<div class="mb-3">
<label for="classtime" class="form-label">Ora</label>
<input type="text" class="form-control" placeholder="Es. 18:30" id="classtime" name="classtime">
</div>
</div>
<div class="col-lg-4">
<div class="mb-3">
<label for="classduration" class="form-label">Durata (ore)</label>
<input type="text" class="form-control" placeholder="Es. 1.5" id="classduration" name="classduration">
</div>
</div>
<div class="col-lg-4">
<div class="mb-3">
<label for="servicecategory" class="form-label">Categoria</label>
<select name="servicecategory" id="servicecategory" class="form-select">
<option value="">Seleziona</option>
<?php foreach ($categories as $cat) : ?>
<option value="<?php echo e($cat['idservicecategory']); ?>">
<?php echo e($cat['namecategory']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<div class="mb-3">
<label for="maxcapacity" class="form-label">Capacità massima</label>
<input type="text" class="form-control" placeholder="Es. 12" id="maxcapacity" name="maxcapacity">
</div>
</div>
<div class="col-lg-4">
<div class="mb-3">
<label for="colorclass" class="form-label">Colore classe</label>
<input type="text" class="form-control" id="colorclass" name="colorclass" value="#1ebf73">
</div>
</div>
<div class="col-lg-4">
<div class="mb-3">
<label for="idteacher" class="form-label">Insegnante</label>
<select name="idteacher" id="idteacher" class="form-select" required>
<?php echo teacherOptions($teachers, $ownerId); ?>
</select>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary btn-save">Inserisci classe</button>
</form>
</div>
</div>
</div>
</div>
<!-- Elenco classi -->
<div class="row">
<div class="col-12">
<div class="card admin-card">
<div class="card-body">
<div class="section-title">Classi esistenti</div>
<p class="section-lead">Propaga una classe sul calendario, associala, assegna l'insegnante o modificane i dettagli.</p>
<div class="table-responsive">
<table class="table class-table align-middle mb-0">
<thead>
<tr>
<th>Classe</th>
<th>Giorno</th>
<th>Orario</th>
<th>Durata</th>
<th>Categoria</th>
<th>Insegnante</th>
<th class="text-end">Azioni</th>
</tr>
</thead>
<tbody>
<?php if (empty($services)) : ?>
<tr>
<td colspan="7" class="text-center text-muted py-4">Nessuna classe presente.</td>
</tr>
<?php else : ?>
<?php foreach ($services as $svc) :
$svcTeacherId = (int) $svc['idteacher'];
$svcTeacher = $teachersById[$svcTeacherId] ?? null;
?>
<tr>
<td>
<span class="color-dot" style="background-color: <?php echo e($svc['colorclass'] ?: '#ccc'); ?>;"></span>
<span class="font-size-14"><?php echo e($svc['servicename']); ?></span>
</td>
<td><?php echo e($svc['day']); ?></td>
<td><?php echo e($svc['time']); ?></td>
<td>
<i class="mdi mdi-clock-time-nine-outline align-middle font-size-16 me-1"></i>
<?php echo e($svc['classduration']); ?>
</td>
<td><?php echo e($svc['namecategory']); ?></td>
<td>
<form method="post" class="teacher-cell">
<input type="hidden" name="action" value="assignteacher">
<input type="hidden" name="idservice" value="<?php echo (int) $svc['idservice']; ?>">
<?php if ($svcTeacher && $svcTeacher['photo']) : ?>
<img class="teacher-mini" src="assets/images/teachers/<?php echo e($svcTeacher['photo']); ?>" alt="">
<?php elseif ($svcTeacher) :
$tName = trim($svcTeacher['nameemployee'] . ' ' . $svcTeacher['surnameemployee']); ?>
<span class="teacher-mini" style="background:<?php echo teacherColor($tName); ?>;"><?php echo e(teacherInitials($svcTeacher['nameemployee'], $svcTeacher['surnameemployee'])); ?></span>
<?php else : ?>
<span class="teacher-mini none"><i class="fas fa-user"></i></span>
<?php endif; ?>
<select name="idteacher" class="form-select teacher-select <?php echo $svcTeacher ? '' : 'unassigned'; ?>"
data-current="<?php echo $svcTeacherId; ?>"
data-classname="<?php echo e($svc['servicename'] . ' ' . $svc['day'] . ' ' . $svc['time']); ?>">
<?php echo teacherOptions($teachers, $svcTeacherId); ?>
</select>
</form>
</td>
<td class="text-end action-btns">
<button type="button" class="btn btn-primary btn-sm toggle-form">Propaga</button>
<a href="associate-services.php?id=<?php echo (int) $svc['idservice']; ?>">
<button type="button" class="btn btn-info btn-sm">Associa</button>
</a>
<a href="updateservice.php?idservice=<?php echo (int) $svc['idservice']; ?>" class="btn btn-warning btn-sm">
<i class="mdi mdi-lead-pencil font-size-16 align-middle"></i>
</a>
<a href="cancelservice.php?idservice=<?php echo (int) $svc['idservice']; ?>"
class="btn btn-danger btn-sm"
title="Elimina classe"
onclick="return confirm(<?php echo e(json_encode('Eliminare la classe ' . $svc['servicename'] . ' (' . $svc['day'] . ' ' . $svc['time'] . ')? L\'operazione non si può annullare.')); ?>);">
<i class="bx bx-block font-size-16 align-middle"></i>
</a>
</td>
</tr>
<tr class="expanded-row">
<td colspan="7">
<div class="expanded-content">
<form class="calendar-form" method="post" action="nextdateclass.php">
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label class="form-label">Data inizio</label>
<input type="text" class="datepicker form-control" name="propagatestartdate" required>
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<label class="form-label">Data fine</label>
<input type="text" class="datepicker form-control" name="propagateenddate" required>
</div>
</div>
</div>
<input type="hidden" name="idservice" value="<?php echo (int) $svc['idservice']; ?>">
<input type="hidden" name="timeclass" value="<?php echo e($svc['time']); ?>">
<input type="hidden" name="dayclass" value="<?php echo e($svc['day']); ?>">
<input type="hidden" name="durationtime" value="<?php echo e($svc['classduration']); ?>">
<button type="submit" class="btn btn-primary btn-save">Propaga sul calendario</button>
</form>
<?php $props = $propagationsByService[$svc['idservice']] ?? []; ?>
<?php if (!empty($props)) : ?>
<hr>
<div class="section-title" style="font-size:13px;">Propagazioni effettuate</div>
<table class="table table-sm mt-2 mb-0">
<thead>
<tr>
<th>Inizio</th>
<th>Fine</th>
<th>Lezioni</th>
<th>Token</th>
<th class="text-end">Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($props as $p) : ?>
<tr>
<td><?php echo e($p['startdate']); ?></td>
<td><?php echo e($p['enddate']); ?></td>
<td><?php echo e($p['occorrenze']); ?></td>
<td><code style="font-size:11px;"><?php echo e(substr((string)$p['token'], 0, 10)); ?>…</code></td>
<td class="text-end">
<a href="cancelpropagation.php?token=<?php echo e($p['token']); ?>"
class="btn btn-danger btn-sm"
onclick="return confirm('Eliminare questa propagazione e tutte le sue lezioni?');">
Elimina
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</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>
$(document).ready(function() {
// Datepicker sui campi di propagazione
$(".datepicker").datepicker({
dateFormat: "yy-mm-dd"
});
// Color picker spectrum collegato al VERO campo colorclass (ora viene salvato)
$('#colorclass').spectrum({
preferredFormat: 'hex',
showInput: true,
showPalette: true,
palette: [
["#FF0000", "#00FF00", "#0000FF"],
["#1ebf73", "#f39c12", "#8e44ad"]
]
});
// Forza il valore del colore nel campo prima dell'invio del form
$('#insertclass').on('submit', function() {
var col = $('#colorclass').spectrum('get');
if (col) {
$('#colorclass').val(col.toHexString());
}
});
// Cambio insegnante dalla tabella: conferma e salva
$('.teacher-select').on('change', function() {
var sel = $(this);
var teacherName = sel.find('option:selected').text();
if (confirm('Assegnare ' + teacherName + ' alla classe ' + sel.data('classname') + '?')) {
sel.closest('form')[0].submit();
} else {
sel.val(String(sel.data('current')) === '0' ? '' : String(sel.data('current')));
}
});
});
// Espansione riga propagazione
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll(".toggle-form").forEach(function(button) {
button.addEventListener("click", function() {
var row = this.closest("tr");
var expandedRow = row.nextElementSibling;
if (expandedRow && expandedRow.classList.contains("expanded-row")) {
expandedRow.classList.toggle("expanded");
}
});
});
});
</script>
</body>
</html>