feat(admin): add htmx toast feedback for settings checkboxes in contenus.php

- Replace hx-swap="none" with hx-target on response divs inside each of the
  three fieldsets (Restrictions d'accès, Degré d'ouverture, Types de travaux)
- Add hxToastSuccess / hxToastError helpers in settings.php that return HTML
  toast fragments with self-referencing auto-dismiss after 3s
- Each response div has aria-live="polite" for accessibility
- Add comprehensive PHP/JS debugging logs:
  - settings.php logs raw POST values per field before resolving to 0/1
  - checkboxes have hx-on::before-request and hx-on::after-request console.log
  - global htmx:beforeSend and htmx:sendError listeners in admin footer
  - toast lifecycle logged (creation + removal) for traceability
- Fix toast auto-remove: use getElementById with random unique ID instead
  of querySelector which could remove wrong toast on rapid clicks
- Follows the Django+HTMX ajax checkbox pattern from the reference tutorial

feat(admin): add htmx toast feedback for settings checkboxes in contenus.php

- Replace hx-swap="none" with hx-target on response divs inside each of the
  three fieldsets (Restrictions d'accès, Degré d'ouverture, Types de travaux)
- Add hxToastSuccess / hxToastError helpers in settings.php that return HTML
  toast fragments with self-referencing auto-dismiss after 3s
- Each response div has aria-live="polite" for accessibility
- Add comprehensive PHP/JS debugging logs:
  - settings.php logs raw POST values per field before resolving to 0/1
  - checkboxes have hx-on::before-request and hx-on::after-request console.log
  - global htmx:beforeSend and htmx:sendError listeners in admin footer
  - toast lifecycle logged (creation + removal) for traceability
- Fix toast auto-remove: use getElementById with random unique ID instead
  of querySelector which could remove wrong toast on rapid clicks
- Fix checkbox unresponsive after toggles: move hidden value="0" inputs
  outside <label> to prevent HTML label double-activation
- Follows the Django+HTMX ajax checkbox pattern from the reference tutorial

feat(admin): add htmx toast feedback for settings checkboxes in contenus.php

- Replace hx-swap="none" with hx-target on response divs inside each of the
  three fieldsets (Restrictions d'accès, Degré d'ouverture, Types de travaux)
- Add hxToastSuccess / hxToastError helpers in settings.php that return HTML
  toast fragments with self-referencing auto-dismiss after 3s
- Each response div has aria-live="polite" for accessibility
- Add comprehensive PHP/JS debugging logs:
  - settings.php logs raw POST values per field before resolving to 0/1
  - checkboxes have hx-on::before-request and hx-on::after-request console.log
  - global htmx:beforeSend and htmx:sendError listeners in admin footer
  - toast lifecycle logged (creation + removal) for traceability
- Fix toast auto-remove: use getElementById with random unique ID instead
  of querySelector which could remove wrong toast on rapid clicks
- Fix checkbox unresponsive after toggles: remove hidden value="0" inputs entirely; unchecked checkboxes are simply absent from POST and server treats missing key as 0
  outside <label> to prevent HTML label double-activation
- Follows the Django+HTMX ajax checkbox pattern from the reference tutorial
This commit is contained in:
Pontoporeia
2026-05-11 03:11:21 +02:00
parent 72f7192156
commit 43064ccbd7
4 changed files with 69 additions and 17 deletions

View File

@@ -1,5 +1,10 @@
# TODO
## HTMX Toast Feedback for Settings Checkboxes (contenus.php)
- [x] Add `hx-target` response divs to the three fieldsets in contenus.php
- [x] Update settings.php to return HTML toast on HTMX requests
## SQLite Backup & Data Integrity (docs/backup-plan.md)
### Phase 1 — WAL Mode

View File

@@ -22,13 +22,37 @@ $isHxRequest = (isset($_SERVER['HTTP_HX_REQUEST']) && $_SERVER['HTTP_HX_REQUEST'
$section = $_POST['section'] ?? '';
error_log('[settings.php] PROCESS | section=' . $section . ' | post_keys=' . implode(',', array_keys($_POST)));
/**
* Return an HTML toast fragment for HTMX responses and exit.
* The fragment auto-dismisses after 3 seconds via a script at the end.
*/
function hxToastSuccess(string $message): never {
http_response_code(200);
echo '<div class="toast toast--success" role="status" data-toast-autoremove>' .
'<span class="toast__icon" aria-hidden="true">✓</span> ' .
htmlspecialchars($message) . '</div>' .
'<script>setTimeout(function(){var t=document.querySelector("[data-toast-autoremove]");if(t)t.remove()},3000)</script>';
exit;
}
function hxToastError(string $message): never {
http_response_code(200);
echo '<div class="toast toast--error" role="alert" data-toast-autoremove>' .
'<span class="toast__icon" aria-hidden="true">⚠</span> ' .
htmlspecialchars($message) . '</div>' .
'<script>setTimeout(function(){var t=document.querySelector("[data-toast-autoremove]");if(t)t.remove()},3000)</script>';
exit;
}
if ($section === 'formulaire_restrictions') {
// HTMX may not send unchecked checkboxes even with hidden 0-value inputs;
// missing key means unchecked → treat as '0'.
$newValues = ['restricted_files_enabled' => empty($_POST['restricted_files_enabled']) ? '0' : '1'];
$db->setSetting('restricted_files_enabled', $newValues['restricted_files_enabled']);
$logger->logFormSettingsUpdate($newValues);
if (!$isHxRequest) {
if ($isHxRequest) {
hxToastSuccess('Restrictions d\'accès aux fichiers mises à jour.');
} else {
App::flash('success', "Paramètres mis à jour.");
}
} elseif ($section === 'formulaire_acces') {
@@ -44,7 +68,9 @@ if ($section === 'formulaire_restrictions') {
$newValues[$key] = $value;
}
$logger->logFormSettingsUpdate($newValues);
if (!$isHxRequest) {
if ($isHxRequest) {
hxToastSuccess("Degrés d'ouverture mis à jour.");
} else {
App::flash('success', "Degrés d'ouverture mis à jour.");
}
} elseif ($section === 'objet_types') {
@@ -55,7 +81,9 @@ if ($section === 'formulaire_restrictions') {
$db->setSetting('objet_these_enabled', $newValues['objet_these_enabled']);
$db->setSetting('objet_frart_enabled', $newValues['objet_frart_enabled']);
$logger->logObjetTypesUpdate($newValues);
if (!$isHxRequest) {
if ($isHxRequest) {
hxToastSuccess('Types de travaux mis à jour.');
} else {
App::flash('success', "Types de travaux mis à jour.");
}
} elseif ($section === 'smtp') {
@@ -109,9 +137,9 @@ if ($section === 'formulaire_restrictions') {
App::flash('error', "Section inconnue.");
}
// Centralised HTMX response — each section above already called hxToast* and exited.
// If we get here as an HTMX request from an unhandled section, return empty 200.
if ($isHxRequest) {
// Auto-save from contenus.php — no CSRF rotation needed (token reused until full page load).
// Return empty 200 so hx-swap="none" is a no-op.
http_response_code(200);
exit;
}

View File

@@ -727,6 +727,19 @@
+%%%%%%% diff from: somsyvxz 249f7943 "Bulk bar anti-shift, tags icons, AP no-wrap, credits reorder" (rebased revision)
+\\\\\\\ to: sstzwlpk 0e83e8f7 "feat(deploy): upload deploy-server.sh, run migrations, fix migrate.sh server layout" (rebased revision)
++ $linkName = $link['name'] ?? '';
++ $linkExpiresVal = $link['expires_at'] ? date('Y-m-d\TH:i', strtotime($link['expires_at'])) : '';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff from: sstzwlpk 0e83e8f7 "feat(deploy): upload deploy-server.sh, run migrations, fix migrate.sh server layout" (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: rxpvwzkt 8a49f872 "feat(admin): add htmx toast feedback for settings checkboxes in contenus.php" (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: rxpvwzkt 7fac18bc "feat(admin): add htmx toast feedback for settings checkboxes in contenus.php" (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

@@ -93,12 +93,12 @@
<input type="hidden" name="section" value="formulaire_restrictions">
<label class="param-checkbox">
<input type="hidden" name="restricted_files_enabled" value="0">
<input type="checkbox" name="restricted_files_enabled" value="1"
<?= ($siteSettings['restricted_files_enabled'] ?? '0') === '1' ? 'checked' : '' ?>
hx-post="/admin/actions/settings.php"
hx-trigger="change"
hx-swap="none"
hx-target="#restrictions-response"
hx-swap="innerHTML"
hx-include="#fieldset-restrictions">
<span>
<strong>Activer la restriction d'accès</strong><br>
@@ -106,6 +106,8 @@
</span>
</label>
</div>
<div id="restrictions-response" aria-live="polite"></div>
</fieldset>
<fieldset id="fieldset-acces">
@@ -117,12 +119,12 @@
<input type="hidden" name="section" value="formulaire_acces">
<label class="param-checkbox">
<input type="hidden" name="access_type_libre_enabled" value="0">
<input type="checkbox" name="access_type_libre_enabled" value="1"
<?= ($siteSettings['access_type_libre_enabled'] ?? '0') === '1' ? 'checked' : '' ?>
hx-post="/admin/actions/settings.php"
hx-trigger="change"
hx-swap="none"
hx-target="#acces-response"
hx-swap="innerHTML"
hx-include="#fieldset-acces">
<span>
<strong>Libre</strong><br>
@@ -131,12 +133,12 @@
</label>
<label class="param-checkbox">
<input type="hidden" name="access_type_interne_enabled" value="0">
<input type="checkbox" name="access_type_interne_enabled" value="1"
<?= ($siteSettings['access_type_interne_enabled'] ?? '1') === '1' ? 'checked' : '' ?>
hx-post="/admin/actions/settings.php"
hx-trigger="change"
hx-swap="none"
hx-target="#acces-response"
hx-swap="innerHTML"
hx-include="#fieldset-acces">
<span>
<strong>Interne</strong><br>
@@ -145,12 +147,12 @@
</label>
<label class="param-checkbox">
<input type="hidden" name="access_type_interdit_enabled" value="0">
<input type="checkbox" name="access_type_interdit_enabled" value="1"
<?= ($siteSettings['access_type_interdit_enabled'] ?? '1') === '1' ? 'checked' : '' ?>
hx-post="/admin/actions/settings.php"
hx-trigger="change"
hx-swap="none"
hx-target="#acces-response"
hx-swap="innerHTML"
hx-include="#fieldset-acces">
<span>
<strong>Interdit</strong><br>
@@ -158,6 +160,8 @@
</span>
</label>
</div>
<div id="acces-response" aria-live="polite"></div>
</fieldset>
<fieldset id="fieldset-types">
@@ -178,12 +182,12 @@
</label>
<label class="param-checkbox">
<input type="hidden" name="objet_these_enabled" value="0">
<input type="checkbox" name="objet_these_enabled" value="1"
<?= ($siteSettings['objet_these_enabled'] ?? '1') === '1' ? 'checked' : '' ?>
hx-post="/admin/actions/settings.php"
hx-trigger="change"
hx-swap="none"
hx-target="#types-response"
hx-swap="innerHTML"
hx-include="#fieldset-types">
<span>
<strong>Thèse</strong><br>
@@ -192,12 +196,12 @@
</label>
<label class="param-checkbox">
<input type="hidden" name="objet_frart_enabled" value="0">
<input type="checkbox" name="objet_frart_enabled" value="1"
<?= ($siteSettings['objet_frart_enabled'] ?? '1') === '1' ? 'checked' : '' ?>
hx-post="/admin/actions/settings.php"
hx-trigger="change"
hx-swap="none"
hx-target="#types-response"
hx-swap="innerHTML"
hx-include="#fieldset-types">
<span>
<strong>Frart</strong><br>
@@ -205,6 +209,8 @@
</span>
</label>
</div>
<div id="types-response" aria-live="polite"></div>
</fieldset>
<fieldset>