getConnection(); if (!isset($iduserlogin)) { die("Errore: ID utente non definito."); } $teacher_id = (int)($_GET['id'] ?? 0); $is_owner_view = ($teacher_id > 0); // se arrivi da teacher_list.php con ?id=... if ($teacher_id > 0) { // === OWNER VIEW: carica teacher per teachers.id SOLO se l'owner ha diritto === $stmt = $pdo->prepare(" SELECT t.*, u.first_name, u.last_name, u.email FROM teachers t JOIN auth_users u ON t.user_id = u.id JOIN teacher_schools ts ON ts.teacher_id = t.id JOIN schools s ON s.id = ts.school_id WHERE t.id = ? AND s.owner_id = ? LIMIT 1 "); $stmt->execute([$teacher_id, $iduserlogin]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); if (!$teacher) { die("Errore: insegnante non trovata o non hai permessi."); } } else { // === TEACHER SELF VIEW: carica il profilo dell'utente loggato === $stmt = $pdo->prepare(" SELECT t.*, u.first_name, u.last_name, u.email FROM auth_users u LEFT JOIN teachers t ON t.user_id = u.id WHERE u.id = ? LIMIT 1 "); $stmt->execute([$iduserlogin]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); } $is_new = empty($teacher['id']); // ok così if ($teacher_id > 0) { $is_new = false; // owner sta editando una teacher esistente } if ($is_new) { $teacher = [ 'id' => null, 'user_id' => $iduserlogin, 'unique_code' => '', 'phone' => '', 'description' => '', 'specializations' => '', 'profile_picture' => '', 'status' => 'active', 'created_at' => '', 'updated_at' => '', 'first_name' => '', 'last_name' => '', 'email' => '' ]; } function generateUniqueCode($pdo, $length = 16) { do { $code = bin2hex(random_bytes($length / 2)); $stmt = $pdo->prepare("SELECT COUNT(*) FROM teachers WHERE unique_code = ?"); $stmt->execute([$code]); } while ($stmt->fetchColumn() > 0); return $code; } function writeQrPng($text, $filename, $size = 150, $margin = 10) { // ✅ nella tua versione il costruttore vuole il testo $qrCode = new \Endroid\QrCode\QrCode($text); // size: alcune versioni hanno setSize(), altre setModuleSize() if (method_exists($qrCode, 'setSize')) { $qrCode->setSize($size); } elseif (method_exists($qrCode, 'setModuleSize')) { $module = max(3, (int)round($size / 25)); // mapping semplice $qrCode->setModuleSize($module); } // margin: alcune versioni setMargin(), altre setPadding() if (method_exists($qrCode, 'setMargin')) { $qrCode->setMargin($margin); } elseif (method_exists($qrCode, 'setPadding')) { $qrCode->setPadding($margin); } $writer = new \Endroid\QrCode\Writer\PngWriter(); if (method_exists($writer, 'writeFile')) { $writer->writeFile($qrCode, $filename); } else { $result = $writer->write($qrCode); if (is_object($result) && method_exists($result, 'saveToFile')) { $result->saveToFile($filename); } else { file_put_contents($filename, (string)$result); } } } $qr_code_path = null; if (!$is_new && !empty($teacher['unique_code'])) { try { $unique_code = $teacher['unique_code']; $base_dir = __DIR__ . '/../../public/phototeachers/qrcodes/'; $qr_filename = "{$base_dir}{$iduserlogin}-{$unique_code}.png"; $qr_code_path = "phototeachers/qrcodes/{$iduserlogin}-{$unique_code}.png"; if (!file_exists($qr_filename)) { if (!is_dir($base_dir)) mkdir($base_dir, 0755, true); writeQrPng($unique_code, $qr_filename, 150, 10); } } catch (Exception $e) { error_log("Errore QR: " . $e->getMessage()); } } $success_message = $error = null; if ($_SERVER['REQUEST_METHOD'] === 'POST') { // ✅ target: se owner sta editando una teacher (?id=..), salva su QUELLA teacher $target_user_id = ($teacher_id > 0) ? (int)$teacher['user_id'] : (int)$iduserlogin; $target_teacher_id = ($teacher_id > 0) ? (int)$teacher['id'] : (int)($teacher['id'] ?? 0); $first_name = trim($_POST['first_name'] ?? ''); $last_name = trim($_POST['last_name'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $description = trim($_POST['description'] ?? ''); $specializations = trim($_POST['specializations'] ?? ''); $status = ($_POST['status'] ?? 'active') === 'active' ? 'active' : 'inactive'; $target_user_id = ($teacher_id > 0) ? (int)$teacher['user_id'] : (int)$iduserlogin; $stmt = $pdo->prepare("UPDATE auth_users SET first_name = ?, last_name = ? WHERE id = ?"); $stmt->execute([$first_name, $last_name, $target_user_id]); $profile_picture = $teacher['profile_picture'] ?? ''; if (!empty($_FILES['profile_picture']['name']) && $_FILES['profile_picture']['error'] === UPLOAD_ERR_OK) { $ext = strtolower(pathinfo($_FILES['profile_picture']['name'], PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) { $new_name = "phototeachers/{$target_user_id}-" . time() . "-profile.$ext"; if (move_uploaded_file($_FILES['profile_picture']['tmp_name'], $new_name)) { if ($profile_picture && file_exists($profile_picture) && !$is_new) @unlink($profile_picture); $profile_picture = $new_name; } else $error = "Errore caricamento foto."; } else $error = "Solo JPG, PNG, GIF ammessi."; } if ($is_new) { $unique_code = generateUniqueCode($pdo); $stmt = $pdo->prepare(" INSERT INTO teachers (user_id, unique_code, phone, description, specializations, profile_picture, status) VALUES (?, ?, ?, ?, ?, ?, ?) "); $success = $stmt->execute([$target_user_id, $unique_code, $phone ?: null, $description, $specializations, $profile_picture, $status]); if ($success) { $success_message = "Profilo creato!"; $stmt = $pdo->prepare("SELECT t.*, u.first_name, u.last_name, u.email FROM teachers t JOIN auth_users u ON t.user_id = u.id WHERE t.user_id = ?"); $stmt->execute([$iduserlogin]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); $is_new = false; try { $base_dir = __DIR__ . '/../../public/phototeachers/qrcodes/'; $qr_filename = "{$base_dir}{$iduserlogin}-{$unique_code}.png"; $qr_code_path = "phototeachers/qrcodes/{$iduserlogin}-{$unique_code}.png"; if (!file_exists($qr_filename)) { if (!is_dir($base_dir)) mkdir($base_dir, 0755, true); $writer = new PngWriter(); if (!file_exists($qr_filename)) { if (!is_dir($base_dir)) mkdir($base_dir, 0755, true); writeQrPng($unique_code, $qr_filename, 150, 10); } $result = $writer->write($qrCode); $result->saveToFile($qr_filename); } } catch (Exception $e) { error_log("Errore QR: " . $e->getMessage()); } } else $error = "Errore creazione."; } else { $stmt = $pdo->prepare(" UPDATE teachers SET phone = ?, description = ?, specializations = ?, profile_picture = ?, status = ? WHERE user_id = ? "); $success = $stmt->execute([$phone ?: null, $description, $specializations, $profile_picture, $status, $target_user_id]); if ($success) { $success_message = "Dati aggiornati!"; $stmt = $pdo->prepare("SELECT t.*, u.first_name, u.last_name, u.email FROM teachers t JOIN auth_users u ON t.user_id = u.id WHERE t.user_id = ?"); $stmt->execute([$iduserlogin]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); } else $error = "Errore aggiornamento."; } } ?>