add ext-form

This commit is contained in:
2026-11-22 16:09:43 +01:00
parent ef4be66673
commit 932c7d21fe
25 changed files with 2681 additions and 0 deletions
@@ -0,0 +1,264 @@
<?php
use Plugi\Extensions\Form\DefinitionsRepository;
$formsRepository = new DefinitionsRepository();
$form = [
'name' => "Neues Formular",
'fields' => "[]",
'finishers' => "[]",
];
if(isset($_GET['id']))
$form = $formsRepository->findWithId($_GET['id']);
$isEditing = isset($form['id']) && $form['id'] > 0;
?>
<div class="mb-6">
<div class="flex items-center gap-3">
<a href="?tab=ext--form" class="btn btn-ghost btn-sm btn-circle">
<i class="ph-duotone ph-arrow-left text-xl"></i>
</a>
<div>
<h2 class="text-xl font-bold"><?= $isEditing ? 'Formular bearbeiten' : 'Neues Formular' ?></h2>
<p class="text-sm text-base-content/60"><?= $isEditing ? 'Ändere die Einstellungen deines Formulars.' : 'Erstelle ein neues Formular mit Feldern und Aktionen.' ?></p>
</div>
</div>
</div>
<script>
const fields = <?= $form["fields"] ?>;
const finishers = <?= $form["finishers"] ?>;
</script>
<form action="/ext--form/save-form" method="post" x-data="{
fields: fields,
finishers: finishers,
defaultField: {name: 'formularfeld', label: 'Formularfeld', type: 'text', required: false, options: []},
defaultFinisher: {type: 'saveToDatabase'},
dragIndex: null,
addField() {
const nextNum = this.fields.length + 1;
this.fields.push({
name: 'feld_' + nextNum,
label: 'Feld ' + nextNum,
type: 'text',
required: false,
options: []
});
},
getValidationErrors(field, index) {
const errors = [];
if (!field.name || !field.name.trim()) {
errors.push('Feldname darf nicht leer sein.');
}
if (!field.label || !field.label.trim()) {
errors.push('Beschriftung darf nicht leer sein.');
}
// Check for duplicate names
const nameCount = this.fields.filter((f, i) => i !== index && f.name && f.name.trim() === field.name && field.name.trim() !== '').length;
if (nameCount > 0) {
errors.push('Feldname wird bereits verwendet.');
}
return errors;
},
getOptionsLabel(type) {
if (type === 'select') return 'Dropdown-Optionen';
if (type === 'checkbox') return 'Checkbox-Optionen (mehrfach wählbar)';
if (type === 'radio') return 'Radio-Optionen (einzeln wählbar)';
return 'Optionen';
},
moveField(fromIndex, toIndex) {
if (toIndex < 0 || toIndex >= this.fields.length) return;
const field = this.fields.splice(fromIndex, 1)[0];
this.fields.splice(toIndex, 0, field);
}
}">
<?php
if(isset($form['id'])) echo '<input type="hidden" name="id" value="'.$form['id'].'">';
?>
<div class="card bg-base-100 shadow mb-6">
<div class="card-body">
<div class="flex items-center gap-2 mb-4">
<i class="ph-duotone ph-text-aa text-2xl text-base-content/60"></i>
<h3 class="card-title text-lg">Grundlegendes</h3>
</div>
<div class="form-control">
<label class="label">
<span class="label-text font-medium">Formularname</span>
</label>
<input type="text" name="name" value="<?= $form['name'] ?>" placeholder="z.B. Kontaktformular" class="input input-bordered w-full" required />
<span class="label-text-alt text-base-content/60">Ein eindeutiger Name für dein Formular.</span>
</div>
</div>
</div>
<div class="card bg-base-100 shadow mb-6">
<div class="card-body">
<div class="flex items-center gap-2 mb-4">
<i class="ph-duotone ph-list-dashes text-2xl text-base-content/60"></i>
<h3 class="card-title text-lg">Formularfelder</h3>
</div>
<p class="text-sm text-base-content/60 mb-4">Definiere die Felder, die Nutzer ausfüllen können.</p>
<input type="hidden" name="fields" :value="JSON.stringify(fields)">
<div x-show="fields.length === 0" class="text-center py-8 border-2 border-dashed border-base-300 rounded-lg">
<i class="ph-duotone ph-plus-circle text-4xl text-base-content/30"></i>
<p class="text-sm text-base-content/50 mt-2">Noch keine Felder hinzugefügt</p>
<a href="javascript:void(0)" @click="addField()" class="btn btn-sm btn-primary mt-2">Erstes Feld hinzufügen</a>
</div>
<div x-show="fields.length > 0" class="space-y-3">
<template x-for="(field, index) in fields">
<div
class="border rounded-lg p-3 bg-base-200/50 transition-colors"
:class="{
'border-error': getValidationErrors(field, index).length > 0,
'border-base-300': getValidationErrors(field, index).length === 0,
'bg-base-200': dragIndex === index
}"
draggable="true"
@dragstart="dragIndex = index; $event.dataTransfer.effectAllowed = 'move'"
@dragover.prevent="$event.dataTransfer.dropEffect = 'move'"
@drop.prevent="moveField(dragIndex, index)"
@dragend="dragIndex = null"
>
<div class="flex items-center gap-2 mb-2">
<!-- Drag handle -->
<button type="button" class="btn btn-ghost btn-xs btn-square cursor-grab active:cursor-grabbing text-base-content/40 hover:text-base-content" title="Ziehen zum Verschieben">
<i class="ph-duotone ph-dots-six-vertical text-lg"></i>
</button>
<span class="text-xs font-mono text-base-content/40 w-6">#[<span x-text="index + 1"></span>]</span>
<div class="flex-1">
<input
type="text"
x-model="field.name"
placeholder="Feldname"
class="input input-bordered input-sm w-full"
:class="{'input-error': getValidationErrors(field, index).some(e => e.includes('Feldname'))}"
/>
</div>
<div class="flex-1">
<input
type="text"
x-model="field.label"
placeholder="Beschriftung"
class="input input-bordered input-sm w-full"
:class="{'input-error': getValidationErrors(field, index).some(e => e.includes('Beschriftung'))}"
/>
</div>
</div>
<!-- Validation errors -->
<div x-show="getValidationErrors(field, index).length > 0" x-transition class="mb-2 p-2 bg-error/10 border border-error/30 rounded text-error text-xs">
<template x-for="(error, errIndex) in getValidationErrors(field, index)">
<div :key="errIndex" class="flex items-center gap-1">
<i class="ph-duotone ph-warning text-sm"></i>
<span x-text="error"></span>
</div>
</template>
</div>
<div class="flex items-center gap-2">
<select x-model="field.type" class="select select-bordered select-sm flex-1">
<option value="text">Text</option>
<option value="email">Email</option>
<option value="textarea">Textbereich</option>
<option value="checkbox">Checkbox</option>
<option value="radio">Radio</option>
<option value="select">Auswahl</option>
</select>
<label class="label cursor-pointer gap-2">
<span class="label-text text-sm">Pflichtfeld</span>
<input type="checkbox" x-model="field.required" class="checkbox checkbox-sm checkbox-primary" />
</label>
<button @click="fields.splice(index, 1)" type="button" class="btn btn-ghost btn-sm btn-circle text-error" title="Entfernen">
<i class="ph-duotone ph-trash"></i>
</button>
</div>
<div x-show="field.type === 'select' || field.type === 'checkbox' || field.type === 'radio'" x-transition class="mt-3 ml-2 pl-4 border-l-2 border-primary/30">
<div class="flex items-center gap-2 mb-2">
<span class="text-xs font-medium text-base-content/60 uppercase tracking-wide" x-text="getOptionsLabel(field.type)"></span>
<a href="javascript:void(0)" @click="field.options.push({label: 'Option', value: 'option'})" class="btn btn-xs btn-ghost btn-primary">
<i class="ph-duotone ph-plus"></i>
Neu
</a>
</div>
<div class="space-y-1">
<template x-for="(option, optIndex) in field.options">
<div class="flex items-center gap-2">
<span class="text-xs text-base-content/40 w-4">#[<span x-text="optIndex + 1"></span>]</span>
<input type="text" x-model="option.label" placeholder="Beschriftung" class="input input-bordered input-sm flex-1" />
<input type="text" x-model="option.value" placeholder="Wert" class="input input-bordered input-sm flex-1" />
<button @click="field.options.splice(optIndex, 1)" type="button" class="btn btn-ghost btn-xs btn-circle text-error" title="Entfernen">
<i class="ph-duotone ph-x"></i>
</button>
</div>
</template>
</div>
</div>
</div>
</template>
</div>
<div class="mt-4">
<a href="javascript:void(0)" @click="addField()" class="btn btn-outline btn-sm btn-primary">
<i class="ph-duotone ph-plus"></i>
Feld hinzufügen
</a>
</div>
</div>
</div>
<div class="card bg-base-100 shadow mb-6">
<div class="card-body">
<div class="flex items-center gap-2 mb-4">
<i class="ph-duotone ph-gear text-2xl text-base-content/60"></i>
<h3 class="card-title text-lg">Aktionen</h3>
</div>
<p class="text-sm text-base-content/60 mb-4">Was soll passieren, wenn ein Formular abgeschickt wird?</p>
<input type="hidden" name="finishers" :value="JSON.stringify(finishers)">
<div x-show="finishers.length === 0" class="text-center py-8 border-2 border-dashed border-base-300 rounded-lg">
<i class="ph-duotone ph-plus-circle text-4xl text-base-content/30"></i>
<p class="text-sm text-base-content/50 mt-2">Noch keine Aktionen hinzugefügt</p>
<a href="javascript:void(0)" @click="finishers.push({...defaultFinisher})" class="btn btn-sm btn-primary mt-2">Erste Aktion hinzufügen</a>
</div>
<div x-show="finishers.length > 0" class="space-y-2">
<template x-for="(finisher, index) in finishers">
<div class="flex items-center gap-3 p-3 border border-base-300 rounded-lg bg-base-200/50">
<span class="text-xs font-mono text-base-content/40">#[<span x-text="index + 1"></span>]</span>
<select x-model="finisher.type" class="select select-bordered select-sm flex-1">
<option value="saveToDatabase">In Datenbank speichern</option>
<option value="sendMailToAdmin">Email an Admin senden</option>
</select>
<button @click="finishers.splice(index, 1)" type="button" class="btn btn-ghost btn-sm btn-circle text-error" title="Entfernen">
<i class="ph-duotone ph-trash"></i>
</button>
</div>
</template>
</div>
<div class="mt-4">
<a href="javascript:void(0)" @click="finishers.push({...defaultFinisher})" class="btn btn-outline btn-sm btn-primary">
<i class="ph-duotone ph-plus"></i>
Aktion hinzufügen
</a>
</div>
</div>
</div>
<div class="flex justify-end gap-2">
<a href="?tab=ext--form" class="btn btn-ghost btn-sm">
Abbrechen
</a>
<button type="submit" class="btn btn-primary btn-sm">
<i class="ph-duotone ph-floppy-disk"></i>
Speichern
</button>
</div>
</form>
@@ -0,0 +1,16 @@
<div class="card bg-base-100 shadow">
<div class="card-body">
<div class="flex items-center gap-2 mb-4">
<i class="ph-duotone ph-gear text-2xl text-base-content/60"></i>
<h3 class="card-title text-lg">Einstellungen</h3>
</div>
<p class="text-sm text-base-content/60 mb-4">Konfiguriere die globalen Einstellungen für das Formulare-Extension.</p>
<div class="space-y-4">
<?php
foreach (\Plugi\Extensions::getSettings('form') as $setting) {
phpb_render_setting($setting);
}
?>
</div>
</div>
</div>
@@ -0,0 +1,88 @@
<?php
use Plugi\Extensions\Form\DefinitionsRepository;
use Plugi\Extensions\Form\EntriesRepository;
$id = $_GET['id'];
$formsRepository = new DefinitionsRepository();
$form = $formsRepository->findWithId($id);
$fields = json_decode($form['fields'], true);
$entriesRepository = new EntriesRepository();
$entries = $entriesRepository->findWhere('formId', $id);
?>
<div class="mb-6">
<div class="flex items-center gap-3">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--form']) ?>" class="btn btn-ghost btn-sm btn-circle">
<i class="ph-duotone ph-arrow-left text-xl"></i>
</a>
<div>
<h2 class="text-xl font-bold"><?= phpb_e($form['name']) ?></h2>
<p class="text-sm text-base-content/60"><?= count($entries) ?> Eintrag<?= count($entries) !== 1 ? 'e' : '' ?> für dieses Formular</p>
</div>
</div>
</div>
<?php if (empty($entries)): ?>
<div class="card bg-base-100 shadow">
<div class="card-body items-center text-center py-16">
<i class="ph-duotone ph-inbox text-6xl text-base-content/20"></i>
<h3 class="card-title text-lg mt-4">Noch keine Einträge</h3>
<p class="text-base-content/60 mt-1">Wenn jemand das Formular ausfüllt, erscheint die Eintragung hier.</p>
</div>
</div>
<?php else: ?>
<div class="card bg-base-100 shadow">
<div class="card-body p-0">
<div class="overflow-x-auto">
<table class="table w-full">
<thead>
<tr class="bg-base-200/50">
<th class="w-16">ID</th>
<th class="w-40">Erstellt</th>
<?php foreach ($fields as $field): ?>
<th><?= phpb_e($field['label']) ?></th>
<?php endforeach; ?>
<th class="w-12 text-center"><?= phpb_trans('website-manager.actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($entries as $entry):
$entryData = json_decode($entry['data'], true);
$createdDate = new DateTime($entry['created'] . ' UTC');
$createdDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
?>
<tr class="hover:bg-base-200/30">
<td class="font-mono text-sm text-base-content/60"><?= phpb_e($entry['id']) ?></td>
<td class="text-sm"><?= $createdDate->format('d.m.Y H:i') ?></td>
<?php foreach ($fields as $field):
$fieldName = $field['name'];
$fieldValue = '';
if (isset($entryData['fields'][$fieldName])) {
$fieldValue = $entryData['fields'][$fieldName];
// Handle array values (e.g., from checkboxes)
if (is_array($fieldValue)) {
$fieldValue = implode(', ', $fieldValue);
}
}
?>
<td class="text-sm"><?= phpb_e($fieldValue) ?></td>
<?php endforeach; ?>
<td>
<div class="flex justify-center">
<a href="/ext--form/delete-entry?id=<?= $entry['id'] ?>&formId=<?= $id ?>" class="btn btn-ghost btn-sm btn-circle text-error" title="<?= phpb_trans('website-manager.remove') ?>">
<i class="ph-duotone ph-trash text-lg"></i>
</a>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
@@ -0,0 +1,72 @@
<?php
use Plugi\Extensions\Form\DefinitionsRepository;
$formsRepository = new DefinitionsRepository();
$forms = $formsRepository->getAll();
?>
<div class="flex items-center justify-between mb-6">
<div>
<h2 class="text-xl font-bold">Formulare</h2>
<p class="text-sm text-base-content/60"><?= count($forms) ?> Formular<?= count($forms) !== 1 ? 'e' : '' ?> vorhanden</p>
</div>
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--form', 'action' => 'editForm']) ?>" class="btn btn-primary btn-sm">
<i class="ph-duotone ph-plus"></i>
Neues Formular
</a>
</div>
<?php if (empty($forms)): ?>
<div class="card bg-base-100 shadow">
<div class="card-body items-center text-center py-16">
<i class="ph-duotone ph-notebook text-6xl text-base-content/20"></i>
<h3 class="card-title text-lg mt-4">Noch keine Formulare</h3>
<p class="text-base-content/60 mt-1">Erstelle dein erstes Formular, um Einträge von deiner Website zu sammeln.</p>
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--form', 'action' => 'editForm']) ?>" class="btn btn-primary mt-4">
<i class="ph-duotone ph-plus"></i>
Erstes Formular erstellen
</a>
</div>
</div>
<?php else: ?>
<div class="card bg-base-100 shadow">
<div class="card-body p-0">
<div class="overflow-x-auto">
<table class="table w-full">
<thead>
<tr class="bg-base-200/50">
<th class="w-16">ID</th>
<th><?= phpb_trans('website-manager.name') ?></th>
<th class="w-16 text-center"><?= phpb_trans('website-manager.actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($forms as $form): ?>
<tr class="hover:bg-base-200/30">
<td class="font-mono text-sm text-base-content/60"><?= phpb_e($form['id']) ?></td>
<td>
<div class="font-medium"><?= phpb_e($form['name']) ?></div>
</td>
<td>
<div class="flex items-center justify-center gap-1">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--form', 'action' => 'listEntries', 'id' => $form['id']]) ?>" class="btn btn-ghost btn-sm btn-circle" title="Einträge ansehen">
<i class="ph-duotone ph-eye text-lg"></i>
</a>
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--form', 'action' => 'editForm', 'id' => $form['id']]) ?>" class="btn btn-ghost btn-sm btn-circle" title="<?= phpb_trans('website-manager.edit') ?>">
<i class="ph-duotone ph-pencil-line text-lg"></i>
</a>
<a href="/ext--form/delete-form?id=<?= $form['id'] ?>" class="btn btn-ghost btn-sm btn-circle text-error" title="<?= phpb_trans('website-manager.remove') ?>">
<i class="ph-duotone ph-trash text-lg"></i>
</a>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>