getPDO(); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['csrf_token'])) { // Verify CSRF token if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) { throw new Exception("Erreur de sécurité : token invalide."); } try { $db->beginTransaction(); // Update thesis basic info $editLicenseId = filter_var($_POST['license_id'] ?? '', FILTER_VALIDATE_INT) ?: null; $editAccessTypeId = filter_var($_POST['access_type_id'] ?? '', FILTER_VALIDATE_INT) ?: null; $editContextNote = trim($_POST['context_note'] ?? ''); $stmt = $pdo->prepare(" UPDATE theses SET title = ?, subtitle = ?, year = ?, orientation_id = ?, ap_program_id = ?, finality_id = ?, synopsis = ?, context_note = ?, file_size_info = ?, baiu_link = ?, license_id = ?, access_type_id = ?, is_published = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? "); $stmt->execute([ trim($_POST['titre']), !empty($_POST['subtitle']) ? trim($_POST['subtitle']) : null, intval($_POST['année']), intval($_POST['orientation']), intval($_POST['ap']), intval($_POST['finality']), trim($_POST['synopsis']), !empty($editContextNote) ? $editContextNote : null, !empty($_POST['duration_info']) ? trim($_POST['duration_info']) : null, !empty($_POST['lien']) ? trim($_POST['lien']) : null, $editLicenseId, $editAccessTypeId, isset($_POST['is_published']) ? 1 : 0, $thesisId ]); // Update authors $pdo->prepare("DELETE FROM thesis_authors WHERE thesis_id = ?")->execute([$thesisId]); $authorsRaw = trim($_POST['auteurice'] ?? ''); if (!empty($authorsRaw)) { $authors = array_map('trim', explode(',', $authorsRaw)); foreach ($authors as $index => $authorName) { if (!empty($authorName)) { $authorId = $db->findOrCreateAuthor($authorName, $index === 0 ? ($_POST['mail'] ?? null) : null); $stmt = $pdo->prepare("INSERT INTO thesis_authors (thesis_id, author_id, author_order) VALUES (?, ?, ?)"); $stmt->execute([$thesisId, $authorId, $index + 1]); } } } // Update jury $editJuryMembers = []; if (!empty(trim($_POST['jury_president'] ?? ''))) { $editJuryMembers[] = ['name' => trim($_POST['jury_president']), 'role' => 'president', 'is_external' => 0]; } if (!empty(trim($_POST['jury_promoteur'] ?? ''))) { $editJuryMembers[] = ['name' => trim($_POST['jury_promoteur']), 'role' => 'promoteur', 'is_external' => isset($_POST['jury_promoteur_ext']) ? 1 : 0]; } foreach ($_POST['jury_lecteurs'] ?? [] as $i => $name) { $name = trim($name); if ($name !== '') { $editJuryMembers[] = ['name' => $name, 'role' => 'lecteur', 'is_external' => isset($_POST['jury_lecteurs_ext'][$i]) ? 1 : 0]; } } $db->setThesisJury($thesisId, $editJuryMembers); // Update languages $pdo->prepare("DELETE FROM thesis_languages WHERE thesis_id = ?")->execute([$thesisId]); if (isset($_POST['languages']) && is_array($_POST['languages'])) { foreach ($_POST['languages'] as $languageId) { $stmt = $pdo->prepare("INSERT INTO thesis_languages (thesis_id, language_id) VALUES (?, ?)"); $stmt->execute([$thesisId, intval($languageId)]); } } // Update formats $pdo->prepare("DELETE FROM thesis_formats WHERE thesis_id = ?")->execute([$thesisId]); if (isset($_POST['formats']) && is_array($_POST['formats'])) { foreach ($_POST['formats'] as $formatId) { $stmt = $pdo->prepare("INSERT INTO thesis_formats (thesis_id, format_id) VALUES (?, ?)"); $stmt->execute([$thesisId, intval($formatId)]); } } // Update tags $pdo->prepare("DELETE FROM thesis_tags WHERE thesis_id = ?")->execute([$thesisId]); $keywordsRaw = trim($_POST['tag'] ?? ''); if (!empty($keywordsRaw)) { $keywords = array_map('trim', explode(',', $keywordsRaw)); $keywords = array_slice($keywords, 0, 10); // Max 10 foreach ($keywords as $keyword) { if (!empty($keyword)) { $tagId = $db->findOrCreateTag($keyword); if ($tagId) { $stmt = $pdo->prepare("INSERT OR IGNORE INTO thesis_tags (tag_id, thesis_id) VALUES (?, ?)"); $stmt->execute([$tagId, $thesisId]); } } } } $db->commit(); // Handle banner upload/removal (after commit, outside transaction) $bannerDir = defined('STORAGE_ROOT') ? STORAGE_ROOT . "/banners/" : null; if ($bannerDir && !file_exists($bannerDir)) { mkdir($bannerDir, 0755, true); } if (isset($_POST['remove_banner'])) { // Unlink existing banner file if present $currentBannerPath = $db->getThesisBannerPath($thesisId); if ($currentBannerPath && $bannerDir) { $absPath = STORAGE_ROOT . '/' . $currentBannerPath; if (file_exists($absPath)) unlink($absPath); } $db->setBannerPath($thesisId, null); } elseif (isset($_FILES['banner']) && $_FILES['banner']['error'] === UPLOAD_ERR_OK && $bannerDir) { $bannerFile = $_FILES['banner']; $finfo = new finfo(FILEINFO_MIME_TYPE); $mimeType = $finfo->file($bannerFile["tmp_name"]); $fileExtension = strtolower(pathinfo($bannerFile["name"], PATHINFO_EXTENSION)); $allowedBannerMimes = ['image/jpeg', 'image/png', 'image/webp']; $allowedBannerExts = ['jpg', 'jpeg', 'png', 'webp']; if (in_array($mimeType, $allowedBannerMimes) && in_array($fileExtension, $allowedBannerExts) && $bannerFile["size"] <= 5 * 1024 * 1024) { $randomName = bin2hex(random_bytes(16)); $safeFileName = $randomName . '.' . $fileExtension; if (move_uploaded_file($bannerFile["tmp_name"], $bannerDir . $safeFileName)) { chmod($bannerDir . $safeFileName, 0644); $db->setBannerPath($thesisId, "banners/" . $safeFileName); } } } $success = "TFE mis à jour avec succès!"; // Regenerate CSRF token $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } catch (Exception $e) { $db->rollback(); $error = $e->getMessage(); error_log("Edit error: " . $e->getMessage()); } } // Load thesis data $thesis = $db->getThesis($thesisId); if (!$thesis) { die("TFE non trouvé"); } // Load current relationships $stmt = $pdo->prepare("SELECT language_id FROM thesis_languages WHERE thesis_id = ?"); $stmt->execute([$thesisId]); $currentLanguages = $stmt->fetchAll(PDO::FETCH_COLUMN); $stmt = $pdo->prepare("SELECT format_id FROM thesis_formats WHERE thesis_id = ?"); $stmt->execute([$thesisId]); $currentFormats = $stmt->fetchAll(PDO::FETCH_COLUMN); // Load jury $jury = $db->getThesisJury($thesisId); // Load reference data $orientations = $db->getAllOrientations(); $apPrograms = $db->getAllAPPrograms(); $finalityTypes = $db->getAllFinalityTypes(); $languages = $db->getAllLanguages(); $formatTypes = $db->getAllFormatTypes(); $licenseTypes = $db->getAllLicenseTypes(); $accessTypes = $db->getAccessTypes(); // Fetch raw FK IDs (view only exposes name strings) $rawRow = $db->getThesisRawFields($thesisId); $currentLicenseId = $rawRow['license_id'] ?? null; $currentAccessTypeId = $rawRow['access_type_id'] ?? null; $currentContextNote = $rawRow['context_note'] ?? ''; // Set page title for header $pageTitle = "Éditer TFE - " . htmlspecialchars($thesis['title']); } catch (Exception $e) { error_log("Error loading edit page: " . $e->getMessage()); die("Erreur lors du chargement: " . $e->getMessage()); } ?>

Modifier un TFE

Composition du jury
$lm): ?>

Visible publiquement pour les TFE Interne ou Interdit. Max 1 500 caractères.

Séparer par des virgules. Max 10.

Bannière actuelle

JPG, PNG ou WEBP. Format paysage recommandé (4:1). Max 5 MB.

Annuler