Extract shared TFE form partial — single source of truth for add/edit/partage

Created templates/partials/form/form.php as the unified form template driven by
$mode ('add'|'edit'|'partage') and boolean flags for optional sections.

The three calling templates (templates/admin/add.php, templates/admin/edit.php,
partage/index.php renderShareLinkForm) now only set variables then include the
shared partial. ~200 lines of duplicated fieldset HTML eliminated.
This commit is contained in:
Pontoporeia
2026-05-07 22:48:18 +02:00
parent ac0008df6c
commit bdd95341b0
13 changed files with 833 additions and 778 deletions

View File

@@ -37,7 +37,7 @@ try {
$isAdmin = true; $bodyClass = 'admin-body';
$extraCss = ['/assets/css/form.css'];
$extraJs = ['/assets/js/sortable.min.js', '/assets/js/file-upload-queue.js'];
$extraJs = ['/assets/js/sortable.min.js', '/assets/js/file-upload-queue.js', '/assets/js/beforeunload-guard.js'];
require_once APP_ROOT . '/templates/head.php';
include APP_ROOT . '/templates/header.php';
include APP_ROOT . '/templates/admin/edit.php';

View File

@@ -75,18 +75,6 @@
/* ── Buttons ────────────────────────────────────────────────────────────── */
.admin-form-footer {
margin-top: var(--space-l);
padding-top: var(--space-m);
}
/* Sticky variant — pinned below admin header, top-right */
.admin-form-footer--sticky {
position: sticky;
top: 0;
z-index: 10;
margin: 0 0 var(--space-m);
display: flex;
justify-content: flex-end;
gap: var(--space-s);
}
/* ── Admin button aliases — see common.css .btn base class ────────────── */

View File

@@ -427,6 +427,7 @@ main {
.btn--primary {
background: var(--accent-primary);
color: var(--accent-foreground);
border: 1px solid transparent;
}
.btn--primary:hover {

View File

@@ -302,6 +302,10 @@
/* ── Submit / form footer ───────────────────────────────────────────────── */
.form-footer {
margin-top: var(--space-l);
margin-bottom: var(--space-l);
display: flex;
gap: var(--space-s);
align-items: center;
}
.form-footer button {

View File

@@ -0,0 +1,25 @@
/**
* Beforeunload guard — prompts the user before navigating away from unsaved changes.
*
* Attach to any form with a data-beforeunload-guard attribute.
* No effect when JavaScript is unavailable (form posts normally).
*/
(function () {
var forms = document.querySelectorAll('form[data-beforeunload-guard]');
if (!forms.length) return;
var dirty = false;
for (var i = 0; i < forms.length; i++) {
var form = forms[i];
form.addEventListener('input', function () { dirty = true; });
form.addEventListener('change', function () { dirty = true; });
form.addEventListener('submit', function () { dirty = false; });
}
window.addEventListener('beforeunload', function (e) {
if (dirty) {
e.preventDefault();
}
});
})();

View File

@@ -254,6 +254,64 @@ function renderShareLinkForm(string $slug, array $link): void
// Load all form help blocks in one query.
$helpBlocks = Database::getInstance()->getAllFormHelpBlocks();
$helpFn = fn(string $key) => $helpBlocks[$key]['content'] ?? '';
// ── Shared form variables ──────────────────────────────────────────────
$mode = 'partage';
$formAction = '/partage/' . urlencode($slug) . '/submit';
$hiddenFields = '<input type="hidden" name="share_link_token" value="' . htmlspecialchars($shareCsrfToken) . '">';
$oldFn = $shareOldFn;
$withAutofocusFn = $shareWithAutofocusFn;
// Synopsis extra: inject fieldset_synopsis help block
ob_start();
$helpContent = $helpFn('fieldset_synopsis');
include APP_ROOT . '/templates/partials/form/form-help-block.php';
$synopsisExtra = ob_get_clean();
// Jury data from repopulation
$juryPromoteur = old($formData, 'jury_promoteur');
$juryPromoteurUlb = old($formData, 'jury_promoteur_ulb_name');
$lecteursInternes = [];
$lecteursExternes = [];
for ($i = 0; $i < 10; $i++) {
$n = old($formData, "jury_lecteur_interne:$i");
if ($n !== '') $lecteursInternes[] = ['name' => $n];
}
for ($i = 0; $i < 10; $i++) {
$n = old($formData, "jury_lecteur_externe:$i");
if ($n !== '') $lecteursExternes[] = ['name' => $n];
}
$juryPresident = null;
$showPresident = false;
$showPromoteurUlb = true;
$promoteurUlbConditional = true;
// Licence / access
$libreEnabled = ($siteSettings['access_type_libre_enabled'] ?? '0') === '1';
$interneEnabled = ($siteSettings['access_type_interne_enabled'] ?? '1') === '1';
$interditEnabled = ($siteSettings['access_type_interdit_enabled'] ?? '1') === '1';
$generalitiesHtml = $helpFn('fieldset_generalites');
$defaultAccessTypeId = 2;
// Optional sections
$showFlash = true;
$showIntroHelp = true;
$showEmailConfirmation = true;
// Files: add mode
$filesMode = 'add';
// Website URL from repopulation
$existingWebsiteUrl = $formData['website_url'] ?? '';
$existingWebsiteLabel = $formData['website_label'] ?? '';
$checkedFormatsForSiteWeb = $formData['formats'] ?? [];
// Context / backoffice not shown in partage
$currentRaw = [];
$currentAuthorEmail = null;
$currentAuthorShowContact = false;
$currentContextNote = null;
?>
<!DOCTYPE html>
<html lang="fr">
@@ -277,195 +335,13 @@ function renderShareLinkForm(string $slug, array $link): void
<body class="student-body">
<main id="main-content">
<div class="thesis-add-header">
<h1>Soumettre un TFE</h1>
<h1><?= htmlspecialchars($pageTitle) ?></h1>
<?php if ($isVerified): ?>
<span class="share-badge">🔓 Accès partagé</span>
<?php endif; ?>
</div>
<?php
// Show flash messages from error redirect
$flashError = $_SESSION['_flash_error'] ?? null;
$flashWarning = $_SESSION['_flash_warning'] ?? null;
$flashSuccess = $_SESSION['_flash_success'] ?? null;
unset($_SESSION['_flash_error'], $_SESSION['_flash_warning'], $_SESSION['_flash_success']);
?>
<?php if ($flashError): ?>
<div class="flash-error" role="alert"><?= htmlspecialchars($flashError) ?></div>
<?php endif; ?>
<?php if ($flashWarning): ?>
<div class="flash-warning" id="flash-warning" role="alert" tabindex="-1"><?= htmlspecialchars($flashWarning) ?></div>
<script>document.addEventListener('DOMContentLoaded',function(){var el=document.getElementById('flash-warning');if(el){el.scrollIntoView({behavior:'smooth',block:'center'});el.focus();}});</script>
<?php endif; ?>
<?php if ($flashSuccess): ?>
<div class="flash-success" role="alert"><?= htmlspecialchars($flashSuccess) ?></div>
<?php endif; ?>
<?php $helpContent = $helpFn('partage_intro'); include APP_ROOT . '/templates/partials/form/form-help-block.php'; ?>
<p class="required-note"><span class="asterisk">*</span> Champs obligatoires</p>
<form action="/partage/<?= urlencode($slug) ?>/submit" method="post" enctype="multipart/form-data" class="admin-form">
<input type="hidden" name="share_link_token" value="<?= htmlspecialchars($shareCsrfToken) ?>">
<!-- ═══════════════════ Informations du TFE ═══════════════════ -->
<?php
$oldFn = $shareOldFn;
$withAutofocusFn = $shareWithAutofocusFn;
ob_start();
$helpContent = $helpFn('fieldset_synopsis');
include APP_ROOT . '/templates/partials/form/form-help-block.php';
$synopsisExtra = ob_get_clean();
$helpContent = $helpFn('fieldset_tfe_info');
include APP_ROOT . '/templates/partials/form/form-help-block.php';
include APP_ROOT . '/templates/partials/form/fieldset-tfe-info.php';
?>
<!-- ═══════════════════ Langue(s) ═══════════════════ -->
<fieldset>
<legend>Langue(s)</legend>
<?php $name = 'languages'; $label = 'Langue(s) du TFE :'; $options = $languages; $checked = $formData['languages'] ?? []; $required = true; include APP_ROOT . '/templates/partials/form/checkbox-list.php'; ?>
<?php $name = 'language_autre'; $label = 'Autre(s) langue(s) :'; $value = old($formData, 'language_autre'); $hint = 'Si votre TFE contient une langue absente de la liste, précisez-la ici.'; include APP_ROOT . '/templates/partials/form/text-field.php'; ?>
</fieldset>
<!-- ═══════════════════ Mots-clés ═══════════════════ -->
<fieldset>
<legend>Mots-clés</legend>
<?php
$name = 'tag'; $label = 'Mots-clés (max 10) :'; $value = old($formData, 'tag');
$placeholder = 'sociologie, anthropologie, ...';
$hint = 'Séparez par des virgules. Max 10 mots-clés.';
include APP_ROOT . '/templates/partials/form/text-field.php';
?>
</fieldset>
<!-- ═══════════════════ Cadre académique ═══════════════════ -->
<?php
$oldFn = $shareOldFn;
$withAutofocusFn = $shareWithAutofocusFn;
$helpContent = $helpFn('fieldset_academic');
include APP_ROOT . '/templates/partials/form/form-help-block.php';
include APP_ROOT . '/templates/partials/form/fieldset-academic.php';
?>
<!-- ═══════════════════ Composition du jury ═══════════════════ -->
<?php
$juryPromoteur = old($formData, 'jury_promoteur');
$juryPromoteurUlb = old($formData, 'jury_promoteur_ulb_name');
$lecteursInternes = [];
$lecteursExternes = [];
for ($i = 0; $i < 10; $i++) {
$n = old($formData, "jury_lecteur_interne:$i");
if ($n !== '') $lecteursInternes[] = ['name' => $n];
}
for ($i = 0; $i < 10; $i++) {
$n = old($formData, "jury_lecteur_externe:$i");
if ($n !== '') $lecteursExternes[] = ['name' => $n];
}
$juryPresident = null;
$showPresident = false;
$showPromoteurUlb = true;
$promoteurUlbConditional = true;
$helpContent = $helpFn('fieldset_jury');
include APP_ROOT . '/templates/partials/form/form-help-block.php';
require APP_ROOT . '/templates/partials/form/jury-fieldset.php';
?>
<!-- ═══════════════════ Format(s) ═══════════════════ -->
<fieldset>
<legend>Format(s)</legend>
<?php
$name = 'formats'; $label = 'Format(s) du TFE :'; $options = $formatTypes; $checked = $formData['formats'] ?? []; $required = true;
$hxPost = '/partage/format-website-fragment';
$hxTarget = '#website-url-fieldset';
// Capture before include unsets it
$_checkedFormatsForSiteWeb = $checked;
include APP_ROOT . '/templates/partials/form/checkbox-list.php';
?>
</fieldset>
<!-- ═══════════════════ Fichiers ═══════════════════ -->
<?php
$helpContent = $helpFn('fieldset_files');
include APP_ROOT . '/templates/partials/form/form-help-block.php';
include APP_ROOT . '/templates/partials/form/fieldset-files.php';
?>
<!-- Website URL fieldset — shown/hidden via HTMX when "Site web" checked -->
<fieldset id="website-url-fieldset" style="display:none">
<legend>Site web</legend>
<div class="admin-form-group">
<label for="website_url">URL du site :</label>
<div class="admin-file-input">
<input type="url"
id="website_url"
name="website_url"
value="<?= htmlspecialchars($formData['website_url'] ?? '') ?>"
placeholder="https://mon-tfe.erg.be">
<small>Si le TFE est un site web, entrez son URL ici. Il sera affiché comme un site embarqué sur la page du TFE.</small>
</div>
</div>
<div class="admin-form-group">
<label for="website_label">Légende :</label>
<input type="text"
id="website_label"
name="website_label"
value="<?= htmlspecialchars($formData['website_label'] ?? '') ?>"
placeholder="Description du site (optionnel)"
class="admin-file-label-input"
style="max-width:400px;">
</div>
</fieldset>
<?php
// Server-side: show if Site web already checked (e.g. on error redirect)
$_stmt = Database::getInstance()->getConnection()->prepare('SELECT id FROM format_types WHERE name = ? LIMIT 1');
$_stmt->execute(['Site web']);
$_siteWebId = $_stmt->fetchColumn();
if ($_siteWebId && in_array((string)$_siteWebId, array_map('strval', $_checkedFormatsForSiteWeb), true)) {
echo '<script>document.getElementById("website-url-fieldset").style.display=""</script>';
}
?>
<!-- ═══════════════════ Métadonnées complémentaires ═══════════════════ -->
<?php
$oldFn = $shareOldFn;
$withAutofocusFn = $shareWithAutofocusFn;
include APP_ROOT . '/templates/partials/form/fieldset-metadata.php';
?>
<!-- ═══════════════════ Degrés d'ouverture et licences ═══════════════════ -->
<?php
$libreEnabled = ($siteSettings['access_type_libre_enabled'] ?? '0') === '1';
$interneEnabled = ($siteSettings['access_type_interne_enabled'] ?? '1') === '1';
$interditEnabled = ($siteSettings['access_type_interdit_enabled'] ?? '1') === '1';
$generalitiesHtml = $helpFn('fieldset_generalites');
$defaultAccessTypeId = 2;
$helpContent = $helpFn('fieldset_access');
include APP_ROOT . '/templates/partials/form/form-help-block.php';
include APP_ROOT . '/templates/partials/form/fieldset-licence-explanation.php';
?>
<!-- ═══════════════════ E-mail de confirmation ═══════════ -->
<fieldset>
<legend>E-mail de confirmation</legend>
<?php $helpContent = $helpFn('fieldset_email'); include APP_ROOT . '/templates/partials/form/form-help-block.php'; ?>
<?php
$name = 'confirmation_email';
$label = 'Adresse e-mail :';
$value = old($formData, 'confirmation_email');
$type = 'email';
$required = true;
$placeholder = 'ton.email@exemple.be';
$hint = 'Nécessaire pour recevoir le récapitulatif de ta soumission.';
include APP_ROOT . '/templates/partials/form/text-field.php';
?>
</fieldset>
<div class="form-footer">
<button type="submit" name="go" class="btn btn--primary">Soumettre</button>
</div>
</form>
<?php include APP_ROOT . '/templates/partials/form/form.php'; ?>
</main>
</body>
</html>