Validate form submissions and action configuration, execute finishers with submission context, and return JSON success or error responses. Add configurable confirmation emails to submitters and report failed submissions in the form UI. Harden database and admin email finishers, and add submission handler and confirmation email checks.
305 lines
16 KiB
PHP
305 lines
16 KiB
PHP
<?php
|
|
|
|
use Plugi\Extensions\Form\DefinitionsRepository;
|
|
|
|
$formsRepository = new DefinitionsRepository();
|
|
|
|
$form = [
|
|
'name' => "New Form",
|
|
'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 ? 'Edit form' : 'New form' ?></h2>
|
|
<p class="text-sm text-base-content/60"><?= $isEditing ? 'Change your form settings.' : 'Create a new form with fields and actions.' ?></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: 'form_field', label: 'Form field', type: 'text', required: false, options: []},
|
|
defaultFinisher: {type: 'saveToDatabase'},
|
|
dragIndex: null,
|
|
addField() {
|
|
const nextNum = this.fields.length + 1;
|
|
this.fields.push({
|
|
name: 'field_' + nextNum,
|
|
label: 'Field ' + nextNum,
|
|
type: 'text',
|
|
required: false,
|
|
options: []
|
|
});
|
|
},
|
|
getValidationErrors(field, index) {
|
|
const errors = [];
|
|
if (!field.name || !field.name.trim()) {
|
|
errors.push('Field name cannot be empty.');
|
|
}
|
|
if (!field.label || !field.label.trim()) {
|
|
errors.push('Label cannot be empty.');
|
|
}
|
|
// 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('Field name is already in use.');
|
|
}
|
|
return errors;
|
|
},
|
|
getOptionsLabel(type) {
|
|
if (type === 'select') return 'Dropdown options';
|
|
if (type === 'checkbox') return 'Checkbox options (multiple selections)';
|
|
if (type === 'radio') return 'Radio options (single selection)';
|
|
return 'Options';
|
|
},
|
|
setFinisherType(finisher, type) {
|
|
finisher.type = type;
|
|
if (type === 'sendConfirmationEmailToSubmitter' && (!finisher.options || typeof finisher.options !== 'object' || Array.isArray(finisher.options))) {
|
|
finisher.options = {
|
|
recipientField: '',
|
|
subject: 'Thank you for your submission',
|
|
message: 'Thank you for contacting us. We have received your message.'
|
|
};
|
|
}
|
|
},
|
|
init() {
|
|
this.finishers.forEach(finisher => {
|
|
if (finisher.type === 'sendConfirmationEmailToSubmitter') {
|
|
this.setFinisherType(finisher, finisher.type);
|
|
}
|
|
});
|
|
},
|
|
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">Basics</h3>
|
|
</div>
|
|
<div class="form-control">
|
|
<label class="label">
|
|
<span class="label-text font-medium">Form name</span>
|
|
</label>
|
|
<input type="text" name="name" value="<?= $form['name'] ?>" placeholder="e.g. Contact form" class="input input-bordered w-full" required />
|
|
<span class="label-text-alt text-base-content/60">A unique name for your form.</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">Form fields</h3>
|
|
</div>
|
|
<p class="text-sm text-base-content/60 mb-4">Define the fields users can fill out.</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">No fields added yet</p>
|
|
<a href="javascript:void(0)" @click="addField()" class="btn btn-sm btn-primary mt-2">Add your first field</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="Drag to move">
|
|
<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="Field name"
|
|
class="input input-bordered input-sm w-full"
|
|
:class="{'input-error': getValidationErrors(field, index).some(e => e.includes('Field name'))}"
|
|
/>
|
|
</div>
|
|
<div class="flex-1">
|
|
<input
|
|
type="text"
|
|
x-model="field.label"
|
|
placeholder="Label"
|
|
class="input input-bordered input-sm w-full"
|
|
:class="{'input-error': getValidationErrors(field, index).some(e => e.includes('Label'))}"
|
|
/>
|
|
</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">Text area</option>
|
|
<option value="checkbox">Checkbox</option>
|
|
<option value="radio">Radio</option>
|
|
<option value="select">Select</option>
|
|
</select>
|
|
<label class="label cursor-pointer gap-2">
|
|
<span class="label-text text-sm">Required</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="Remove">
|
|
<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>
|
|
Add
|
|
</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="Label" class="input input-bordered input-sm flex-1" />
|
|
<input type="text" x-model="option.value" placeholder="Value" 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="Remove">
|
|
<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>
|
|
Add field
|
|
</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">Actions</h3>
|
|
</div>
|
|
<p class="text-sm text-base-content/60 mb-4">What should happen when a form is submitted?</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">No actions added yet</p>
|
|
<a href="javascript:void(0)" @click="finishers.push({...defaultFinisher})" class="btn btn-sm btn-primary mt-2">Add your first action</a>
|
|
</div>
|
|
|
|
<div x-show="finishers.length > 0" class="space-y-2">
|
|
<template x-for="(finisher, index) in finishers">
|
|
<div class="p-3 border border-base-300 rounded-lg bg-base-200/50 space-y-3">
|
|
<div class="flex items-center gap-3">
|
|
<span class="text-xs font-mono text-base-content/40">#[<span x-text="index + 1"></span>]</span>
|
|
<select :value="finisher.type" @change="setFinisherType(finisher, $event.target.value)" class="select select-bordered select-sm flex-1">
|
|
<option value="saveToDatabase">Save to database</option>
|
|
<option value="sendMailToAdmin">Send email to admin</option>
|
|
<option value="sendConfirmationEmailToSubmitter">Send confirmation email to submitter</option>
|
|
</select>
|
|
<button @click="finishers.splice(index, 1)" type="button" class="btn btn-ghost btn-sm btn-circle text-error" title="Remove">
|
|
<i class="ph-duotone ph-trash"></i>
|
|
</button>
|
|
</div>
|
|
<div x-show="finisher.type === 'sendConfirmationEmailToSubmitter'" class="space-y-3 pl-8">
|
|
<div class="form-control">
|
|
<label class="label"><span class="label-text">Recipient email field</span></label>
|
|
<select x-model="finisher.options.recipientField" class="select select-bordered select-sm w-full">
|
|
<option value="">Select an email field</option>
|
|
<template x-for="field in fields.filter(field => field.type === 'email')" :key="field.name">
|
|
<option :value="field.name" x-text="field.label + ' (' + field.name + ')' "></option>
|
|
</template>
|
|
</select>
|
|
<span x-show="fields.filter(field => field.type === 'email').length === 0" class="label-text-alt text-warning mt-1">Add an email field to the form before using this action.</span>
|
|
</div>
|
|
<div class="form-control">
|
|
<label class="label"><span class="label-text">Subject</span></label>
|
|
<input type="text" x-model="finisher.options.subject" class="input input-bordered input-sm w-full" />
|
|
</div>
|
|
<div class="form-control">
|
|
<label class="label"><span class="label-text">Message</span></label>
|
|
<textarea x-model="finisher.options.message" class="textarea textarea-bordered w-full" rows="4"></textarea>
|
|
</div>
|
|
</div>
|
|
</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>
|
|
Add action
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex justify-end gap-2">
|
|
<a href="?tab=ext--form" class="btn btn-ghost btn-sm">
|
|
Cancel
|
|
</a>
|
|
<button type="submit" class="btn btn-primary btn-sm">
|
|
<i class="ph-duotone ph-floppy-disk"></i>
|
|
Save
|
|
</button>
|
|
</div>
|
|
</form>
|