<?php
/**
 * language-autre-fragment.php
 *
 * Shared HTMX fragment include: returns the requirement asterisk for the
 * "Autre(s) langue(s)" label. When no standard language checkbox is checked,
 * an asterisk is rendered to signal the field is required (server-side validated).
 *
 * Included by:
 *   - /admin/language-autre-fragment.php  (AdminAuth gated)
 *   - partage/index.php special route     (public, session already booted)
 *
 * Expected POST:
 *   languages[]      — selected language IDs (may be absent)
 */

$selectedIds  = isset($_POST['languages']) && is_array($_POST['languages'])
    ? $_POST['languages']
    : [];
$anyChecked   = !empty($selectedIds);

// Also check if any "autre" language pills are present (posted as language_autre[])
$hasLangAutre = isset($_POST['language_autre']) && is_array($_POST['language_autre'])
    && count(array_filter($_POST['language_autre'], fn($l) => is_string($l) && trim($l) !== '')) > 0;

// The "Autre(s) langue(s)" label is required if no standard language is checked.
// The "Langue(s) du TFE" checkbox list is required if neither standard languages
// nor "autre" languages are set.
$langAutreRequired = !$anyChecked;
$checkboxesRequired = !$anyChecked && !$hasLangAutre;
?>
<span id="language-autre-required"><?= $langAutreRequired ? ' <span class="asterisk">*</span>' : '' ?></span>
<span id="languages-required-asterisk" hx-swap-oob="true"><?= $checkboxesRequired ? ' <span class="asterisk">*</span>' : '' ?></span>
