mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 19:19:19 +02:00
refactor: use encapsulated Database methods in formulaire.php and edit.php
This commit is contained in:
@@ -109,71 +109,28 @@ try {
|
||||
$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)]);
|
||||
}
|
||||
}
|
||||
$db->setThesisLanguages($thesisId, isset($_POST['languages']) && is_array($_POST['languages']) ? $_POST['languages'] : []);
|
||||
|
||||
// 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)]);
|
||||
}
|
||||
}
|
||||
$db->setThesisFormats($thesisId, isset($_POST['formats']) && is_array($_POST['formats']) ? $_POST['formats'] : []);
|
||||
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$editKeywords = !empty($keywordsRaw) ? array_map('trim', explode(',', $keywordsRaw)) : [];
|
||||
$db->setThesisTags($thesisId, $editKeywords);
|
||||
|
||||
$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) {
|
||||
if ($currentBannerPath && defined('STORAGE_ROOT')) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$db->handleBannerUpload($thesisId, $_FILES['banner'] ?? null);
|
||||
}
|
||||
|
||||
$success = "TFE mis à jour avec succès!";
|
||||
|
||||
Reference in New Issue
Block a user