Refactor HTMX fragment architecture: DRY split into auth endpoints + shared templates

- Created templates/partials/form/_licence.php (shared HTML, no auth logic)
- Created templates/partials/form/_format-website.php (shared HTML, no auth logic)
- Created src/FragmentRenderer.php helper for clean fragment rendering
- Created public/{admin,partage}/fragments/ subdirectories
- Created thin fragment endpoint files: auth guard + data fetch + render template
- Updated all hx-post references in templates to new fragments/ paths
- Updated partage/index.php routing for new fragments subdirectory
- Kept old fragment files as thin delegates for backward compat
- Updated nginx config: added PHP handler in /partage/ location block
This commit is contained in:
Pontoporeia
2026-05-12 15:01:17 +02:00
parent 2632730fa0
commit da153fc604
38 changed files with 503 additions and 527 deletions

View File

@@ -1728,6 +1728,19 @@
+%%%%%%% diff from: somsyvxz 249f7943 "Bulk bar anti-shift, tags icons, AP no-wrap, credits reorder" (rebased revision)
+\\\\\\\ to: sqvvzxku e3b36997 ".gitignore ignore rate_limit and theses and logs." (rebased revision)
++ $linkName = $link['name'] ?? '';
++ $linkExpiresVal = $link['expires_at'] ? date('Y-m-d\TH:i', strtotime($link['expires_at'])) : '';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff from: sqvvzxku e3b36997 ".gitignore ignore rate_limit and theses and logs." (rebased revision)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ to: somsyvxz 249f7943 "Bulk bar anti-shift, tags icons, AP no-wrap, credits reorder" (rebased revision)
- $linkName = $link['name'] ?? '';
- $linkExpiresVal = $link['expires_at'] ? date('Y-m-d\TH:i', strtotime($link['expires_at'])) : '';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff from: somsyvxz 14a3cd10 "Bulk bar anti-shift, tags icons, AP no-wrap, credits reorder" (rebase destination)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ to: tkqwzqos dffabd97 "Refactor HTMX fragment architecture: DRY split into auth endpoints + shared templates" (rebased revision)
$linkName = $link['name'] ?? '';
$linkExpiresVal = $link['expires_at'] ? date('Y-m-d\TH:i', strtotime($link['expires_at'])) : '';
$linkLockedYear = $link['locked_year'] ?? null;
+%%%%%%% diff from: somsyvxz 249f7943 "Bulk bar anti-shift, tags icons, AP no-wrap, credits reorder" (rebased revision)
+\\\\\\\ to: tkqwzqos e51fc3eb "Refactor HTMX fragment architecture: DRY split into auth endpoints + shared templates" (rebased revision)
++ $linkName = $link['name'] ?? '';
++ $linkExpiresVal = $link['expires_at'] ? date('Y-m-d\TH:i', strtotime($link['expires_at'])) : '';
?>
<tr class="admin-table-row" onclick="event.stopPropagation(); window.open('/partage/<?= urlencode($link['slug']) ?>', '_blank')" style="cursor:pointer">

View File

@@ -0,0 +1,48 @@
<?php
/**
* _format-website.php — Shared HTMX fragment template for the Site web fieldset.
*
* Returns the website URL + label inputs when "Site web" is among the selected
* format checkboxes. Pure presentation — receives all data via variables.
*
* Expected variables:
* string $fieldsetId — HTML id for the <fieldset> ('website-url-fieldset' or 'edit-website-url-fieldset')
* array $selectedFormats — selected format_type IDs
* int $websiteFormatId — ID of the "Site web" format_type
* string $websiteUrl — current value (for repopulation)
* string $websiteLabel — current value (for repopulation)
*/
if (!$websiteFormatId) {
echo '<fieldset id="' . $fieldsetId . '" style="display:none"></fieldset>';
return;
}
if (!in_array((int)$websiteFormatId, array_map('intval', $selectedFormats), true)) {
echo '<fieldset id="' . $fieldsetId . '" style="display:none"></fieldset>';
return;
}
$escUrl = htmlspecialchars($websiteUrl);
$escLabel = htmlspecialchars($websiteLabel);
?>
<fieldset id="<?= $fieldsetId ?>">
<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="<?= $escUrl ?>"
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="<?= $escLabel ?>"
placeholder="Description du site (optionnel)"
class="admin-file-label-input"
style="max-width:400px;">
</div>
</fieldset>

View File

@@ -0,0 +1,102 @@
<?php
/**
* _licence.php — Shared HTMX fragment template for the licence section.
*
* Renders conditional licence options based on access_type_id.
* Pure presentation — receives all data via variables.
*
* Expected variables:
* string $accessTypeId — '1' (Libre), '2' (Interne), '3' (Interdit)
* string $licenseId — selected license_id (for repopulation)
* string $licenseCustom — custom licence text (for repopulation)
* bool $cc2r — CC2r checkbox state
* bool $wantLicense — want_license checkbox state (Interne only)
* array $licenseTypes — [{id, name}] from getAllLicenseTypes()
* bool $adminMode — false for partage, true for admin
* string $hxPost — HTMX endpoint for the want_license checkbox re-fetch
*/
?>
<div class="licence-license-choice">
<?php if ($accessTypeId === '1'): ?>
<fieldset class="licence-options-fieldset">
<legend>Options de licence</legend>
<p class="licence-note">J'autorise que mon TFE soit disponible en libre accès. Je suis conscient·e des responsabilités légales.</p>
<div class="admin-form-group">
<label class="admin-checkbox-label">
<input type="checkbox" name="cc2r" value="1"
<?= $cc2r ? 'checked' : '' ?>>
J'accepte les conditions collectives de réutilisation (CC2r)
</label>
<small><a href="https://cc2r.net/" target="_blank" rel="noopener">En savoir plus sur la CC2r ↗</a></small>
</div>
<div class="licence-or-separator"><span>et / ou</span></div>
<?php
$name = 'license_id'; $label = 'Licence :'; $options = $licenseTypes;
$selected = $licenseId; $placeholder = '— Sélectionner —'; $required = false;
include APP_ROOT . '/templates/partials/form/select-field.php';
?>
<?php
$name = 'license_custom'; $label = 'Ou précisez une autre licence :';
$value = htmlspecialchars($licenseCustom);
$hint = 'Ex: CC BY-NC 4.0, Tous droits réservés…';
include APP_ROOT . '/templates/partials/form/text-field.php';
?>
</fieldset>
<?php elseif ($accessTypeId === '2'): ?>
<fieldset class="licence-options-fieldset">
<legend>Options de licence</legend>
<p class="licence-note">Mon TFE est accessible sur place et sur la plateforme xamxam par la communauté erg. J'autorise une (ré-)utilisation dans un contexte académique au sein de l'erg.</p>
<div class="admin-form-group">
<label class="admin-checkbox-label">
<input type="checkbox" name="want_license" value="1"
hx-post="<?= $hxPost ?>"
hx-target=".licence-license-choice"
hx-swap="outerHTML"
hx-include="closest fieldset"
<?= $wantLicense ? 'checked' : '' ?>>
<strong>Je souhaite appliquer une licence sur mon TFE</strong>
</label>
<small>Par défaut, aucune licence spécifique n'est appliquée. Cochez pour en choisir une.</small>
</div>
<?php if ($wantLicense): ?>
<div class="admin-form-group">
<label class="admin-checkbox-label">
<input type="checkbox" name="cc2r" value="1"
<?= $cc2r ? 'checked' : '' ?>>
J'accepte les conditions collectives de réutilisation (CC2r)
</label>
<small><a href="https://cc2r.net/" target="_blank" rel="noopener">En savoir plus sur la CC2r ↗</a></small>
</div>
<?php
$name = 'license_id'; $label = 'Licence :'; $options = $licenseTypes;
$selected = $licenseId; $placeholder = '— Sélectionner —'; $required = false;
include APP_ROOT . '/templates/partials/form/select-field.php';
?>
<?php
$name = 'license_custom'; $label = 'Ou précisez une autre licence :';
$value = htmlspecialchars($licenseCustom);
$hint = 'Ex: CC BY-NC 4.0, Tous droits réservés…';
include APP_ROOT . '/templates/partials/form/text-field.php';
?>
<p class="licence-note"><em>Je suis conscient·e des obligations légales venant avec la licence choisie et acquiesce avoir lu la documentation prévue à cet effet par l'erg, ainsi qu'avoir discuté des enjeux des licences avec l'équipe pédagogique.</em></p>
<?php endif; ?>
</fieldset>
<?php elseif ($accessTypeId === '3'): ?>
<fieldset class="licence-options-fieldset">
<legend>Options de licence</legend>
<p class="licence-note">Mon TFE n'est pas disponible en physique ni sur le site. Une note descriptive est visible publiquement.</p>
<p class="licence-note"><em>Aucune licence n'est applicable.</em></p>
</fieldset>
<?php endif; ?>
</div>

View File

@@ -42,7 +42,7 @@ $adminMode = $adminMode ?? false;
<div class="licence-degree">
<label class="admin-checkbox-label">
<input type="radio" name="access_type_id" value=""
hx-post="/admin/licence-fragment.php"
hx-post="/admin/fragments/licence.php"
hx-target=".licence-license-choice"
hx-swap="outerHTML"
hx-include="closest fieldset"
@@ -56,7 +56,7 @@ $adminMode = $adminMode ?? false;
<div class="licence-degree">
<label class="admin-checkbox-label">
<input type="radio" name="access_type_id" value="1"
hx-post="<?= $adminMode ? '/admin/licence-fragment.php' : '/partage/licence-fragment' ?>"
hx-post="<?= $adminMode ? '/admin/fragments/licence.php' : '/partage/fragments/licence.php' ?>"
hx-target=".licence-license-choice"
hx-swap="outerHTML"
hx-include="closest fieldset"
@@ -77,7 +77,7 @@ $adminMode = $adminMode ?? false;
<div class="licence-degree">
<label class="admin-checkbox-label">
<input type="radio" name="access_type_id" value="2"
hx-post="<?= $adminMode ? '/admin/licence-fragment.php' : '/partage/licence-fragment' ?>"
hx-post="<?= $adminMode ? '/admin/fragments/licence.php' : '/partage/fragments/licence.php' ?>"
hx-target=".licence-license-choice"
hx-swap="outerHTML"
hx-include="closest fieldset"
@@ -99,7 +99,7 @@ $adminMode = $adminMode ?? false;
<div class="licence-degree">
<label class="admin-checkbox-label">
<input type="radio" name="access_type_id" value="3"
hx-post="<?= $adminMode ? '/admin/licence-fragment.php' : '/partage/licence-fragment' ?>"
hx-post="<?= $adminMode ? '/admin/fragments/licence.php' : '/partage/fragments/licence.php' ?>"
hx-target=".licence-license-choice"
hx-swap="outerHTML"
hx-include="closest fieldset"
@@ -121,7 +121,7 @@ $adminMode = $adminMode ?? false;
<!-- Licence — swapped via htmx when radio changes -->
<div class="licence-license-choice"
hx-post="<?= $adminMode ? '/admin/licence-fragment.php' : '/partage/licence-fragment' ?>"
hx-post="<?= $adminMode ? '/admin/fragments/licence.php' : '/partage/fragments/licence.php' ?>"
hx-trigger="load"
hx-include="closest fieldset"
hx-swap="outerHTML">

View File

@@ -24,9 +24,9 @@ $previewId = 'fp-' . htmlspecialchars($id);
// Determine HTMX POST endpoint for inline file validation
if (defined('ADMIN_MODE') && ADMIN_MODE) {
$validateUrl = '/admin/validate-file-fragment.php';
$validateUrl = '/admin/fragments/validate-file.php';
} else {
$validateUrl = '/partage/validate-file-fragment';
$validateUrl = '/partage/fragments/validate-file.php';
}
?>
<div>

View File

@@ -196,7 +196,7 @@ $checkedFormatsForSiteWeb = $checkedFormatsForSiteWeb ?? [];
$checked = $formData["languages"] ?? [];
$_hasLangAutre = !empty($formData['language_autre']) && is_array($formData['language_autre']) && count(array_filter($formData['language_autre'], fn($l) => is_string($l) && trim($l) !== '')) > 0;
$required = !$adminMode && !$_hasLangAutre;
$hxPost = $mode === 'partage' ? "/partage/language-autre-fragment" : "/admin/language-autre-fragment.php";
$hxPost = $mode === 'partage' ? "/partage/fragments/language-autre.php" : "/admin/fragments/language-autre.php";
$hxTarget = "#languages-required-asterisk";
$hxSwap = "outerHTML";
$labelHtml = htmlspecialchars($label) . '<span id="languages-required-asterisk">' . ($required ? ' <span class="asterisk">*</span>' : '') . '</span>';
@@ -233,7 +233,7 @@ $checkedFormatsForSiteWeb = $checkedFormatsForSiteWeb ?? [];
$hint = "Si votre TFE contient une langue absente de la liste, précisez-la ici.";
$selectedLanguages = $_selectedOtherLangs;
$required = $_langAutreRequired && !$adminMode;
$hxPost = ($mode === 'partage') ? "/partage/language-search-fragment" : "/admin/language-search-fragment.php";
$hxPost = ($mode === 'partage') ? "/partage/fragments/language-search.php" : "/admin/fragments/language-search.php";
include APP_ROOT . "/templates/partials/form/language-search.php";
unset($_langAutreRequired, $_selectedOtherLangs, $_langRaw, $_l, $name, $label, $placeholder, $hint, $selectedLanguages, $required, $hxPost);
?>
@@ -279,7 +279,7 @@ $checkedFormatsForSiteWeb = $checkedFormatsForSiteWeb ?? [];
$selectedTags = $_selectedTags;
$required = !$adminMode;
$minTags = ($mode === 'partage') ? 3 : 0;
$hxPost = ($mode === 'partage') ? "/partage/tag-search-fragment" : "/admin/tag-search-fragment.php";
$hxPost = ($mode === 'partage') ? "/partage/fragments/tag-search.php" : "/admin/fragments/tag-search.php";
include APP_ROOT . "/templates/partials/form/tag-search.php";
unset($_tagsRaw, $_selectedTags, $_t, $name, $label, $placeholder, $hint, $selectedTags, $hxPost, $minTags, $required);
?>

View File

@@ -24,7 +24,7 @@ $name = $name ?? 'language_autre';
$label = $label ?? 'Autre(s) langue(s)';
$placeholder = $placeholder ?? 'Rechercher une langue…';
$hint = $hint ?? null;
$hxPost = $hxPost ?? '/admin/language-search-fragment.php';
$hxPost = $hxPost ?? '/admin/fragments/language-search.php';
$selectedLanguages = $selectedLanguages ?? [];
$id = $id ?? $name;
$maxLanguages = $maxLanguages ?? 10;

View File

@@ -25,7 +25,7 @@ $name = $name ?? 'tag';
$label = $label ?? 'Mots-clés';
$placeholder = $placeholder ?? 'Rechercher un mot-clé…';
$hint = $hint ?? null;
$hxPost = $hxPost ?? '/admin/tag-search-fragment.php';
$hxPost = $hxPost ?? '/admin/fragments/tag-search.php';
$selectedTags = $selectedTags ?? [];
$id = $id ?? $name;
$maxTags = $maxTags ?? 10;