Extract FormBootstrap helper to eliminate bootstrap duplication across add/edit form pages

This commit is contained in:
Pontoporeia
2026-06-11 12:11:37 +02:00
parent f4a3e26901
commit a19e9e1454
5 changed files with 354 additions and 247 deletions

View File

@@ -1,47 +1,167 @@
<?php
require_once __DIR__ . "/../../bootstrap.php";
require_once __DIR__ . '/../../bootstrap.php';
require_once __DIR__ . '/../../src/AdminAuth.php';
require_once __DIR__ . '/../../src/Form/FormBootstrap.php';
AdminAuth::requireLogin();
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
require_once APP_ROOT . '/src/Controllers/ThesisEditController.php';
$thesisId = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($thesisId <= 0) {
die("ID invalide");
die('ID invalide');
}
$autofocusField = App::consumeAutofocus();
require_once __DIR__ . '/../../src/Controllers/ThesisEditController.php';
// Form help blocks for editable généralités
$helpBlocks = Database::getInstance()->getAllFormHelpBlocks();
$helpFn = fn(string $key) => empty($helpBlocks[$key]['enabled']) ? '' : ($helpBlocks[$key]['content'] ?? '');
function old($key, $default = "") {
global $formData;
if (!isset($formData[$key])) return $default;
if (is_array($formData[$key])) return $formData[$key]; // Return raw array for callers that handle it
if ($formData[$key] === null) return $default;
return htmlspecialchars((string)$formData[$key]);
}
$formData = [];
$siteSettings = Database::getInstance()->getAllSettings();
$helpBlocks = Database::getInstance()->getAllFormHelpBlocks();
try {
$ctrl = ThesisEditController::create();
$view = $ctrl->load($thesisId);
extract($view);
} catch (Exception $e) {
error_log("Error loading edit page: " . $e->getMessage());
die("Erreur lors du chargement: " . $e->getMessage());
error_log('Error loading edit page: ' . $e->getMessage());
die('Erreur lors du chargement: ' . $e->getMessage());
}
$isAdmin = true; $bodyClass = 'admin-body';
$extraCss = ['/assets/css/form-base.css'];
$extraCssAdmin = ['/assets/css/form-admin.css', '/assets/css/filepond.min.css', '/assets/css/filepond-plugin-image-preview.min.css'];
$extraJs = ['/assets/js/vendor/filepond.min.js', '/assets/js/vendor/filepond-plugin-file-validate-type.min.js', '/assets/js/vendor/filepond-plugin-file-validate-size.min.js', '/assets/js/vendor/filepond-plugin-image-preview.min.js', '/assets/js/vendor/filepond-plugin-image-exif-orientation.min.js', '/assets/js/app/file-upload-filepond.js', '/assets/js/app/beforeunload-guard.js', '/assets/js/app/pill-search.js', '/assets/js/app/jury-autocomplete.js'];
// Merge flash repopulation data (from redirects after validation errors)
// with current thesis data so old() can fall back to DB values.
$formData = array_merge($formData, [
'titre' => $thesis['title'],
'subtitle' => $thesis['subtitle'] ?? '',
'auteurice' => $thesis['authors'] ?? '',
'mail' => $contactInterne ?? '',
'contact_visible' => $currentContactVisible ?? '',
'synopsis' => $thesis['synopsis'] ?? '',
'tag' => $thesis['keywords'] ?? '',
'année' => $thesis['year'],
'orientation' => $thesis['orientation'],
'ap' => $thesis['ap_program'],
'finality' => $thesis['finality_type'],
'lien' => $thesis['baiu_link'] ?? '',
'contact_public' => $contactPublic ?? false,
'contact_interne' => $contactInterne ?? '',
]);
// Build jury arrays
$juryPromoteurs = [];
$juryPromoteursUlb = [];
$lecteursInternes = [];
$lecteursExternes = [];
$juryPromoteur = null;
$juryPromoteurUlb = null;
foreach ($jury as $jm) {
if ($jm['role'] === 'president') continue;
if ($jm['role'] === 'promoteur') {
if (($jm['is_ulb'] ?? 0) == 1) {
$juryPromoteursUlb[] = $jm;
} else {
$juryPromoteurs[] = $jm;
}
} elseif ($jm['role'] === 'lecteur') {
if (($jm['is_external'] ?? 0) == 1) {
$lecteursExternes[] = $jm;
} else {
$lecteursInternes[] = $jm;
}
}
}
if (!empty($juryPromoteurs) && $juryPromoteur === null) $juryPromoteur = $juryPromoteurs[0]['name'];
if (!empty($juryPromoteursUlb) && $juryPromoteurUlb === null) $juryPromoteurUlb = $juryPromoteursUlb[0]['name'];
// Build existing website URL / label from current files
$existingWebsiteUrl = '';
$existingWebsiteLabel = '';
foreach ($currentFiles as $f) {
if ($f['file_type'] === 'website') {
$existingWebsiteUrl = $f['file_path'] ?? '';
$existingWebsiteLabel = $f['display_label'] ?? '';
break;
}
}
// Languages — either from flash repopulation or current thesis data
$formData['languages'] = $formData['languages'] ?? $currentLanguages ?? [];
// Compute "other" languages (not in the predefined checkbox list)
$predefinedLangIds = array_column($languages, 'id');
$otherLangIds = array_diff($currentLanguages ?? [], $predefinedLangIds);
$selectedOtherLanguages = [];
if (!empty($otherLangIds)) {
$allLangs = Database::getInstance()->getAllLanguages();
$allLangMap = [];
foreach ($allLangs as $al) {
$allLangMap[(int)$al['id']] = $al['name'];
}
foreach ($otherLangIds as $lid) {
$lid = (int)$lid;
if (isset($allLangMap[$lid])) {
$selectedOtherLanguages[] = $allLangMap[$lid];
}
}
sort($selectedOtherLanguages, SORT_NATURAL | SORT_FLAG_CASE);
}
// Tags — either from flash repopulation or current thesis data
$keywordsStr = $thesis['keywords'] ?? '';
$currentTags = $keywordsStr !== '' ? array_map('trim', explode(',', $keywordsStr)) : [];
if (!empty($formData['tag']) && is_array($formData['tag'])) {
$currentTags = $formData['tag'];
}
// Formats — either from flash repopulation or current thesis data
$checkedFormats = $formData['formats'] ?? $currentFormats ?? [];
$formData['formats'] = $checkedFormats;
// Shared form variables from the bootstrap helper
extract(FormBootstrap::adminFormVariables(
mode: 'edit',
formAction: '/admin/actions/edit.php',
hiddenFields:
'<input type="hidden" name="csrf_token" value="' . htmlspecialchars($_SESSION['csrf_token']) . '">'
. '<input type="hidden" name="thesis_id" value="' . $thesisId . '">',
formData: $formData,
siteSettings: $siteSettings,
helpBlocks: $helpBlocks,
options: [
'filesMode' => 'edit',
'existingWebsiteUrl' => $existingWebsiteUrl,
'existingWebsiteLabel' => $existingWebsiteLabel,
'checkedFormats' => $checkedFormats,
'juryPromoteurs' => $juryPromoteurs,
'juryPromoteursUlb' => $juryPromoteursUlb,
'lecteursInternes' => $lecteursInternes,
'lecteursExternes' => $lecteursExternes,
'juryPromoteur' => $juryPromoteur,
'juryPromoteurUlb' => $juryPromoteurUlb,
'currentRaw' => $currentRaw ?? [],
'currentCover' => $currentCover ?? null,
'currentFiles' => $currentFiles ?? [],
'currentContextNote' => $currentContextNote ?? null,
'currentContactVisible' => $currentContactVisible ?? null,
'contactInterne' => $contactInterne ?? null,
'contactPublic' => $contactPublic ?? false,
'showCoverPreview' => true,
'showExistingFiles' => true,
'defaultAccessTypeId' => $currentAccessTypeId ?? 2,
],
));
// Inject thesis-derived values into formData for the template
$formData['access_type_id'] = $currentAccessTypeId;
$formData['license_id'] = $currentLicenseId;
$formData['license_custom'] = $currentRaw['license_custom'] ?? '';
$formData['cc2r'] = $currentRaw['cc2r'] ?? false;
// Asset arrays and page chrome
$isAdmin = true;
$bodyClass = 'admin-body';
extract(FormBootstrap::adminAssetArrays());
require_once APP_ROOT . '/templates/head.php';
include APP_ROOT . '/templates/header.php';
include APP_ROOT . '/templates/admin/edit.php';