Inserisci i dettagli della classe da aggiungere al catalogo.
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 .= ''; } if (!$found) { $html = '' . $html; } return $html; } ?>
Inserisci i dettagli della classe da aggiungere al catalogo.