Files
plugi/extensions/plugi-ext-form/blocks/form-wrapper/view.php
T
deepDiverPaul 151ac9df4c refresh form extension admin interface
Improve form editing, settings, submission lists, and navigation with responsive layouts and accessibility enhancements. Update the form wrapper block view and regenerate the extension stylesheet. Ignore .agents and skills-lock.json.
2026-11-25 09:59:51 +01:00

156 lines
8.4 KiB
PHP

<?php
$formRepository = new \Plugi\Extensions\Form\DefinitionsRepository();
$id = $block->setting("formId");
if ($id > 0):
$form = $formRepository->findWithId($id);
$fields = json_decode($form["fields"], true);
?>
<div class="mx-auto w-full max-w-2xl">
<div x-data="{data: {fields : {}}, status: 'idle', submitted: false, errorMessage: ''}" class="space-y-4">
<form x-show="!submitted" x-transition:leave x-transition:leave-opacity-0 x-on:submit.prevent="
status = 'busy';
errorMessage = '';
try {
const response = await fetch('/ext--form/submit?id=<?= $id ?>', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
});
const result = await response.json();
if (!response.ok || !result.success) {
throw new Error((result.errors || []).join(' ') || 'Your submission could not be completed. Please try again.');
}
submitted = true;
} catch (error) {
errorMessage = error.message || 'Your submission could not be completed. Please try again.';
} finally {
status = 'idle';
}
">
<?php foreach ($fields as $field):
$fieldName = $field["name"];
$fieldLabel = $field["label"];
$fieldType = $field["type"];
$fieldRequired =
isset($field["required"]) && $field["required"];
$fieldOptions = isset($field["options"])
? $field["options"]
: [];
?>
<div class="form-control mb-5">
<?php if ($fieldType === "checkbox" || $fieldType === "radio"): ?>
<fieldset class="space-y-2">
<legend class="label mb-1 px-0">
<span class="label-text font-medium">
<?= phpb_e($fieldLabel) ?>
<?php if ($fieldRequired): ?>
<span class="ml-1 text-error" aria-hidden="true">*</span>
<span class="sr-only">(required)</span>
<?php endif; ?>
</span>
</legend>
<?php else: ?>
<label for="form-field-<?= phpb_e($fieldName) ?>" class="label mb-1 px-0">
<span class="label-text font-medium">
<?= phpb_e($fieldLabel) ?>
<?php if ($fieldRequired): ?>
<span class="ml-1 text-error" aria-hidden="true">*</span>
<span class="sr-only">(required)</span>
<?php endif; ?>
</span>
</label>
<?php endif; ?>
<?php if ($fieldType === "select"): ?>
<select id="form-field-<?= phpb_e($fieldName) ?>" x-model="data.fields.<?= $fieldName ?>" class="select select-bordered w-full" <?= $fieldRequired
? "required"
: "" ?>>
<option value="" disabled selected>Please select</option>
<?php foreach ($fieldOptions as $option): ?>
<option value="<?= phpb_e($option[
"value"
]) ?>"><?= phpb_e($option["label"]) ?></option>
<?php endforeach; ?>
</select>
<?php elseif ($fieldType === "checkbox"): ?>
<div class="space-y-2">
<?php foreach ($fieldOptions as $option): ?>
<label class="label cursor-pointer justify-start gap-3 rounded-box border border-base-300 px-3 py-2">
<input type="checkbox" value="<?= phpb_e($option[
"value"
]) ?>" x-model="data.fields.<?= $fieldName ?>" class="checkbox checkbox-sm checkbox-primary" />
<span class="label-text"><?= phpb_e($option[
"label"
]) ?></span>
</label>
<?php endforeach; ?>
</div>
<?php elseif ($fieldType === "radio"): ?>
<div class="space-y-2">
<?php foreach ($fieldOptions as $option): ?>
<label class="label cursor-pointer justify-start gap-3 rounded-box border border-base-300 px-3 py-2">
<input type="radio" name="<?= phpb_e($fieldName) ?>" value="<?= phpb_e($option[
"value"
]) ?>" x-model="data.fields.<?= $fieldName ?>" class="radio radio-sm radio-primary" />
<span class="label-text"><?= phpb_e($option[
"label"
]) ?></span>
</label>
<?php endforeach; ?>
</div>
<?php elseif ($fieldType === "textarea"): ?>
<textarea id="form-field-<?= phpb_e($fieldName) ?>" x-model="data.fields.<?= $fieldName ?>" class="textarea textarea-bordered w-full" placeholder="<?= phpb_e($fieldLabel) ?>" <?= $fieldRequired
? "required"
: "" ?> rows="4"></textarea>
<?php else: ?>
<input id="form-field-<?= phpb_e($fieldName) ?>" type="<?= phpb_e($fieldType) ?>" x-model="data.fields.<?= $fieldName ?>" class="input input-bordered w-full" placeholder="<?= phpb_e($fieldLabel) ?>" <?= $fieldRequired
? "required"
: "" ?>>
<?php endif; ?>
<?php if ($fieldType === "checkbox" || $fieldType === "radio"): ?>
</fieldset>
<?php endif; ?>
</div>
<?php
endforeach; ?>
<div class="mt-6">
<button type="submit" class="btn btn-primary w-full" :disabled="status === 'busy'" :aria-busy="status === 'busy'">
<span x-show="status === 'idle'" aria-live="polite">
<i class="ph-duotone ph-paper-plane-tilt" aria-hidden="true"></i>
Submit
</span>
<span x-show="status === 'busy'" role="status" aria-live="polite">
<span class="loading loading-spinner loading-sm" aria-hidden="true"></span>
Sending...
</span>
</button>
</div>
</form>
<div x-show="errorMessage" x-text="errorMessage" class="alert alert-error mt-4 shadow-sm" role="alert" aria-live="assertive" aria-atomic="true"></div>
<div x-show="submitted" x-transition:enter x-transition:enter-opacity-0 x-transition:enter-delay-100 class="card mt-4 border border-success/30 bg-base-100 shadow-sm" role="status" aria-live="polite">
<div class="card-body items-center text-center">
<div class="mb-2 flex h-16 w-16 items-center justify-center rounded-full bg-success/20">
<i class="ph-duotone ph-check text-3xl text-success" aria-hidden="true"></i>
</div>
<h3 class="card-title text-lg">Thank you!</h3>
<p class="text-base-content/60">Your message has been sent successfully.</p>
</div>
</div>
</div>
</div>
<?php
else:
?>
<div class="form-wrapper mx-auto w-full max-w-2xl">
<div class="card border-2 border-dashed border-base-300 bg-base-200/50">
<div class="card-body items-center py-8 text-center">
<i class="ph-duotone ph-plus-circle text-4xl text-base-content/30" aria-hidden="true"></i>
<p class="text-sm text-base-content/50 mt-2">Select a form in the settings</p>
</div>
</div>
</div>
<?php
endif; ?>