collections-select.php

Code Hygiene Score: 100

Keine Issues gefunden.

Versionen 2

Code

<?php

/**
 * Collections Select Partial
 *
 * Einheitliches Dropdown für RAG-Collection-Auswahl.
 * Verwendet in: Chat, Content Studio
 *
 * @param array  $collections Array von Collection-Daten aus CollectionRepository
 * @param array  $selected    Array von ausgewählten collection_ids
 * @param string $name        Form-Feldname (default: 'collections[]')
 * @param string $variant     'default', 'inline' oder 'checkbox'
 * @param bool   $multiple    Multi-Select (default: true)
 * @param bool   $required    Mindestens eine Auswahl erforderlich
 */

$collections ??= [];
$selected ??= [];

// JSON-String zu Array konvertieren (Abwärtskompatibilität)
if (is_string($selected)) {
    $selected = json_decode($selected, true) ?: [];
}

$name ??= 'collections[]';
$variant ??= 'default';
$multiple ??= true;
$required ??= false;

$class = match ($variant) {
    'inline' => 'form-select form-select--inline',
    'checkbox' => '', // Für Checkbox-Variante
    default => 'form-select',
};
?>
<?php if ($variant === 'checkbox'): ?>
<div class="checkbox-group collections-select">
    <?php foreach ($collections as $col):
        $colId = $col['collection_id'];
        $isSelected = in_array($colId, $selected, true);
        $points = (int) ($col['points_count'] ?? 0);
        ?>
    <label class="checkbox-label" title="<?= number_format($points) ?> Dokumente">
        <input type="checkbox" name="<?= htmlspecialchars($name) ?>"
               value="<?= htmlspecialchars($colId) ?>"
               <?= $isSelected ? 'checked' : '' ?>>
        <?= htmlspecialchars($col['display_name']) ?>
        <span class="checkbox-meta">(<?= number_format($points) ?>)</span>
    </label>
    <?php endforeach; ?>
</div>
<?php else: ?>
<select name="<?= htmlspecialchars($name) ?>"
        <?= $multiple ? 'multiple' : '' ?>
        class="<?= $class ?> collections-select"
        title="Collections für RAG auswählen"
        <?= $required ? 'required' : '' ?>>
    <?php foreach ($collections as $col):
        $colId = $col['collection_id'];
        $isSelected = in_array($colId, $selected, true);
        $points = (int) ($col['points_count'] ?? 0);
        ?>
    <option value="<?= htmlspecialchars($colId) ?>"
            <?= $isSelected ? 'selected' : '' ?>
            title="<?= number_format($points) ?> Dokumente">
        <?= htmlspecialchars($col['display_name']) ?>
    </option>
    <?php endforeach; ?>
</select>
<?php endif; ?>
← Übersicht