add form submission handling and confirmation emails
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.
This commit is contained in:
@@ -1,29 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Extensions\Form\Finishers;
|
||||
use Plugi\Extensions\Form\DefinitionsRepository;
|
||||
|
||||
class DefaultFinisher {
|
||||
public $formId;
|
||||
public $form;
|
||||
public $data;
|
||||
public function __construct()
|
||||
public $submission;
|
||||
public $options;
|
||||
|
||||
public function __construct(array $context = [], array $options = [])
|
||||
{
|
||||
$formRepository = new DefinitionsRepository();
|
||||
$this->formId = $_GET['id'] ?? 0;
|
||||
$this->form = $formRepository->findWithId($this->formId);
|
||||
$this->data = file_get_contents('php://input');
|
||||
$this->formId = $context['formId'] ?? null;
|
||||
$this->form = $context['form'] ?? [];
|
||||
$this->data = $context['rawData'] ?? '';
|
||||
$this->submission = $context['submission'] ?? [];
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
public function execute (): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getFormFields (): array {
|
||||
return json_decode($this->form['fields'], true);
|
||||
return $this->form['formFields'] ?? [];
|
||||
}
|
||||
|
||||
public function getEntryFields (): array {
|
||||
return json_decode($this->data, true)['fields'];
|
||||
return $this->submission['fields'] ?? [];
|
||||
}
|
||||
|
||||
public function getFormName (): string {
|
||||
return $this->form['name'];
|
||||
return (string) ($this->form['name'] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,9 @@ class SaveEntryToDatabase extends DefaultFinisher
|
||||
public function execute(): bool
|
||||
{
|
||||
$entriesRepository = new \Plugi\Extensions\Form\EntriesRepository();
|
||||
$entriesRepository->create([
|
||||
return $entriesRepository->create([
|
||||
'formId' => $this->formId,
|
||||
'data' => $this->data,
|
||||
]);
|
||||
return true;
|
||||
]) !== null;
|
||||
}
|
||||
}
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Plugi\Extensions\Form\Finishers;
|
||||
|
||||
class SendConfirmationEmailToSubmitter extends DefaultFinisher
|
||||
{
|
||||
public function execute(): bool
|
||||
{
|
||||
$recipientField = $this->options['recipientField'] ?? null;
|
||||
if (!is_string($recipientField) || $recipientField === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$emailFieldExists = false;
|
||||
foreach ($this->getFormFields() as $field) {
|
||||
if (($field['name'] ?? null) === $recipientField && ($field['type'] ?? null) === 'email') {
|
||||
$emailFieldExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$emailFieldExists) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$recipient = $this->getEntryFields()[$recipientField] ?? null;
|
||||
if (!is_string($recipient) || filter_var($recipient, FILTER_VALIDATE_EMAIL) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$subject = $this->options['subject'] ?? null;
|
||||
$message = $this->options['message'] ?? null;
|
||||
if (!is_string($subject) || trim($subject) === '' || strpbrk($subject, "\r\n") !== false || !is_string($message) || trim($message) === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->sendMail($recipient, $subject, $message);
|
||||
}
|
||||
|
||||
protected function sendMail(string $recipient, string $subject, string $message): bool
|
||||
{
|
||||
return mail($recipient, $subject, $message);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,12 @@ class SendEmailToAdmin extends DefaultFinisher
|
||||
{
|
||||
public function execute(): bool
|
||||
{
|
||||
$to = Extensions::getSettings('form')['email']['value'];
|
||||
$settings = Extensions::getSettings('form');
|
||||
$to = $settings['email']['value'] ?? null;
|
||||
if (!is_string($to) || filter_var($to, FILTER_VALIDATE_EMAIL) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$subject = 'Form submission: '.$this->getFormName();
|
||||
$message = $this->getNiceFields();
|
||||
return mail(
|
||||
@@ -21,7 +26,15 @@ class SendEmailToAdmin extends DefaultFinisher
|
||||
private function getNiceFields(): string {
|
||||
$text = '';
|
||||
foreach ($this->getFormFields() as $field) {
|
||||
$text .= $field['label'] . ': ' . $this->getEntryFields()[$field['name']] . "\n";
|
||||
$name = $field['name'] ?? '';
|
||||
$label = $field['label'] ?? $name;
|
||||
$value = $this->getEntryFields()[$name] ?? '';
|
||||
if (is_array($value)) {
|
||||
$value = implode(', ', array_map('strval', $value));
|
||||
} elseif (!is_scalar($value)) {
|
||||
$value = '';
|
||||
}
|
||||
$text .= $label . ': ' . $value . "\n";
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,10 @@ return [
|
||||
'sendMailToAdmin' => [
|
||||
'class' => \Plugi\Extensions\Form\Finishers\SendEmailToAdmin::class,
|
||||
'label' => 'Send email to admin'
|
||||
],
|
||||
'sendConfirmationEmailToSubmitter' => [
|
||||
'class' => \Plugi\Extensions\Form\Finishers\SendConfirmationEmailToSubmitter::class,
|
||||
'label' => 'Send confirmation email to submitter'
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user