form.php

Code Hygiene Score: 100

Keine Issues gefunden.

Versionen 8

Code

<?php ob_start();

// Helper function for version increment
$incrementVersion = function (string $version): string {
    $parts = explode('.', $version);
    $last = (int) array_pop($parts);
    $parts[] = (string) ($last + 1);

    return implode('.', $parts);
};
?>

<h1><?= $isEdit ? 'Bearbeiten: ' . htmlspecialchars($config['name']) : 'Neue Konfiguration' ?></h1>


<form class="form"
      hx-post="<?= $isEdit ? '/config/' . $config['id'] : '/config' ?>"
      hx-headers='{"X-CSRF-TOKEN": "<?= $csrfToken ?>"}'
      hx-disabled-elt="button[type=submit]">

    <div class="form-row">
        <div class="form-group form-group--half">
            <label for="type">Typ *</label>
            <select name="type" id="type" class="form-select" <?= $isEdit ? 'disabled' : 'required' ?>>
                <option value="">-- Typ wählen --</option>
                <?php foreach ($types as $type): ?>
                <option value="<?= $type ?>" <?= ($config['type'] ?? '') === $type ? 'selected' : '' ?>><?= $typeLabels[$type] ?? $type ?></option>
                <?php endforeach; ?>
            </select>
            <?php if ($isEdit): ?>
            <input type="hidden" name="type" value="<?= $config['type'] ?>">
            <?php endif; ?>
        </div>
        <div class="form-group form-group--half">
            <label for="status">Status</label>
            <select name="status" id="status" class="form-select">
                <option value="draft" <?= ($config['status'] ?? 'draft') === 'draft' ? 'selected' : '' ?>>Entwurf</option>
                <option value="active" <?= ($config['status'] ?? '') === 'active' ? 'selected' : '' ?>>Aktiv</option>
                <option value="deprecated" <?= ($config['status'] ?? '') === 'deprecated' ? 'selected' : '' ?>>Deprecated</option>
            </select>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group form-group--half">
            <label for="name">Name *</label>
            <input type="text" name="name" id="name" class="form-input" value="<?= htmlspecialchars($config['name'] ?? '') ?>" required>
        </div>
        <div class="form-group form-group--half">
            <label for="slug">Slug *</label>
            <input type="text" name="slug" id="slug" class="form-input" value="<?= htmlspecialchars($config['slug'] ?? '') ?>" required pattern="[a-z0-9-]+">
            <small>Nur Kleinbuchstaben, Zahlen und Bindestriche</small>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group form-group--half">
            <label for="version"><?= $isEdit ? 'Neue Version *' : 'Version' ?></label>
            <input type="text" name="<?= $isEdit ? 'new_version' : 'version' ?>" id="version" class="form-input"
                   value="<?= $isEdit ? '' : htmlspecialchars($config['version'] ?? '1.0') ?>"
                   placeholder="<?= $isEdit ? 'z.B. ' . $incrementVersion($config['version'] ?? '1.0') : '1.0' ?>"
                   <?= $isEdit ? 'required' : '' ?>>
            <?php if ($isEdit): ?>
            <small>Aktuelle Version: <?= htmlspecialchars($config['version']) ?></small>
            <?php endif; ?>
        </div>
        <div class="form-group form-group--half">
            <label for="parent_id">Parent (optional)</label>
            <select name="parent_id" id="parent_id" class="form-select">
                <option value="">-- Kein Parent --</option>
                <?php foreach ($parents as $parent): ?>
                <option value="<?= $parent['id'] ?>" <?= ($config['parent_id'] ?? '') == $parent['id'] ? 'selected' : '' ?>>
                    [<?= $typeLabels[$parent['type']] ?? $parent['type'] ?>] <?= htmlspecialchars($parent['name']) ?>
                </option>
                <?php endforeach; ?>
            </select>
            <small>Nur für Rules: Verknüpfung zu Contract/Organization</small>
        </div>
    </div>

    <div class="form-group">
        <label for="description">Beschreibung</label>
        <textarea name="description" id="description" class="form-textarea" rows="2"><?= htmlspecialchars($config['description'] ?? '') ?></textarea>
    </div>

    <!-- Critic-spezifische Felder -->
    <div id="critic-fields" class="form-row" style="display: <?= ($config['type'] ?? '') === 'critic' ? 'flex' : 'none' ?>;">
        <div class="form-group form-group--half">
            <label for="prompt_id">Prompt-Template</label>
            <select name="prompt_id" id="prompt_id" class="form-select">
                <option value="">-- Generischer Prompt --</option>
                <?php foreach ($prompts ?? [] as $prompt): ?>
                <option value="<?= $prompt['id'] ?>" <?= ($config['prompt_id'] ?? '') == $prompt['id'] ? 'selected' : '' ?>>
                    <?= htmlspecialchars($prompt['name']) ?> (v<?= $prompt['version'] ?>)
                </option>
                <?php endforeach; ?>
            </select>
            <small>Welcher Prompt fuer diesen Kritiker verwendet werden soll</small>
        </div>
        <div class="form-group form-group--half">
            <label for="sort_order">Reihenfolge</label>
            <input type="number" name="sort_order" id="sort_order" class="form-input"
                   value="<?= $config['sort_order'] ?? 0 ?>" min="0">
            <small>Niedrigere Werte werden zuerst ausgefuehrt</small>
        </div>
    </div>

    <?php if ($isEdit): ?>
    <div class="form-group">
        <label for="change_description">Änderungsbeschreibung</label>
        <input type="text" name="change_description" id="change_description" class="form-input" placeholder="Was wurde geändert?">
    </div>
    <?php endif; ?>

    <div class="form-group">
        <label for="content">Content (JSON) *</label>
        <textarea name="content" id="content" class="form-textarea form-textarea--code" rows="15" required><?= htmlspecialchars($config['content'] ?? '{}') ?></textarea>
        <small>
            <button type="button" class="btn btn--small btn--secondary" onclick="formatJson()">JSON formatieren</button>
            <button type="button" class="btn btn--small btn--secondary" onclick="validateJson()">JSON prüfen</button>
            <span id="json-status"></span>
        </small>
    </div>

    <div class="form-actions">
        <button type="submit" class="btn btn--primary"><?= $isEdit ? 'Speichern' : 'Erstellen' ?></button>
        <a href="<?= $isEdit ? '/config/' . $config['id'] : '/config' ?>" class="btn btn--secondary">Abbrechen</a>
    </div>
</form>

<?php if ($isEdit): ?>
<hr>
<h3>Gefahrenzone</h3>
<button type="button" class="btn btn--danger"
        hx-post="/config/<?= $config['id'] ?>/delete"
        hx-headers='{"X-CSRF-TOKEN": "<?= $csrfToken ?>"}'
        hx-confirm="Wirklich loeschen? Diese Aktion kann nicht rueckgaengig gemacht werden."
        hx-disabled-elt="this">
    Konfiguration loeschen
</button>
<?php endif; ?>

<script>
// Auto-generate slug from name
document.getElementById('name')?.addEventListener('input', function() {
    const slugField = document.getElementById('slug');
    if (slugField && !slugField.dataset.manual) {
        slugField.value = this.value
            .toLowerCase()
            .replace(/[äöüß]/g, m => ({ä:'ae',ö:'oe',ü:'ue',ß:'ss'}[m]))
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-|-$/g, '');
    }
});

document.getElementById('slug')?.addEventListener('input', function() {
    this.dataset.manual = 'true';
});

// Show/hide critic fields based on type selection
document.getElementById('type')?.addEventListener('change', function() {
    const criticFields = document.getElementById('critic-fields');
    if (criticFields) {
        criticFields.style.display = this.value === 'critic' ? 'flex' : 'none';
    }
});

function formatJson() {
    const textarea = document.getElementById('content');
    try {
        const obj = JSON.parse(textarea.value);
        textarea.value = JSON.stringify(obj, null, 2);
        document.getElementById('json-status').innerHTML = '<span style="color:green">Formatiert</span>';
    } catch (e) {
        document.getElementById('json-status').innerHTML = '<span style="color:red">Ungültiges JSON: ' + e.message + '</span>';
    }
}

function validateJson() {
    const textarea = document.getElementById('content');
    try {
        JSON.parse(textarea.value);
        document.getElementById('json-status').innerHTML = '<span style="color:green">Gültiges JSON</span>';
    } catch (e) {
        document.getElementById('json-status').innerHTML = '<span style="color:red">Ungültiges JSON: ' + e.message + '</span>';
    }
}

// Initial validation
validateJson();
</script>

<style>
.form-textarea--code {
    font-family: 'Fira Code', 'Monaco', 'Consolas', monospace;
    font-size: 13px;
    line-height: 1.5;
    tab-size: 2;
}
</style>

<?php $content = ob_get_clean(); ?>
<?php require VIEW_PATH . '/layout.php'; ?>
← Übersicht