Files
yogibook_aury_new/public/admin-services.php
T

913 lines
48 KiB
PHP

<?php
require_once('include/headscript.php');
/**
* Catalogo classi (service) della scuola corrente:
* inserimento, assegnazione insegnante, propagazione sul calendario.
*/
SchoolContext::requireSchoolAdmin();
$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 della scuola (titolare per primo, poi attivi, poi alfabetico)
* ---------------------------------------------------------------------- */
$st = $pdo->prepare(
"SELECT idemployee, nameemployee, surnameemployee, photo, is_owner, active
FROM employee
WHERE idschool = ?
ORDER BY is_owner DESC, active DESC, surnameemployee, nameemployee"
);
$st->execute([$idschool]);
$teachers = $st->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 (SchoolScope::belongs('service', $idservice) && isset($teachersById[$idteacher])) {
$pdo->prepare("UPDATE service SET idteacher = ? WHERE idservice = ? AND idschool = ?")
->execute([$idteacher, $idservice, $idschool]);
header('Location: admin-services.php?message=teacherassigned');
} else {
header('Location: admin-services.php?message=teachererror');
}
exit;
}
/* -------------------------------------------------------------------------
* Gestione categorie (POST): aggiungi / rinomina / elimina
* ---------------------------------------------------------------------- */
$catAction = $_POST['cataction'] ?? '';
if ($catAction !== '') {
$catName = trim((string) ($_POST['namecategory'] ?? ''));
$catId = (int) ($_POST['idservicecategory'] ?? 0);
$nameOk = $catName !== '' && mb_strlen($catName) <= 50;
if (in_array($catAction, ['add', 'rename'], true)) {
if (!$nameOk) {
header('Location: admin-services.php?message=catinvalid');
exit;
}
$dup = $pdo->prepare("SELECT COUNT(*) FROM servicecategory WHERE idschool = ? AND namecategory = ? AND idservicecategory <> ?");
$dup->execute([$idschool, $catName, $catId]);
if ((int) $dup->fetchColumn() > 0) {
header('Location: admin-services.php?message=catdup');
exit;
}
}
if ($catAction === 'add') {
$pdo->prepare("INSERT INTO servicecategory (idschool, namecategory) VALUES (?, ?)")->execute([$idschool, $catName]);
header('Location: admin-services.php?message=catadded');
exit;
}
if ($catAction === 'rename' && SchoolScope::belongs('servicecategory', $catId)) {
$pdo->prepare("UPDATE servicecategory SET namecategory = ? WHERE idservicecategory = ? AND idschool = ?")
->execute([$catName, $catId, $idschool]);
header('Location: admin-services.php?message=catrenamed');
exit;
}
if ($catAction === 'delete' && SchoolScope::belongs('servicecategory', $catId)) {
$used = $pdo->prepare("SELECT COUNT(*) FROM service WHERE category = ? AND idschool = ?");
$used->execute([$catId, $idschool]);
if ((int) $used->fetchColumn() > 0) {
header('Location: admin-services.php?message=catinuse');
exit;
}
$pdo->prepare("DELETE FROM servicecategory WHERE idservicecategory = ? AND idschool = ?")->execute([$catId, $idschool]);
header('Location: admin-services.php?message=catdeleted');
exit;
}
header('Location: admin-services.php?message=notfound');
exit;
}
/* -------------------------------------------------------------------------
* Categorie della scuola (servono anche per validare l'inserimento)
* ---------------------------------------------------------------------- */
$st = $pdo->prepare("SELECT idservicecategory, namecategory FROM servicecategory WHERE idschool = ? ORDER BY namecategory");
$st->execute([$idschool]);
$categories = $st->fetchAll();
$categoryIds = array_map('intval', array_column($categories, 'idservicecategory'));
$st = $pdo->prepare("SELECT category, COUNT(*) AS n FROM service WHERE idschool = ? GROUP BY category");
$st->execute([$idschool]);
$catUsage = array_column($st->fetchAll(), 'n', 'category');
/* -------------------------------------------------------------------------
* Inserimento nuova classe (POST)
* ---------------------------------------------------------------------- */
if (($_POST['classinsert'] ?? '') === 'Y') {
$servicename = trim($_POST['servicename'] ?? '');
$wpcatalog = preg_replace('/[^0-9,]/', '', (string) ($_POST['wpcatalognumber'] ?? ''));
$day = trim($_POST['classdayday'] ?? '');
$time = trim($_POST['classtime'] ?? '');
$category = (int) ($_POST['servicecategory'] ?? 0);
$maxcapacity = trim((string) ($_POST['maxcapacity'] ?? ''));
$colorclass = trim($_POST['colorclass'] ?? '');
$classduration = str_replace(',', '.', trim((string) ($_POST['classduration'] ?? '')));
$idteacher = (int) ($_POST['idteacher'] ?? 0);
$validDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
if ($servicename === '') {
$insertFeedback = ['type' => 'error', 'text' => 'La descrizione della classe è obbligatoria.'];
} elseif (!in_array($day, $validDays, true)) {
$insertFeedback = ['type' => 'error', 'text' => 'Seleziona il giorno della classe.'];
} elseif (!preg_match('/^\d{1,2}:\d{2}$/', $time)) {
$insertFeedback = ['type' => 'error', 'text' => 'Inserisci l\'ora nel formato HH:MM (es. 18:30).'];
} elseif (!isset($teachersById[$idteacher])) {
$insertFeedback = ['type' => 'error', 'text' => 'Seleziona l\'insegnante della classe.'];
} else {
$stmt = $pdo->prepare(
"INSERT INTO service
(idschool, servicename, wpcatalognumber, day, time, category, maxcapacity, colorclass, classduration, idteacher, idlocation)
VALUES
(:idschool, :servicename, :wpcatalog, :day, :time, :category, :maxcapacity, :colorclass, :classduration, :idteacher, 0)"
);
$ok = $stmt->execute([
':idschool' => $idschool,
':servicename' => $servicename,
':wpcatalog' => $wpcatalog !== '' ? $wpcatalog : null,
':day' => $day,
':time' => $time,
':category' => in_array($category, $categoryIds, true) ? $category : 0,
':maxcapacity' => $maxcapacity !== '' ? (int) $maxcapacity : null,
':colorclass' => preg_match('/^#[0-9a-fA-F]{6}$/', $colorclass) ? $colorclass : '#1ebf73',
':classduration' => is_numeric($classduration) ? (float) $classduration : 1,
':idteacher' => $idteacher,
]);
if ($ok) {
header('Location: admin-services.php?message=inserted');
exit;
}
$insertFeedback = ['type' => 'error', 'text' => 'Errore durante l\'inserimento della classe. Riprova.'];
}
}
/* -------------------------------------------------------------------------
* Elenco classi della scuola
* ---------------------------------------------------------------------- */
$st = $pdo->prepare(
"SELECT service.*, servicecategory.namecategory
FROM service
LEFT JOIN servicecategory ON service.category = servicecategory.idservicecategory
WHERE service.idschool = ?
ORDER BY service.servicename"
);
$st->execute([$idschool]);
$services = $st->fetchAll();
// Propagazioni della scuola, raggruppate per servizio
$propagationsByService = [];
$st = $pdo->prepare(
"SELECT p.idpropagation, p.idservice, p.token, p.startdate, p.enddate, p.created_at,
(SELECT COUNT(*) FROM serviceschedule s
WHERE s.propagation_token = p.token AND s.idschool = p.idschool) AS occorrenze
FROM propagation p
WHERE p.idschool = ?
ORDER BY p.created_at DESC"
);
$st->execute([$idschool]);
foreach ($st->fetchAll() 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;
}
$dayLabels = [
'Monday' => 'Lunedì',
'Tuesday' => 'Martedì',
'Wednesday' => 'Mercoledì',
'Thursday' => 'Giovedì',
'Friday' => 'Venerdì',
'Saturday' => 'Sabato',
'Sunday' => 'Domenica',
];
$msgMap = [
'inserted' => ['success', 'Classe inserita con successo.'],
'updated' => ['success', 'Classe aggiornata con successo.'],
'deleted' => ['success', 'Classe eliminata con successo.'],
'teacherassigned' => ['success', 'Insegnante assegnato con successo.'],
'success' => ['success', 'Propagazione avvenuta con successo.'],
'propdeleted' => ['success', 'Propagazione eliminata con successo.'],
'teachererror' => ['danger', 'Impossibile assegnare l\'insegnante. Riprova.'],
'error' => ['danger', 'Dati non validi per la propagazione.'],
'notfound' => ['danger', 'Elemento non trovato in questa scuola.'],
'hasdata' => ['warning', 'Questa classe ha lezioni o ordini collegati e non può essere eliminata. Elimina prima le propagazioni; se ci sono ordini, la classe va mantenuta per lo storico.'],
'catadded' => ['success', 'Categoria aggiunta.'],
'catrenamed' => ['success', 'Categoria rinominata.'],
'catdeleted' => ['success', 'Categoria eliminata.'],
'catinvalid' => ['danger', 'Nome categoria obbligatorio (massimo 50 caratteri).'],
'catdup' => ['danger', 'Esiste già una categoria con questo nome.'],
'catinuse' => ['warning', 'Categoria usata da una o più classi: cambia prima la categoria di quelle classi.'],
'range' => ['danger', 'Intervallo non valido: la data di fine deve seguire l\'inizio e il periodo non può superare un anno.'],
];
?>
<!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" /><?php include('include/schooltheme.php'); ?>
<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 src="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.8.1/spectrum.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></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;
}
.cat-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 16px;
}
.cat-chip {
display: inline-flex;
align-items: center;
gap: 6px;
background: #f3f4f6;
border-radius: 20px;
padding: 5px 6px 5px 14px;
font-size: 13px;
font-weight: 600;
color: #374151;
}
.cat-chip .cat-count {
background: #fff;
border-radius: 20px;
padding: 1px 8px;
font-size: 11px;
color: #6b7280;
}
.cat-chip button {
border: none;
background: transparent;
color: #9ca3af;
padding: 2px 5px;
border-radius: 50%;
cursor: pointer;
}
.cat-chip button:hover {
color: #374151;
background: #fff;
}
.cat-chip button.cat-del:hover {
color: #c0392b;
}
.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/schoolswitcher.php'); ?><?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 (isset($msgMap[$message])) : ?>
<div class="alert alert-<?php echo $msgMap[$message][0]; ?>" role="alert"><?php echo e($msgMap[$message][1]); ?></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 e alle prenotazioni?
<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-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" maxlength="50" 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="Es. 123 oppure 123,456" 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" required>
<option value="">Seleziona</option>
<?php foreach ($dayLabels as $k => $lbl) : ?>
<option value="<?php echo $k; ?>"><?php echo $lbl; ?></option>
<?php endforeach; ?>
</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="time" class="form-control" id="classtime" name="classtime" required>
</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 (int) $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="number" min="1" 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>
<!-- Categorie -->
<div class="row">
<div class="col-12">
<div class="card admin-card mb-4">
<div class="card-body">
<div class="section-title">Categorie</div>
<p class="section-lead">Raggruppano le classi (es. Yoga, Pilates, Meditazione). Una categoria usata da qualche classe non può essere eliminata.</p>
<?php if (!empty($categories)) : ?>
<div class="cat-list">
<?php foreach ($categories as $cat) :
$cid = (int) $cat['idservicecategory'];
$used = (int) ($catUsage[$cid] ?? 0); ?>
<span class="cat-chip">
<?php echo e($cat['namecategory']); ?>
<span class="cat-count"><?php echo $used; ?></span>
<button type="button" class="cat-rename" title="Rinomina" data-id="<?php echo $cid; ?>" data-name="<?php echo e($cat['namecategory']); ?>"><i class="fas fa-pen"></i></button>
<button type="button" class="cat-del" title="Elimina" data-id="<?php echo $cid; ?>" data-name="<?php echo e($cat['namecategory']); ?>" data-used="<?php echo $used; ?>"><i class="fas fa-xmark"></i></button>
</span>
<?php endforeach; ?>
</div>
<?php else : ?>
<p class="text-muted">Nessuna categoria: aggiungi la prima.</p>
<?php endif; ?>
<form method="post" class="row g-2" style="max-width:520px;">
<input type="hidden" name="cataction" value="add">
<div class="col-8">
<input type="text" name="namecategory" class="form-control" maxlength="50" placeholder="Nuova categoria" required>
</div>
<div class="col-4">
<button type="submit" class="btn btn-primary w-100"><i class="fas fa-plus me-1"></i> Aggiungi</button>
</div>
</form>
<form method="post" id="cat-action-form" class="d-none">
<input type="hidden" name="cataction" id="cat-action">
<input type="hidden" name="idservicecategory" id="cat-id">
<input type="hidden" name="namecategory" id="cat-name">
</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) :
$sid = (int) $svc['idservice'];
$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($dayLabels[$svc['day']] ?? $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 $sid; ?>">
<?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'] . ' ' . ($dayLabels[$svc['day']] ?? $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 $sid; ?>" class="btn btn-info btn-sm">Associa</a>
<a href="updateservice.php?idservice=<?php echo $sid; ?>" class="btn btn-warning btn-sm" title="Modifica">
<i class="mdi mdi-lead-pencil font-size-16 align-middle"></i>
</a>
<button type="button" class="btn btn-danger btn-sm btn-delete-service" title="Elimina"
data-id="<?php echo $sid; ?>" data-name="<?php echo e($svc['servicename']); ?>">
<i class="bx bx-block font-size-16 align-middle"></i>
</button>
</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" autocomplete="off" 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" autocomplete="off" required>
</div>
</div>
</div>
<input type="hidden" name="idservice" value="<?php echo $sid; ?>">
<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").datepicker({
dateFormat: "yy-mm-dd"
});
$('#colorclass').spectrum({
preferredFormat: 'hex',
showInput: true,
showPalette: true,
palette: [
["#FF0000", "#00FF00", "#0000FF"],
["#1ebf73", "#f39c12", "#8e44ad"]
]
});
$('#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);
if (confirm('Assegnare ' + sel.find('option:selected').text() + ' alla classe ' + sel.data('classname') + '?')) {
sel.closest('form')[0].submit();
} else {
sel.val(String(sel.data('current')) === '0' ? '' : String(sel.data('current')));
}
});
// Eliminazione classe con conferma
$('.btn-delete-service').on('click', function() {
var id = $(this).data('id');
Swal.fire({
title: 'Eliminare questa classe?',
html: '<strong>' + $('<div>').text($(this).data('name')).html() + '</strong><br><br>Possibile solo se non ha lezioni o ordini collegati.',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#e74c3c',
confirmButtonText: 'Sì, elimina',
cancelButtonText: 'Annulla'
}).then(function(r) {
if (r.isConfirmed) window.location.href = 'cancelservice.php?idservice=' + id;
});
});
// Categorie: rinomina / elimina
function catSubmit(action, id, name) {
$('#cat-action').val(action);
$('#cat-id').val(id);
$('#cat-name').val(name || '');
$('#cat-action-form')[0].submit();
}
$('.cat-rename').on('click', function() {
const id = $(this).data('id');
Swal.fire({
title: 'Rinomina categoria',
input: 'text',
inputValue: $(this).data('name'),
inputAttributes: {
maxlength: 50
},
showCancelButton: true,
confirmButtonText: 'Salva',
cancelButtonText: 'Annulla',
inputValidator: v => !v.trim() ? 'Inserisci un nome' : null
}).then(r => {
if (r.isConfirmed) catSubmit('rename', id, r.value.trim());
});
});
$('.cat-del').on('click', function() {
const id = $(this).data('id');
if (parseInt($(this).data('used'), 10) > 0) {
Swal.fire('Categoria in uso', 'Cambia prima la categoria delle classi che la usano.', 'info');
return;
}
Swal.fire({
title: 'Eliminare la categoria?',
html: '<strong>' + $('<div>').text($(this).data('name')).html() + '</strong>',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#e74c3c',
confirmButtonText: 'Sì, elimina',
cancelButtonText: 'Annulla'
}).then(r => {
if (r.isConfirmed) catSubmit('delete', id);
});
});
// Espansione riga propagazione
$('.toggle-form').on('click', function() {
$(this).closest('tr').next('.expanded-row').toggleClass('expanded');
});
});
</script>
</body>
</html>